@tenzir/codemirror-lang-tql
v0.5.4
Published
Tenzir Query Language (TQL) support for CodeMirror, backed by tree-sitter-tql
Readme
@tenzir/codemirror-lang-tql
Tenzir Query Language (TQL) support for CodeMirror 6, backed by tree-sitter-tql, the grammar that Zed, VS Code, and Neovim use as well.
The package parses TQL with tree-sitter through web-tree-sitter and converts every tree-sitter tree into a Lezer tree, so all of CodeMirror works from the one grammar:
- Highlighting from tree-sitter-tql's
highlights.scm, as standard@lezer/highlighttags - Bracket matching and quote pairing that skip strings and comments, including strings that are not closed yet
- Indentation from
indents.scm, folding fromfolds.scm, bracket pairs frombrackets.scm - Comment toggling
- Nested languages from
injections.scm, such as YAML in front matter
Install
pnpm add @tenzir/codemirror-lang-tql web-tree-sitter tree-sitter-tqlweb-tree-sitter, @codemirror/language, @lezer/common, and
@lezer/highlight are peer dependencies. tree-sitter-tql provides the
grammar's WebAssembly; its queries ship inside this package.
Usage
Tree-sitter runs as WebAssembly, which loads asynchronously, and the host
decides where it comes from. Load web-tree-sitter and the grammar, then pass
both to tql():
import { syntaxHighlighting } from "@codemirror/language";
import { EditorView, basicSetup } from "codemirror";
import { tql, tqlHighlighter } from "@tenzir/codemirror-lang-tql";
import grammarWasmUrl from "tree-sitter-tql/tree-sitter-tql.wasm?url";
import runtimeWasmUrl from "web-tree-sitter/web-tree-sitter.wasm?url";
// Import web-tree-sitter on demand to keep it out of the main chunk.
const { Language, Parser, Query } = await import("web-tree-sitter");
await Parser.init({ locateFile: () => runtimeWasmUrl });
const language = await Language.load(grammarWasmUrl);
// Create the language once and share it between editors.
const tqlSupport = tql({ Parser, Query, language });
new EditorView({
parent: document.body,
doc: 'from "events.json" | where severity == "high"',
extensions: [
basicSetup,
tqlSupport,
syntaxHighlighting(
tqlHighlighter({
keyword: "text-red",
operator: "text-red",
attribute: "text-red",
function: "text-purple",
string: "text-blue",
number: "text-blue",
boolean: "text-blue",
constant: "text-blue",
variable: "text-blue",
comment: "text-gray",
}),
),
],
});tql() takes web-tree-sitter's classes from the host rather than importing
them, so the package never loads web-tree-sitter by itself, and the parser
always comes from the same web-tree-sitter instance as the grammar.
If you don't need control over loading, loadTql() does all of the above:
import { loadTql } from "@tenzir/codemirror-lang-tql";
const tqlSupport = await loadTql({ runtimeWasmUrl, grammarWasmUrl });It imports web-tree-sitter on demand and only initializes it when nothing initialized it before, since initializing it again would invalidate grammars that the host loaded already.
Migrating from 0.4
0.5 replaces the hand-written Lezer grammar with tree-sitter-tql, which loads asynchronously:
TQL()andTenzirQueryLangare gone. Create the language support once withawait loadTql({ runtimeWasmUrl, grammarWasmUrl }), or load web-tree-sitter yourself and calltql({ Parser, Query, language }), and share it between editors. Until it has loaded, editors can run without it.- Instead of
TenzirQueryLang.parser, usesupport.language.parser, for example to highlight read-only views withhighlightTree. It only exists once the grammar loaded. - Highlighting uses the tags of the capture mapping instead
of the coarse tags of 0.4. A
HighlightStylefor those still matches most nodes, since functions and attributes fall back tonameand numbers and booleans toliteral, but operators are nowoperatorrather thanpunctuation,nullfalls back tokeywordrather thanliteral, and$variables toname. Check your style, or style by capture name withtqlHighlighter(classes). - Install
web-tree-sitterandtree-sitter-tqland serve their.wasmfiles, for example with Vite's?urlimports. - Front matter now parses, and its fences highlight as comments. To edit its
body as YAML, pass
nested: { yaml: yamlLanguage.parser }, as shown under Front matter.
API
function tql(treeSitter: TqlTreeSitter, options?: TqlOptions): LanguageSupport;
function loadTql(options: LoadTqlOptions): Promise<LanguageSupport>;
function tqlHighlighter(classes: Readonly<Record<string, string>>): Highlighter;
const tqlCaptureTags: Readonly<Record<string, Tag>>;
const tqlQueries: {
highlights: string;
indents: string;
folds: string;
brackets: string;
injections: string;
};
// The type of `support.language.parser`.
class TqlParser extends Parser {
delete(): void; // frees the combined query and unfinished parses
}
interface TqlTreeSitter {
Parser: typeof import("web-tree-sitter").Parser; // after Parser.init()
Query: typeof import("web-tree-sitter").Query;
language: import("web-tree-sitter").Language;
}
interface TqlOptions {
highlights?: string; // highlight query, tree-sitter-tql's by default
indents?: string; // indent query, tree-sitter-tql's by default
folds?: string; // fold query, tree-sitter-tql's by default
brackets?: string; // bracket query, tree-sitter-tql's by default
injections?: string; // injection query, tree-sitter-tql's by default
tags?: TqlCaptureTags; // capture tags over the defaults
nested?: Readonly<Record<string, Parser>>; // parsers by injected language
}
interface LoadTqlOptions extends TqlOptions {
runtimeWasmUrl?: string; // web-tree-sitter.wasm
grammarWasmUrl: string; // tree-sitter-tql.wasm
}
type TqlCaptureTags = Readonly<Record<string, Tag | null>>;Highlighting
The highlight query's captures become these tags (tqlCaptureTags):
| Capture | Tag |
| ----------------------- | ---------------------------------- |
| keyword | tags.keyword |
| operator | tags.operator |
| attribute | tags.attributeName |
| function | tags.function(tags.variableName) |
| function.call | tags.function(tags.variableName) |
| function.operator | tags.function(tags.variableName) |
| function.method | tags.function(tags.propertyName) |
| string | tags.string |
| string.escape | tags.escape |
| string.special | tags.special(tags.string) |
| number | tags.number |
| number.time | tags.number |
| number.duration | tags.number |
| boolean | tags.bool |
| constant | tags.constant(tags.name) |
| constant.ip | tags.constant(tags.name) |
| constant.subnet | tags.constant(tags.name) |
| constant.builtin | tags.null |
| variable | tags.variableName |
| variable.builtin | tags.special(tags.variableName) |
| variable.parameter | tags.local(tags.variableName) |
| property | tags.propertyName |
| type | tags.typeName |
| label | tags.labelName |
| comment | tags.comment |
| punctuation | tags.punctuation |
| punctuation.bracket | tags.bracket |
| punctuation.delimiter | tags.separator |
| punctuation.special | tags.special(tags.punctuation) |
A capture without an entry takes the tag of the nearest capture it refines, so
function.builtin would be a function. Pipeline operators (function.operator),
times and durations (number.time, number.duration), and IP addresses and
subnets (constant.ip, constant.subnet) share the tags of the captures they
refine, so standard highlight styles color them as before, while
tqlHighlighter can color them apart by capture name. Captures starting with _ never
color.
Captures follow tree-sitter's rules: an inner node's capture wins over the capture of a node around it, such as the braces of a replacement in a format string, and of two captures of the same node, the later pattern wins. Nodes inside a captured node without a capture of their own take its tag, so a whole format string is a string except where its parts say otherwise.
A node's tag is the tag of its capture with a modifier naming the capture,
so captures that share a tag, like function and function.call, stay
distinct. Any @lezer/highlight highlighter works, like HighlightStyle or
classHighlighter, and falls back to the tags in the table.
tqlHighlighter(classes) styles by capture name instead, the way tree-sitter
themes do: classes maps a capture to a CSS class, and an entry also covers the
captures that refine it, so function covers function.call and
function.method unless they have entries of their own. Unlike
tagHighlighter, it does not fall back from a tag to its parents.
A tree-sitter theme that leaves a capture unstyled shows the color of the node
around it, such as the parentheses of a call inside a format string. To do the
same, map such captures to null:
const extensions = [
tql({ Parser, Query, language }, { tags: { punctuation: null } }),
syntaxHighlighting(tqlHighlighter(classes)),
];An entry covers the captures that refine it, so punctuation: null also
leaves out the braces around an expression in a format string
(punctuation.special), which then take the color of the string. To color
them apart, give them their tag back with
"punctuation.special": tqlCaptureTags["punctuation.special"].
Editing
- Quote pairing:
closeBrackets()pairs(,[,{, and", also after the format prefixfand the byte and raw prefixesb,r, andbrwith up to 8#. TQL has no single-quoted strings, so'doesn't pair. tree-sitter-tql parses a string without its closing quote as a string that runs to the end of the document, as TQL's strings may span lines, so typing the closing quote doesn't insert a pair. The same goes for a/*comment without its*/. - Bracket matching: the tokens that
brackets.scmpairs as@openand@close(( ),[ ], and{ }) match in the tree, so brackets in strings and comments don't count. - Indentation: lines inside the nodes that
indents.scmcaptures as@indent(blocks, pipelines, records, lists, match arms) are indented one unit past the line where the node starts, and a line starting with an@outdenttoken (}and]) lines up with that line. A line that starts inside a node captured as@indent.auto(strings, the text of format strings, comments, and the body of front matter) keeps its indentation, so a new line there takes the indentation of the line before, as in nvim-treesitter. At the top level, a new line keeps the indentation of the line before, too.indentOnInput()reindents a line once you type its closing bracket. - Folding: the nodes that
folds.scmcaptures as@foldfold between their@openand@closebrackets, and front matter between its fences. - Comments:
//for lines and/* */for blocks.
Front matter
TQL files may start with YAML front matter between --- fences. The fences
highlight as comments. tree-sitter-tql's injections.scm injects the yaml
language into the body, and nested maps an injected language to the parser
that parses it. To edit the body as YAML, pass the parser of
@codemirror/lang-yaml's language, which brings YAML's highlighting,
indentation, and # comments:
import { yamlLanguage } from "@codemirror/lang-yaml";
const tqlSupport = tql({ Parser, Query, language }, { nested: { yaml: yamlLanguage.parser } });The bare parser of @lezer/yaml only highlights the body. A language without
a parser in nested stays part of the TQL tree. The bridge reads the language
from #set! injection.language, not from @injection.language captures.
Lifetime and performance
Each parse runs tree-sitter on the document, or the prefix where CodeMirror
asks it to stop, without reusing an earlier tree. A generator yields between
steps targeting 4 ms, so CodeMirror can continue parsing in the background.
The queries run combined into one, on subtrees of up to 256 nodes; larger
subtrees are queried one level at a time instead of repeatedly querying their
enclosing nodes. Sibling patterns, like the bracket pairs of brackets.scm,
only match among the children of nodes with up to 256 children. A list of
thousands of items thus keeps each step short, but its own brackets lose their
bracket props: they still match by name, and the list folds by lines, like
front matter, rather than between its brackets.
Each parse owns its native parser and tree and frees them as soon as their work finishes, also on errors. CodeMirror has no cancellation hook for parses it drops, so a finalizer frees an abandoned parse's native handles once the parse is garbage-collected. Active parses are never evicted, including when several editors share the language. To free the combined query and all unfinished parses immediately, without waiting for garbage collection, use:
import type { TqlParser } from "@tenzir/codemirror-lang-tql";
(tqlSupport.language.parser as TqlParser).delete();Afterwards, editors must not use the language anymore. Since the language is meant to be created once per page, most hosts never need to free it.
The Lezer tree also serves views without an editor:
import { highlightTree } from "@lezer/highlight";
const tree = tqlSupport.language.parser.parse(text);
highlightTree(tree, highlighter, (from, to, classes) => { /* … */ });Limitations
- The bundled queries come from tree-sitter-tql 1.3.0 (
tqlQueries). A query that names a node type the loaded grammar lacks does not compile, andtql()throws. With a different version of the grammar, pass its queries ashighlights,indents,folds,brackets, andinjections, for example as Vite?rawimports. - A new line at the very end of the document indents by the nodes around the
cursor, even right after a string or
/*comment that is not closed yet. CodeMirror only asks the nodes that a position lies inside of, and the end of the document lies inside none. Inside a block, such a line takes the block's indentation instead of the indentation of the line before. - A nested parse over several ranges sees the gaps between them as spaces, so a token cannot continue across a gap.
- Around syntax errors, tree-sitter may recover differently when it resumes a parse that yielded than when it parses the whole document at once.
- The 4 ms target is not a hard deadline. Custom queries that match many descendants, large individual tokens, freeing the native tree of a document with a million nodes, and garbage collection can make a step take longer.
- Cleanup of abandoned parses depends on garbage collection and is not
immediate. Call
delete()when retiring the language to free all native handles deterministically.
Development
pnpm install
pnpm build
pnpm testThe tests load the real grammar from node_modules.
