@singapore-editor/tree-sitter-x 0.28.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.
@@ -0,0 +1,1095 @@
1
+ declare module 'web-tree-sitter' {
2
+ /**
3
+ * A position in a multi-line text document, in terms of rows and columns.
4
+ *
5
+ * Rows and columns are zero-based.
6
+ */
7
+ export interface Point {
8
+ /** The zero-based row number. */
9
+ row: number;
10
+ /** The zero-based column number. */
11
+ column: number;
12
+ }
13
+ /**
14
+ * A range of positions in a multi-line text document, both in terms of bytes
15
+ * and of rows and columns.
16
+ */
17
+ export interface Range {
18
+ /** The start position of the range. */
19
+ startPosition: Point;
20
+ /** The end position of the range. */
21
+ endPosition: Point;
22
+ /** The start index of the range. */
23
+ startIndex: number;
24
+ /** The end index of the range. */
25
+ endIndex: number;
26
+ }
27
+ /**
28
+ * A callback for parsing that takes an index and point, and should return a string.
29
+ */
30
+ export type ParseCallback = (index: number, position: Point) => string | undefined;
31
+ /**
32
+ * A callback that receives the parse state during parsing.
33
+ */
34
+ export type ProgressCallback = (progress: ParseState) => boolean;
35
+ /**
36
+ * A callback for logging messages.
37
+ *
38
+ * If `isLex` is `true`, the message is from the lexer, otherwise it's from the parser.
39
+ */
40
+ export type LogCallback = (message: string, isLex: boolean) => void;
41
+ export class Edit {
42
+ /** The start position of the change. */
43
+ startPosition: Point;
44
+ /** The end position of the change before the edit. */
45
+ oldEndPosition: Point;
46
+ /** The end position of the change after the edit. */
47
+ newEndPosition: Point;
48
+ /** The start index of the change. */
49
+ startIndex: number;
50
+ /** The end index of the change before the edit. */
51
+ oldEndIndex: number;
52
+ /** The end index of the change after the edit. */
53
+ newEndIndex: number;
54
+ constructor({ startIndex, oldEndIndex, newEndIndex, startPosition, oldEndPosition, newEndPosition, }: {
55
+ startIndex: number;
56
+ oldEndIndex: number;
57
+ newEndIndex: number;
58
+ startPosition: Point;
59
+ oldEndPosition: Point;
60
+ newEndPosition: Point;
61
+ });
62
+ /**
63
+ * Edit a point and index to keep it in-sync with source code that has been edited.
64
+ *
65
+ * This function updates a single point's byte offset and row/column position
66
+ * based on an edit operation. This is useful for editing points without
67
+ * requiring a tree or node instance.
68
+ */
69
+ editPoint(point: Point, index: number): {
70
+ point: Point;
71
+ index: number;
72
+ };
73
+ /**
74
+ * Edit a range to keep it in-sync with source code that has been edited.
75
+ *
76
+ * This function updates a range's start and end positions based on an edit
77
+ * operation. This is useful for editing ranges without requiring a tree
78
+ * or node instance.
79
+ */
80
+ editRange(range: Range): Range;
81
+ }
82
+ /**
83
+ * Options for parsing
84
+ *
85
+ * The `includedRanges` property is an array of {@link Range} objects that
86
+ * represent the ranges of text that the parser should include when parsing.
87
+ *
88
+ * The `progressCallback` property is a function that is called periodically
89
+ * during parsing to check whether parsing should be cancelled.
90
+ *
91
+ * See {@link Parser#parse} for more information.
92
+ */
93
+ export interface ParseOptions {
94
+ /**
95
+ * An array of {@link Range} objects that
96
+ * represent the ranges of text that the parser should include when parsing.
97
+ *
98
+ * This sets the ranges of text that the parser should include when parsing.
99
+ * By default, the parser will always include entire documents. This
100
+ * function allows you to parse only a *portion* of a document but
101
+ * still return a syntax tree whose ranges match up with the document
102
+ * as a whole. You can also pass multiple disjoint ranges.
103
+ * If `ranges` is empty, then the entire document will be parsed.
104
+ * Otherwise, the given ranges must be ordered from earliest to latest
105
+ * in the document, and they must not overlap. That is, the following
106
+ * must hold for all `i` < `length - 1`:
107
+ * ```text
108
+ * ranges[i].end_byte <= ranges[i + 1].start_byte
109
+ * ```
110
+ */
111
+ includedRanges?: Range[];
112
+ /**
113
+ * A function that is called periodically during parsing to check
114
+ * whether parsing should be cancelled. If the progress callback returns
115
+ * `true`, then parsing will be cancelled. You can also use this to instrument
116
+ * parsing and check where the parser is at in the document. The progress callback
117
+ * takes a single argument, which is a {@link ParseState} representing the current
118
+ * state of the parser.
119
+ */
120
+ progressCallback?: (state: ParseState) => void;
121
+ }
122
+ /**
123
+ * A stateful object that is passed into the progress callback {@link ParseOptions#progressCallback}
124
+ * to provide the current state of the parser.
125
+ */
126
+ export interface ParseState {
127
+ /** The byte offset in the document that the parser is at. */
128
+ currentOffset: number;
129
+ /** Indicates whether the parser has encountered an error during parsing. */
130
+ hasError: boolean;
131
+ }
132
+ /**
133
+ * The latest ABI version that is supported by the current version of the
134
+ * library.
135
+ *
136
+ * When Languages are generated by the Tree-sitter CLI, they are
137
+ * assigned an ABI version number that corresponds to the current CLI version.
138
+ * The Tree-sitter library is generally backwards-compatible with languages
139
+ * generated using older CLI versions, but is not forwards-compatible.
140
+ */
141
+ export let LANGUAGE_VERSION: number;
142
+ /**
143
+ * The earliest ABI version that is supported by the current version of the
144
+ * library.
145
+ */
146
+ export let MIN_COMPATIBLE_VERSION: number;
147
+ /**
148
+ * A stateful object that is used to produce a {@link Tree} based on some
149
+ * source code.
150
+ */
151
+ export class Parser {
152
+ /** The parser's current language. */
153
+ language: Language | null;
154
+ /**
155
+ * This must always be called before creating a Parser.
156
+ *
157
+ * You can optionally pass in options to configure the Wasm module, the most common
158
+ * one being `locateFile` to help the module find the `.wasm` file.
159
+ */
160
+ static init(moduleOptions?: ModuleOptions): Promise<void>;
161
+ /**
162
+ * Create a new parser.
163
+ */
164
+ constructor();
165
+ /** Delete the parser, freeing its resources. */
166
+ delete(): void;
167
+ /**
168
+ * Set the language that the parser should use for parsing.
169
+ *
170
+ * If the language was not successfully assigned, an error will be thrown.
171
+ * This happens if the language was generated with an incompatible
172
+ * version of the Tree-sitter CLI. Check the language's version using
173
+ * {@link Language#version} and compare it to this library's
174
+ * {@link LANGUAGE_VERSION} and {@link MIN_COMPATIBLE_VERSION} constants.
175
+ */
176
+ setLanguage(language: Language | null): this;
177
+ /**
178
+ * Parse a slice of UTF8 text.
179
+ *
180
+ * @param callback - The text to parse, a callback function, or a
181
+ * {@link TextBuffer}, which is read in place.
182
+ *
183
+ * @param oldTree - A previous syntax tree parsed from the same document. If the text of the
184
+ * document has changed since `oldTree` was created, then you must edit `oldTree` to match
185
+ * the new text using {@link Tree#edit}.
186
+ *
187
+ * @param options - Options for parsing the text.
188
+ * This can be used to set the included ranges, or a progress callback.
189
+ *
190
+ * @returns A {@link Tree} if parsing succeeded, or `null` if:
191
+ * - The parser has not yet had a language assigned with {@link Parser#setLanguage}.
192
+ * - The progress callback returned true.
193
+ */
194
+ parse(callback: string | ParseCallback | TextBuffer, oldTree?: Tree | null, options?: ParseOptions): Tree | null;
195
+ /**
196
+ * Instruct the parser to start the next parse from the beginning.
197
+ *
198
+ * If the parser previously failed because of a callback,
199
+ * then by default, it will resume where it left off on the
200
+ * next call to {@link Parser#parse} or other parsing functions.
201
+ * If you don't want to resume, and instead intend to use this parser to
202
+ * parse some other document, you must call `reset` first.
203
+ */
204
+ reset(): void;
205
+ /** Get the ranges of text that the parser will include when parsing. */
206
+ getIncludedRanges(): Range[];
207
+ /** Set the logging callback that a parser should use during parsing. */
208
+ setLogger(callback: LogCallback | boolean | null): this;
209
+ /** Get the parser's current logger. */
210
+ getLogger(): LogCallback | null;
211
+ }
212
+ interface LanguageMetadata {
213
+ readonly major_version: number;
214
+ readonly minor_version: number;
215
+ readonly patch_version: number;
216
+ }
217
+ /**
218
+ * An opaque object that defines how to parse a particular language.
219
+ * The code for each `Language` is generated by the Tree-sitter CLI.
220
+ */
221
+ export class Language {
222
+ /**
223
+ * A list of all node types in the language. The index of each type in this
224
+ * array is its node type id.
225
+ */
226
+ types: string[];
227
+ /**
228
+ * A list of all field names in the language. The index of each field name in
229
+ * this array is its field id.
230
+ */
231
+ fields: (string | null)[];
232
+ /**
233
+ * Gets the name of the language.
234
+ */
235
+ get name(): string | null;
236
+ /**
237
+ * Gets the ABI version of the language.
238
+ */
239
+ get abiVersion(): number;
240
+ /**
241
+ * Get the metadata for this language. This information is generated by the
242
+ * CLI, and relies on the language author providing the correct metadata in
243
+ * the language's `tree-sitter.json` file.
244
+ */
245
+ get metadata(): LanguageMetadata | null;
246
+ /**
247
+ * Gets the number of fields in the language.
248
+ */
249
+ get fieldCount(): number;
250
+ /**
251
+ * Gets the number of states in the language.
252
+ */
253
+ get stateCount(): number;
254
+ /**
255
+ * Get the field id for a field name.
256
+ */
257
+ fieldIdForName(fieldName: string): number | null;
258
+ /**
259
+ * Get the field name for a field id.
260
+ */
261
+ fieldNameForId(fieldId: number): string | null;
262
+ /**
263
+ * Get the node type id for a node type name.
264
+ */
265
+ idForNodeType(type: string, named: boolean): number | null;
266
+ /**
267
+ * Gets the number of node types in the language.
268
+ */
269
+ get nodeTypeCount(): number;
270
+ /**
271
+ * Get the node type name for a node type id.
272
+ */
273
+ nodeTypeForId(typeId: number): string | null;
274
+ /**
275
+ * Check if a node type is named.
276
+ *
277
+ * @see {@link https://tree-sitter.github.io/tree-sitter/using-parsers/2-basic-parsing.html#named-vs-anonymous-nodes}
278
+ */
279
+ nodeTypeIsNamed(typeId: number): boolean;
280
+ /**
281
+ * Check if a node type is visible.
282
+ */
283
+ nodeTypeIsVisible(typeId: number): boolean;
284
+ /**
285
+ * Get the supertypes ids of this language.
286
+ *
287
+ * @see {@link https://tree-sitter.github.io/tree-sitter/using-parsers/6-static-node-types.html?highlight=supertype#supertype-nodes}
288
+ */
289
+ get supertypes(): number[];
290
+ /**
291
+ * Get the subtype ids for a given supertype node id.
292
+ */
293
+ subtypes(supertype: number): number[];
294
+ /**
295
+ * Get the next state id for a given state id and node type id.
296
+ */
297
+ nextState(stateId: number, typeId: number): number;
298
+ /**
299
+ * Create a new lookahead iterator for this language and parse state.
300
+ *
301
+ * This returns `null` if state is invalid for this language.
302
+ *
303
+ * Iterating {@link LookaheadIterator} will yield valid symbols in the given
304
+ * parse state. A newly created iterator is not positioned on a symbol, so
305
+ * {@link LookaheadIterator#currentType} returns `null` until the first
306
+ * iteration step.
307
+ *
308
+ * Lookahead iterators can be useful for generating suggestions and improving
309
+ * syntax error diagnostics. To get symbols valid in an `ERROR` node, use the
310
+ * lookahead iterator on its first leaf node state. For `MISSING` nodes, a
311
+ * lookahead iterator created on the previous non-extra leaf node may be
312
+ * appropriate.
313
+ */
314
+ lookaheadIterator(stateId: number): LookaheadIterator | null;
315
+ /**
316
+ * Load a language from a WebAssembly module.
317
+ * The module can be provided as a path to a file, a `URL` to a file, or as a
318
+ * buffer.
319
+ */
320
+ static load(input: string | URL | Uint8Array): Promise<Language>;
321
+ private static loadFromWasmExports;
322
+ /**
323
+ * Load a language synchronously from a pre-compiled WebAssembly module.
324
+ * Use this when the host environment provides a `WebAssembly.Module` directly.
325
+ */
326
+ static loadSync(wasmModule: WebAssembly.Module): Language;
327
+ }
328
+ /** A tree that represents the syntactic structure of a source code file. */
329
+ export class Tree {
330
+ /** The language that was used to parse the syntax tree. */
331
+ language: Language;
332
+ /** Create a shallow copy of the syntax tree. This is very fast. */
333
+ copy(): Tree;
334
+ /** Delete the syntax tree, freeing its resources. */
335
+ delete(): void;
336
+ /** Get the root node of the syntax tree. */
337
+ get rootNode(): Node;
338
+ /**
339
+ * Get the root node of the syntax tree, but with its position shifted
340
+ * forward by the given offset.
341
+ */
342
+ rootNodeWithOffset(offsetBytes: number, offsetExtent: Point): Node;
343
+ /**
344
+ * Edit the syntax tree to keep it in sync with source code that has been
345
+ * edited.
346
+ *
347
+ * You must describe the edit both in terms of byte offsets and in terms of
348
+ * row/column coordinates.
349
+ */
350
+ edit(edit: Edit): void;
351
+ /** Create a new {@link TreeCursor} starting from the root of the tree. */
352
+ walk(): TreeCursor;
353
+ /**
354
+ * Compare this old edited syntax tree to a new syntax tree representing
355
+ * the same document, returning a sequence of ranges whose syntactic
356
+ * structure has changed.
357
+ *
358
+ * For this to work correctly, this syntax tree must have been edited such
359
+ * that its ranges match up to the new tree. Generally, you'll want to
360
+ * call this method right after calling one of the [`Parser::parse`]
361
+ * functions. Call it on the old tree that was passed to parse, and
362
+ * pass the new tree that was returned from `parse`.
363
+ */
364
+ getChangedRanges(other: Tree): Range[];
365
+ /** Get the included ranges that were used to parse the syntax tree. */
366
+ getIncludedRanges(): Range[];
367
+ }
368
+ /**
369
+ * UTF-16 text kept in the parser's memory. {@link Parser#parse} reads it in place, and
370
+ * {@link TextBuffer#edit} moves only the text after the edit, so a keystroke copies the
371
+ * inserted characters instead of the whole document.
372
+ *
373
+ * Trees parsed from a buffer read their text from it, so they see later edits.
374
+ */
375
+ export class TextBuffer {
376
+ /** The length of the text, in UTF-16 code units. */
377
+ length: number;
378
+ constructor(text?: string);
379
+ /** Replace the whole text. */
380
+ set(text: string): void;
381
+ /** Replace the code units in `[startIndex, oldEndIndex)` with `text`. */
382
+ edit(startIndex: number, oldEndIndex: number, text: string): void;
383
+ /** The text in `[startIndex, endIndex)`. */
384
+ slice(startIndex?: number, endIndex?: number): string;
385
+ private chunkStart;
386
+ private chunk;
387
+ /** The text from `index` on, in chunks, for {@link Tree#textCallback}. */
388
+ readonly read: (index: number) => string;
389
+ /** Free the buffer's memory. */
390
+ delete(): void;
391
+ private units;
392
+ private write;
393
+ private reserve;
394
+ }
395
+ /** The functions an extension module exports. */
396
+ export type ExtensionExports = Record<string, unknown>;
397
+ /**
398
+ * Load a C extension: a module built like a grammar (`-fPIC -shared`, no libc) that calls
399
+ * tree-sitter's C API directly. It shares the parser's memory, so it can read trees and
400
+ * languages by address (`tree[0]`, `language[0]`) with no copying. Its imports resolve to
401
+ * the runtime's exports: the public C API and the libc functions grammars may use.
402
+ *
403
+ * Call {@link Parser.init} first.
404
+ */
405
+ export function loadExtension(binary: Uint8Array | WebAssembly.Module): Promise<ExtensionExports>;
406
+ /** The parser's memory, for exchanging data with extensions. Growth replaces the view. */
407
+ export function heap(): Uint8Array;
408
+ /** A single node within a syntax {@link Tree}. */
409
+ export class Node {
410
+ /**
411
+ * The numeric id for this node that is unique.
412
+ *
413
+ * Within a given syntax tree, no two nodes have the same id. However:
414
+ *
415
+ * * If a new tree is created based on an older tree, and a node from the old tree is reused in
416
+ * the process, then that node will have the same id in both trees.
417
+ *
418
+ * * A node not marked as having changes does not guarantee it was reused.
419
+ *
420
+ * * If a node is marked as having changed in the old tree, it will not be reused.
421
+ */
422
+ id: number;
423
+ /** The byte index where this node starts. */
424
+ startIndex: number;
425
+ /** The position where this node starts. */
426
+ startPosition: Point;
427
+ /** The tree that this node belongs to. */
428
+ tree: Tree;
429
+ /** Get this node's type as a numerical id. */
430
+ get typeId(): number;
431
+ /**
432
+ * Get the node's type as a numerical id as it appears in the grammar,
433
+ * ignoring aliases.
434
+ */
435
+ get grammarId(): number;
436
+ /** Get this node's type as a string. */
437
+ get type(): string;
438
+ /**
439
+ * Get this node's symbol name as it appears in the grammar, ignoring
440
+ * aliases as a string.
441
+ */
442
+ get grammarType(): string;
443
+ /**
444
+ * Check if this node is *named*.
445
+ *
446
+ * Named nodes correspond to named rules in the grammar, whereas
447
+ * *anonymous* nodes correspond to string literals in the grammar.
448
+ */
449
+ get isNamed(): boolean;
450
+ /**
451
+ * Check if this node is *extra*.
452
+ *
453
+ * Extra nodes represent things like comments, which are not required
454
+ * by the grammar, but can appear anywhere.
455
+ */
456
+ get isExtra(): boolean;
457
+ /**
458
+ * Check if this node represents a syntax error.
459
+ *
460
+ * Syntax errors represent parts of the code that could not be incorporated
461
+ * into a valid syntax tree.
462
+ */
463
+ get isError(): boolean;
464
+ /**
465
+ * Check if this node is *missing*.
466
+ *
467
+ * Missing nodes are inserted by the parser in order to recover from
468
+ * certain kinds of syntax errors.
469
+ */
470
+ get isMissing(): boolean;
471
+ /** Check if this node has been edited. */
472
+ get hasChanges(): boolean;
473
+ /**
474
+ * Check if this node represents a syntax error or contains any syntax
475
+ * errors anywhere within it.
476
+ */
477
+ get hasError(): boolean;
478
+ /** Get the byte index where this node ends. */
479
+ get endIndex(): number;
480
+ /** Get the position where this node ends. */
481
+ get endPosition(): Point;
482
+ /** Get the string content of this node. */
483
+ get text(): string;
484
+ /** Get this node's parse state. */
485
+ get parseState(): number;
486
+ /** Get the parse state after this node. */
487
+ get nextParseState(): number;
488
+ /** Check if this node is equal to another node. */
489
+ equals(other: Node): boolean;
490
+ /**
491
+ * Get the node's child at the given index, where zero represents the first child.
492
+ *
493
+ * This method is fairly fast, but its cost is technically log(n), so if
494
+ * you might be iterating over a long list of children, you should use
495
+ * {@link Node#children} instead.
496
+ */
497
+ child(index: number): Node | null;
498
+ /**
499
+ * Get this node's *named* child at the given index.
500
+ *
501
+ * See also {@link Node#isNamed}.
502
+ * This method is fairly fast, but its cost is technically log(n), so if
503
+ * you might be iterating over a long list of children, you should use
504
+ * {@link Node#namedChildren} instead.
505
+ */
506
+ namedChild(index: number): Node | null;
507
+ /**
508
+ * Get this node's child with the given numerical field id.
509
+ *
510
+ * See also {@link Node#childForFieldName}. You can
511
+ * convert a field name to an id using {@link Language#fieldIdForName}.
512
+ */
513
+ childForFieldId(fieldId: number): Node | null;
514
+ /**
515
+ * Get the first child with the given field name.
516
+ *
517
+ * If multiple children may have the same field name, access them using
518
+ * {@link Node#childrenForFieldName}.
519
+ */
520
+ childForFieldName(fieldName: string): Node | null;
521
+ /** Get the field name of this node's child at the given index. */
522
+ fieldNameForChild(index: number): string | null;
523
+ /** Get the field name of this node's named child at the given index. */
524
+ fieldNameForNamedChild(index: number): string | null;
525
+ /**
526
+ * Get an array of this node's children with a given field name.
527
+ *
528
+ * See also {@link Node#children}.
529
+ */
530
+ childrenForFieldName(fieldName: string): Node[];
531
+ /**
532
+ * Get an array of this node's children with a given field id.
533
+ *
534
+ * See also {@link Node#childrenForFieldName}.
535
+ */
536
+ childrenForFieldId(fieldId: number): Node[];
537
+ /** Get the node's first child that contains or starts after the given byte offset. */
538
+ firstChildForIndex(index: number): Node | null;
539
+ /** Get the node's first named child that contains or starts after the given byte offset. */
540
+ firstNamedChildForIndex(index: number): Node | null;
541
+ /** Get this node's number of children. */
542
+ get childCount(): number;
543
+ /**
544
+ * Get this node's number of *named* children.
545
+ *
546
+ * See also {@link Node#isNamed}.
547
+ */
548
+ get namedChildCount(): number;
549
+ /** Get this node's first child. */
550
+ get firstChild(): Node | null;
551
+ /**
552
+ * Get this node's first named child.
553
+ *
554
+ * See also {@link Node#isNamed}.
555
+ */
556
+ get firstNamedChild(): Node | null;
557
+ /** Get this node's last child. */
558
+ get lastChild(): Node | null;
559
+ /**
560
+ * Get this node's last named child.
561
+ *
562
+ * See also {@link Node#isNamed}.
563
+ */
564
+ get lastNamedChild(): Node | null;
565
+ /**
566
+ * Iterate over this node's children.
567
+ *
568
+ * If you're walking the tree recursively, you may want to use the
569
+ * {@link TreeCursor} APIs directly instead.
570
+ */
571
+ get children(): Node[];
572
+ /**
573
+ * Iterate over this node's named children.
574
+ *
575
+ * See also {@link Node#children}.
576
+ */
577
+ get namedChildren(): Node[];
578
+ /**
579
+ * Get the descendants of this node that are the given type, or in the given types array.
580
+ *
581
+ * The types array should contain node type strings, which can be retrieved from {@link Language#types}.
582
+ *
583
+ * Additionally, a `startPosition` and `endPosition` can be passed in to restrict the search to a byte range.
584
+ */
585
+ descendantsOfType(types: string | string[], startPosition?: Point, endPosition?: Point): Node[];
586
+ /** Get this node's next sibling. */
587
+ get nextSibling(): Node | null;
588
+ /** Get this node's previous sibling. */
589
+ get previousSibling(): Node | null;
590
+ /**
591
+ * Get this node's next *named* sibling.
592
+ *
593
+ * See also {@link Node#isNamed}.
594
+ */
595
+ get nextNamedSibling(): Node | null;
596
+ /**
597
+ * Get this node's previous *named* sibling.
598
+ *
599
+ * See also {@link Node#isNamed}.
600
+ */
601
+ get previousNamedSibling(): Node | null;
602
+ /** Get the node's number of descendants, including one for the node itself. */
603
+ get descendantCount(): number;
604
+ /**
605
+ * Get this node's immediate parent.
606
+ * Prefer {@link Node#childWithDescendant} for iterating over this node's ancestors.
607
+ */
608
+ get parent(): Node | null;
609
+ /**
610
+ * Get the node that contains `descendant`.
611
+ *
612
+ * Note that this can return `descendant` itself.
613
+ */
614
+ childWithDescendant(descendant: Node): Node | null;
615
+ /** Get the smallest node within this node that spans the given byte range. */
616
+ descendantForIndex(start: number, end?: number): Node | null;
617
+ /** Get the smallest named node within this node that spans the given byte range. */
618
+ namedDescendantForIndex(start: number, end?: number): Node | null;
619
+ /** Get the smallest node within this node that spans the given point range. */
620
+ descendantForPosition(start: Point, end?: Point): Node | null;
621
+ /** Get the smallest named node within this node that spans the given point range. */
622
+ namedDescendantForPosition(start: Point, end?: Point): Node | null;
623
+ /**
624
+ * Create a new {@link TreeCursor} starting from this node.
625
+ *
626
+ * Note that the given node is considered the root of the cursor,
627
+ * and the cursor cannot walk outside this node.
628
+ */
629
+ walk(): TreeCursor;
630
+ /**
631
+ * Edit this node to keep it in-sync with source code that has been edited.
632
+ *
633
+ * This function is only rarely needed. When you edit a syntax tree with
634
+ * the {@link Tree#edit} method, all of the nodes that you retrieve from
635
+ * the tree afterward will already reflect the edit. You only need to
636
+ * use {@link Node#edit} when you have a specific {@link Node} instance that
637
+ * you want to keep and continue to use after an edit.
638
+ */
639
+ edit(edit: Edit): void;
640
+ /** Get the S-expression representation of this node. */
641
+ toString(): string;
642
+ }
643
+ /** A stateful object for walking a syntax {@link Tree} efficiently. */
644
+ export class TreeCursor {
645
+ /** Creates a deep copy of the tree cursor. This allocates new memory. */
646
+ copy(): TreeCursor;
647
+ /** Delete the tree cursor, freeing its resources. */
648
+ delete(): void;
649
+ /** Get the tree cursor's current {@link Node}. */
650
+ get currentNode(): Node;
651
+ /**
652
+ * Get the numerical field id of this tree cursor's current node.
653
+ *
654
+ * See also {@link TreeCursor#currentFieldName}.
655
+ */
656
+ get currentFieldId(): number;
657
+ /** Get the field name of this tree cursor's current node. */
658
+ get currentFieldName(): string | null;
659
+ /**
660
+ * Get the depth of the cursor's current node relative to the original
661
+ * node that the cursor was constructed with.
662
+ */
663
+ get currentDepth(): number;
664
+ /**
665
+ * Get the index of the cursor's current node out of all of the
666
+ * descendants of the original node that the cursor was constructed with.
667
+ */
668
+ get currentDescendantIndex(): number;
669
+ /** Get the type of the cursor's current node. */
670
+ get nodeType(): string;
671
+ /** Get the type id of the cursor's current node. */
672
+ get nodeTypeId(): number;
673
+ /** Get the state id of the cursor's current node. */
674
+ get nodeStateId(): number;
675
+ /** Get the id of the cursor's current node. */
676
+ get nodeId(): number;
677
+ /**
678
+ * Check if the cursor's current node is *named*.
679
+ *
680
+ * Named nodes correspond to named rules in the grammar, whereas
681
+ * *anonymous* nodes correspond to string literals in the grammar.
682
+ */
683
+ get nodeIsNamed(): boolean;
684
+ /**
685
+ * Check if the cursor's current node is *missing*.
686
+ *
687
+ * Missing nodes are inserted by the parser in order to recover from
688
+ * certain kinds of syntax errors.
689
+ */
690
+ get nodeIsMissing(): boolean;
691
+ /** Get the string content of the cursor's current node. */
692
+ get nodeText(): string;
693
+ /** Get the start position of the cursor's current node. */
694
+ get startPosition(): Point;
695
+ /** Get the end position of the cursor's current node. */
696
+ get endPosition(): Point;
697
+ /** Get the start index of the cursor's current node. */
698
+ get startIndex(): number;
699
+ /** Get the end index of the cursor's current node. */
700
+ get endIndex(): number;
701
+ /**
702
+ * Move this cursor to the first child of its current node.
703
+ *
704
+ * This returns `true` if the cursor successfully moved, and returns
705
+ * `false` if there were no children.
706
+ */
707
+ gotoFirstChild(): boolean;
708
+ /**
709
+ * Move this cursor to the last child of its current node.
710
+ *
711
+ * This returns `true` if the cursor successfully moved, and returns
712
+ * `false` if there were no children.
713
+ *
714
+ * Note that this function may be slower than
715
+ * {@link TreeCursor#gotoFirstChild} because it needs to
716
+ * iterate through all the children to compute the child's position.
717
+ */
718
+ gotoLastChild(): boolean;
719
+ /**
720
+ * Move this cursor to the parent of its current node.
721
+ *
722
+ * This returns `true` if the cursor successfully moved, and returns
723
+ * `false` if there was no parent node (the cursor was already on the
724
+ * root node).
725
+ *
726
+ * Note that the node the cursor was constructed with is considered the root
727
+ * of the cursor, and the cursor cannot walk outside this node.
728
+ */
729
+ gotoParent(): boolean;
730
+ /**
731
+ * Move this cursor to the next sibling of its current node.
732
+ *
733
+ * This returns `true` if the cursor successfully moved, and returns
734
+ * `false` if there was no next sibling node.
735
+ *
736
+ * Note that the node the cursor was constructed with is considered the root
737
+ * of the cursor, and the cursor cannot walk outside this node.
738
+ */
739
+ gotoNextSibling(): boolean;
740
+ /**
741
+ * Move this cursor to the previous sibling of its current node.
742
+ *
743
+ * This returns `true` if the cursor successfully moved, and returns
744
+ * `false` if there was no previous sibling node.
745
+ *
746
+ * Note that this function may be slower than
747
+ * {@link TreeCursor#gotoNextSibling} due to how node
748
+ * positions are stored. In the worst case, this will need to iterate
749
+ * through all the children up to the previous sibling node to recalculate
750
+ * its position. Also note that the node the cursor was constructed with is
751
+ * considered the root of the cursor, and the cursor cannot walk outside this node.
752
+ */
753
+ gotoPreviousSibling(): boolean;
754
+ /**
755
+ * Move the cursor to the node that is the nth descendant of
756
+ * the original node that the cursor was constructed with, where
757
+ * zero represents the original node itself.
758
+ */
759
+ gotoDescendant(goalDescendantIndex: number): void;
760
+ /**
761
+ * Move this cursor to the first child of its current node that contains or
762
+ * starts after the given byte offset.
763
+ *
764
+ * This returns `true` if the cursor successfully moved to a child node, and returns
765
+ * `false` if no such child was found.
766
+ */
767
+ gotoFirstChildForIndex(goalIndex: number): boolean;
768
+ /**
769
+ * Move this cursor to the first child of its current node that contains or
770
+ * starts after the given byte offset.
771
+ *
772
+ * This returns the index of the child node if one was found, and returns
773
+ * `null` if no such child was found.
774
+ */
775
+ gotoFirstChildForPosition(goalPosition: Point): boolean;
776
+ /**
777
+ * Re-initialize this tree cursor to start at the original node that the
778
+ * cursor was constructed with.
779
+ */
780
+ reset(node: Node): void;
781
+ /**
782
+ * Re-initialize a tree cursor to the same position as another cursor.
783
+ *
784
+ * Unlike {@link TreeCursor#reset}, this will not lose parent
785
+ * information and allows reusing already created cursors.
786
+ */
787
+ resetTo(cursor: TreeCursor): void;
788
+ }
789
+ /**
790
+ * Options for query execution
791
+ */
792
+ export interface QueryOptions {
793
+ /** The start position of the range to query */
794
+ startPosition?: Point;
795
+ /** The end position of the range to query */
796
+ endPosition?: Point;
797
+ /** The start position of the range to query Only the matches that are fully
798
+ * contained within provided range will be returned.
799
+ **/
800
+ startContainingPosition?: Point;
801
+ /** The end position of the range to query Only the matches that are fully
802
+ * contained within provided range will be returned.
803
+ **/
804
+ endContainingPosition?: Point;
805
+ /** The start index of the range to query */
806
+ startIndex?: number;
807
+ /** The end index of the range to query */
808
+ endIndex?: number;
809
+ /** The start index of the range to query Only the matches that are fully
810
+ * contained within provided range will be returned.
811
+ **/
812
+ startContainingIndex?: number;
813
+ /** The end index of the range to query Only the matches that are fully
814
+ * contained within provided range will be returned.
815
+ **/
816
+ endContainingIndex?: number;
817
+ /**
818
+ * The maximum number of in-progress matches for this query.
819
+ * The limit must be > 0 and <= 65536.
820
+ */
821
+ matchLimit?: number;
822
+ /**
823
+ * The maximum start depth for a query cursor.
824
+ *
825
+ * This prevents cursors from exploring children nodes at a certain depth.
826
+ * Note if a pattern includes many children, then they will still be
827
+ * checked.
828
+ *
829
+ * The zero max start depth value can be used as a special behavior and
830
+ * it helps to destructure a subtree by staying on a node and using
831
+ * captures for interested parts. Note that the zero max start depth
832
+ * only limit a search depth for a pattern's root node but other nodes
833
+ * that are parts of the pattern may be searched at any depth what
834
+ * defined by the pattern structure.
835
+ *
836
+ * Set to `null` to remove the maximum start depth.
837
+ */
838
+ maxStartDepth?: number;
839
+ /**
840
+ * A function that will be called periodically during the execution of the query to check
841
+ * if query execution should be cancelled. You can also use this to instrument query execution
842
+ * and check where the query is at in the document. The progress callback takes a single argument,
843
+ * which is a {@link QueryState} representing the current state of the query.
844
+ */
845
+ progressCallback?: (state: QueryState) => void;
846
+ }
847
+ /**
848
+ * A stateful object that is passed into the progress callback {@link QueryOptions#progressCallback}
849
+ * to provide the current state of the query.
850
+ */
851
+ export interface QueryState {
852
+ /** The byte offset in the document that the query is at. */
853
+ currentOffset: number;
854
+ }
855
+ /** A record of key-value pairs associated with a particular pattern in a {@link Query}. */
856
+ export type QueryProperties = Record<string, string | null>;
857
+ /**
858
+ * A predicate that contains an operator and list of operands.
859
+ */
860
+ export interface QueryPredicate {
861
+ /** The operator of the predicate, like `match?`, `eq?`, `set!`, etc. */
862
+ operator: string;
863
+ /** The operands of the predicate, which are either captures or strings. */
864
+ operands: PredicateStep[];
865
+ }
866
+ /**
867
+ * A particular {@link Node} that has been captured with a particular name within a
868
+ * {@link Query}.
869
+ */
870
+ export interface QueryCapture {
871
+ /** The index of the pattern that matched. */
872
+ patternIndex: number;
873
+ /** The name of the capture */
874
+ name: string;
875
+ /** The captured node */
876
+ node: Node;
877
+ /** The properties for predicates declared with the operator `set!`. */
878
+ setProperties?: QueryProperties;
879
+ /** The properties for predicates declared with the operator `is?`. */
880
+ assertedProperties?: QueryProperties;
881
+ /** The properties for predicates declared with the operator `is-not?`. */
882
+ refutedProperties?: QueryProperties;
883
+ }
884
+ /** A match of a {@link Query} to a particular set of {@link Node}s. */
885
+ export interface QueryMatch {
886
+ /** The index of the pattern that matched. */
887
+ patternIndex: number;
888
+ /** The captures associated with the match. */
889
+ captures: QueryCapture[];
890
+ /** The properties for predicates declared with the operator `set!`. */
891
+ setProperties?: QueryProperties;
892
+ /** The properties for predicates declared with the operator `is?`. */
893
+ assertedProperties?: QueryProperties;
894
+ /** The properties for predicates declared with the operator `is-not?`. */
895
+ refutedProperties?: QueryProperties;
896
+ }
897
+ /** A quantifier for captures */
898
+ export const CaptureQuantifier: {
899
+ readonly Zero: 0;
900
+ readonly ZeroOrOne: 1;
901
+ readonly ZeroOrMore: 2;
902
+ readonly One: 3;
903
+ readonly OneOrMore: 4;
904
+ };
905
+ /** A quantifier for captures */
906
+ export type CaptureQuantifier = typeof CaptureQuantifier[keyof typeof CaptureQuantifier];
907
+ /**
908
+ * Predicates are represented as a single array of steps. There are two
909
+ * types of steps, which correspond to the two legal values for
910
+ * the `type` field:
911
+ *
912
+ * - `CapturePredicateStep` - Steps with this type represent names
913
+ * of captures.
914
+ *
915
+ * - `StringPredicateStep` - Steps with this type represent literal
916
+ * strings.
917
+ */
918
+ export type PredicateStep = CapturePredicateStep | StringPredicateStep;
919
+ /**
920
+ * A step in a predicate that refers to a capture.
921
+ *
922
+ * The `name` field is the name of the capture.
923
+ */
924
+ interface CapturePredicateStep {
925
+ type: 'capture';
926
+ name: string;
927
+ }
928
+ /**
929
+ * A step in a predicate that refers to a string.
930
+ *
931
+ * The `value` field is the string value.
932
+ */
933
+ interface StringPredicateStep {
934
+ type: 'string';
935
+ value: string;
936
+ }
937
+ export class Query {
938
+ /** The names of the captures used in the query. */
939
+ readonly captureNames: string[];
940
+ /** The quantifiers of the captures used in the query. */
941
+ readonly captureQuantifiers: CaptureQuantifier[][];
942
+ /**
943
+ * The other user-defined predicates associated with the given index.
944
+ *
945
+ * This includes predicates with operators other than:
946
+ * - `match?`
947
+ * - `eq?` and `not-eq?`
948
+ * - `any-of?` and `not-any-of?`
949
+ * - `is?` and `is-not?`
950
+ * - `set!`
951
+ */
952
+ readonly predicates: QueryPredicate[][];
953
+ /** The properties for predicates with the operator `set!`. */
954
+ readonly setProperties: QueryProperties[];
955
+ /** The properties for predicates with the operator `is?`. */
956
+ readonly assertedProperties: QueryProperties[];
957
+ /** The properties for predicates with the operator `is-not?`. */
958
+ readonly refutedProperties: QueryProperties[];
959
+ /** The maximum number of in-progress matches for this cursor. */
960
+ matchLimit?: number;
961
+ /**
962
+ * Create a new query from a string containing one or more S-expression
963
+ * patterns.
964
+ *
965
+ * The query is associated with a particular language, and can only be run
966
+ * on syntax nodes parsed with that language. References to Queries can be
967
+ * shared between multiple threads.
968
+ *
969
+ * @link {@see https://tree-sitter.github.io/tree-sitter/using-parsers/queries}
970
+ */
971
+ constructor(language: Language, source: string);
972
+ /** Delete the query, freeing its resources. */
973
+ delete(): void;
974
+ /**
975
+ * Iterate over all of the matches in the order that they were found.
976
+ *
977
+ * Each match contains the index of the pattern that matched, and a list of
978
+ * captures. Because multiple patterns can match the same set of nodes,
979
+ * one match may contain captures that appear *before* some of the
980
+ * captures from a previous match.
981
+ *
982
+ * @param node - The node to execute the query on.
983
+ *
984
+ * @param options - Options for query execution.
985
+ */
986
+ matches(node: Node, options?: QueryOptions): QueryMatch[];
987
+ /**
988
+ * Iterate over all of the individual captures in the order that they
989
+ * appear.
990
+ *
991
+ * This is useful if you don't care about which pattern matched, and just
992
+ * want a single, ordered sequence of captures.
993
+ *
994
+ * @param node - The node to execute the query on.
995
+ *
996
+ * @param options - Options for query execution.
997
+ */
998
+ captures(node: Node, options?: QueryOptions): QueryCapture[];
999
+ /** Get the predicates for a given pattern. */
1000
+ predicatesForPattern(patternIndex: number): QueryPredicate[];
1001
+ /**
1002
+ * Disable a certain capture within a query.
1003
+ *
1004
+ * This prevents the capture from being returned in matches, and also
1005
+ * avoids any resource usage associated with recording the capture.
1006
+ */
1007
+ disableCapture(captureName: string): void;
1008
+ /**
1009
+ * Disable a certain pattern within a query.
1010
+ *
1011
+ * This prevents the pattern from matching, and also avoids any resource
1012
+ * usage associated with the pattern. This throws an error if the pattern
1013
+ * index is out of bounds.
1014
+ */
1015
+ disablePattern(patternIndex: number): void;
1016
+ /**
1017
+ * Check if, on its last execution, this cursor exceeded its maximum number
1018
+ * of in-progress matches.
1019
+ */
1020
+ didExceedMatchLimit(): boolean;
1021
+ /** Get the byte offset where the given pattern starts in the query's source. */
1022
+ startIndexForPattern(patternIndex: number): number;
1023
+ /** Get the byte offset where the given pattern ends in the query's source. */
1024
+ endIndexForPattern(patternIndex: number): number;
1025
+ /** Get the number of patterns in the query. */
1026
+ patternCount(): number;
1027
+ /** Get the index for a given capture name. */
1028
+ captureIndexForName(captureName: string): number;
1029
+ /** Check if a given pattern within a query has a single root node. */
1030
+ isPatternRooted(patternIndex: number): boolean;
1031
+ /** Check if a given pattern within a query has a single root node. */
1032
+ isPatternNonLocal(patternIndex: number): boolean;
1033
+ /**
1034
+ * Check if a given step in a query is 'definite'.
1035
+ *
1036
+ * A query step is 'definite' if its parent pattern will be guaranteed to
1037
+ * match successfully once it reaches the step.
1038
+ */
1039
+ isPatternGuaranteedAtStep(byteIndex: number): boolean;
1040
+ }
1041
+ export class LookaheadIterator implements Iterable<string> {
1042
+ /**
1043
+ * Get the current symbol of the lookahead iterator.
1044
+ *
1045
+ * Returns `null` if the iterator is not positioned on a symbol:
1046
+ *
1047
+ * - Before the first iteration step
1048
+ * - After the iterator is exhausted
1049
+ * - After a {@link reset} or {@link resetState} call
1050
+ */
1051
+ get currentTypeId(): number | null;
1052
+ /**
1053
+ * Get the current symbol name of the lookahead iterator.
1054
+ *
1055
+ * Returns `null` if the iterator is not positioned on a symbol.
1056
+ */
1057
+ get currentType(): string | null;
1058
+ /** Delete the lookahead iterator, freeing its resources. */
1059
+ delete(): void;
1060
+ /**
1061
+ * Reset the lookahead iterator.
1062
+ *
1063
+ * This returns `true` if the language was set successfully and `false`
1064
+ * otherwise.
1065
+ */
1066
+ reset(language: Language, stateId: number): boolean;
1067
+ /**
1068
+ * Reset the lookahead iterator to another state.
1069
+ *
1070
+ * This returns `true` if the iterator was reset to the given state and
1071
+ * `false` otherwise.
1072
+ */
1073
+ resetState(stateId: number): boolean;
1074
+ /**
1075
+ * Returns an iterator that iterates over the symbols of the lookahead iterator.
1076
+ *
1077
+ * The iterator will yield the current symbol name as a string for each step
1078
+ * until there are no more symbols to iterate over.
1079
+ */
1080
+ [Symbol.iterator](): Iterator<string>;
1081
+ }
1082
+ /**
1083
+ * Options for {@link createModule}, a subset of Emscripten's module options.
1084
+ */
1085
+ interface ModuleOptions {
1086
+ /** Maps the runtime's file name to its URL or path. */
1087
+ locateFile?: (path: string, scriptDirectory: string) => string;
1088
+ /** The runtime's bytes, or a compiled module, instead of fetching it. */
1089
+ wasmBinary?: ArrayBufferView | ArrayBuffer | WebAssembly.Module;
1090
+ }
1091
+
1092
+ export {};
1093
+ }
1094
+
1095
+ //# sourceMappingURL=web-tree-sitter.d.ts.map