@codemirror/language 6.10.8 → 6.11.1
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 +12 -0
- package/README.md +48 -0
- package/dist/index.cjs +14 -8
- package/dist/index.d.cts +7 -2
- package/dist/index.d.ts +7 -2
- package/dist/index.js +14 -8
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
## 6.11.1 (2025-06-02)
|
|
2
|
+
|
|
3
|
+
### Bug fixes
|
|
4
|
+
|
|
5
|
+
Fix an issue where indentation would sometimes miss nodes in mixed-language situations.
|
|
6
|
+
|
|
7
|
+
## 6.11.0 (2025-03-13)
|
|
8
|
+
|
|
9
|
+
### New features
|
|
10
|
+
|
|
11
|
+
Stream parsers now support a `mergeTokens` option that can be used to turn off automatic merging of adjacent tokens.
|
|
12
|
+
|
|
1
13
|
## 6.10.8 (2024-12-23)
|
|
2
14
|
|
|
3
15
|
### 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
|
@@ -803,8 +803,8 @@ service.
|
|
|
803
803
|
const indentService = state.Facet.define();
|
|
804
804
|
/**
|
|
805
805
|
Facet for overriding the unit by which indentation happens. Should
|
|
806
|
-
be a string consisting
|
|
807
|
-
|
|
806
|
+
be a string consisting entirely of the same whitespace character.
|
|
807
|
+
When not set, this defaults to 2 spaces.
|
|
808
808
|
*/
|
|
809
809
|
const indentUnit = state.Facet.define({
|
|
810
810
|
combine: values => {
|
|
@@ -998,7 +998,8 @@ function syntaxIndentation(cx, ast, pos) {
|
|
|
998
998
|
let inner = ast.resolveInner(pos, -1).resolve(pos, 0).enterUnfinishedNodesBefore(pos);
|
|
999
999
|
if (inner != stack.node) {
|
|
1000
1000
|
let add = [];
|
|
1001
|
-
for (let cur = inner; cur && !(cur.from
|
|
1001
|
+
for (let cur = inner; cur && !(cur.from < stack.node.from || cur.to > stack.node.to ||
|
|
1002
|
+
cur.from == stack.node.from && cur.type == stack.node.type); cur = cur.parent)
|
|
1002
1003
|
add.push(cur);
|
|
1003
1004
|
for (let i = add.length - 1; i >= 0; i--)
|
|
1004
1005
|
stack = { node: add[i], next: stack };
|
|
@@ -1579,7 +1580,7 @@ fold status indicator before foldable lines (which can be clicked
|
|
|
1579
1580
|
to fold or unfold the line).
|
|
1580
1581
|
*/
|
|
1581
1582
|
function foldGutter(config = {}) {
|
|
1582
|
-
let fullConfig =
|
|
1583
|
+
let fullConfig = { ...foldGutterDefaults, ...config };
|
|
1583
1584
|
let canFold = new FoldMarker(fullConfig, true), canUnfold = new FoldMarker(fullConfig, false);
|
|
1584
1585
|
let markers = view.ViewPlugin.fromClass(class {
|
|
1585
1586
|
constructor(view) {
|
|
@@ -1614,7 +1615,9 @@ function foldGutter(config = {}) {
|
|
|
1614
1615
|
initialSpacer() {
|
|
1615
1616
|
return new FoldMarker(fullConfig, false);
|
|
1616
1617
|
},
|
|
1617
|
-
domEventHandlers:
|
|
1618
|
+
domEventHandlers: {
|
|
1619
|
+
...domEventHandlers,
|
|
1620
|
+
click: (view, line, event) => {
|
|
1618
1621
|
if (domEventHandlers.click && domEventHandlers.click(view, line, event))
|
|
1619
1622
|
return true;
|
|
1620
1623
|
let folded = findFold(view.state, line.from, line.to);
|
|
@@ -1628,7 +1631,8 @@ function foldGutter(config = {}) {
|
|
|
1628
1631
|
return true;
|
|
1629
1632
|
}
|
|
1630
1633
|
return false;
|
|
1631
|
-
}
|
|
1634
|
+
}
|
|
1635
|
+
}
|
|
1632
1636
|
}),
|
|
1633
1637
|
codeFolding()
|
|
1634
1638
|
];
|
|
@@ -2181,7 +2185,8 @@ function fullParser(spec) {
|
|
|
2181
2185
|
copyState: spec.copyState || defaultCopyState,
|
|
2182
2186
|
indent: spec.indent || (() => null),
|
|
2183
2187
|
languageData: spec.languageData || {},
|
|
2184
|
-
tokenTable: spec.tokenTable || noTokens
|
|
2188
|
+
tokenTable: spec.tokenTable || noTokens,
|
|
2189
|
+
mergeTokens: spec.mergeTokens !== false
|
|
2185
2190
|
};
|
|
2186
2191
|
}
|
|
2187
2192
|
function defaultCopyState(state) {
|
|
@@ -2404,7 +2409,8 @@ class Parse {
|
|
|
2404
2409
|
size += this.chunk.length - len0;
|
|
2405
2410
|
}
|
|
2406
2411
|
let last = this.chunk.length - 4;
|
|
2407
|
-
if (size == 4 && last >= 0 &&
|
|
2412
|
+
if (this.lang.streamParser.mergeTokens && size == 4 && last >= 0 &&
|
|
2413
|
+
this.chunk[last] == id && this.chunk[last + 2] == from)
|
|
2408
2414
|
this.chunk[last + 2] = to;
|
|
2409
2415
|
else
|
|
2410
2416
|
this.chunk.push(id, from, to, size);
|
package/dist/index.d.cts
CHANGED
|
@@ -429,8 +429,8 @@ service.
|
|
|
429
429
|
declare const indentService: Facet<(context: IndentContext, pos: number) => number | null | undefined, readonly ((context: IndentContext, pos: number) => number | null | undefined)[]>;
|
|
430
430
|
/**
|
|
431
431
|
Facet for overriding the unit by which indentation happens. Should
|
|
432
|
-
be a string consisting
|
|
433
|
-
|
|
432
|
+
be a string consisting entirely of the same whitespace character.
|
|
433
|
+
When not set, this defaults to 2 spaces.
|
|
434
434
|
*/
|
|
435
435
|
declare const indentUnit: Facet<string, string>;
|
|
436
436
|
/**
|
|
@@ -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
|
@@ -429,8 +429,8 @@ service.
|
|
|
429
429
|
declare const indentService: Facet<(context: IndentContext, pos: number) => number | null | undefined, readonly ((context: IndentContext, pos: number) => number | null | undefined)[]>;
|
|
430
430
|
/**
|
|
431
431
|
Facet for overriding the unit by which indentation happens. Should
|
|
432
|
-
be a string consisting
|
|
433
|
-
|
|
432
|
+
be a string consisting entirely of the same whitespace character.
|
|
433
|
+
When not set, this defaults to 2 spaces.
|
|
434
434
|
*/
|
|
435
435
|
declare const indentUnit: Facet<string, string>;
|
|
436
436
|
/**
|
|
@@ -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
|
@@ -801,8 +801,8 @@ service.
|
|
|
801
801
|
const indentService = /*@__PURE__*/Facet.define();
|
|
802
802
|
/**
|
|
803
803
|
Facet for overriding the unit by which indentation happens. Should
|
|
804
|
-
be a string consisting
|
|
805
|
-
|
|
804
|
+
be a string consisting entirely of the same whitespace character.
|
|
805
|
+
When not set, this defaults to 2 spaces.
|
|
806
806
|
*/
|
|
807
807
|
const indentUnit = /*@__PURE__*/Facet.define({
|
|
808
808
|
combine: values => {
|
|
@@ -996,7 +996,8 @@ function syntaxIndentation(cx, ast, pos) {
|
|
|
996
996
|
let inner = ast.resolveInner(pos, -1).resolve(pos, 0).enterUnfinishedNodesBefore(pos);
|
|
997
997
|
if (inner != stack.node) {
|
|
998
998
|
let add = [];
|
|
999
|
-
for (let cur = inner; cur && !(cur.from
|
|
999
|
+
for (let cur = inner; cur && !(cur.from < stack.node.from || cur.to > stack.node.to ||
|
|
1000
|
+
cur.from == stack.node.from && cur.type == stack.node.type); cur = cur.parent)
|
|
1000
1001
|
add.push(cur);
|
|
1001
1002
|
for (let i = add.length - 1; i >= 0; i--)
|
|
1002
1003
|
stack = { node: add[i], next: stack };
|
|
@@ -1577,7 +1578,7 @@ fold status indicator before foldable lines (which can be clicked
|
|
|
1577
1578
|
to fold or unfold the line).
|
|
1578
1579
|
*/
|
|
1579
1580
|
function foldGutter(config = {}) {
|
|
1580
|
-
let fullConfig =
|
|
1581
|
+
let fullConfig = { ...foldGutterDefaults, ...config };
|
|
1581
1582
|
let canFold = new FoldMarker(fullConfig, true), canUnfold = new FoldMarker(fullConfig, false);
|
|
1582
1583
|
let markers = ViewPlugin.fromClass(class {
|
|
1583
1584
|
constructor(view) {
|
|
@@ -1612,7 +1613,9 @@ function foldGutter(config = {}) {
|
|
|
1612
1613
|
initialSpacer() {
|
|
1613
1614
|
return new FoldMarker(fullConfig, false);
|
|
1614
1615
|
},
|
|
1615
|
-
domEventHandlers:
|
|
1616
|
+
domEventHandlers: {
|
|
1617
|
+
...domEventHandlers,
|
|
1618
|
+
click: (view, line, event) => {
|
|
1616
1619
|
if (domEventHandlers.click && domEventHandlers.click(view, line, event))
|
|
1617
1620
|
return true;
|
|
1618
1621
|
let folded = findFold(view.state, line.from, line.to);
|
|
@@ -1626,7 +1629,8 @@ function foldGutter(config = {}) {
|
|
|
1626
1629
|
return true;
|
|
1627
1630
|
}
|
|
1628
1631
|
return false;
|
|
1629
|
-
}
|
|
1632
|
+
}
|
|
1633
|
+
}
|
|
1630
1634
|
}),
|
|
1631
1635
|
codeFolding()
|
|
1632
1636
|
];
|
|
@@ -2179,7 +2183,8 @@ function fullParser(spec) {
|
|
|
2179
2183
|
copyState: spec.copyState || defaultCopyState,
|
|
2180
2184
|
indent: spec.indent || (() => null),
|
|
2181
2185
|
languageData: spec.languageData || {},
|
|
2182
|
-
tokenTable: spec.tokenTable || noTokens
|
|
2186
|
+
tokenTable: spec.tokenTable || noTokens,
|
|
2187
|
+
mergeTokens: spec.mergeTokens !== false
|
|
2183
2188
|
};
|
|
2184
2189
|
}
|
|
2185
2190
|
function defaultCopyState(state) {
|
|
@@ -2402,7 +2407,8 @@ class Parse {
|
|
|
2402
2407
|
size += this.chunk.length - len0;
|
|
2403
2408
|
}
|
|
2404
2409
|
let last = this.chunk.length - 4;
|
|
2405
|
-
if (size == 4 && last >= 0 &&
|
|
2410
|
+
if (this.lang.streamParser.mergeTokens && size == 4 && last >= 0 &&
|
|
2411
|
+
this.chunk[last] == id && this.chunk[last + 2] == from)
|
|
2406
2412
|
this.chunk[last + 2] = to;
|
|
2407
2413
|
else
|
|
2408
2414
|
this.chunk.push(id, from, to, size);
|