index.d.ts 38 KB

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