Skip to content

Supporting a new language (tree sitter)

Andy Massimino edited this page Jan 15, 2022 · 2 revisions

Definitions for tree-sitter based matches are defined in a file after/queries/{language}/matchup.scm. Note that the language must be supported by nvim-treesitter and you must have installed the parser using :TSInstall.

You must provide at least a @scope definition and an @open definition. Optionally, you may provide @mid, @close, or @skip definitions.

A straightforward example to define #if:#else:#endif matching of the C pre-processor. Multiple tags can be specified in the same expression, although it is not necessary to do so.

(preproc_if
  "#if" @open.def
  "#endif" @close.def) @scope.def

(preproc_elif "#elif" @mid.def.1)
(preproc_else "#else" @mid.def.2)

Note that the syntax for mids is different from that of open, scope, and close, since you can provide multiple suffixed by .1, .2, etc. If your language construct does not have mids then you don't need to provide them, but if you do they must be numbered, even if there's only one.

Sometimes there is no end to match in your language construct. In these cases, match-up uses the @scope to determine where the end is. You don't need to specify it. However, it doesn't make much sense to omit both the mid and the end, since match-up won't be able to "match" only one thing.

Example (python):

(if_statement
  "if" @open.if
  alternative: (elif_clause "elif" @mid.if.1)?
  alternative: (else_clause "else" @mid.if.2)?) @scope.if

Skips

Sometimes it is impossible for match-up to tell that syntax shouldn't be matched (usually comments and inside strings). For these cases, you can provide a @skip definition, elements matching this will be ignored.

(if_modifier) @skip
Clone this wiki locally