Module: processTargets/modifiers/surroundingPair/findSurroundingPairParseTreeBased
Functions
findSurroundingPairParseTreeBased
▸ findSurroundingPairParseTreeBased(editor
, selection
, node
, delimiters
, scopeType
): null
| SurroundingPairInfo
Implements the version of the surrounding pair finding algorithm that leverages the parse tree. We use this algorithm when we are in a language for which we have parser support, unless we are in a string or comment, where we revert to text-based.
The approach is actually roughly the same as the approach we use when we do not have access to a parse tree. In both cases we create a list of candidate delimiters in the region of the selection, and then pass them to the core algorithm, implemented by findSurroundingPairCore.
To generate a list of delimiters to pass to findSurroundingPairCore, we repeatedly walk up the parse tree starting at the given node. Each time, we ask for all descendant tokens whose type is that of one of the delimiters that we're looking for. repeatedly walk up the parse tree starting at the given node. Each time, we ask for all descendant tokens whose type is that of one of the delimiters that we're looking for, and pass this list of tokens to findSurroundingPairCore.
Note that walking up the hierarchy one parent at a time is just an optimization to avoid handling the entire file if we don't need to. The result would be the same if we just operated on the root node of the parse tree, just slower if our delimiter pair is actually contained in a small piece of a large file.
The main benefits of the parse tree-based approach over the text-based approach are the following:
- We can leverage the lexer to ensure that we only consider proper language tokens
- We can let the language normalize surface forms of delimiter types, so eg
in Python the leading
f"
on an f-string just has type"
like any other string. - We can more easily narrow the scope of our search by walking up the parse tree
- The actual lexing is done in fast wasm code rather than using a regex
- We can disambiguate delimiters whose opening and closing symbol is the
same (eg
"
). Without a parse tree we have to guess whether it is an opening or closing quote.
Parameters
Name | Type | Description |
---|---|---|
editor | TextEditor | The text editor containing the selection |
selection | Range | The selection to find surrounding pair around |
node | SyntaxNode | A parse tree node overlapping with the selection |
delimiters | SimpleSurroundingPairName [] | The acceptable surrounding pair names |
scopeType | SurroundingPairScopeType | - |
Returns
null
| SurroundingPairInfo
The newly expanded selection, including editor info
Defined in
processTargets/modifiers/surroundingPair/findSurroundingPairParseTreeBased.ts:60