Diffdown is a real-time collaborative Markdown editor/previewer built on the AT Protocol
diffdown.com
1import { NodeProp, SyntaxNode, Parser, Tree, Input, TreeFragment, NodeType } from '@lezer/common';
2import { LRParser, ParserConfig } from '@lezer/lr';
3import * as _codemirror_state from '@codemirror/state';
4import { Facet, EditorState, Extension, Text, StateField, Range } from '@codemirror/state';
5import { EditorView, DecorationSet, Command, KeyBinding, ViewUpdate, BlockInfo, Decoration } from '@codemirror/view';
6import { Highlighter, Tag } from '@lezer/highlight';
7import { StyleModule, StyleSpec } from 'style-mod';
8
9/**
10Node prop stored in a parser's top syntax node to provide the
11facet that stores language-specific data for that language.
12*/
13declare const languageDataProp: NodeProp<Facet<{
14 [name: string]: any;
15}, readonly {
16 [name: string]: any;
17}[]>>;
18/**
19Helper function to define a facet (to be added to the top syntax
20node(s) for a language via
21[`languageDataProp`](https://codemirror.net/6/docs/ref/#language.languageDataProp)), that will be
22used to associate language data with the language. You
23probably only need this when subclassing
24[`Language`](https://codemirror.net/6/docs/ref/#language.Language).
25*/
26declare function defineLanguageFacet(baseData?: {
27 [name: string]: any;
28}): Facet<{
29 [name: string]: any;
30}, readonly {
31 [name: string]: any;
32}[]>;
33/**
34Some languages need to return different [language
35data](https://codemirror.net/6/docs/ref/#state.EditorState.languageDataAt) for some parts of their
36tree. Sublanguages, registered by adding a [node
37prop](https://codemirror.net/6/docs/ref/#language.sublanguageProp) to the language's top syntax
38node, provide a mechanism to do this.
39
40(Note that when using nested parsing, where nested syntax is
41parsed by a different parser and has its own top node type, you
42don't need a sublanguage.)
43*/
44interface Sublanguage {
45 /**
46 Determines whether the data provided by this sublanguage should
47 completely replace the regular data or be added to it (with
48 higher-precedence). The default is `"extend"`.
49 */
50 type?: "replace" | "extend";
51 /**
52 A predicate that returns whether the node at the queried
53 position is part of the sublanguage.
54 */
55 test: (node: SyntaxNode, state: EditorState) => boolean;
56 /**
57 The language data facet that holds the sublanguage's data.
58 You'll want to use
59 [`defineLanguageFacet`](https://codemirror.net/6/docs/ref/#language.defineLanguageFacet) to create
60 this.
61 */
62 facet: Facet<{
63 [name: string]: any;
64 }>;
65}
66/**
67Syntax node prop used to register sublanguages. Should be added to
68the top level node type for the language.
69*/
70declare const sublanguageProp: NodeProp<Sublanguage[]>;
71/**
72A language object manages parsing and per-language
73[metadata](https://codemirror.net/6/docs/ref/#state.EditorState.languageDataAt). Parse data is
74managed as a [Lezer](https://lezer.codemirror.net) tree. The class
75can be used directly, via the [`LRLanguage`](https://codemirror.net/6/docs/ref/#language.LRLanguage)
76subclass for [Lezer](https://lezer.codemirror.net/) LR parsers, or
77via the [`StreamLanguage`](https://codemirror.net/6/docs/ref/#language.StreamLanguage) subclass
78for stream parsers.
79*/
80declare class Language {
81 /**
82 The [language data](https://codemirror.net/6/docs/ref/#state.EditorState.languageDataAt) facet
83 used for this language.
84 */
85 readonly data: Facet<{
86 [name: string]: any;
87 }>;
88 /**
89 A language name.
90 */
91 readonly name: string;
92 /**
93 The extension value to install this as the document language.
94 */
95 readonly extension: Extension;
96 /**
97 The parser object. Can be useful when using this as a [nested
98 parser](https://lezer.codemirror.net/docs/ref#common.Parser).
99 */
100 parser: Parser;
101 /**
102 Construct a language object. If you need to invoke this
103 directly, first define a data facet with
104 [`defineLanguageFacet`](https://codemirror.net/6/docs/ref/#language.defineLanguageFacet), and then
105 configure your parser to [attach](https://codemirror.net/6/docs/ref/#language.languageDataProp) it
106 to the language's outer syntax node.
107 */
108 constructor(
109 /**
110 The [language data](https://codemirror.net/6/docs/ref/#state.EditorState.languageDataAt) facet
111 used for this language.
112 */
113 data: Facet<{
114 [name: string]: any;
115 }>, parser: Parser, extraExtensions?: Extension[],
116 /**
117 A language name.
118 */
119 name?: string);
120 /**
121 Query whether this language is active at the given position.
122 */
123 isActiveAt(state: EditorState, pos: number, side?: -1 | 0 | 1): boolean;
124 /**
125 Find the document regions that were parsed using this language.
126 The returned regions will _include_ any nested languages rooted
127 in this language, when those exist.
128 */
129 findRegions(state: EditorState): {
130 from: number;
131 to: number;
132 }[];
133 /**
134 Indicates whether this language allows nested languages. The
135 default implementation returns true.
136 */
137 get allowsNesting(): boolean;
138}
139/**
140A subclass of [`Language`](https://codemirror.net/6/docs/ref/#language.Language) for use with Lezer
141[LR parsers](https://lezer.codemirror.net/docs/ref#lr.LRParser)
142parsers.
143*/
144declare class LRLanguage extends Language {
145 readonly parser: LRParser;
146 private constructor();
147 /**
148 Define a language from a parser.
149 */
150 static define(spec: {
151 /**
152 The [name](https://codemirror.net/6/docs/ref/#Language.name) of the language.
153 */
154 name?: string;
155 /**
156 The parser to use. Should already have added editor-relevant
157 node props (and optionally things like dialect and top rule)
158 configured.
159 */
160 parser: LRParser;
161 /**
162 [Language data](https://codemirror.net/6/docs/ref/#state.EditorState.languageDataAt)
163 to register for this language.
164 */
165 languageData?: {
166 [name: string]: any;
167 };
168 }): LRLanguage;
169 /**
170 Create a new instance of this language with a reconfigured
171 version of its parser and optionally a new name.
172 */
173 configure(options: ParserConfig, name?: string): LRLanguage;
174 get allowsNesting(): boolean;
175}
176/**
177Get the syntax tree for a state, which is the current (possibly
178incomplete) parse tree of the active
179[language](https://codemirror.net/6/docs/ref/#language.Language), or the empty tree if there is no
180language available.
181*/
182declare function syntaxTree(state: EditorState): Tree;
183/**
184Try to get a parse tree that spans at least up to `upto`. The
185method will do at most `timeout` milliseconds of work to parse
186up to that point if the tree isn't already available.
187*/
188declare function ensureSyntaxTree(state: EditorState, upto: number, timeout?: number): Tree | null;
189/**
190Queries whether there is a full syntax tree available up to the
191given document position. If there isn't, the background parse
192process _might_ still be working and update the tree further, but
193there is no guarantee of that—the parser will [stop
194working](https://codemirror.net/6/docs/ref/#language.syntaxParserRunning) when it has spent a
195certain amount of time or has moved beyond the visible viewport.
196Always returns false if no language has been enabled.
197*/
198declare function syntaxTreeAvailable(state: EditorState, upto?: number): boolean;
199/**
200Move parsing forward, and update the editor state afterwards to
201reflect the new tree. Will work for at most `timeout`
202milliseconds. Returns true if the parser managed get to the given
203position in that time.
204*/
205declare function forceParsing(view: EditorView, upto?: number, timeout?: number): boolean;
206/**
207Tells you whether the language parser is planning to do more
208parsing work (in a `requestIdleCallback` pseudo-thread) or has
209stopped running, either because it parsed the entire document,
210because it spent too much time and was cut off, or because there
211is no language parser enabled.
212*/
213declare function syntaxParserRunning(view: EditorView): boolean;
214/**
215Lezer-style
216[`Input`](https://lezer.codemirror.net/docs/ref#common.Input)
217object for a [`Text`](https://codemirror.net/6/docs/ref/#state.Text) object.
218*/
219declare class DocInput implements Input {
220 readonly doc: Text;
221 private cursor;
222 private cursorPos;
223 private string;
224 /**
225 Create an input object for the given document.
226 */
227 constructor(doc: Text);
228 get length(): number;
229 private syncTo;
230 chunk(pos: number): string;
231 get lineChunks(): boolean;
232 read(from: number, to: number): string;
233}
234/**
235A parse context provided to parsers working on the editor content.
236*/
237declare class ParseContext {
238 private parser;
239 /**
240 The current editor state.
241 */
242 readonly state: EditorState;
243 /**
244 Tree fragments that can be reused by incremental re-parses.
245 */
246 fragments: readonly TreeFragment[];
247 /**
248 The current editor viewport (or some overapproximation
249 thereof). Intended to be used for opportunistically avoiding
250 work (in which case
251 [`skipUntilInView`](https://codemirror.net/6/docs/ref/#language.ParseContext.skipUntilInView)
252 should be called to make sure the parser is restarted when the
253 skipped region becomes visible).
254 */
255 viewport: {
256 from: number;
257 to: number;
258 };
259 private parse;
260 private constructor();
261 private startParse;
262 private withContext;
263 private withoutTempSkipped;
264 /**
265 Notify the parse scheduler that the given region was skipped
266 because it wasn't in view, and the parse should be restarted
267 when it comes into view.
268 */
269 skipUntilInView(from: number, to: number): void;
270 /**
271 Returns a parser intended to be used as placeholder when
272 asynchronously loading a nested parser. It'll skip its input and
273 mark it as not-really-parsed, so that the next update will parse
274 it again.
275
276 When `until` is given, a reparse will be scheduled when that
277 promise resolves.
278 */
279 static getSkippingParser(until?: Promise<unknown>): Parser;
280 /**
281 Get the context for the current parse, or `null` if no editor
282 parse is in progress.
283 */
284 static get(): ParseContext | null;
285}
286/**
287The facet used to associate a language with an editor state. Used
288by `Language` object's `extension` property (so you don't need to
289manually wrap your languages in this). Can be used to access the
290current language on a state.
291*/
292declare const language: Facet<Language, Language | null>;
293/**
294This class bundles a [language](https://codemirror.net/6/docs/ref/#language.Language) with an
295optional set of supporting extensions. Language packages are
296encouraged to export a function that optionally takes a
297configuration object and returns a `LanguageSupport` instance, as
298the main way for client code to use the package.
299*/
300declare class LanguageSupport {
301 /**
302 The language object.
303 */
304 readonly language: Language;
305 /**
306 An optional set of supporting extensions. When nesting a
307 language in another language, the outer language is encouraged
308 to include the supporting extensions for its inner languages
309 in its own set of support extensions.
310 */
311 readonly support: Extension;
312 /**
313 An extension including both the language and its support
314 extensions. (Allowing the object to be used as an extension
315 value itself.)
316 */
317 extension: Extension;
318 /**
319 Create a language support object.
320 */
321 constructor(
322 /**
323 The language object.
324 */
325 language: Language,
326 /**
327 An optional set of supporting extensions. When nesting a
328 language in another language, the outer language is encouraged
329 to include the supporting extensions for its inner languages
330 in its own set of support extensions.
331 */
332 support?: Extension);
333}
334/**
335Language descriptions are used to store metadata about languages
336and to dynamically load them. Their main role is finding the
337appropriate language for a filename or dynamically loading nested
338parsers.
339*/
340declare class LanguageDescription {
341 /**
342 The name of this language.
343 */
344 readonly name: string;
345 /**
346 Alternative names for the mode (lowercased, includes `this.name`).
347 */
348 readonly alias: readonly string[];
349 /**
350 File extensions associated with this language.
351 */
352 readonly extensions: readonly string[];
353 /**
354 Optional filename pattern that should be associated with this
355 language.
356 */
357 readonly filename: RegExp | undefined;
358 private loadFunc;
359 /**
360 If the language has been loaded, this will hold its value.
361 */
362 support: LanguageSupport | undefined;
363 private loading;
364 private constructor();
365 /**
366 Start loading the the language. Will return a promise that
367 resolves to a [`LanguageSupport`](https://codemirror.net/6/docs/ref/#language.LanguageSupport)
368 object when the language successfully loads.
369 */
370 load(): Promise<LanguageSupport>;
371 /**
372 Create a language description.
373 */
374 static of(spec: {
375 /**
376 The language's name.
377 */
378 name: string;
379 /**
380 An optional array of alternative names.
381 */
382 alias?: readonly string[];
383 /**
384 An optional array of filename extensions associated with this
385 language.
386 */
387 extensions?: readonly string[];
388 /**
389 An optional filename pattern associated with this language.
390 */
391 filename?: RegExp;
392 /**
393 A function that will asynchronously load the language.
394 */
395 load?: () => Promise<LanguageSupport>;
396 /**
397 Alternatively to `load`, you can provide an already loaded
398 support object. Either this or `load` should be provided.
399 */
400 support?: LanguageSupport;
401 }): LanguageDescription;
402 /**
403 Look for a language in the given array of descriptions that
404 matches the filename. Will first match
405 [`filename`](https://codemirror.net/6/docs/ref/#language.LanguageDescription.filename) patterns,
406 and then [extensions](https://codemirror.net/6/docs/ref/#language.LanguageDescription.extensions),
407 and return the first language that matches.
408 */
409 static matchFilename(descs: readonly LanguageDescription[], filename: string): LanguageDescription | null;
410 /**
411 Look for a language whose name or alias matches the the given
412 name (case-insensitively). If `fuzzy` is true, and no direct
413 matchs is found, this'll also search for a language whose name
414 or alias occurs in the string (for names shorter than three
415 characters, only when surrounded by non-word characters).
416 */
417 static matchLanguageName(descs: readonly LanguageDescription[], name: string, fuzzy?: boolean): LanguageDescription | null;
418}
419
420/**
421Facet that defines a way to provide a function that computes the
422appropriate indentation depth, as a column number (see
423[`indentString`](https://codemirror.net/6/docs/ref/#language.indentString)), at the start of a given
424line. A return value of `null` indicates no indentation can be
425determined, and the line should inherit the indentation of the one
426above it. A return value of `undefined` defers to the next indent
427service.
428*/
429declare const indentService: Facet<(context: IndentContext, pos: number) => number | null | undefined, readonly ((context: IndentContext, pos: number) => number | null | undefined)[]>;
430/**
431Facet for overriding the unit by which indentation happens. Should
432be a string consisting entirely of the same whitespace character.
433When not set, this defaults to 2 spaces.
434*/
435declare const indentUnit: Facet<string, string>;
436/**
437Return the _column width_ of an indent unit in the state.
438Determined by the [`indentUnit`](https://codemirror.net/6/docs/ref/#language.indentUnit)
439facet, and [`tabSize`](https://codemirror.net/6/docs/ref/#state.EditorState^tabSize) when that
440contains tabs.
441*/
442declare function getIndentUnit(state: EditorState): number;
443/**
444Create an indentation string that covers columns 0 to `cols`.
445Will use tabs for as much of the columns as possible when the
446[`indentUnit`](https://codemirror.net/6/docs/ref/#language.indentUnit) facet contains
447tabs.
448*/
449declare function indentString(state: EditorState, cols: number): string;
450/**
451Get the indentation, as a column number, at the given position.
452Will first consult any [indent services](https://codemirror.net/6/docs/ref/#language.indentService)
453that are registered, and if none of those return an indentation,
454this will check the syntax tree for the [indent node
455prop](https://codemirror.net/6/docs/ref/#language.indentNodeProp) and use that if found. Returns a
456number when an indentation could be determined, and null
457otherwise.
458*/
459declare function getIndentation(context: IndentContext | EditorState, pos: number): number | null;
460/**
461Create a change set that auto-indents all lines touched by the
462given document range.
463*/
464declare function indentRange(state: EditorState, from: number, to: number): _codemirror_state.ChangeSet;
465/**
466Indentation contexts are used when calling [indentation
467services](https://codemirror.net/6/docs/ref/#language.indentService). They provide helper utilities
468useful in indentation logic, and can selectively override the
469indentation reported for some lines.
470*/
471declare class IndentContext {
472 /**
473 The editor state.
474 */
475 readonly state: EditorState;
476 /**
477 The indent unit (number of columns per indentation level).
478 */
479 unit: number;
480 /**
481 Create an indent context.
482 */
483 constructor(
484 /**
485 The editor state.
486 */
487 state: EditorState,
488 /**
489 @internal
490 */
491 options?: {
492 /**
493 Override line indentations provided to the indentation
494 helper function, which is useful when implementing region
495 indentation, where indentation for later lines needs to refer
496 to previous lines, which may have been reindented compared to
497 the original start state. If given, this function should
498 return -1 for lines (given by start position) that didn't
499 change, and an updated indentation otherwise.
500 */
501 overrideIndentation?: (pos: number) => number;
502 /**
503 Make it look, to the indent logic, like a line break was
504 added at the given position (which is mostly just useful for
505 implementing something like
506 [`insertNewlineAndIndent`](https://codemirror.net/6/docs/ref/#commands.insertNewlineAndIndent)).
507 */
508 simulateBreak?: number;
509 /**
510 When `simulateBreak` is given, this can be used to make the
511 simulated break behave like a double line break.
512 */
513 simulateDoubleBreak?: boolean;
514 });
515 /**
516 Get a description of the line at the given position, taking
517 [simulated line
518 breaks](https://codemirror.net/6/docs/ref/#language.IndentContext.constructor^options.simulateBreak)
519 into account. If there is such a break at `pos`, the `bias`
520 argument determines whether the part of the line line before or
521 after the break is used.
522 */
523 lineAt(pos: number, bias?: -1 | 1): {
524 text: string;
525 from: number;
526 };
527 /**
528 Get the text directly after `pos`, either the entire line
529 or the next 100 characters, whichever is shorter.
530 */
531 textAfterPos(pos: number, bias?: -1 | 1): string;
532 /**
533 Find the column for the given position.
534 */
535 column(pos: number, bias?: -1 | 1): number;
536 /**
537 Find the column position (taking tabs into account) of the given
538 position in the given string.
539 */
540 countColumn(line: string, pos?: number): number;
541 /**
542 Find the indentation column of the line at the given point.
543 */
544 lineIndent(pos: number, bias?: -1 | 1): number;
545 /**
546 Returns the [simulated line
547 break](https://codemirror.net/6/docs/ref/#language.IndentContext.constructor^options.simulateBreak)
548 for this context, if any.
549 */
550 get simulatedBreak(): number | null;
551}
552/**
553A syntax tree node prop used to associate indentation strategies
554with node types. Such a strategy is a function from an indentation
555context to a column number (see also
556[`indentString`](https://codemirror.net/6/docs/ref/#language.indentString)) or null, where null
557indicates that no definitive indentation can be determined.
558*/
559declare const indentNodeProp: NodeProp<(context: TreeIndentContext) => number | null>;
560/**
561Objects of this type provide context information and helper
562methods to indentation functions registered on syntax nodes.
563*/
564declare class TreeIndentContext extends IndentContext {
565 private base;
566 /**
567 The position at which indentation is being computed.
568 */
569 readonly pos: number;
570 private constructor();
571 /**
572 The syntax tree node to which the indentation strategy
573 applies.
574 */
575 get node(): SyntaxNode;
576 /**
577 Get the text directly after `this.pos`, either the entire line
578 or the next 100 characters, whichever is shorter.
579 */
580 get textAfter(): string;
581 /**
582 Get the indentation at the reference line for `this.node`, which
583 is the line on which it starts, unless there is a node that is
584 _not_ a parent of this node covering the start of that line. If
585 so, the line at the start of that node is tried, again skipping
586 on if it is covered by another such node.
587 */
588 get baseIndent(): number;
589 /**
590 Get the indentation for the reference line of the given node
591 (see [`baseIndent`](https://codemirror.net/6/docs/ref/#language.TreeIndentContext.baseIndent)).
592 */
593 baseIndentFor(node: SyntaxNode): number;
594 /**
595 Continue looking for indentations in the node's parent nodes,
596 and return the result of that.
597 */
598 continue(): number | null;
599}
600/**
601An indentation strategy for delimited (usually bracketed) nodes.
602Will, by default, indent one unit more than the parent's base
603indent unless the line starts with a closing token. When `align`
604is true and there are non-skipped nodes on the node's opening
605line, the content of the node will be aligned with the end of the
606opening node, like this:
607
608 foo(bar,
609 baz)
610*/
611declare function delimitedIndent({ closing, align, units }: {
612 closing: string;
613 align?: boolean;
614 units?: number;
615}): (context: TreeIndentContext) => number;
616/**
617An indentation strategy that aligns a node's content to its base
618indentation.
619*/
620declare const flatIndent: (context: TreeIndentContext) => number;
621/**
622Creates an indentation strategy that, by default, indents
623continued lines one unit more than the node's base indentation.
624You can provide `except` to prevent indentation of lines that
625match a pattern (for example `/^else\b/` in `if`/`else`
626constructs), and you can change the amount of units used with the
627`units` option.
628*/
629declare function continuedIndent({ except, units }?: {
630 except?: RegExp;
631 units?: number;
632}): (context: TreeIndentContext) => number;
633/**
634Enables reindentation on input. When a language defines an
635`indentOnInput` field in its [language
636data](https://codemirror.net/6/docs/ref/#state.EditorState.languageDataAt), which must hold a regular
637expression, the line at the cursor will be reindented whenever new
638text is typed and the input from the start of the line up to the
639cursor matches that regexp.
640
641To avoid unneccesary reindents, it is recommended to start the
642regexp with `^` (usually followed by `\s*`), and end it with `$`.
643For example, `/^\s*\}$/` will reindent when a closing brace is
644added at the start of a line.
645*/
646declare function indentOnInput(): Extension;
647
648/**
649A facet that registers a code folding service. When called with
650the extent of a line, such a function should return a foldable
651range that starts on that line (but continues beyond it), if one
652can be found.
653*/
654declare const foldService: Facet<(state: EditorState, lineStart: number, lineEnd: number) => ({
655 from: number;
656 to: number;
657} | null), readonly ((state: EditorState, lineStart: number, lineEnd: number) => ({
658 from: number;
659 to: number;
660} | null))[]>;
661/**
662This node prop is used to associate folding information with
663syntax node types. Given a syntax node, it should check whether
664that tree is foldable and return the range that can be collapsed
665when it is.
666*/
667declare const foldNodeProp: NodeProp<(node: SyntaxNode, state: EditorState) => ({
668 from: number;
669 to: number;
670} | null)>;
671/**
672[Fold](https://codemirror.net/6/docs/ref/#language.foldNodeProp) function that folds everything but
673the first and the last child of a syntax node. Useful for nodes
674that start and end with delimiters.
675*/
676declare function foldInside(node: SyntaxNode): {
677 from: number;
678 to: number;
679} | null;
680/**
681Check whether the given line is foldable. First asks any fold
682services registered through
683[`foldService`](https://codemirror.net/6/docs/ref/#language.foldService), and if none of them return
684a result, tries to query the [fold node
685prop](https://codemirror.net/6/docs/ref/#language.foldNodeProp) of syntax nodes that cover the end
686of the line.
687*/
688declare function foldable(state: EditorState, lineStart: number, lineEnd: number): {
689 from: number;
690 to: number;
691} | null;
692type DocRange = {
693 from: number;
694 to: number;
695};
696/**
697State effect that can be attached to a transaction to fold the
698given range. (You probably only need this in exceptional
699circumstances—usually you'll just want to let
700[`foldCode`](https://codemirror.net/6/docs/ref/#language.foldCode) and the [fold
701gutter](https://codemirror.net/6/docs/ref/#language.foldGutter) create the transactions.)
702*/
703declare const foldEffect: _codemirror_state.StateEffectType<DocRange>;
704/**
705State effect that unfolds the given range (if it was folded).
706*/
707declare const unfoldEffect: _codemirror_state.StateEffectType<DocRange>;
708/**
709The state field that stores the folded ranges (as a [decoration
710set](https://codemirror.net/6/docs/ref/#view.DecorationSet)). Can be passed to
711[`EditorState.toJSON`](https://codemirror.net/6/docs/ref/#state.EditorState.toJSON) and
712[`fromJSON`](https://codemirror.net/6/docs/ref/#state.EditorState^fromJSON) to serialize the fold
713state.
714*/
715declare const foldState: StateField<DecorationSet>;
716/**
717Get a [range set](https://codemirror.net/6/docs/ref/#state.RangeSet) containing the folded ranges
718in the given state.
719*/
720declare function foldedRanges(state: EditorState): DecorationSet;
721/**
722Fold the lines that are selected, if possible.
723*/
724declare const foldCode: Command;
725/**
726Unfold folded ranges on selected lines.
727*/
728declare const unfoldCode: Command;
729/**
730Fold all top-level foldable ranges. Note that, in most cases,
731folding information will depend on the [syntax
732tree](https://codemirror.net/6/docs/ref/#language.syntaxTree), and folding everything may not work
733reliably when the document hasn't been fully parsed (either
734because the editor state was only just initialized, or because the
735document is so big that the parser decided not to parse it
736entirely).
737*/
738declare const foldAll: Command;
739/**
740Unfold all folded code.
741*/
742declare const unfoldAll: Command;
743/**
744Toggle folding at cursors. Unfolds if there is an existing fold
745starting in that line, tries to find a foldable range around it
746otherwise.
747*/
748declare const toggleFold: Command;
749/**
750Default fold-related key bindings.
751
752 - Ctrl-Shift-[ (Cmd-Alt-[ on macOS): [`foldCode`](https://codemirror.net/6/docs/ref/#language.foldCode).
753 - Ctrl-Shift-] (Cmd-Alt-] on macOS): [`unfoldCode`](https://codemirror.net/6/docs/ref/#language.unfoldCode).
754 - Ctrl-Alt-[: [`foldAll`](https://codemirror.net/6/docs/ref/#language.foldAll).
755 - Ctrl-Alt-]: [`unfoldAll`](https://codemirror.net/6/docs/ref/#language.unfoldAll).
756*/
757declare const foldKeymap: readonly KeyBinding[];
758interface FoldConfig {
759 /**
760 A function that creates the DOM element used to indicate the
761 position of folded code. The `onclick` argument is the default
762 click event handler, which toggles folding on the line that
763 holds the element, and should probably be added as an event
764 handler to the returned element. If
765 [`preparePlaceholder`](https://codemirror.net/6/docs/ref/#language.FoldConfig.preparePlaceholder)
766 is given, its result will be passed as 3rd argument. Otherwise,
767 this will be null.
768
769 When this option isn't given, the `placeholderText` option will
770 be used to create the placeholder element.
771 */
772 placeholderDOM?: ((view: EditorView, onclick: (event: Event) => void, prepared: any) => HTMLElement) | null;
773 /**
774 Text to use as placeholder for folded text. Defaults to `"…"`.
775 Will be styled with the `"cm-foldPlaceholder"` class.
776 */
777 placeholderText?: string;
778 /**
779 Given a range that is being folded, create a value that
780 describes it, to be used by `placeholderDOM` to render a custom
781 widget that, for example, indicates something about the folded
782 range's size or type.
783 */
784 preparePlaceholder?: (state: EditorState, range: {
785 from: number;
786 to: number;
787 }) => any;
788}
789/**
790Create an extension that configures code folding.
791*/
792declare function codeFolding(config?: FoldConfig): Extension;
793type Handlers = {
794 [event: string]: (view: EditorView, line: BlockInfo, event: Event) => boolean;
795};
796interface FoldGutterConfig {
797 /**
798 A function that creates the DOM element used to indicate a
799 given line is folded or can be folded.
800 When not given, the `openText`/`closeText` option will be used instead.
801 */
802 markerDOM?: ((open: boolean) => HTMLElement) | null;
803 /**
804 Text used to indicate that a given line can be folded.
805 Defaults to `"⌄"`.
806 */
807 openText?: string;
808 /**
809 Text used to indicate that a given line is folded.
810 Defaults to `"›"`.
811 */
812 closedText?: string;
813 /**
814 Supply event handlers for DOM events on this gutter.
815 */
816 domEventHandlers?: Handlers;
817 /**
818 When given, if this returns true for a given view update,
819 recompute the fold markers.
820 */
821 foldingChanged?: (update: ViewUpdate) => boolean;
822}
823/**
824Create an extension that registers a fold gutter, which shows a
825fold status indicator before foldable lines (which can be clicked
826to fold or unfold the line).
827*/
828declare function foldGutter(config?: FoldGutterConfig): Extension;
829
830/**
831A highlight style associates CSS styles with highlighting
832[tags](https://lezer.codemirror.net/docs/ref#highlight.Tag).
833*/
834declare class HighlightStyle implements Highlighter {
835 /**
836 The tag styles used to create this highlight style.
837 */
838 readonly specs: readonly TagStyle[];
839 /**
840 A style module holding the CSS rules for this highlight style.
841 When using
842 [`highlightTree`](https://lezer.codemirror.net/docs/ref#highlight.highlightTree)
843 outside of the editor, you may want to manually mount this
844 module to show the highlighting.
845 */
846 readonly module: StyleModule | null;
847 readonly style: (tags: readonly Tag[]) => string | null;
848 readonly scope?: (type: NodeType) => boolean;
849 private constructor();
850 /**
851 Create a highlighter style that associates the given styles to
852 the given tags. The specs must be objects that hold a style tag
853 or array of tags in their `tag` property, and either a single
854 `class` property providing a static CSS class (for highlighter
855 that rely on external styling), or a
856 [`style-mod`](https://github.com/marijnh/style-mod#documentation)-style
857 set of CSS properties (which define the styling for those tags).
858
859 The CSS rules created for a highlighter will be emitted in the
860 order of the spec's properties. That means that for elements that
861 have multiple tags associated with them, styles defined further
862 down in the list will have a higher CSS precedence than styles
863 defined earlier.
864 */
865 static define(specs: readonly TagStyle[], options?: {
866 /**
867 By default, highlighters apply to the entire document. You can
868 scope them to a single language by providing the language
869 object or a language's top node type here.
870 */
871 scope?: Language | NodeType;
872 /**
873 Add a style to _all_ content. Probably only useful in
874 combination with `scope`.
875 */
876 all?: string | StyleSpec;
877 /**
878 Specify that this highlight style should only be active then
879 the theme is dark or light. By default, it is active
880 regardless of theme.
881 */
882 themeType?: "dark" | "light";
883 }): HighlightStyle;
884}
885/**
886Wrap a highlighter in an editor extension that uses it to apply
887syntax highlighting to the editor content.
888
889When multiple (non-fallback) styles are provided, the styling
890applied is the union of the classes they emit.
891*/
892declare function syntaxHighlighting(highlighter: Highlighter, options?: {
893 /**
894 When enabled, this marks the highlighter as a fallback, which
895 only takes effect if no other highlighters are registered.
896 */
897 fallback: boolean;
898}): Extension;
899/**
900Returns the CSS classes (if any) that the highlighters active in
901the state would assign to the given style
902[tags](https://lezer.codemirror.net/docs/ref#highlight.Tag) and
903(optional) language
904[scope](https://codemirror.net/6/docs/ref/#language.HighlightStyle^define^options.scope).
905*/
906declare function highlightingFor(state: EditorState, tags: readonly Tag[], scope?: NodeType): string | null;
907/**
908The type of object used in
909[`HighlightStyle.define`](https://codemirror.net/6/docs/ref/#language.HighlightStyle^define).
910Assigns a style to one or more highlighting
911[tags](https://lezer.codemirror.net/docs/ref#highlight.Tag), which can either be a fixed class name
912(which must be defined elsewhere), or a set of CSS properties, for
913which the library will define an anonymous class.
914*/
915interface TagStyle {
916 /**
917 The tag or tags to target.
918 */
919 tag: Tag | readonly Tag[];
920 /**
921 If given, this maps the tags to a fixed class name.
922 */
923 class?: string;
924 /**
925 Any further properties (if `class` isn't given) will be
926 interpreted as in style objects given to
927 [style-mod](https://github.com/marijnh/style-mod#documentation).
928 (The type here is `any` because of TypeScript limitations.)
929 */
930 [styleProperty: string]: any;
931}
932/**
933A default highlight style (works well with light themes).
934*/
935declare const defaultHighlightStyle: HighlightStyle;
936
937interface Config {
938 /**
939 Whether the bracket matching should look at the character after
940 the cursor when matching (if the one before isn't a bracket).
941 Defaults to true.
942 */
943 afterCursor?: boolean;
944 /**
945 The bracket characters to match, as a string of pairs. Defaults
946 to `"()[]{}"`. Note that these are only used as fallback when
947 there is no [matching
948 information](https://lezer.codemirror.net/docs/ref/#common.NodeProp^closedBy)
949 in the syntax tree.
950 */
951 brackets?: string;
952 /**
953 The maximum distance to scan for matching brackets. This is only
954 relevant for brackets not encoded in the syntax tree. Defaults
955 to 10 000.
956 */
957 maxScanDistance?: number;
958 /**
959 Can be used to configure the way in which brackets are
960 decorated. The default behavior is to add the
961 `cm-matchingBracket` class for matching pairs, and
962 `cm-nonmatchingBracket` for mismatched pairs or single brackets.
963 */
964 renderMatch?: (match: MatchResult, state: EditorState) => readonly Range<Decoration>[];
965}
966/**
967Create an extension that enables bracket matching. Whenever the
968cursor is next to a bracket, that bracket and the one it matches
969are highlighted. Or, when no matching bracket is found, another
970highlighting style is used to indicate this.
971*/
972declare function bracketMatching(config?: Config): Extension;
973/**
974When larger syntax nodes, such as HTML tags, are marked as
975opening/closing, it can be a bit messy to treat the whole node as
976a matchable bracket. This node prop allows you to define, for such
977a node, a ‘handle’—the part of the node that is highlighted, and
978that the cursor must be on to activate highlighting in the first
979place.
980*/
981declare const bracketMatchingHandle: NodeProp<(node: SyntaxNode) => SyntaxNode | null>;
982/**
983The result returned from `matchBrackets`.
984*/
985interface MatchResult {
986 /**
987 The extent of the bracket token found.
988 */
989 start: {
990 from: number;
991 to: number;
992 };
993 /**
994 The extent of the matched token, if any was found.
995 */
996 end?: {
997 from: number;
998 to: number;
999 };
1000 /**
1001 Whether the tokens match. This can be false even when `end` has
1002 a value, if that token doesn't match the opening token.
1003 */
1004 matched: boolean;
1005}
1006/**
1007Find the matching bracket for the token at `pos`, scanning
1008direction `dir`. Only the `brackets` and `maxScanDistance`
1009properties are used from `config`, if given. Returns null if no
1010bracket was found at `pos`, or a match result otherwise.
1011*/
1012declare function matchBrackets(state: EditorState, pos: number, dir: -1 | 1, config?: Config): MatchResult | null;
1013
1014/**
1015Encapsulates a single line of input. Given to stream syntax code,
1016which uses it to tokenize the content.
1017*/
1018declare class StringStream {
1019 /**
1020 The line.
1021 */
1022 string: string;
1023 private tabSize;
1024 /**
1025 The current indent unit size.
1026 */
1027 indentUnit: number;
1028 private overrideIndent?;
1029 /**
1030 The current position on the line.
1031 */
1032 pos: number;
1033 /**
1034 The start position of the current token.
1035 */
1036 start: number;
1037 private lastColumnPos;
1038 private lastColumnValue;
1039 /**
1040 Create a stream.
1041 */
1042 constructor(
1043 /**
1044 The line.
1045 */
1046 string: string, tabSize: number,
1047 /**
1048 The current indent unit size.
1049 */
1050 indentUnit: number, overrideIndent?: number | undefined);
1051 /**
1052 True if we are at the end of the line.
1053 */
1054 eol(): boolean;
1055 /**
1056 True if we are at the start of the line.
1057 */
1058 sol(): boolean;
1059 /**
1060 Get the next code unit after the current position, or undefined
1061 if we're at the end of the line.
1062 */
1063 peek(): string | undefined;
1064 /**
1065 Read the next code unit and advance `this.pos`.
1066 */
1067 next(): string | void;
1068 /**
1069 Match the next character against the given string, regular
1070 expression, or predicate. Consume and return it if it matches.
1071 */
1072 eat(match: string | RegExp | ((ch: string) => boolean)): string | void;
1073 /**
1074 Continue matching characters that match the given string,
1075 regular expression, or predicate function. Return true if any
1076 characters were consumed.
1077 */
1078 eatWhile(match: string | RegExp | ((ch: string) => boolean)): boolean;
1079 /**
1080 Consume whitespace ahead of `this.pos`. Return true if any was
1081 found.
1082 */
1083 eatSpace(): boolean;
1084 /**
1085 Move to the end of the line.
1086 */
1087 skipToEnd(): void;
1088 /**
1089 Move to directly before the given character, if found on the
1090 current line.
1091 */
1092 skipTo(ch: string): boolean | void;
1093 /**
1094 Move back `n` characters.
1095 */
1096 backUp(n: number): void;
1097 /**
1098 Get the column position at `this.pos`.
1099 */
1100 column(): number;
1101 /**
1102 Get the indentation column of the current line.
1103 */
1104 indentation(): number;
1105 /**
1106 Match the input against the given string or regular expression
1107 (which should start with a `^`). Return true or the regexp match
1108 if it matches.
1109
1110 Unless `consume` is set to `false`, this will move `this.pos`
1111 past the matched text.
1112
1113 When matching a string `caseInsensitive` can be set to true to
1114 make the match case-insensitive.
1115 */
1116 match(pattern: string | RegExp, consume?: boolean, caseInsensitive?: boolean): boolean | RegExpMatchArray | null;
1117 /**
1118 Get the current token.
1119 */
1120 current(): string;
1121}
1122
1123/**
1124A stream parser parses or tokenizes content from start to end,
1125emitting tokens as it goes over it. It keeps a mutable (but
1126copyable) object with state, in which it can store information
1127about the current context.
1128*/
1129interface StreamParser<State> {
1130 /**
1131 A name for this language.
1132 */
1133 name?: string;
1134 /**
1135 Produce a start state for the parser.
1136 */
1137 startState?(indentUnit: number): State;
1138 /**
1139 Read one token, advancing the stream past it, and returning a
1140 string indicating the token's style tag—either the name of one
1141 of the tags in
1142 [`tags`](https://lezer.codemirror.net/docs/ref#highlight.tags)
1143 or [`tokenTable`](https://codemirror.net/6/docs/ref/#language.StreamParser.tokenTable), or such a
1144 name suffixed by one or more tag
1145 [modifier](https://lezer.codemirror.net/docs/ref#highlight.Tag^defineModifier)
1146 names, separated by periods. For example `"keyword"` or
1147 "`variableName.constant"`, or a space-separated set of such
1148 token types.
1149
1150 It is okay to return a zero-length token, but only if that
1151 updates the state so that the next call will return a non-empty
1152 token again.
1153 */
1154 token(stream: StringStream, state: State): string | null;
1155 /**
1156 This notifies the parser of a blank line in the input. It can
1157 update its state here if it needs to.
1158 */
1159 blankLine?(state: State, indentUnit: number): void;
1160 /**
1161 Copy a given state. By default, a shallow object copy is done
1162 which also copies arrays held at the top level of the object.
1163 */
1164 copyState?(state: State): State;
1165 /**
1166 Compute automatic indentation for the line that starts with the
1167 given state and text.
1168 */
1169 indent?(state: State, textAfter: string, context: IndentContext): number | null;
1170 /**
1171 Default [language data](https://codemirror.net/6/docs/ref/#state.EditorState.languageDataAt) to
1172 attach to this language.
1173 */
1174 languageData?: {
1175 [name: string]: any;
1176 };
1177 /**
1178 Extra tokens to use in this parser. When the tokenizer returns a
1179 token name that exists as a property in this object, the
1180 corresponding tags will be assigned to the token.
1181 */
1182 tokenTable?: {
1183 [name: string]: Tag | readonly Tag[];
1184 };
1185 /**
1186 By default, adjacent tokens of the same type are merged in the
1187 output tree. Set this to false to disable that.
1188 */
1189 mergeTokens?: boolean;
1190}
1191/**
1192A [language](https://codemirror.net/6/docs/ref/#language.Language) class based on a CodeMirror
11935-style [streaming parser](https://codemirror.net/6/docs/ref/#language.StreamParser).
1194*/
1195declare class StreamLanguage<State> extends Language {
1196 private constructor();
1197 /**
1198 Define a stream language.
1199 */
1200 static define<State>(spec: StreamParser<State>): StreamLanguage<State>;
1201 get allowsNesting(): boolean;
1202}
1203
1204/**
1205Make sure nodes
1206[marked](https://lezer.codemirror.net/docs/ref/#common.NodeProp^isolate)
1207as isolating for bidirectional text are rendered in a way that
1208isolates them from the surrounding text.
1209*/
1210declare function bidiIsolates(options?: {
1211 /**
1212 By default, isolating elements are only added when the editor
1213 direction isn't uniformly left-to-right, or if it is, on lines
1214 that contain right-to-left character. When true, disable this
1215 optimization and add them everywhere.
1216 */
1217 alwaysIsolate?: boolean;
1218}): Extension;
1219
1220export { type Config, DocInput, HighlightStyle, IndentContext, LRLanguage, Language, LanguageDescription, LanguageSupport, type MatchResult, ParseContext, StreamLanguage, type StreamParser, StringStream, type Sublanguage, type TagStyle, TreeIndentContext, bidiIsolates, bracketMatching, bracketMatchingHandle, codeFolding, continuedIndent, defaultHighlightStyle, defineLanguageFacet, delimitedIndent, ensureSyntaxTree, flatIndent, foldAll, foldCode, foldEffect, foldGutter, foldInside, foldKeymap, foldNodeProp, foldService, foldState, foldable, foldedRanges, forceParsing, getIndentUnit, getIndentation, highlightingFor, indentNodeProp, indentOnInput, indentRange, indentService, indentString, indentUnit, language, languageDataProp, matchBrackets, sublanguageProp, syntaxHighlighting, syntaxParserRunning, syntaxTree, syntaxTreeAvailable, toggleFold, unfoldAll, unfoldCode, unfoldEffect };