tinymist_analysis::syntax

Module matcher

Source
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 by classify_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 by classify_def, it describes the definition class of the node at the position. The difference between SyntaxClass and DefClass is that the latter matcher will skip the nodes that do not define a definition.
  • SyntaxContext: Provided by classify_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 by surrounding_syntax, it describes the surrounding syntax of the node that are more suitable for IDE operations. The difference between SyntaxContext and SurroundingSyntax is that the former is more specific and the latter is more general can be used for filtering customized snippets.
  • InterpretMode: Provided by interpret_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§

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§