The text framework includes support for locating matching structural text delimiters, such as brackets.
Once the language service is implemented for structure matching, SyntaxEditor features such as "move to matching bracket" and "select to matching bracket" are automatically supported. Structure matching is also a requirement for the delimiter highlighting feature.
Matching Logic
Structure matchers implement the IStructure
The Text
A structure matcher generally does text scanning, token scanning, or even AST examination to build its results. The IText
Options
The IStructuretrue
when the request is being made for navigation purposes, such as SyntaxEditor's "move to matching bracket" feature. When this property is true
, the match logic should be a bit more liberal and allow any possible bracket surrounding or next to the source offset to be a valid result.
When this property is false
, the request is more likely for a feature such as delimiter highlighting. And in that case, the logic for finding a match next to the source offset should be a bit stricter.
Results
The Match method returns an IStructure
If a structural delimiter is next to the source offset, a result for that delimiter should be included in the result set, and its Istrue
. Any other "matching" structural delimiters should also be included in the collection.
For example, if parenthesis structure matching is supported, the match logic would first use an IText
Structure matching also supports multiple results. Say that the structure matching should support #if...#else...#endif
blocks. If the source offset is within the #else
then the result set would include three results (one for each directive text range) and only the one for #else
would have its Istrue
.
Each result also has a MatchNone
, Start
, End
, or Both
) of the result's range can cause the structure matcher to match the result again. This information is used for optimizations by the delimiter highlighting feature.
Registering a Structure Matcher with a Syntax Language
An IStructure
Registering structure matchers with a language is optional but recommended.
Default Structure Matcher Implementation
A built-in Structure
- Angle braces,
<
and>
(disabled by default) - Curly braces,
{
and}
- Parenthesis,
(
and)
- Square braces,
[
and]
The various supported brace match options can be turned on or off with properties such as Structure
By default, this matcher will just perform basic text scanning to match brackets. However, this can cause issues when scanning for a pair of parentheses and there is a parenthesis character within a contained string. Thus, the matcher also provides optional token IDs that can be set for each character. When a potential text match is found and a related token ID is specified, it will double-check that the token at the offset has that token ID. This eliminates the problem. Properties such as Structure