@codemirror/language 0.17.4 → 0.18.2

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/dist/index.d.ts CHANGED
@@ -1,13 +1,25 @@
1
- import { NodeProp, Input, ParseContext, PartialParse, Tree, TreeFragment, SyntaxNode } from 'lezer-tree';
1
+ import { NodeProp, NodeType, Input, ParseContext, PartialParse, Tree, TreeFragment, SyntaxNode } from 'lezer-tree';
2
2
  import { Parser, ParserConfig } from 'lezer';
3
3
  import { Facet, Extension, EditorState } from '@codemirror/state';
4
4
  import { Line } from '@codemirror/text';
5
5
 
6
+ /**
7
+ Node prop stored in a grammar's top syntax node to provide the
8
+ facet that stores language data for that language.
9
+ */
6
10
  declare const languageDataProp: NodeProp<Facet<{
7
11
  [name: string]: any;
8
12
  }, readonly {
9
13
  [name: string]: any;
10
14
  }[]>>;
15
+ /**
16
+ Helper function to define a facet (to be added to the top syntax
17
+ node(s) for a language via
18
+ [`languageDataProp`](https://codemirror.net/6/docs/ref/#language.languageDataProp)), that will be
19
+ used to associate language data with the language. You
20
+ probably only need this when subclassing
21
+ [`Language`](https://codemirror.net/6/docs/ref/#language.Language).
22
+ */
11
23
  declare function defineLanguageFacet(baseData?: {
12
24
  [name: string]: any;
13
25
  }): Facet<{
@@ -15,121 +27,496 @@ declare function defineLanguageFacet(baseData?: {
15
27
  }, readonly {
16
28
  [name: string]: any;
17
29
  }[]>;
30
+ /**
31
+ A language object manages parsing and per-language
32
+ [metadata](https://codemirror.net/6/docs/ref/#state.EditorState.languageDataAt). Parse data is
33
+ managed as a [Lezer](https://lezer.codemirror.net) tree. You'll
34
+ want to subclass this class for custom parsers, or use the
35
+ [`LezerLanguage`](https://codemirror.net/6/docs/ref/#language.LezerLanguage) or
36
+ [`StreamLanguage`](https://codemirror.net/6/docs/ref/#stream-parser.StreamLanguage) abstractions for
37
+ [Lezer](https://lezer.codemirror.net/) or stream parsers.
38
+ */
18
39
  declare class Language {
40
+ /**
41
+ The [language data](https://codemirror.net/6/docs/ref/#state.EditorState.languageDataAt) data
42
+ facet used for this language.
43
+ */
19
44
  readonly data: Facet<{
20
45
  [name: string]: any;
21
46
  }>;
47
+ /**
48
+ The node type of the top node of trees produced by this parser.
49
+ */
50
+ readonly topNode: NodeType;
51
+ /**
52
+ The extension value to install this provider.
53
+ */
22
54
  readonly extension: Extension;
55
+ /**
56
+ The parser object. Can be useful when using this as a [nested
57
+ parser](https://lezer.codemirror.net/docs/ref#lezer.NestedParserSpec).
58
+ */
23
59
  parser: {
24
60
  startParse: (input: Input, startPos: number, context: ParseContext) => PartialParse;
25
61
  };
26
- constructor(data: Facet<{
62
+ /**
63
+ Construct a language object. You usually don't need to invoke
64
+ this directly. But when you do, make sure you use
65
+ [`defineLanguageFacet`](https://codemirror.net/6/docs/ref/#language.defineLanguageFacet) to create
66
+ the first argument.
67
+ */
68
+ constructor(
69
+ /**
70
+ The [language data](https://codemirror.net/6/docs/ref/#state.EditorState.languageDataAt) data
71
+ facet used for this language.
72
+ */
73
+ data: Facet<{
27
74
  [name: string]: any;
28
75
  }>, parser: {
29
76
  startParse(input: Input, pos: number, context: EditorParseContext): PartialParse;
30
- }, extraExtensions?: Extension[]);
77
+ },
78
+ /**
79
+ The node type of the top node of trees produced by this parser.
80
+ */
81
+ topNode: NodeType, extraExtensions?: Extension[]);
82
+ /**
83
+ Query whether this language is active at the given position.
84
+ */
31
85
  isActiveAt(state: EditorState, pos: number): boolean;
86
+ /**
87
+ Find the document regions that were parsed using this language.
88
+ The returned regions will _include_ any nested languages rooted
89
+ in this language, when those exist.
90
+ */
32
91
  findRegions(state: EditorState): {
33
92
  from: number;
34
93
  to: number;
35
94
  }[];
95
+ /**
96
+ Indicates whether this language allows nested languages. The
97
+ default implementation returns true.
98
+ */
36
99
  get allowsNesting(): boolean;
100
+ /**
101
+ Use this language to parse the given string into a tree.
102
+ */
37
103
  parseString(code: string): Tree;
38
104
  }
105
+ /**
106
+ A subclass of [`Language`](https://codemirror.net/6/docs/ref/#language.Language) for use with
107
+ [Lezer](https://lezer.codemirror.net/docs/ref#lezer.Parser)
108
+ parsers.
109
+ */
39
110
  declare class LezerLanguage extends Language {
40
111
  readonly parser: Parser;
41
112
  private constructor();
113
+ /**
114
+ Define a language from a parser.
115
+ */
42
116
  static define(spec: {
117
+ /**
118
+ The parser to use. Should already have added editor-relevant
119
+ node props (and optionally things like dialect and top rule)
120
+ configured.
121
+ */
43
122
  parser: Parser;
123
+ /**
124
+ [Language data](https://codemirror.net/6/docs/ref/#state.EditorState.languageDataAt)
125
+ to register for this language.
126
+ */
44
127
  languageData?: {
45
128
  [name: string]: any;
46
129
  };
47
130
  }): LezerLanguage;
131
+ /**
132
+ Create a new instance of this language with a reconfigured
133
+ version of its parser.
134
+ */
48
135
  configure(options: ParserConfig): LezerLanguage;
49
136
  get allowsNesting(): boolean;
50
137
  }
138
+ /**
139
+ Get the syntax tree for a state, which is the current (possibly
140
+ incomplete) parse tree of active [language](https://codemirror.net/6/docs/ref/#language.Language),
141
+ or the empty tree if there is no language available.
142
+ */
51
143
  declare function syntaxTree(state: EditorState): Tree;
144
+ /**
145
+ Try to get a parse tree that spans at least up to `upto`. The
146
+ method will do at most `timeout` milliseconds of work to parse
147
+ up to that point if the tree isn't already available.
148
+ */
52
149
  declare function ensureSyntaxTree(state: EditorState, upto: number, timeout?: number): Tree | null;
150
+ /**
151
+ A parse context provided to parsers working on the editor content.
152
+ */
53
153
  declare class EditorParseContext implements ParseContext {
54
154
  private parser;
155
+ /**
156
+ The current editor state.
157
+ */
55
158
  readonly state: EditorState;
159
+ /**
160
+ Tree fragments that can be reused by incremental re-parses.
161
+ */
56
162
  fragments: readonly TreeFragment[];
163
+ /**
164
+ The current editor viewport (or some overapproximation
165
+ thereof). Intended to be used for opportunistically avoiding
166
+ work (in which case
167
+ [`skipUntilInView`](https://codemirror.net/6/docs/ref/#language.EditorParseContext.skipUntilInView)
168
+ should be called to make sure the parser is restarted when the
169
+ skipped region becomes visible).
170
+ */
57
171
  viewport: {
58
172
  from: number;
59
173
  to: number;
60
174
  };
61
175
  private parse;
62
176
  private withoutTempSkipped;
177
+ /**
178
+ Notify the parse scheduler that the given region was skipped
179
+ because it wasn't in view, and the parse should be restarted
180
+ when it comes into view.
181
+ */
63
182
  skipUntilInView(from: number, to: number): void;
64
- static skippingParser: {
183
+ /**
184
+ Returns a parser intended to be used as placeholder when
185
+ asynchronously loading a nested parser. It'll skip its input and
186
+ mark it as not-really-parsed, so that the next update will parse
187
+ it again.
188
+
189
+ When `until` is given, a reparse will be scheduled when that
190
+ promise resolves.
191
+ */
192
+ static getSkippingParser(until?: Promise<unknown>): {
65
193
  startParse(input: Input, startPos: number, context: ParseContext): PartialParse;
66
194
  };
67
195
  }
196
+ /**
197
+ The facet used to associate a language with an editor state.
198
+ */
68
199
  declare const language: Facet<Language, Language | null>;
200
+ /**
201
+ This class bundles a [language object](https://codemirror.net/6/docs/ref/#language.Language) with an
202
+ optional set of supporting extensions. Language packages are
203
+ encouraged to export a function that optionally takes a
204
+ configuration object and returns a `LanguageSupport` instance, as
205
+ the main way for client code to use the package.
206
+ */
69
207
  declare class LanguageSupport {
208
+ /**
209
+ The language object.
210
+ */
70
211
  readonly language: Language;
212
+ /**
213
+ An optional set of supporting extensions. When nesting a
214
+ language in another language, the outer language is encouraged
215
+ to include the supporting extensions for its inner languages
216
+ in its own set of support extensions.
217
+ */
71
218
  readonly support: Extension;
219
+ /**
220
+ An extension including both the language and its support
221
+ extensions. (Allowing the object to be used as an extension
222
+ value itself.)
223
+ */
72
224
  extension: Extension;
73
- constructor(language: Language, support?: Extension);
225
+ /**
226
+ Create a support object.
227
+ */
228
+ constructor(
229
+ /**
230
+ The language object.
231
+ */
232
+ language: Language,
233
+ /**
234
+ An optional set of supporting extensions. When nesting a
235
+ language in another language, the outer language is encouraged
236
+ to include the supporting extensions for its inner languages
237
+ in its own set of support extensions.
238
+ */
239
+ support?: Extension);
74
240
  }
241
+ /**
242
+ Language descriptions are used to store metadata about languages
243
+ and to dynamically load them. Their main role is finding the
244
+ appropriate language for a filename or dynamically loading nested
245
+ parsers.
246
+ */
75
247
  declare class LanguageDescription {
248
+ /**
249
+ The name of this language.
250
+ */
76
251
  readonly name: string;
252
+ /**
253
+ Alternative names for the mode (lowercased, includes `this.name`).
254
+ */
77
255
  readonly alias: readonly string[];
256
+ /**
257
+ File extensions associated with this language.
258
+ */
78
259
  readonly extensions: readonly string[];
260
+ /**
261
+ Optional filename pattern that should be associated with this
262
+ language.
263
+ */
79
264
  readonly filename: RegExp | undefined;
80
265
  private loadFunc;
266
+ /**
267
+ If the language has been loaded, this will hold its value.
268
+ */
81
269
  support: LanguageSupport | undefined;
82
270
  private loading;
83
271
  private constructor();
272
+ /**
273
+ Start loading the the language. Will return a promise that
274
+ resolves to a [`LanguageSupport`](https://codemirror.net/6/docs/ref/#language.LanguageSupport)
275
+ object when the language successfully loads.
276
+ */
84
277
  load(): Promise<LanguageSupport>;
278
+ /**
279
+ Create a language description.
280
+ */
85
281
  static of(spec: {
282
+ /**
283
+ The language's name.
284
+ */
86
285
  name: string;
286
+ /**
287
+ An optional array of alternative names.
288
+ */
87
289
  alias?: readonly string[];
290
+ /**
291
+ An optional array of extensions associated with this language.
292
+ */
88
293
  extensions?: readonly string[];
294
+ /**
295
+ An optional filename pattern associated with this language.
296
+ */
89
297
  filename?: RegExp;
298
+ /**
299
+ A function that will asynchronously load the language.
300
+ */
90
301
  load: () => Promise<LanguageSupport>;
91
302
  }): LanguageDescription;
303
+ /**
304
+ Look for a language in the given array of descriptions that
305
+ matches the filename. Will first match
306
+ [`filename`](https://codemirror.net/6/docs/ref/#language.LanguageDescription.filename) patterns,
307
+ and then [extensions](https://codemirror.net/6/docs/ref/#language.LanguageDescription.extensions),
308
+ and return the first language that matches.
309
+ */
92
310
  static matchFilename(descs: readonly LanguageDescription[], filename: string): LanguageDescription | null;
311
+ /**
312
+ Look for a language whose name or alias matches the the given
313
+ name (case-insensitively). If `fuzzy` is true, and no direct
314
+ matchs is found, this'll also search for a language whose name
315
+ or alias occurs in the string (for names shorter than three
316
+ characters, only when surrounded by non-word characters).
317
+ */
93
318
  static matchLanguageName(descs: readonly LanguageDescription[], name: string, fuzzy?: boolean): LanguageDescription | null;
94
319
  }
95
320
 
321
+ /**
322
+ Facet that defines a way to provide a function that computes the
323
+ appropriate indentation depth at the start of a given line, or
324
+ `null` to indicate no appropriate indentation could be determined.
325
+ */
96
326
  declare const indentService: Facet<(context: IndentContext, pos: number) => number | null, readonly ((context: IndentContext, pos: number) => number | null)[]>;
327
+ /**
328
+ Facet for overriding the unit by which indentation happens.
329
+ Should be a string consisting either entirely of spaces or
330
+ entirely of tabs. When not set, this defaults to 2 spaces.
331
+ */
97
332
  declare const indentUnit: Facet<string, string>;
333
+ /**
334
+ Return the _column width_ of an indent unit in the state.
335
+ Determined by the [`indentUnit`](https://codemirror.net/6/docs/ref/#language.indentUnit)
336
+ facet, and [`tabSize`](https://codemirror.net/6/docs/ref/#state.EditorState^tabSize) when that
337
+ contains tabs.
338
+ */
98
339
  declare function getIndentUnit(state: EditorState): number;
340
+ /**
341
+ Create an indentation string that covers columns 0 to `cols`.
342
+ Will use tabs for as much of the columns as possible when the
343
+ [`indentUnit`](https://codemirror.net/6/docs/ref/#language.indentUnit) facet contains
344
+ tabs.
345
+ */
99
346
  declare function indentString(state: EditorState, cols: number): string;
347
+ /**
348
+ Get the indentation at the given position. Will first consult any
349
+ [indent services](https://codemirror.net/6/docs/ref/#language.indentService) that are registered,
350
+ and if none of those return an indentation, this will check the
351
+ syntax tree for the [indent node prop](https://codemirror.net/6/docs/ref/#language.indentNodeProp)
352
+ and use that if found. Returns a number when an indentation could
353
+ be determined, and null otherwise.
354
+ */
100
355
  declare function getIndentation(context: IndentContext | EditorState, pos: number): number | null;
356
+ /**
357
+ Indentation contexts are used when calling [indentation
358
+ services](https://codemirror.net/6/docs/ref/#language.indentService). They provide helper utilities
359
+ useful in indentation logic, and can selectively override the
360
+ indentation reported for some lines.
361
+ */
101
362
  declare class IndentContext {
363
+ /**
364
+ The editor state.
365
+ */
102
366
  readonly state: EditorState;
367
+ /**
368
+ The indent unit (number of columns per indentation level).
369
+ */
103
370
  unit: number;
104
- constructor(state: EditorState, options?: {
371
+ /**
372
+ Create an indent context.
373
+ */
374
+ constructor(
375
+ /**
376
+ The editor state.
377
+ */
378
+ state: EditorState,
379
+ /**
380
+ @internal
381
+ */
382
+ options?: {
383
+ /**
384
+ Override line indentations provided to the indentation
385
+ helper function, which is useful when implementing region
386
+ indentation, where indentation for later lines needs to refer
387
+ to previous lines, which may have been reindented compared to
388
+ the original start state. If given, this function should
389
+ return -1 for lines (given by start position) that didn't
390
+ change, and an updated indentation otherwise.
391
+ */
105
392
  overrideIndentation?: (pos: number) => number;
393
+ /**
394
+ Make it look, to the indent logic, like a line break was
395
+ added at the given position (which is mostly just useful for
396
+ implementing something like
397
+ [`insertNewlineAndIndent`](https://codemirror.net/6/docs/ref/#commands.insertNewlineAndIndent)).
398
+ */
106
399
  simulateBreak?: number;
400
+ /**
401
+ When `simulateBreak` is given, this can be used to make the
402
+ simulate break behave like a double line break.
403
+ */
107
404
  simulateDoubleBreak?: boolean;
108
405
  });
406
+ /**
407
+ Get the text directly after `pos`, either the entire line
408
+ or the next 100 characters, whichever is shorter.
409
+ */
109
410
  textAfterPos(pos: number): string;
411
+ /**
412
+ Find the column for the given position.
413
+ */
110
414
  column(pos: number): number;
415
+ /**
416
+ find the column position (taking tabs into account) of the given
417
+ position in the given string.
418
+ */
111
419
  countColumn(line: string, pos: number): number;
420
+ /**
421
+ Find the indentation column of the given document line.
422
+ */
112
423
  lineIndent(line: Line): number;
113
424
  }
425
+ /**
426
+ A syntax tree node prop used to associate indentation strategies
427
+ with node types. Such a strategy is a function from an indentation
428
+ context to a column number or null, where null indicates that no
429
+ definitive indentation can be determined.
430
+ */
114
431
  declare const indentNodeProp: NodeProp<(context: TreeIndentContext) => number | null>;
432
+ /**
433
+ Objects of this type provide context information and helper
434
+ methods to indentation functions.
435
+ */
115
436
  declare class TreeIndentContext extends IndentContext {
437
+ private base;
438
+ /**
439
+ The position at which indentation is being computed.
440
+ */
116
441
  readonly pos: number;
442
+ /**
443
+ The syntax tree node to which the indentation strategy
444
+ applies.
445
+ */
117
446
  readonly node: SyntaxNode;
447
+ /**
448
+ Get the text directly after `this.pos`, either the entire line
449
+ or the next 100 characters, whichever is shorter.
450
+ */
118
451
  get textAfter(): string;
452
+ /**
453
+ Get the indentation at the reference line for `this.node`, which
454
+ is the line on which it starts, unless there is a node that is
455
+ _not_ a parent of this node covering the start of that line. If
456
+ so, the line at the start of that node is tried, again skipping
457
+ on if it is covered by another such node.
458
+ */
119
459
  get baseIndent(): number;
460
+ /**
461
+ Continue looking for indentations in the node's parent nodes,
462
+ and return the result of that.
463
+ */
464
+ continue(): number | null;
120
465
  }
466
+ /**
467
+ An indentation strategy for delimited (usually bracketed) nodes.
468
+ Will, by default, indent one unit more than the parent's base
469
+ indent unless the line starts with a closing token. When `align`
470
+ is true and there are non-skipped nodes on the node's opening
471
+ line, the content of the node will be aligned with the end of the
472
+ opening node, like this:
473
+
474
+ foo(bar,
475
+ baz)
476
+ */
121
477
  declare function delimitedIndent({ closing, align, units }: {
122
478
  closing: string;
123
479
  align?: boolean;
124
480
  units?: number;
125
481
  }): (context: TreeIndentContext) => number;
482
+ /**
483
+ An indentation strategy that aligns a node's content to its base
484
+ indentation.
485
+ */
126
486
  declare const flatIndent: (context: TreeIndentContext) => number;
487
+ /**
488
+ Creates an indentation strategy that, by default, indents
489
+ continued lines one unit more than the node's base indentation.
490
+ You can provide `except` to prevent indentation of lines that
491
+ match a pattern (for example `/^else\b/` in `if`/`else`
492
+ constructs), and you can change the amount of units used with the
493
+ `units` option.
494
+ */
127
495
  declare function continuedIndent({ except, units }?: {
128
496
  except?: RegExp;
129
497
  units?: number;
130
498
  }): (context: TreeIndentContext) => number;
499
+ /**
500
+ Enables reindentation on input. When a language defines an
501
+ `indentOnInput` field in its [language
502
+ data](https://codemirror.net/6/docs/ref/#state.EditorState.languageDataAt), which must hold a regular
503
+ expression, the line at the cursor will be reindented whenever new
504
+ text is typed and the input from the start of the line up to the
505
+ cursor matches that regexp.
506
+
507
+ To avoid unneccesary reindents, it is recommended to start the
508
+ regexp with `^` (usually followed by `\s*`), and end it with `$`.
509
+ For example, `/^\s*\}$/` will reindent when a closing brace is
510
+ added at the start of a line.
511
+ */
131
512
  declare function indentOnInput(): Extension;
132
513
 
514
+ /**
515
+ A facet that registers a code folding service. When called with
516
+ the extent of a line, such a function should return a foldable
517
+ range that starts on that line (but continues beyond it), if one
518
+ can be found.
519
+ */
133
520
  declare const foldService: Facet<(state: EditorState, lineStart: number, lineEnd: number) => ({
134
521
  from: number;
135
522
  to: number;
@@ -137,13 +524,36 @@ declare const foldService: Facet<(state: EditorState, lineStart: number, lineEnd
137
524
  from: number;
138
525
  to: number;
139
526
  } | null))[]>;
527
+ /**
528
+ This node prop is used to associate folding information with
529
+ syntax node types. Given a syntax node, it should check whether
530
+ that tree is foldable and return the range that can be collapsed
531
+ when it is.
532
+ */
140
533
  declare const foldNodeProp: NodeProp<(node: SyntaxNode, state: EditorState) => ({
141
534
  from: number;
142
535
  to: number;
143
536
  } | null)>;
537
+ /**
538
+ [Fold](https://codemirror.net/6/docs/ref/#language.foldNodeProp) function that folds everything but
539
+ the first and the last child of a syntax node. Useful for nodes
540
+ that start and end with delimiters.
541
+ */
542
+ declare function foldInside(node: SyntaxNode): {
543
+ from: number;
544
+ to: number;
545
+ } | null;
546
+ /**
547
+ Check whether the given line is foldable. First asks any fold
548
+ services registered through
549
+ [`foldService`](https://codemirror.net/6/docs/ref/#language.foldService), and if none of them return
550
+ a result, tries to query the [fold node
551
+ prop](https://codemirror.net/6/docs/ref/#language.foldNodeProp) of syntax nodes that cover the end
552
+ of the line.
553
+ */
144
554
  declare function foldable(state: EditorState, lineStart: number, lineEnd: number): {
145
555
  from: number;
146
556
  to: number;
147
557
  } | null;
148
558
 
149
- export { EditorParseContext, IndentContext, Language, LanguageDescription, LanguageSupport, LezerLanguage, TreeIndentContext, continuedIndent, defineLanguageFacet, delimitedIndent, ensureSyntaxTree, flatIndent, foldNodeProp, foldService, foldable, getIndentUnit, getIndentation, indentNodeProp, indentOnInput, indentService, indentString, indentUnit, language, languageDataProp, syntaxTree };
559
+ export { EditorParseContext, IndentContext, Language, LanguageDescription, LanguageSupport, LezerLanguage, TreeIndentContext, continuedIndent, defineLanguageFacet, delimitedIndent, ensureSyntaxTree, flatIndent, foldInside, foldNodeProp, foldService, foldable, getIndentUnit, getIndentation, indentNodeProp, indentOnInput, indentService, indentString, indentUnit, language, languageDataProp, syntaxTree };