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 istypst::syntax::LinkedNode
, containing AST information, like inner text andSyntaxKind
, 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§
- Interpret
Mode Iter - An iterator over the variants of InterpretMode
- Surrounding
Syntax Iter - An iterator over the variants of SurroundingSyntax
Enums§
- ArgClass
- Classes of arguments that can be operated on by IDE functionality.
- BadCompletion
Cursor - The cursor is on an invalid position.
- DefClass
- Classes of def items that can be operated on by IDE functionality.
- Field
Class - Classes of field syntax that can be operated on by IDE functionality.
- Interpret
Mode - A mode in which a text document is interpreted.
- Previous
Decl - A declaration that is an ancestor of the given node or the previous sibling of some ancestor.
- Previous
Item - A node that is an ancestor of the given node or the previous sibling of some ancestor.
- Surrounding
Syntax - Classes of syntax context (outer syntax) that can be operated on by IDE
- Syntax
Class - Classes of syntax that can be operated on by IDE functionality.
- Syntax
Context - Classes of syntax context (outer syntax) that can be operated on by IDE functionality.
- VarClass
- Classes of variable (access) syntax that can be operated on by IDE functionality.
Functions§
- bad_
completion_ cursor - Checks if the cursor is on an invalid position for completion.
- classify_
context - Classifies node’s context (outer syntax) that can be operated on by IDE functionality.
- classify_
context_ outer - Classifies node’s context (outer syntax) by outer node that can be operated on by IDE functionality.
- classify_
def - Classifies a definition strictly.
- classify_
def_ loosely - Classifies a definition loosely.
- classify_
syntax - Classifies node’s syntax (inner syntax) that can be operated on by IDE functionality.
- first_
ancestor_ expr - Finds the first ancestor node that is an expression.
- interpret_
mode_ at - Determine the interpretation mode at the given position (context-sensitive).
- is_
ident_ like - Whether the node can be recognized as an identifier.
- is_mark
- Whether the node can be recognized as a mark.
- node_
ancestors - Returns the ancestor iterator of the given node.
- previous_
decls - Finds the previous declarations starting from the given position. It checks
PreviousItem
and returns the found declarations. - previous_
items - Finds the previous items (in the scope) starting from the given position
inclusively. See
PreviousItem
for the possible items. - surrounding_
syntax - Determines the surrounding syntax of the node at the position.