@tenzir/codemirror-lang-tql 0.4.8 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +333 -8
- package/dist/index.cjs +625 -103
- package/dist/index.d.cts +141 -4
- package/dist/index.d.ts +141 -4
- package/dist/index.js +622 -102
- package/package.json +31 -18
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright © 2023 Tenzir GmbH
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the “Software”), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights to
|
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
10
|
+
subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
|
|
18
|
+
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
19
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
21
|
+
|
package/README.md
CHANGED
|
@@ -1,15 +1,340 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @tenzir/codemirror-lang-tql
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[Tenzir Query Language (TQL)](https://docs.tenzir.com/explanations/language) support for
|
|
4
|
+
[CodeMirror 6](https://codemirror.net), backed by
|
|
5
|
+
[tree-sitter-tql](https://github.com/tenzir/tree-sitter-tql), the grammar that
|
|
6
|
+
Zed, VS Code, and Neovim use as well.
|
|
4
7
|
|
|
5
|
-
|
|
8
|
+
The package parses TQL with tree-sitter through
|
|
9
|
+
[web-tree-sitter](https://www.npmjs.com/package/web-tree-sitter) and converts
|
|
10
|
+
every tree-sitter tree into a Lezer tree, so all of CodeMirror works from the
|
|
11
|
+
one grammar:
|
|
6
12
|
|
|
7
|
-
|
|
8
|
-
|
|
13
|
+
- Highlighting from tree-sitter-tql's `highlights.scm`, as standard
|
|
14
|
+
`@lezer/highlight` tags
|
|
15
|
+
- Bracket matching and quote pairing that skip strings and comments, including
|
|
16
|
+
strings that are not closed yet
|
|
17
|
+
- Indentation from `indents.scm`, folding from `folds.scm`, bracket pairs from
|
|
18
|
+
`brackets.scm`
|
|
19
|
+
- Comment toggling
|
|
20
|
+
- Nested languages from `injections.scm`, such as YAML in front matter
|
|
21
|
+
|
|
22
|
+
## Install
|
|
23
|
+
|
|
24
|
+
```sh
|
|
25
|
+
pnpm add @tenzir/codemirror-lang-tql web-tree-sitter tree-sitter-tql
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
`web-tree-sitter`, `@codemirror/language`, `@lezer/common`, and
|
|
29
|
+
`@lezer/highlight` are peer dependencies. `tree-sitter-tql` provides the
|
|
30
|
+
grammar's WebAssembly; its queries ship inside this package.
|
|
31
|
+
|
|
32
|
+
## Usage
|
|
33
|
+
|
|
34
|
+
Tree-sitter runs as WebAssembly, which loads asynchronously, and the host
|
|
35
|
+
decides where it comes from. Load web-tree-sitter and the grammar, then pass
|
|
36
|
+
both to `tql()`:
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
import { syntaxHighlighting } from "@codemirror/language";
|
|
40
|
+
import { EditorView, basicSetup } from "codemirror";
|
|
41
|
+
import { tql, tqlHighlighter } from "@tenzir/codemirror-lang-tql";
|
|
42
|
+
import grammarWasmUrl from "tree-sitter-tql/tree-sitter-tql.wasm?url";
|
|
43
|
+
import runtimeWasmUrl from "web-tree-sitter/web-tree-sitter.wasm?url";
|
|
44
|
+
|
|
45
|
+
// Import web-tree-sitter on demand to keep it out of the main chunk.
|
|
46
|
+
const { Language, Parser, Query } = await import("web-tree-sitter");
|
|
47
|
+
await Parser.init({ locateFile: () => runtimeWasmUrl });
|
|
48
|
+
const language = await Language.load(grammarWasmUrl);
|
|
49
|
+
|
|
50
|
+
// Create the language once and share it between editors.
|
|
51
|
+
const tqlSupport = tql({ Parser, Query, language });
|
|
52
|
+
|
|
53
|
+
new EditorView({
|
|
54
|
+
parent: document.body,
|
|
55
|
+
doc: 'from "events.json" | where severity == "high"',
|
|
56
|
+
extensions: [
|
|
57
|
+
basicSetup,
|
|
58
|
+
tqlSupport,
|
|
59
|
+
syntaxHighlighting(
|
|
60
|
+
tqlHighlighter({
|
|
61
|
+
keyword: "text-red",
|
|
62
|
+
operator: "text-red",
|
|
63
|
+
attribute: "text-red",
|
|
64
|
+
function: "text-purple",
|
|
65
|
+
string: "text-blue",
|
|
66
|
+
number: "text-blue",
|
|
67
|
+
boolean: "text-blue",
|
|
68
|
+
constant: "text-blue",
|
|
69
|
+
variable: "text-blue",
|
|
70
|
+
comment: "text-gray",
|
|
71
|
+
}),
|
|
72
|
+
),
|
|
73
|
+
],
|
|
74
|
+
});
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
`tql()` takes web-tree-sitter's classes from the host rather than importing
|
|
78
|
+
them, so the package never loads web-tree-sitter by itself, and the parser
|
|
79
|
+
always comes from the same web-tree-sitter instance as the grammar.
|
|
80
|
+
|
|
81
|
+
If you don't need control over loading, `loadTql()` does all of the above:
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
import { loadTql } from "@tenzir/codemirror-lang-tql";
|
|
85
|
+
|
|
86
|
+
const tqlSupport = await loadTql({ runtimeWasmUrl, grammarWasmUrl });
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
It imports web-tree-sitter on demand and only initializes it when nothing
|
|
90
|
+
initialized it before, since initializing it again would invalidate grammars
|
|
91
|
+
that the host loaded already.
|
|
92
|
+
|
|
93
|
+
## Migrating from 0.4
|
|
94
|
+
|
|
95
|
+
0.5 replaces the hand-written Lezer grammar with tree-sitter-tql, which loads
|
|
96
|
+
asynchronously:
|
|
97
|
+
|
|
98
|
+
- `TQL()` and `TenzirQueryLang` are gone. Create the language support once with
|
|
99
|
+
`await loadTql({ runtimeWasmUrl, grammarWasmUrl })`, or load web-tree-sitter
|
|
100
|
+
yourself and call `tql({ Parser, Query, language })`, and share it between
|
|
101
|
+
editors. Until it has loaded, editors can run without it.
|
|
102
|
+
- Instead of `TenzirQueryLang.parser`, use `support.language.parser`, for
|
|
103
|
+
example to highlight read-only views with `highlightTree`. It only exists
|
|
104
|
+
once the grammar loaded.
|
|
105
|
+
- Highlighting uses the tags of the [capture mapping](#highlighting) instead
|
|
106
|
+
of the coarse tags of 0.4. A `HighlightStyle` for those still matches most
|
|
107
|
+
nodes, since functions and attributes fall back to `name` and numbers and
|
|
108
|
+
booleans to `literal`, but operators are now `operator` rather than
|
|
109
|
+
`punctuation`, `null` falls back to `keyword` rather than `literal`, and `$`
|
|
110
|
+
variables to `name`. Check your style, or style by capture name with
|
|
111
|
+
`tqlHighlighter(classes)`.
|
|
112
|
+
- Install `web-tree-sitter` and `tree-sitter-tql` and serve their `.wasm`
|
|
113
|
+
files, for example with Vite's `?url` imports.
|
|
114
|
+
- Front matter now parses, and its fences highlight as comments. To edit its
|
|
115
|
+
body as YAML, pass `nested: { yaml: yamlLanguage.parser }`, as shown under
|
|
116
|
+
[Front matter](#front-matter).
|
|
117
|
+
|
|
118
|
+
## API
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
function tql(treeSitter: TqlTreeSitter, options?: TqlOptions): LanguageSupport;
|
|
122
|
+
|
|
123
|
+
function loadTql(options: LoadTqlOptions): Promise<LanguageSupport>;
|
|
124
|
+
|
|
125
|
+
function tqlHighlighter(classes: Readonly<Record<string, string>>): Highlighter;
|
|
126
|
+
|
|
127
|
+
const tqlCaptureTags: Readonly<Record<string, Tag>>;
|
|
128
|
+
|
|
129
|
+
const tqlQueries: {
|
|
130
|
+
highlights: string;
|
|
131
|
+
indents: string;
|
|
132
|
+
folds: string;
|
|
133
|
+
brackets: string;
|
|
134
|
+
injections: string;
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
// The type of `support.language.parser`.
|
|
138
|
+
class TqlParser extends Parser {
|
|
139
|
+
delete(): void; // frees the combined query and unfinished parses
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
interface TqlTreeSitter {
|
|
143
|
+
Parser: typeof import("web-tree-sitter").Parser; // after Parser.init()
|
|
144
|
+
Query: typeof import("web-tree-sitter").Query;
|
|
145
|
+
language: import("web-tree-sitter").Language;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
interface TqlOptions {
|
|
149
|
+
highlights?: string; // highlight query, tree-sitter-tql's by default
|
|
150
|
+
indents?: string; // indent query, tree-sitter-tql's by default
|
|
151
|
+
folds?: string; // fold query, tree-sitter-tql's by default
|
|
152
|
+
brackets?: string; // bracket query, tree-sitter-tql's by default
|
|
153
|
+
injections?: string; // injection query, tree-sitter-tql's by default
|
|
154
|
+
tags?: TqlCaptureTags; // capture tags over the defaults
|
|
155
|
+
nested?: Readonly<Record<string, Parser>>; // parsers by injected language
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
interface LoadTqlOptions extends TqlOptions {
|
|
159
|
+
runtimeWasmUrl?: string; // web-tree-sitter.wasm
|
|
160
|
+
grammarWasmUrl: string; // tree-sitter-tql.wasm
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
type TqlCaptureTags = Readonly<Record<string, Tag | null>>;
|
|
9
164
|
```
|
|
10
165
|
|
|
11
|
-
|
|
166
|
+
## Highlighting
|
|
12
167
|
|
|
13
|
-
|
|
14
|
-
|
|
168
|
+
The highlight query's captures become these tags (`tqlCaptureTags`):
|
|
169
|
+
|
|
170
|
+
| Capture | Tag |
|
|
171
|
+
| ----------------------- | ---------------------------------- |
|
|
172
|
+
| `keyword` | `tags.keyword` |
|
|
173
|
+
| `operator` | `tags.operator` |
|
|
174
|
+
| `attribute` | `tags.attributeName` |
|
|
175
|
+
| `function` | `tags.function(tags.variableName)` |
|
|
176
|
+
| `function.call` | `tags.function(tags.variableName)` |
|
|
177
|
+
| `function.method` | `tags.function(tags.propertyName)` |
|
|
178
|
+
| `string` | `tags.string` |
|
|
179
|
+
| `string.escape` | `tags.escape` |
|
|
180
|
+
| `string.special` | `tags.special(tags.string)` |
|
|
181
|
+
| `number` | `tags.number` |
|
|
182
|
+
| `boolean` | `tags.bool` |
|
|
183
|
+
| `constant` | `tags.constant(tags.name)` |
|
|
184
|
+
| `constant.builtin` | `tags.null` |
|
|
185
|
+
| `variable` | `tags.variableName` |
|
|
186
|
+
| `variable.builtin` | `tags.special(tags.variableName)` |
|
|
187
|
+
| `variable.parameter` | `tags.local(tags.variableName)` |
|
|
188
|
+
| `property` | `tags.propertyName` |
|
|
189
|
+
| `type` | `tags.typeName` |
|
|
190
|
+
| `label` | `tags.labelName` |
|
|
191
|
+
| `comment` | `tags.comment` |
|
|
192
|
+
| `punctuation` | `tags.punctuation` |
|
|
193
|
+
| `punctuation.bracket` | `tags.bracket` |
|
|
194
|
+
| `punctuation.delimiter` | `tags.separator` |
|
|
195
|
+
|
|
196
|
+
A capture without an entry takes the tag of the nearest capture it refines, so
|
|
197
|
+
`function.builtin` would be a `function`. Captures starting with `_` never
|
|
198
|
+
color.
|
|
199
|
+
|
|
200
|
+
Captures follow tree-sitter's rules: an inner node's capture wins over the
|
|
201
|
+
capture of a node around it, such as the braces of a replacement in a format
|
|
202
|
+
string, and of two captures of the same node, the later pattern wins. Nodes
|
|
203
|
+
inside a captured node without a capture of their own take its tag, so a whole
|
|
204
|
+
format string is a string except where its parts say otherwise.
|
|
205
|
+
|
|
206
|
+
A node's tag is the tag of its capture with a modifier naming the capture,
|
|
207
|
+
so captures that share a tag, like `function` and `function.call`, stay
|
|
208
|
+
distinct. Any `@lezer/highlight` highlighter works, like `HighlightStyle` or
|
|
209
|
+
`classHighlighter`, and falls back to the tags in the table.
|
|
210
|
+
`tqlHighlighter(classes)` styles by capture name instead, the way tree-sitter
|
|
211
|
+
themes do: `classes` maps a capture to a CSS class, and an entry also covers the
|
|
212
|
+
captures that refine it, so `function` covers `function.call` and
|
|
213
|
+
`function.method` unless they have entries of their own. Unlike
|
|
214
|
+
`tagHighlighter`, it does not fall back from a tag to its parents.
|
|
215
|
+
|
|
216
|
+
A tree-sitter theme that leaves a capture unstyled shows the color of the node
|
|
217
|
+
around it, such as the parentheses of a call inside a format string. To do the
|
|
218
|
+
same, map such captures to `null`:
|
|
219
|
+
|
|
220
|
+
```ts
|
|
221
|
+
const extensions = [
|
|
222
|
+
tql({ Parser, Query, language }, { tags: { punctuation: null } }),
|
|
223
|
+
syntaxHighlighting(tqlHighlighter(classes)),
|
|
224
|
+
];
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
## Editing
|
|
228
|
+
|
|
229
|
+
- **Quote pairing:** `closeBrackets()` pairs `(`, `[`, `{`, `"`, and `'`, also
|
|
230
|
+
after the string prefixes `f` and `b`, and the raw prefixes `r` and `br`
|
|
231
|
+
with up to 8 `#`. tree-sitter-tql parses a string without its closing quote
|
|
232
|
+
as a string that runs to the end of the document, as TQL's strings may span
|
|
233
|
+
lines, so typing the closing quote doesn't insert a pair. The same goes for a
|
|
234
|
+
`/*` comment without its `*/`.
|
|
235
|
+
- **Bracket matching:** the tokens that `brackets.scm` pairs as `@open` and
|
|
236
|
+
`@close` (`( )`, `[ ]`, and `{ }`) match in the tree, so brackets in strings
|
|
237
|
+
and comments don't count.
|
|
238
|
+
- **Indentation:** lines inside the nodes that `indents.scm` captures as
|
|
239
|
+
`@indent` (blocks, pipelines, records, lists, match arms) are indented one
|
|
240
|
+
unit past the line where the node starts, and a line starting with an
|
|
241
|
+
`@outdent` token (`}` and `]`) lines up with that line. A line that starts
|
|
242
|
+
inside a node captured as `@indent.auto` (strings, the text of format
|
|
243
|
+
strings, comments, and the body of front matter) keeps its indentation, so a
|
|
244
|
+
new line there takes the indentation of the line before, as in
|
|
245
|
+
nvim-treesitter. At the top level, a new line keeps the indentation of the
|
|
246
|
+
line before, too. `indentOnInput()` reindents a line once you type its
|
|
247
|
+
closing bracket.
|
|
248
|
+
- **Folding:** the nodes that `folds.scm` captures as `@fold` fold between
|
|
249
|
+
their `@open` and `@close` brackets, and front matter between its fences.
|
|
250
|
+
- **Comments:** `//` for lines and `/* */` for blocks.
|
|
251
|
+
|
|
252
|
+
## Front matter
|
|
253
|
+
|
|
254
|
+
TQL files may start with YAML front matter between `---` fences. The fences
|
|
255
|
+
highlight as comments. tree-sitter-tql's `injections.scm` injects the `yaml`
|
|
256
|
+
language into the body, and `nested` maps an injected language to the parser
|
|
257
|
+
that parses it. To edit the body as YAML, pass the parser of
|
|
258
|
+
`@codemirror/lang-yaml`'s language, which brings YAML's highlighting,
|
|
259
|
+
indentation, and `#` comments:
|
|
260
|
+
|
|
261
|
+
```ts
|
|
262
|
+
import { yamlLanguage } from "@codemirror/lang-yaml";
|
|
263
|
+
|
|
264
|
+
const tqlSupport = tql({ Parser, Query, language }, { nested: { yaml: yamlLanguage.parser } });
|
|
15
265
|
```
|
|
266
|
+
|
|
267
|
+
The bare parser of `@lezer/yaml` only highlights the body. A language without
|
|
268
|
+
a parser in `nested` stays part of the TQL tree. The bridge reads the language
|
|
269
|
+
from `#set! injection.language`, not from `@injection.language` captures.
|
|
270
|
+
|
|
271
|
+
## Lifetime and performance
|
|
272
|
+
|
|
273
|
+
Each parse runs tree-sitter on the document, or the prefix where CodeMirror
|
|
274
|
+
asks it to stop, without reusing an earlier tree. A generator yields between
|
|
275
|
+
steps targeting 4 ms, so CodeMirror can continue parsing in the background.
|
|
276
|
+
The queries run combined into one, on subtrees of up to 256 nodes; larger
|
|
277
|
+
subtrees are queried one level at a time instead of repeatedly querying their
|
|
278
|
+
enclosing nodes. Sibling patterns, like the bracket pairs of `brackets.scm`,
|
|
279
|
+
only match among the children of nodes with up to 256 children. A list of
|
|
280
|
+
thousands of items thus keeps each step short, but its own brackets lose their
|
|
281
|
+
bracket props: they still match by name, and the list folds by lines, like
|
|
282
|
+
front matter, rather than between its brackets.
|
|
283
|
+
|
|
284
|
+
Each parse owns its native parser and tree and frees them as soon as their
|
|
285
|
+
work finishes, also on errors. CodeMirror has no cancellation hook for parses
|
|
286
|
+
it drops, so a finalizer frees an abandoned parse's native handles once the
|
|
287
|
+
parse is garbage-collected. Active parses are never evicted, including when
|
|
288
|
+
several editors share the language. To free the combined query and all
|
|
289
|
+
unfinished parses immediately, without waiting for garbage collection, use:
|
|
290
|
+
|
|
291
|
+
```ts
|
|
292
|
+
import type { TqlParser } from "@tenzir/codemirror-lang-tql";
|
|
293
|
+
|
|
294
|
+
(tqlSupport.language.parser as TqlParser).delete();
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
Afterwards, editors must not use the language anymore. Since the language is
|
|
298
|
+
meant to be created once per page, most hosts never need to free it.
|
|
299
|
+
|
|
300
|
+
The Lezer tree also serves views without an editor:
|
|
301
|
+
|
|
302
|
+
```ts
|
|
303
|
+
import { highlightTree } from "@lezer/highlight";
|
|
304
|
+
|
|
305
|
+
const tree = tqlSupport.language.parser.parse(text);
|
|
306
|
+
highlightTree(tree, highlighter, (from, to, classes) => { /* … */ });
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
## Limitations
|
|
310
|
+
|
|
311
|
+
- The bundled queries come from tree-sitter-tql 1.3.0 (`tqlQueries`). A query
|
|
312
|
+
that names a node type the loaded grammar lacks does not compile, and `tql()`
|
|
313
|
+
throws. With a different version of the grammar, pass its queries as
|
|
314
|
+
`highlights`, `indents`, `folds`, `brackets`, and `injections`, for example
|
|
315
|
+
as Vite `?raw` imports.
|
|
316
|
+
- A new line at the very end of the document indents by the nodes around the
|
|
317
|
+
cursor, even right after a string or `/*` comment that is not closed yet.
|
|
318
|
+
CodeMirror only asks the nodes that a position lies inside of, and the end
|
|
319
|
+
of the document lies inside none. Inside a block, such a line takes the
|
|
320
|
+
block's indentation instead of the indentation of the line before.
|
|
321
|
+
- A nested parse over several ranges sees the gaps between them as spaces, so
|
|
322
|
+
a token cannot continue across a gap.
|
|
323
|
+
- Around syntax errors, tree-sitter may recover differently when it resumes a
|
|
324
|
+
parse that yielded than when it parses the whole document at once.
|
|
325
|
+
- The 4 ms target is not a hard deadline. Custom queries that match many
|
|
326
|
+
descendants, large individual tokens, freeing the native tree of a document
|
|
327
|
+
with a million nodes, and garbage collection can make a step take longer.
|
|
328
|
+
- Cleanup of abandoned parses depends on garbage collection and is not
|
|
329
|
+
immediate. Call `delete()` when retiring the language to free all native
|
|
330
|
+
handles deterministically.
|
|
331
|
+
|
|
332
|
+
## Development
|
|
333
|
+
|
|
334
|
+
```sh
|
|
335
|
+
pnpm install
|
|
336
|
+
pnpm build
|
|
337
|
+
pnpm test
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
The tests load the real grammar from `node_modules`.
|