@codemirror/language 6.10.8 → 6.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## 6.11.0 (2025-03-13)
2
+
3
+ ### New features
4
+
5
+ Stream parsers now support a `mergeTokens` option that can be used to turn off automatic merging of adjacent tokens.
6
+
1
7
  ## 6.10.8 (2024-12-23)
2
8
 
3
9
  ### Bug fixes
package/README.md CHANGED
@@ -16,3 +16,51 @@ We aim to be an inclusive, welcoming community. To make that explicit,
16
16
  we have a [code of
17
17
  conduct](http://contributor-covenant.org/version/1/1/0/) that applies
18
18
  to communication around the project.
19
+
20
+ ## Usage
21
+
22
+ Setting up a language from a [Lezer](https://lezer.codemirror.net)
23
+ parser looks like this:
24
+
25
+ ```javascript
26
+ import {parser} from "@lezer/json"
27
+ import {LRLanguage, continuedIndent, indentNodeProp,
28
+ foldNodeProp, foldInside} from "@codemirror/language"
29
+
30
+ export const jsonLanguage = LRLanguage.define({
31
+ name: "json",
32
+ parser: parser.configure({
33
+ props: [
34
+ indentNodeProp.add({
35
+ Object: continuedIndent({except: /^\s*\}/}),
36
+ Array: continuedIndent({except: /^\s*\]/})
37
+ }),
38
+ foldNodeProp.add({
39
+ "Object Array": foldInside
40
+ })
41
+ ]
42
+ }),
43
+ languageData: {
44
+ closeBrackets: {brackets: ["[", "{", '"']},
45
+ indentOnInput: /^\s*[\}\]]$/
46
+ }
47
+ })
48
+ ```
49
+
50
+ Often, you'll also use this package just to access some specific
51
+ language-related features, such as accessing the editor's syntax
52
+ tree...
53
+
54
+ ```javascript
55
+ import {syntaxTree} from "@codemirror/language"
56
+
57
+ const tree = syntaxTree(view)
58
+ ```
59
+
60
+ ... or computing the appriate indentation at a given point.
61
+
62
+ ```javascript
63
+ import {getIndentation} from "@codemirror/language"
64
+
65
+ console.log(getIndentation(view.state, view.state.selection.main.head))
66
+ ```
package/dist/index.cjs CHANGED
@@ -2181,7 +2181,8 @@ function fullParser(spec) {
2181
2181
  copyState: spec.copyState || defaultCopyState,
2182
2182
  indent: spec.indent || (() => null),
2183
2183
  languageData: spec.languageData || {},
2184
- tokenTable: spec.tokenTable || noTokens
2184
+ tokenTable: spec.tokenTable || noTokens,
2185
+ mergeTokens: spec.mergeTokens !== false
2185
2186
  };
2186
2187
  }
2187
2188
  function defaultCopyState(state) {
@@ -2404,7 +2405,8 @@ class Parse {
2404
2405
  size += this.chunk.length - len0;
2405
2406
  }
2406
2407
  let last = this.chunk.length - 4;
2407
- if (size == 4 && last >= 0 && this.chunk[last] == id && this.chunk[last + 2] == from)
2408
+ if (this.lang.streamParser.mergeTokens && size == 4 && last >= 0 &&
2409
+ this.chunk[last] == id && this.chunk[last + 2] == from)
2408
2410
  this.chunk[last + 2] = to;
2409
2411
  else
2410
2412
  this.chunk.push(id, from, to, size);
package/dist/index.d.cts CHANGED
@@ -1182,6 +1182,11 @@ interface StreamParser<State> {
1182
1182
  tokenTable?: {
1183
1183
  [name: string]: Tag | readonly Tag[];
1184
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;
1185
1190
  }
1186
1191
  /**
1187
1192
  A [language](https://codemirror.net/6/docs/ref/#language.Language) class based on a CodeMirror
package/dist/index.d.ts CHANGED
@@ -1182,6 +1182,11 @@ interface StreamParser<State> {
1182
1182
  tokenTable?: {
1183
1183
  [name: string]: Tag | readonly Tag[];
1184
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;
1185
1190
  }
1186
1191
  /**
1187
1192
  A [language](https://codemirror.net/6/docs/ref/#language.Language) class based on a CodeMirror
package/dist/index.js CHANGED
@@ -2179,7 +2179,8 @@ function fullParser(spec) {
2179
2179
  copyState: spec.copyState || defaultCopyState,
2180
2180
  indent: spec.indent || (() => null),
2181
2181
  languageData: spec.languageData || {},
2182
- tokenTable: spec.tokenTable || noTokens
2182
+ tokenTable: spec.tokenTable || noTokens,
2183
+ mergeTokens: spec.mergeTokens !== false
2183
2184
  };
2184
2185
  }
2185
2186
  function defaultCopyState(state) {
@@ -2402,7 +2403,8 @@ class Parse {
2402
2403
  size += this.chunk.length - len0;
2403
2404
  }
2404
2405
  let last = this.chunk.length - 4;
2405
- if (size == 4 && last >= 0 && this.chunk[last] == id && this.chunk[last + 2] == from)
2406
+ if (this.lang.streamParser.mergeTokens && size == 4 && last >= 0 &&
2407
+ this.chunk[last] == id && this.chunk[last + 2] == from)
2406
2408
  this.chunk[last + 2] = to;
2407
2409
  else
2408
2410
  this.chunk.push(id, from, to, size);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codemirror/language",
3
- "version": "6.10.8",
3
+ "version": "6.11.0",
4
4
  "description": "Language support infrastructure for the CodeMirror code editor",
5
5
  "scripts": {
6
6
  "test": "cm-runtests",