Expand description
Convenient utilities to match syntax structures of code.
- Iterators/Finders to traverse nodes.
- Predicates to check nodes’ properties.
- Classifiers to check nodes’ syntax.
§Classifiers of syntax structures
A node can have a quadruple to describe its syntax:
(InterpretMode, SurroundingSyntax/SyntaxContext, DefClass/SyntaxClass, SyntaxNode)
Among them, InterpretMode
, SurroundingSyntax
, and SyntaxContext
describes outer syntax. DefClass
, SyntaxClass
and
[typst::syntax::SyntaxNode
] describes inner syntax.
- [
typst::syntax::SyntaxNode
]: Its contextual version is [typst::syntax::LinkedNode
], containing AST information, like inner text and [SyntaxKind
], on the position. SyntaxClass
: Provided byclassify_syntax
, it describes the context-free syntax of the node that are more suitable for IDE operations. For example, it identifies users’ half-typed syntax like half-completed labels and dot accesses.DefClass
: Provided byclassify_def
, it describes the definition class of the node at the position. The difference betweenSyntaxClass
andDefClass
is that the latter matcher will skip the nodes that do not define a definition.SyntaxContext
: Provided byclassify_context
, it describes the outer syntax of the node that are more suitable for IDE operations. For example, it identifies the context of a cursor on the comma in a function call.SurroundingSyntax
: Provided bysurrounding_syntax
, it describes the surrounding syntax of the node that are more suitable for IDE operations. The difference betweenSyntaxContext
andSurroundingSyntax
is that the former is more specific and the latter is more general can be used for filtering customized snippets.InterpretMode
: Provided byinterpret_mode_at
, it describes the how an interpreter should interpret the code at the position.
Some examples of the quadruple (the cursor is marked by |
):
#(x|);
^ SyntaxContext::Paren, SyntaxClass::Normal(SyntaxKind::Ident)
#(x,|);
^ SyntaxContext::Element, SyntaxClass::Normal(SyntaxKind::Array)
#f(x,|);
^ SyntaxContext::Arg, SyntaxClass::Normal(SyntaxKind::FuncCall)
#show raw|: |it => it|
^ SurroundingSyntax::Selector
^ SurroundingSyntax::ShowTransform
^ SurroundingSyntax::Regular
Structs§
- An iterator over the variants of InterpretMode
- An iterator over the variants of SurroundingSyntax
Enums§
- Classes of arguments that can be operated on by IDE functionality.
- The cursor is on an invalid position.
- Classes of def items that can be operated on by IDE functionality.
- Classes of field syntax that can be operated on by IDE functionality.
- A mode in which a text document is interpreted.
- A declaration that is an ancestor of the given node or the previous sibling of some ancestor.
- A node that is an ancestor of the given node or the previous sibling of some ancestor.
- Classes of syntax context (outer syntax) that can be operated on by IDE
- Classes of syntax that can be operated on by IDE functionality.
- Classes of syntax context (outer syntax) that can be operated on by IDE functionality.
- Classes of variable (access) syntax that can be operated on by IDE functionality.
Functions§
- Checks if the cursor is on an invalid position for completion.
- Classifies node’s context (outer syntax) that can be operated on by IDE functionality.
- Classifies node’s context (outer syntax) by outer node that can be operated on by IDE functionality.
- Classifies a definition strictly.
- Classifies a definition loosely.
- Classifies node’s syntax (inner syntax) that can be operated on by IDE functionality.
- Finds the first ancestor node that is an expression.
- Determine the interpretation mode at the given position (context-sensitive).
- Whether the node can be recognized as an identifier.
- Whether the node can be recognized as a mark.
- Returns the ancestor iterator of the given node.
- Finds the previous declarations starting from the given position. It checks
PreviousItem
and returns the found declarations. - Finds the previous items (in the scope) starting from the given position inclusively. See
PreviousItem
for the possible items. - Determines the surrounding syntax of the node at the position.