@onereach/ui-components 4.0.2 → 4.1.0-beta.2508.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/dist/bundled/v2/components/OrCode/OrCode.js +6 -3727
- package/dist/bundled/v2/components/OrCode/index.js +6 -2
- package/dist/bundled/v2/components/OrCode/lang.js +3898 -3
- package/dist/bundled/v2/components/OrCode/theme.js +3 -1
- package/dist/bundled/v2/components/OrRichTextEditorV3/OrRichTextEditor.js +2033 -0
- package/dist/bundled/v2/components/OrRichTextEditorV3/OrRichTextEditor.vue.d.ts +181 -0
- package/dist/bundled/v2/components/OrRichTextEditorV3/index.d.ts +1 -0
- package/dist/bundled/v2/components/OrRichTextEditorV3/index.js +67 -0
- package/dist/bundled/v2/components/OrRichTextEditorV3/styles.d.ts +7 -0
- package/dist/bundled/v2/components/OrRichTextEditorV3/styles.js +43 -0
- package/dist/bundled/v2/components/OrRichTextEditorV3/utils/codemirror/codemirrorNode.d.ts +3 -0
- package/dist/bundled/v2/components/OrRichTextEditorV3/utils/codemirror/codemirrorNode.js +45 -0
- package/dist/bundled/v2/components/OrRichTextEditorV3/utils/codemirror/codemirrorView.d.ts +27 -0
- package/dist/bundled/v2/components/OrRichTextEditorV3/utils/codemirror/codemirrorView.js +183 -0
- package/dist/bundled/v2/components/OrRichTextEditorV3/utils/codemirror/index.d.ts +1 -0
- package/dist/bundled/v2/components/OrRichTextEditorV3/utils/codemirror/index.js +4 -0
- package/dist/bundled/v2/components/OrRichTextEditorV3/utils/codemirror/types.d.ts +19 -0
- package/dist/bundled/v2/components/OrRichTextEditorV3/utils/codemirror/types.js +1 -0
- package/dist/bundled/v2/components/OrRichTextEditorV3/utils/markdown.d.ts +3 -0
- package/dist/bundled/v2/components/OrRichTextEditorV3/utils/markdown.js +5 -0
- package/dist/bundled/v2/components/index.d.ts +1 -0
- package/dist/bundled/v2/components/index.js +14 -2
- package/dist/bundled/v2/index-62c3221b.js +6956 -0
- package/dist/bundled/v2/index-62e39ec2.js +4534 -0
- package/dist/bundled/{v3/OrCode.vue_vue_type_script_lang-c5a9adb7.js → v2/index-92e69736.js} +15 -234
- package/dist/bundled/v2/{index-6976c52a.js → index-c76372bb.js} +16 -4659
- package/dist/bundled/{v3/lang-02d2bb2d.js → v2/index-db5e8c99.js} +74 -3965
- package/dist/bundled/v2/index.es-3f39f316.js +115 -0
- package/dist/bundled/v2/index.js +17 -5
- package/dist/bundled/v2/markdown-00716a39.js +18683 -0
- package/dist/bundled/v2/tiptap-core.esm-f85402b1.js +9360 -0
- package/dist/bundled/v3/OrCode.vue_vue_type_script_lang-0c5e41b0.js +236 -0
- package/dist/bundled/v3/OrRichTextEditor.vue_vue_type_script_lang-33b56bd6.js +1815 -0
- package/dist/bundled/v3/components/OrCode/OrCode.js +9 -5
- package/dist/bundled/v3/components/OrCode/index.js +7 -3
- package/dist/bundled/v3/components/OrCode/lang.js +3898 -3
- package/dist/bundled/v3/components/OrCode/theme.js +3 -1
- package/dist/bundled/v3/components/OrRichTextEditorV3/OrRichTextEditor.js +219 -0
- package/dist/bundled/v3/components/OrRichTextEditorV3/OrRichTextEditor.vue.d.ts +181 -0
- package/dist/bundled/v3/components/OrRichTextEditorV3/index.d.ts +1 -0
- package/dist/bundled/v3/components/OrRichTextEditorV3/index.js +83 -0
- package/dist/bundled/v3/components/OrRichTextEditorV3/styles.d.ts +7 -0
- package/dist/bundled/v3/components/OrRichTextEditorV3/styles.js +43 -0
- package/dist/bundled/v3/components/OrRichTextEditorV3/utils/codemirror/codemirrorNode.d.ts +3 -0
- package/dist/bundled/v3/components/OrRichTextEditorV3/utils/codemirror/codemirrorNode.js +45 -0
- package/dist/bundled/v3/components/OrRichTextEditorV3/utils/codemirror/codemirrorView.d.ts +27 -0
- package/dist/bundled/v3/components/OrRichTextEditorV3/utils/codemirror/codemirrorView.js +183 -0
- package/dist/bundled/v3/components/OrRichTextEditorV3/utils/codemirror/index.d.ts +1 -0
- package/dist/bundled/v3/components/OrRichTextEditorV3/utils/codemirror/index.js +4 -0
- package/dist/bundled/v3/components/OrRichTextEditorV3/utils/codemirror/types.d.ts +19 -0
- package/dist/bundled/v3/components/OrRichTextEditorV3/utils/codemirror/types.js +1 -0
- package/dist/bundled/v3/components/OrRichTextEditorV3/utils/markdown.d.ts +3 -0
- package/dist/bundled/v3/components/OrRichTextEditorV3/utils/markdown.js +5 -0
- package/dist/bundled/v3/components/index.d.ts +1 -0
- package/dist/bundled/v3/components/index.js +16 -3
- package/dist/bundled/v3/index-62c3221b.js +6956 -0
- package/dist/bundled/v3/index-62e39ec2.js +4534 -0
- package/dist/bundled/v3/index-92e69736.js +3741 -0
- package/dist/bundled/v3/{index-6976c52a.js → index-c76372bb.js} +16 -4659
- package/dist/bundled/{v2/lang-02d2bb2d.js → v3/index-db5e8c99.js} +74 -3965
- package/dist/bundled/v3/index.es-3f39f316.js +115 -0
- package/dist/bundled/v3/index.js +20 -7
- package/dist/bundled/v3/markdown-00716a39.js +18683 -0
- package/dist/bundled/v3/tiptap-core.esm-f85402b1.js +9360 -0
- package/dist/esm/v2/OrRichTextEditor-45fb1867.js +718 -0
- package/dist/esm/v2/codemirrorView-2e87c938.js +184 -0
- package/dist/esm/v2/components/index.d.ts +1 -0
- package/dist/esm/v2/components/index.js +24 -0
- package/dist/esm/v2/components/or-rich-text-editor-v3/OrRichTextEditor.vue.d.ts +181 -0
- package/dist/esm/v2/components/or-rich-text-editor-v3/index.d.ts +1 -0
- package/dist/esm/v2/components/or-rich-text-editor-v3/index.js +56 -0
- package/dist/esm/v2/components/or-rich-text-editor-v3/styles.d.ts +7 -0
- package/dist/esm/v2/components/or-rich-text-editor-v3/utils/codemirror/codemirrorNode.d.ts +3 -0
- package/dist/esm/v2/components/or-rich-text-editor-v3/utils/codemirror/codemirrorView.d.ts +27 -0
- package/dist/esm/v2/components/or-rich-text-editor-v3/utils/codemirror/index.d.ts +1 -0
- package/dist/esm/v2/components/or-rich-text-editor-v3/utils/codemirror/index.js +5 -0
- package/dist/esm/v2/components/or-rich-text-editor-v3/utils/codemirror/types.d.ts +19 -0
- package/dist/esm/v2/components/or-rich-text-editor-v3/utils/markdown.d.ts +3 -0
- package/dist/esm/v2/index.js +24 -0
- package/dist/esm/v3/OrRichTextEditor-4c0b6030.js +680 -0
- package/dist/esm/v3/codemirrorView-2e87c938.js +184 -0
- package/dist/esm/v3/components/index.d.ts +1 -0
- package/dist/esm/v3/components/index.js +24 -0
- package/dist/esm/v3/components/or-rich-text-editor-v3/OrRichTextEditor.vue.d.ts +181 -0
- package/dist/esm/v3/components/or-rich-text-editor-v3/index.d.ts +1 -0
- package/dist/esm/v3/components/or-rich-text-editor-v3/index.js +54 -0
- package/dist/esm/v3/components/or-rich-text-editor-v3/styles.d.ts +7 -0
- package/dist/esm/v3/components/or-rich-text-editor-v3/utils/codemirror/codemirrorNode.d.ts +3 -0
- package/dist/esm/v3/components/or-rich-text-editor-v3/utils/codemirror/codemirrorView.d.ts +27 -0
- package/dist/esm/v3/components/or-rich-text-editor-v3/utils/codemirror/index.d.ts +1 -0
- package/dist/esm/v3/components/or-rich-text-editor-v3/utils/codemirror/index.js +5 -0
- package/dist/esm/v3/components/or-rich-text-editor-v3/utils/codemirror/types.d.ts +19 -0
- package/dist/esm/v3/components/or-rich-text-editor-v3/utils/markdown.d.ts +3 -0
- package/dist/esm/v3/index.js +24 -0
- package/package.json +25 -5
- package/src/components/index.ts +1 -0
- package/src/components/or-rich-text-editor-v3/OrRichTextEditor.docs.mdx +7 -0
- package/src/components/or-rich-text-editor-v3/OrRichTextEditor.stories3.ts +79 -0
- package/src/components/or-rich-text-editor-v3/OrRichTextEditor.vue +622 -0
- package/src/components/or-rich-text-editor-v3/index.ts +1 -0
- package/src/components/or-rich-text-editor-v3/styles.ts +72 -0
- package/src/components/or-rich-text-editor-v3/utils/codemirror/codemirrorNode.ts +40 -0
- package/src/components/or-rich-text-editor-v3/utils/codemirror/codemirrorView.ts +252 -0
- package/src/components/or-rich-text-editor-v3/utils/codemirror/index.ts +1 -0
- package/src/components/or-rich-text-editor-v3/utils/codemirror/types.ts +22 -0
- package/src/components/or-rich-text-editor-v3/utils/markdown.ts +110 -0
|
@@ -0,0 +1,4534 @@
|
|
|
1
|
+
import { E as EditorView, P as Prec, V as ViewPlugin, a as EditorState, g as StateEffect, S as StateField, F as Facet, G as gutter, H as RangeSet, I as StyleModule, i as Decoration, q as logException, f as countColumn, W as WidgetType, J as GutterMarker, R as RangeSetBuilder, c as combineConfig } from './index-c76372bb.js';
|
|
2
|
+
|
|
3
|
+
// FIXME profile adding a per-Tree TreeNode cache, validating it by
|
|
4
|
+
// parent pointer
|
|
5
|
+
/// The default maximum length of a `TreeBuffer` node.
|
|
6
|
+
const DefaultBufferLength = 1024;
|
|
7
|
+
let nextPropID = 0;
|
|
8
|
+
class Range {
|
|
9
|
+
constructor(from, to) {
|
|
10
|
+
this.from = from;
|
|
11
|
+
this.to = to;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
/// Each [node type](#common.NodeType) or [individual tree](#common.Tree)
|
|
15
|
+
/// can have metadata associated with it in props. Instances of this
|
|
16
|
+
/// class represent prop names.
|
|
17
|
+
class NodeProp {
|
|
18
|
+
/// Create a new node prop type.
|
|
19
|
+
constructor(config = {}) {
|
|
20
|
+
this.id = nextPropID++;
|
|
21
|
+
this.perNode = !!config.perNode;
|
|
22
|
+
this.deserialize = config.deserialize || (() => {
|
|
23
|
+
throw new Error("This node type doesn't define a deserialize function");
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
/// This is meant to be used with
|
|
27
|
+
/// [`NodeSet.extend`](#common.NodeSet.extend) or
|
|
28
|
+
/// [`LRParser.configure`](#lr.ParserConfig.props) to compute
|
|
29
|
+
/// prop values for each node type in the set. Takes a [match
|
|
30
|
+
/// object](#common.NodeType^match) or function that returns undefined
|
|
31
|
+
/// if the node type doesn't get this prop, and the prop's value if
|
|
32
|
+
/// it does.
|
|
33
|
+
add(match) {
|
|
34
|
+
if (this.perNode) throw new RangeError("Can't add per-node props to node types");
|
|
35
|
+
if (typeof match != "function") match = NodeType.match(match);
|
|
36
|
+
return type => {
|
|
37
|
+
let result = match(type);
|
|
38
|
+
return result === undefined ? null : [this, result];
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/// Prop that is used to describe matching delimiters. For opening
|
|
43
|
+
/// delimiters, this holds an array of node names (written as a
|
|
44
|
+
/// space-separated string when declaring this prop in a grammar)
|
|
45
|
+
/// for the node types of closing delimiters that match it.
|
|
46
|
+
NodeProp.closedBy = new NodeProp({
|
|
47
|
+
deserialize: str => str.split(" ")
|
|
48
|
+
});
|
|
49
|
+
/// The inverse of [`closedBy`](#common.NodeProp^closedBy). This is
|
|
50
|
+
/// attached to closing delimiters, holding an array of node names
|
|
51
|
+
/// of types of matching opening delimiters.
|
|
52
|
+
NodeProp.openedBy = new NodeProp({
|
|
53
|
+
deserialize: str => str.split(" ")
|
|
54
|
+
});
|
|
55
|
+
/// Used to assign node types to groups (for example, all node
|
|
56
|
+
/// types that represent an expression could be tagged with an
|
|
57
|
+
/// `"Expression"` group).
|
|
58
|
+
NodeProp.group = new NodeProp({
|
|
59
|
+
deserialize: str => str.split(" ")
|
|
60
|
+
});
|
|
61
|
+
/// The hash of the [context](#lr.ContextTracker.constructor)
|
|
62
|
+
/// that the node was parsed in, if any. Used to limit reuse of
|
|
63
|
+
/// contextual nodes.
|
|
64
|
+
NodeProp.contextHash = new NodeProp({
|
|
65
|
+
perNode: true
|
|
66
|
+
});
|
|
67
|
+
/// The distance beyond the end of the node that the tokenizer
|
|
68
|
+
/// looked ahead for any of the tokens inside the node. (The LR
|
|
69
|
+
/// parser only stores this when it is larger than 25, for
|
|
70
|
+
/// efficiency reasons.)
|
|
71
|
+
NodeProp.lookAhead = new NodeProp({
|
|
72
|
+
perNode: true
|
|
73
|
+
});
|
|
74
|
+
/// This per-node prop is used to replace a given node, or part of a
|
|
75
|
+
/// node, with another tree. This is useful to include trees from
|
|
76
|
+
/// different languages in mixed-language parsers.
|
|
77
|
+
NodeProp.mounted = new NodeProp({
|
|
78
|
+
perNode: true
|
|
79
|
+
});
|
|
80
|
+
/// A mounted tree, which can be [stored](#common.NodeProp^mounted) on
|
|
81
|
+
/// a tree node to indicate that parts of its content are
|
|
82
|
+
/// represented by another tree.
|
|
83
|
+
class MountedTree {
|
|
84
|
+
constructor(
|
|
85
|
+
/// The inner tree.
|
|
86
|
+
tree,
|
|
87
|
+
/// If this is null, this tree replaces the entire node (it will
|
|
88
|
+
/// be included in the regular iteration instead of its host
|
|
89
|
+
/// node). If not, only the given ranges are considered to be
|
|
90
|
+
/// covered by this tree. This is used for trees that are mixed in
|
|
91
|
+
/// a way that isn't strictly hierarchical. Such mounted trees are
|
|
92
|
+
/// only entered by [`resolveInner`](#common.Tree.resolveInner)
|
|
93
|
+
/// and [`enter`](#common.SyntaxNode.enter).
|
|
94
|
+
overlay,
|
|
95
|
+
/// The parser used to create this subtree.
|
|
96
|
+
parser) {
|
|
97
|
+
this.tree = tree;
|
|
98
|
+
this.overlay = overlay;
|
|
99
|
+
this.parser = parser;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
const noProps = Object.create(null);
|
|
103
|
+
/// Each node in a syntax tree has a node type associated with it.
|
|
104
|
+
class NodeType {
|
|
105
|
+
/// @internal
|
|
106
|
+
constructor(
|
|
107
|
+
/// The name of the node type. Not necessarily unique, but if the
|
|
108
|
+
/// grammar was written properly, different node types with the
|
|
109
|
+
/// same name within a node set should play the same semantic
|
|
110
|
+
/// role.
|
|
111
|
+
name,
|
|
112
|
+
/// @internal
|
|
113
|
+
props,
|
|
114
|
+
/// The id of this node in its set. Corresponds to the term ids
|
|
115
|
+
/// used in the parser.
|
|
116
|
+
id,
|
|
117
|
+
/// @internal
|
|
118
|
+
flags = 0) {
|
|
119
|
+
this.name = name;
|
|
120
|
+
this.props = props;
|
|
121
|
+
this.id = id;
|
|
122
|
+
this.flags = flags;
|
|
123
|
+
}
|
|
124
|
+
/// Define a node type.
|
|
125
|
+
static define(spec) {
|
|
126
|
+
let props = spec.props && spec.props.length ? Object.create(null) : noProps;
|
|
127
|
+
let flags = (spec.top ? 1 /* NodeFlag.Top */ : 0) | (spec.skipped ? 2 /* NodeFlag.Skipped */ : 0) | (spec.error ? 4 /* NodeFlag.Error */ : 0) | (spec.name == null ? 8 /* NodeFlag.Anonymous */ : 0);
|
|
128
|
+
let type = new NodeType(spec.name || "", props, spec.id, flags);
|
|
129
|
+
if (spec.props) for (let src of spec.props) {
|
|
130
|
+
if (!Array.isArray(src)) src = src(type);
|
|
131
|
+
if (src) {
|
|
132
|
+
if (src[0].perNode) throw new RangeError("Can't store a per-node prop on a node type");
|
|
133
|
+
props[src[0].id] = src[1];
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return type;
|
|
137
|
+
}
|
|
138
|
+
/// Retrieves a node prop for this type. Will return `undefined` if
|
|
139
|
+
/// the prop isn't present on this node.
|
|
140
|
+
prop(prop) {
|
|
141
|
+
return this.props[prop.id];
|
|
142
|
+
}
|
|
143
|
+
/// True when this is the top node of a grammar.
|
|
144
|
+
get isTop() {
|
|
145
|
+
return (this.flags & 1 /* NodeFlag.Top */) > 0;
|
|
146
|
+
}
|
|
147
|
+
/// True when this node is produced by a skip rule.
|
|
148
|
+
get isSkipped() {
|
|
149
|
+
return (this.flags & 2 /* NodeFlag.Skipped */) > 0;
|
|
150
|
+
}
|
|
151
|
+
/// Indicates whether this is an error node.
|
|
152
|
+
get isError() {
|
|
153
|
+
return (this.flags & 4 /* NodeFlag.Error */) > 0;
|
|
154
|
+
}
|
|
155
|
+
/// When true, this node type doesn't correspond to a user-declared
|
|
156
|
+
/// named node, for example because it is used to cache repetition.
|
|
157
|
+
get isAnonymous() {
|
|
158
|
+
return (this.flags & 8 /* NodeFlag.Anonymous */) > 0;
|
|
159
|
+
}
|
|
160
|
+
/// Returns true when this node's name or one of its
|
|
161
|
+
/// [groups](#common.NodeProp^group) matches the given string.
|
|
162
|
+
is(name) {
|
|
163
|
+
if (typeof name == 'string') {
|
|
164
|
+
if (this.name == name) return true;
|
|
165
|
+
let group = this.prop(NodeProp.group);
|
|
166
|
+
return group ? group.indexOf(name) > -1 : false;
|
|
167
|
+
}
|
|
168
|
+
return this.id == name;
|
|
169
|
+
}
|
|
170
|
+
/// Create a function from node types to arbitrary values by
|
|
171
|
+
/// specifying an object whose property names are node or
|
|
172
|
+
/// [group](#common.NodeProp^group) names. Often useful with
|
|
173
|
+
/// [`NodeProp.add`](#common.NodeProp.add). You can put multiple
|
|
174
|
+
/// names, separated by spaces, in a single property name to map
|
|
175
|
+
/// multiple node names to a single value.
|
|
176
|
+
static match(map) {
|
|
177
|
+
let direct = Object.create(null);
|
|
178
|
+
for (let prop in map) for (let name of prop.split(" ")) direct[name] = map[prop];
|
|
179
|
+
return node => {
|
|
180
|
+
for (let groups = node.prop(NodeProp.group), i = -1; i < (groups ? groups.length : 0); i++) {
|
|
181
|
+
let found = direct[i < 0 ? node.name : groups[i]];
|
|
182
|
+
if (found) return found;
|
|
183
|
+
}
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
/// An empty dummy node type to use when no actual type is available.
|
|
188
|
+
NodeType.none = new NodeType("", Object.create(null), 0, 8 /* NodeFlag.Anonymous */);
|
|
189
|
+
/// A node set holds a collection of node types. It is used to
|
|
190
|
+
/// compactly represent trees by storing their type ids, rather than a
|
|
191
|
+
/// full pointer to the type object, in a numeric array. Each parser
|
|
192
|
+
/// [has](#lr.LRParser.nodeSet) a node set, and [tree
|
|
193
|
+
/// buffers](#common.TreeBuffer) can only store collections of nodes
|
|
194
|
+
/// from the same set. A set can have a maximum of 2**16 (65536) node
|
|
195
|
+
/// types in it, so that the ids fit into 16-bit typed array slots.
|
|
196
|
+
class NodeSet {
|
|
197
|
+
/// Create a set with the given types. The `id` property of each
|
|
198
|
+
/// type should correspond to its position within the array.
|
|
199
|
+
constructor(
|
|
200
|
+
/// The node types in this set, by id.
|
|
201
|
+
types) {
|
|
202
|
+
this.types = types;
|
|
203
|
+
for (let i = 0; i < types.length; i++) if (types[i].id != i) throw new RangeError("Node type ids should correspond to array positions when creating a node set");
|
|
204
|
+
}
|
|
205
|
+
/// Create a copy of this set with some node properties added. The
|
|
206
|
+
/// arguments to this method can be created with
|
|
207
|
+
/// [`NodeProp.add`](#common.NodeProp.add).
|
|
208
|
+
extend(...props) {
|
|
209
|
+
let newTypes = [];
|
|
210
|
+
for (let type of this.types) {
|
|
211
|
+
let newProps = null;
|
|
212
|
+
for (let source of props) {
|
|
213
|
+
let add = source(type);
|
|
214
|
+
if (add) {
|
|
215
|
+
if (!newProps) newProps = Object.assign({}, type.props);
|
|
216
|
+
newProps[add[0].id] = add[1];
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
newTypes.push(newProps ? new NodeType(type.name, newProps, type.id, type.flags) : type);
|
|
220
|
+
}
|
|
221
|
+
return new NodeSet(newTypes);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
const CachedNode = new WeakMap(),
|
|
225
|
+
CachedInnerNode = new WeakMap();
|
|
226
|
+
/// Options that control iteration. Can be combined with the `|`
|
|
227
|
+
/// operator to enable multiple ones.
|
|
228
|
+
var IterMode;
|
|
229
|
+
(function (IterMode) {
|
|
230
|
+
/// When enabled, iteration will only visit [`Tree`](#common.Tree)
|
|
231
|
+
/// objects, not nodes packed into
|
|
232
|
+
/// [`TreeBuffer`](#common.TreeBuffer)s.
|
|
233
|
+
IterMode[IterMode["ExcludeBuffers"] = 1] = "ExcludeBuffers";
|
|
234
|
+
/// Enable this to make iteration include anonymous nodes (such as
|
|
235
|
+
/// the nodes that wrap repeated grammar constructs into a balanced
|
|
236
|
+
/// tree).
|
|
237
|
+
IterMode[IterMode["IncludeAnonymous"] = 2] = "IncludeAnonymous";
|
|
238
|
+
/// By default, regular [mounted](#common.NodeProp^mounted) nodes
|
|
239
|
+
/// replace their base node in iteration. Enable this to ignore them
|
|
240
|
+
/// instead.
|
|
241
|
+
IterMode[IterMode["IgnoreMounts"] = 4] = "IgnoreMounts";
|
|
242
|
+
/// This option only applies in
|
|
243
|
+
/// [`enter`](#common.SyntaxNode.enter)-style methods. It tells the
|
|
244
|
+
/// library to not enter mounted overlays if one covers the given
|
|
245
|
+
/// position.
|
|
246
|
+
IterMode[IterMode["IgnoreOverlays"] = 8] = "IgnoreOverlays";
|
|
247
|
+
})(IterMode || (IterMode = {}));
|
|
248
|
+
/// A piece of syntax tree. There are two ways to approach these
|
|
249
|
+
/// trees: the way they are actually stored in memory, and the
|
|
250
|
+
/// convenient way.
|
|
251
|
+
///
|
|
252
|
+
/// Syntax trees are stored as a tree of `Tree` and `TreeBuffer`
|
|
253
|
+
/// objects. By packing detail information into `TreeBuffer` leaf
|
|
254
|
+
/// nodes, the representation is made a lot more memory-efficient.
|
|
255
|
+
///
|
|
256
|
+
/// However, when you want to actually work with tree nodes, this
|
|
257
|
+
/// representation is very awkward, so most client code will want to
|
|
258
|
+
/// use the [`TreeCursor`](#common.TreeCursor) or
|
|
259
|
+
/// [`SyntaxNode`](#common.SyntaxNode) interface instead, which provides
|
|
260
|
+
/// a view on some part of this data structure, and can be used to
|
|
261
|
+
/// move around to adjacent nodes.
|
|
262
|
+
class Tree {
|
|
263
|
+
/// Construct a new tree. See also [`Tree.build`](#common.Tree^build).
|
|
264
|
+
constructor(
|
|
265
|
+
/// The type of the top node.
|
|
266
|
+
type,
|
|
267
|
+
/// This node's child nodes.
|
|
268
|
+
children,
|
|
269
|
+
/// The positions (offsets relative to the start of this tree) of
|
|
270
|
+
/// the children.
|
|
271
|
+
positions,
|
|
272
|
+
/// The total length of this tree
|
|
273
|
+
length,
|
|
274
|
+
/// Per-node [node props](#common.NodeProp) to associate with this node.
|
|
275
|
+
props) {
|
|
276
|
+
this.type = type;
|
|
277
|
+
this.children = children;
|
|
278
|
+
this.positions = positions;
|
|
279
|
+
this.length = length;
|
|
280
|
+
/// @internal
|
|
281
|
+
this.props = null;
|
|
282
|
+
if (props && props.length) {
|
|
283
|
+
this.props = Object.create(null);
|
|
284
|
+
for (let [prop, value] of props) this.props[typeof prop == "number" ? prop : prop.id] = value;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
/// @internal
|
|
288
|
+
toString() {
|
|
289
|
+
let mounted = this.prop(NodeProp.mounted);
|
|
290
|
+
if (mounted && !mounted.overlay) return mounted.tree.toString();
|
|
291
|
+
let children = "";
|
|
292
|
+
for (let ch of this.children) {
|
|
293
|
+
let str = ch.toString();
|
|
294
|
+
if (str) {
|
|
295
|
+
if (children) children += ",";
|
|
296
|
+
children += str;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
return !this.type.name ? children : (/\W/.test(this.type.name) && !this.type.isError ? JSON.stringify(this.type.name) : this.type.name) + (children.length ? "(" + children + ")" : "");
|
|
300
|
+
}
|
|
301
|
+
/// Get a [tree cursor](#common.TreeCursor) positioned at the top of
|
|
302
|
+
/// the tree. Mode can be used to [control](#common.IterMode) which
|
|
303
|
+
/// nodes the cursor visits.
|
|
304
|
+
cursor(mode = 0) {
|
|
305
|
+
return new TreeCursor(this.topNode, mode);
|
|
306
|
+
}
|
|
307
|
+
/// Get a [tree cursor](#common.TreeCursor) pointing into this tree
|
|
308
|
+
/// at the given position and side (see
|
|
309
|
+
/// [`moveTo`](#common.TreeCursor.moveTo).
|
|
310
|
+
cursorAt(pos, side = 0, mode = 0) {
|
|
311
|
+
let scope = CachedNode.get(this) || this.topNode;
|
|
312
|
+
let cursor = new TreeCursor(scope);
|
|
313
|
+
cursor.moveTo(pos, side);
|
|
314
|
+
CachedNode.set(this, cursor._tree);
|
|
315
|
+
return cursor;
|
|
316
|
+
}
|
|
317
|
+
/// Get a [syntax node](#common.SyntaxNode) object for the top of the
|
|
318
|
+
/// tree.
|
|
319
|
+
get topNode() {
|
|
320
|
+
return new TreeNode(this, 0, 0, null);
|
|
321
|
+
}
|
|
322
|
+
/// Get the [syntax node](#common.SyntaxNode) at the given position.
|
|
323
|
+
/// If `side` is -1, this will move into nodes that end at the
|
|
324
|
+
/// position. If 1, it'll move into nodes that start at the
|
|
325
|
+
/// position. With 0, it'll only enter nodes that cover the position
|
|
326
|
+
/// from both sides.
|
|
327
|
+
///
|
|
328
|
+
/// Note that this will not enter
|
|
329
|
+
/// [overlays](#common.MountedTree.overlay), and you often want
|
|
330
|
+
/// [`resolveInner`](#common.Tree.resolveInner) instead.
|
|
331
|
+
resolve(pos, side = 0) {
|
|
332
|
+
let node = resolveNode(CachedNode.get(this) || this.topNode, pos, side, false);
|
|
333
|
+
CachedNode.set(this, node);
|
|
334
|
+
return node;
|
|
335
|
+
}
|
|
336
|
+
/// Like [`resolve`](#common.Tree.resolve), but will enter
|
|
337
|
+
/// [overlaid](#common.MountedTree.overlay) nodes, producing a syntax node
|
|
338
|
+
/// pointing into the innermost overlaid tree at the given position
|
|
339
|
+
/// (with parent links going through all parent structure, including
|
|
340
|
+
/// the host trees).
|
|
341
|
+
resolveInner(pos, side = 0) {
|
|
342
|
+
let node = resolveNode(CachedInnerNode.get(this) || this.topNode, pos, side, true);
|
|
343
|
+
CachedInnerNode.set(this, node);
|
|
344
|
+
return node;
|
|
345
|
+
}
|
|
346
|
+
/// Iterate over the tree and its children, calling `enter` for any
|
|
347
|
+
/// node that touches the `from`/`to` region (if given) before
|
|
348
|
+
/// running over such a node's children, and `leave` (if given) when
|
|
349
|
+
/// leaving the node. When `enter` returns `false`, that node will
|
|
350
|
+
/// not have its children iterated over (or `leave` called).
|
|
351
|
+
iterate(spec) {
|
|
352
|
+
let {
|
|
353
|
+
enter,
|
|
354
|
+
leave,
|
|
355
|
+
from = 0,
|
|
356
|
+
to = this.length
|
|
357
|
+
} = spec;
|
|
358
|
+
for (let c = this.cursor((spec.mode || 0) | IterMode.IncludeAnonymous);;) {
|
|
359
|
+
let entered = false;
|
|
360
|
+
if (c.from <= to && c.to >= from && (c.type.isAnonymous || enter(c) !== false)) {
|
|
361
|
+
if (c.firstChild()) continue;
|
|
362
|
+
entered = true;
|
|
363
|
+
}
|
|
364
|
+
for (;;) {
|
|
365
|
+
if (entered && leave && !c.type.isAnonymous) leave(c);
|
|
366
|
+
if (c.nextSibling()) break;
|
|
367
|
+
if (!c.parent()) return;
|
|
368
|
+
entered = true;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
/// Get the value of the given [node prop](#common.NodeProp) for this
|
|
373
|
+
/// node. Works with both per-node and per-type props.
|
|
374
|
+
prop(prop) {
|
|
375
|
+
return !prop.perNode ? this.type.prop(prop) : this.props ? this.props[prop.id] : undefined;
|
|
376
|
+
}
|
|
377
|
+
/// Returns the node's [per-node props](#common.NodeProp.perNode) in a
|
|
378
|
+
/// format that can be passed to the [`Tree`](#common.Tree)
|
|
379
|
+
/// constructor.
|
|
380
|
+
get propValues() {
|
|
381
|
+
let result = [];
|
|
382
|
+
if (this.props) for (let id in this.props) result.push([+id, this.props[id]]);
|
|
383
|
+
return result;
|
|
384
|
+
}
|
|
385
|
+
/// Balance the direct children of this tree, producing a copy of
|
|
386
|
+
/// which may have children grouped into subtrees with type
|
|
387
|
+
/// [`NodeType.none`](#common.NodeType^none).
|
|
388
|
+
balance(config = {}) {
|
|
389
|
+
return this.children.length <= 8 /* Balance.BranchFactor */ ? this : balanceRange(NodeType.none, this.children, this.positions, 0, this.children.length, 0, this.length, (children, positions, length) => new Tree(this.type, children, positions, length, this.propValues), config.makeTree || ((children, positions, length) => new Tree(NodeType.none, children, positions, length)));
|
|
390
|
+
}
|
|
391
|
+
/// Build a tree from a postfix-ordered buffer of node information,
|
|
392
|
+
/// or a cursor over such a buffer.
|
|
393
|
+
static build(data) {
|
|
394
|
+
return buildTree(data);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
/// The empty tree
|
|
398
|
+
Tree.empty = new Tree(NodeType.none, [], [], 0);
|
|
399
|
+
class FlatBufferCursor {
|
|
400
|
+
constructor(buffer, index) {
|
|
401
|
+
this.buffer = buffer;
|
|
402
|
+
this.index = index;
|
|
403
|
+
}
|
|
404
|
+
get id() {
|
|
405
|
+
return this.buffer[this.index - 4];
|
|
406
|
+
}
|
|
407
|
+
get start() {
|
|
408
|
+
return this.buffer[this.index - 3];
|
|
409
|
+
}
|
|
410
|
+
get end() {
|
|
411
|
+
return this.buffer[this.index - 2];
|
|
412
|
+
}
|
|
413
|
+
get size() {
|
|
414
|
+
return this.buffer[this.index - 1];
|
|
415
|
+
}
|
|
416
|
+
get pos() {
|
|
417
|
+
return this.index;
|
|
418
|
+
}
|
|
419
|
+
next() {
|
|
420
|
+
this.index -= 4;
|
|
421
|
+
}
|
|
422
|
+
fork() {
|
|
423
|
+
return new FlatBufferCursor(this.buffer, this.index);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
/// Tree buffers contain (type, start, end, endIndex) quads for each
|
|
427
|
+
/// node. In such a buffer, nodes are stored in prefix order (parents
|
|
428
|
+
/// before children, with the endIndex of the parent indicating which
|
|
429
|
+
/// children belong to it).
|
|
430
|
+
class TreeBuffer {
|
|
431
|
+
/// Create a tree buffer.
|
|
432
|
+
constructor(
|
|
433
|
+
/// The buffer's content.
|
|
434
|
+
buffer,
|
|
435
|
+
/// The total length of the group of nodes in the buffer.
|
|
436
|
+
length,
|
|
437
|
+
/// The node set used in this buffer.
|
|
438
|
+
set) {
|
|
439
|
+
this.buffer = buffer;
|
|
440
|
+
this.length = length;
|
|
441
|
+
this.set = set;
|
|
442
|
+
}
|
|
443
|
+
/// @internal
|
|
444
|
+
get type() {
|
|
445
|
+
return NodeType.none;
|
|
446
|
+
}
|
|
447
|
+
/// @internal
|
|
448
|
+
toString() {
|
|
449
|
+
let result = [];
|
|
450
|
+
for (let index = 0; index < this.buffer.length;) {
|
|
451
|
+
result.push(this.childString(index));
|
|
452
|
+
index = this.buffer[index + 3];
|
|
453
|
+
}
|
|
454
|
+
return result.join(",");
|
|
455
|
+
}
|
|
456
|
+
/// @internal
|
|
457
|
+
childString(index) {
|
|
458
|
+
let id = this.buffer[index],
|
|
459
|
+
endIndex = this.buffer[index + 3];
|
|
460
|
+
let type = this.set.types[id],
|
|
461
|
+
result = type.name;
|
|
462
|
+
if (/\W/.test(result) && !type.isError) result = JSON.stringify(result);
|
|
463
|
+
index += 4;
|
|
464
|
+
if (endIndex == index) return result;
|
|
465
|
+
let children = [];
|
|
466
|
+
while (index < endIndex) {
|
|
467
|
+
children.push(this.childString(index));
|
|
468
|
+
index = this.buffer[index + 3];
|
|
469
|
+
}
|
|
470
|
+
return result + "(" + children.join(",") + ")";
|
|
471
|
+
}
|
|
472
|
+
/// @internal
|
|
473
|
+
findChild(startIndex, endIndex, dir, pos, side) {
|
|
474
|
+
let {
|
|
475
|
+
buffer
|
|
476
|
+
} = this,
|
|
477
|
+
pick = -1;
|
|
478
|
+
for (let i = startIndex; i != endIndex; i = buffer[i + 3]) {
|
|
479
|
+
if (checkSide(side, pos, buffer[i + 1], buffer[i + 2])) {
|
|
480
|
+
pick = i;
|
|
481
|
+
if (dir > 0) break;
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
return pick;
|
|
485
|
+
}
|
|
486
|
+
/// @internal
|
|
487
|
+
slice(startI, endI, from) {
|
|
488
|
+
let b = this.buffer;
|
|
489
|
+
let copy = new Uint16Array(endI - startI),
|
|
490
|
+
len = 0;
|
|
491
|
+
for (let i = startI, j = 0; i < endI;) {
|
|
492
|
+
copy[j++] = b[i++];
|
|
493
|
+
copy[j++] = b[i++] - from;
|
|
494
|
+
let to = copy[j++] = b[i++] - from;
|
|
495
|
+
copy[j++] = b[i++] - startI;
|
|
496
|
+
len = Math.max(len, to);
|
|
497
|
+
}
|
|
498
|
+
return new TreeBuffer(copy, len, this.set);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
function checkSide(side, pos, from, to) {
|
|
502
|
+
switch (side) {
|
|
503
|
+
case -2 /* Side.Before */:
|
|
504
|
+
return from < pos;
|
|
505
|
+
case -1 /* Side.AtOrBefore */:
|
|
506
|
+
return to >= pos && from < pos;
|
|
507
|
+
case 0 /* Side.Around */:
|
|
508
|
+
return from < pos && to > pos;
|
|
509
|
+
case 1 /* Side.AtOrAfter */:
|
|
510
|
+
return from <= pos && to > pos;
|
|
511
|
+
case 2 /* Side.After */:
|
|
512
|
+
return to > pos;
|
|
513
|
+
case 4 /* Side.DontCare */:
|
|
514
|
+
return true;
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
function enterUnfinishedNodesBefore(node, pos) {
|
|
518
|
+
let scan = node.childBefore(pos);
|
|
519
|
+
while (scan) {
|
|
520
|
+
let last = scan.lastChild;
|
|
521
|
+
if (!last || last.to != scan.to) break;
|
|
522
|
+
if (last.type.isError && last.from == last.to) {
|
|
523
|
+
node = scan;
|
|
524
|
+
scan = last.prevSibling;
|
|
525
|
+
} else {
|
|
526
|
+
scan = last;
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
return node;
|
|
530
|
+
}
|
|
531
|
+
function resolveNode(node, pos, side, overlays) {
|
|
532
|
+
var _a;
|
|
533
|
+
// Move up to a node that actually holds the position, if possible
|
|
534
|
+
while (node.from == node.to || (side < 1 ? node.from >= pos : node.from > pos) || (side > -1 ? node.to <= pos : node.to < pos)) {
|
|
535
|
+
let parent = !overlays && node instanceof TreeNode && node.index < 0 ? null : node.parent;
|
|
536
|
+
if (!parent) return node;
|
|
537
|
+
node = parent;
|
|
538
|
+
}
|
|
539
|
+
let mode = overlays ? 0 : IterMode.IgnoreOverlays;
|
|
540
|
+
// Must go up out of overlays when those do not overlap with pos
|
|
541
|
+
if (overlays) for (let scan = node, parent = scan.parent; parent; scan = parent, parent = scan.parent) {
|
|
542
|
+
if (scan instanceof TreeNode && scan.index < 0 && ((_a = parent.enter(pos, side, mode)) === null || _a === void 0 ? void 0 : _a.from) != scan.from) node = parent;
|
|
543
|
+
}
|
|
544
|
+
for (;;) {
|
|
545
|
+
let inner = node.enter(pos, side, mode);
|
|
546
|
+
if (!inner) return node;
|
|
547
|
+
node = inner;
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
class TreeNode {
|
|
551
|
+
constructor(_tree, from,
|
|
552
|
+
// Index in parent node, set to -1 if the node is not a direct child of _parent.node (overlay)
|
|
553
|
+
index, _parent) {
|
|
554
|
+
this._tree = _tree;
|
|
555
|
+
this.from = from;
|
|
556
|
+
this.index = index;
|
|
557
|
+
this._parent = _parent;
|
|
558
|
+
}
|
|
559
|
+
get type() {
|
|
560
|
+
return this._tree.type;
|
|
561
|
+
}
|
|
562
|
+
get name() {
|
|
563
|
+
return this._tree.type.name;
|
|
564
|
+
}
|
|
565
|
+
get to() {
|
|
566
|
+
return this.from + this._tree.length;
|
|
567
|
+
}
|
|
568
|
+
nextChild(i, dir, pos, side, mode = 0) {
|
|
569
|
+
for (let parent = this;;) {
|
|
570
|
+
for (let {
|
|
571
|
+
children,
|
|
572
|
+
positions
|
|
573
|
+
} = parent._tree, e = dir > 0 ? children.length : -1; i != e; i += dir) {
|
|
574
|
+
let next = children[i],
|
|
575
|
+
start = positions[i] + parent.from;
|
|
576
|
+
if (!checkSide(side, pos, start, start + next.length)) continue;
|
|
577
|
+
if (next instanceof TreeBuffer) {
|
|
578
|
+
if (mode & IterMode.ExcludeBuffers) continue;
|
|
579
|
+
let index = next.findChild(0, next.buffer.length, dir, pos - start, side);
|
|
580
|
+
if (index > -1) return new BufferNode(new BufferContext(parent, next, i, start), null, index);
|
|
581
|
+
} else if (mode & IterMode.IncludeAnonymous || !next.type.isAnonymous || hasChild(next)) {
|
|
582
|
+
let mounted;
|
|
583
|
+
if (!(mode & IterMode.IgnoreMounts) && next.props && (mounted = next.prop(NodeProp.mounted)) && !mounted.overlay) return new TreeNode(mounted.tree, start, i, parent);
|
|
584
|
+
let inner = new TreeNode(next, start, i, parent);
|
|
585
|
+
return mode & IterMode.IncludeAnonymous || !inner.type.isAnonymous ? inner : inner.nextChild(dir < 0 ? next.children.length - 1 : 0, dir, pos, side);
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
if (mode & IterMode.IncludeAnonymous || !parent.type.isAnonymous) return null;
|
|
589
|
+
if (parent.index >= 0) i = parent.index + dir;else i = dir < 0 ? -1 : parent._parent._tree.children.length;
|
|
590
|
+
parent = parent._parent;
|
|
591
|
+
if (!parent) return null;
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
get firstChild() {
|
|
595
|
+
return this.nextChild(0, 1, 0, 4 /* Side.DontCare */);
|
|
596
|
+
}
|
|
597
|
+
get lastChild() {
|
|
598
|
+
return this.nextChild(this._tree.children.length - 1, -1, 0, 4 /* Side.DontCare */);
|
|
599
|
+
}
|
|
600
|
+
childAfter(pos) {
|
|
601
|
+
return this.nextChild(0, 1, pos, 2 /* Side.After */);
|
|
602
|
+
}
|
|
603
|
+
childBefore(pos) {
|
|
604
|
+
return this.nextChild(this._tree.children.length - 1, -1, pos, -2 /* Side.Before */);
|
|
605
|
+
}
|
|
606
|
+
enter(pos, side, mode = 0) {
|
|
607
|
+
let mounted;
|
|
608
|
+
if (!(mode & IterMode.IgnoreOverlays) && (mounted = this._tree.prop(NodeProp.mounted)) && mounted.overlay) {
|
|
609
|
+
let rPos = pos - this.from;
|
|
610
|
+
for (let {
|
|
611
|
+
from,
|
|
612
|
+
to
|
|
613
|
+
} of mounted.overlay) {
|
|
614
|
+
if ((side > 0 ? from <= rPos : from < rPos) && (side < 0 ? to >= rPos : to > rPos)) return new TreeNode(mounted.tree, mounted.overlay[0].from + this.from, -1, this);
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
return this.nextChild(0, 1, pos, side, mode);
|
|
618
|
+
}
|
|
619
|
+
nextSignificantParent() {
|
|
620
|
+
let val = this;
|
|
621
|
+
while (val.type.isAnonymous && val._parent) val = val._parent;
|
|
622
|
+
return val;
|
|
623
|
+
}
|
|
624
|
+
get parent() {
|
|
625
|
+
return this._parent ? this._parent.nextSignificantParent() : null;
|
|
626
|
+
}
|
|
627
|
+
get nextSibling() {
|
|
628
|
+
return this._parent && this.index >= 0 ? this._parent.nextChild(this.index + 1, 1, 0, 4 /* Side.DontCare */) : null;
|
|
629
|
+
}
|
|
630
|
+
get prevSibling() {
|
|
631
|
+
return this._parent && this.index >= 0 ? this._parent.nextChild(this.index - 1, -1, 0, 4 /* Side.DontCare */) : null;
|
|
632
|
+
}
|
|
633
|
+
cursor(mode = 0) {
|
|
634
|
+
return new TreeCursor(this, mode);
|
|
635
|
+
}
|
|
636
|
+
get tree() {
|
|
637
|
+
return this._tree;
|
|
638
|
+
}
|
|
639
|
+
toTree() {
|
|
640
|
+
return this._tree;
|
|
641
|
+
}
|
|
642
|
+
resolve(pos, side = 0) {
|
|
643
|
+
return resolveNode(this, pos, side, false);
|
|
644
|
+
}
|
|
645
|
+
resolveInner(pos, side = 0) {
|
|
646
|
+
return resolveNode(this, pos, side, true);
|
|
647
|
+
}
|
|
648
|
+
enterUnfinishedNodesBefore(pos) {
|
|
649
|
+
return enterUnfinishedNodesBefore(this, pos);
|
|
650
|
+
}
|
|
651
|
+
getChild(type, before = null, after = null) {
|
|
652
|
+
let r = getChildren(this, type, before, after);
|
|
653
|
+
return r.length ? r[0] : null;
|
|
654
|
+
}
|
|
655
|
+
getChildren(type, before = null, after = null) {
|
|
656
|
+
return getChildren(this, type, before, after);
|
|
657
|
+
}
|
|
658
|
+
/// @internal
|
|
659
|
+
toString() {
|
|
660
|
+
return this._tree.toString();
|
|
661
|
+
}
|
|
662
|
+
get node() {
|
|
663
|
+
return this;
|
|
664
|
+
}
|
|
665
|
+
matchContext(context) {
|
|
666
|
+
return matchNodeContext(this, context);
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
function getChildren(node, type, before, after) {
|
|
670
|
+
let cur = node.cursor(),
|
|
671
|
+
result = [];
|
|
672
|
+
if (!cur.firstChild()) return result;
|
|
673
|
+
if (before != null) while (!cur.type.is(before)) if (!cur.nextSibling()) return result;
|
|
674
|
+
for (;;) {
|
|
675
|
+
if (after != null && cur.type.is(after)) return result;
|
|
676
|
+
if (cur.type.is(type)) result.push(cur.node);
|
|
677
|
+
if (!cur.nextSibling()) return after == null ? result : [];
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
function matchNodeContext(node, context, i = context.length - 1) {
|
|
681
|
+
for (let p = node.parent; i >= 0; p = p.parent) {
|
|
682
|
+
if (!p) return false;
|
|
683
|
+
if (!p.type.isAnonymous) {
|
|
684
|
+
if (context[i] && context[i] != p.name) return false;
|
|
685
|
+
i--;
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
return true;
|
|
689
|
+
}
|
|
690
|
+
class BufferContext {
|
|
691
|
+
constructor(parent, buffer, index, start) {
|
|
692
|
+
this.parent = parent;
|
|
693
|
+
this.buffer = buffer;
|
|
694
|
+
this.index = index;
|
|
695
|
+
this.start = start;
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
class BufferNode {
|
|
699
|
+
get name() {
|
|
700
|
+
return this.type.name;
|
|
701
|
+
}
|
|
702
|
+
get from() {
|
|
703
|
+
return this.context.start + this.context.buffer.buffer[this.index + 1];
|
|
704
|
+
}
|
|
705
|
+
get to() {
|
|
706
|
+
return this.context.start + this.context.buffer.buffer[this.index + 2];
|
|
707
|
+
}
|
|
708
|
+
constructor(context, _parent, index) {
|
|
709
|
+
this.context = context;
|
|
710
|
+
this._parent = _parent;
|
|
711
|
+
this.index = index;
|
|
712
|
+
this.type = context.buffer.set.types[context.buffer.buffer[index]];
|
|
713
|
+
}
|
|
714
|
+
child(dir, pos, side) {
|
|
715
|
+
let {
|
|
716
|
+
buffer
|
|
717
|
+
} = this.context;
|
|
718
|
+
let index = buffer.findChild(this.index + 4, buffer.buffer[this.index + 3], dir, pos - this.context.start, side);
|
|
719
|
+
return index < 0 ? null : new BufferNode(this.context, this, index);
|
|
720
|
+
}
|
|
721
|
+
get firstChild() {
|
|
722
|
+
return this.child(1, 0, 4 /* Side.DontCare */);
|
|
723
|
+
}
|
|
724
|
+
get lastChild() {
|
|
725
|
+
return this.child(-1, 0, 4 /* Side.DontCare */);
|
|
726
|
+
}
|
|
727
|
+
childAfter(pos) {
|
|
728
|
+
return this.child(1, pos, 2 /* Side.After */);
|
|
729
|
+
}
|
|
730
|
+
childBefore(pos) {
|
|
731
|
+
return this.child(-1, pos, -2 /* Side.Before */);
|
|
732
|
+
}
|
|
733
|
+
enter(pos, side, mode = 0) {
|
|
734
|
+
if (mode & IterMode.ExcludeBuffers) return null;
|
|
735
|
+
let {
|
|
736
|
+
buffer
|
|
737
|
+
} = this.context;
|
|
738
|
+
let index = buffer.findChild(this.index + 4, buffer.buffer[this.index + 3], side > 0 ? 1 : -1, pos - this.context.start, side);
|
|
739
|
+
return index < 0 ? null : new BufferNode(this.context, this, index);
|
|
740
|
+
}
|
|
741
|
+
get parent() {
|
|
742
|
+
return this._parent || this.context.parent.nextSignificantParent();
|
|
743
|
+
}
|
|
744
|
+
externalSibling(dir) {
|
|
745
|
+
return this._parent ? null : this.context.parent.nextChild(this.context.index + dir, dir, 0, 4 /* Side.DontCare */);
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
get nextSibling() {
|
|
749
|
+
let {
|
|
750
|
+
buffer
|
|
751
|
+
} = this.context;
|
|
752
|
+
let after = buffer.buffer[this.index + 3];
|
|
753
|
+
if (after < (this._parent ? buffer.buffer[this._parent.index + 3] : buffer.buffer.length)) return new BufferNode(this.context, this._parent, after);
|
|
754
|
+
return this.externalSibling(1);
|
|
755
|
+
}
|
|
756
|
+
get prevSibling() {
|
|
757
|
+
let {
|
|
758
|
+
buffer
|
|
759
|
+
} = this.context;
|
|
760
|
+
let parentStart = this._parent ? this._parent.index + 4 : 0;
|
|
761
|
+
if (this.index == parentStart) return this.externalSibling(-1);
|
|
762
|
+
return new BufferNode(this.context, this._parent, buffer.findChild(parentStart, this.index, -1, 0, 4 /* Side.DontCare */));
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
cursor(mode = 0) {
|
|
766
|
+
return new TreeCursor(this, mode);
|
|
767
|
+
}
|
|
768
|
+
get tree() {
|
|
769
|
+
return null;
|
|
770
|
+
}
|
|
771
|
+
toTree() {
|
|
772
|
+
let children = [],
|
|
773
|
+
positions = [];
|
|
774
|
+
let {
|
|
775
|
+
buffer
|
|
776
|
+
} = this.context;
|
|
777
|
+
let startI = this.index + 4,
|
|
778
|
+
endI = buffer.buffer[this.index + 3];
|
|
779
|
+
if (endI > startI) {
|
|
780
|
+
let from = buffer.buffer[this.index + 1];
|
|
781
|
+
children.push(buffer.slice(startI, endI, from));
|
|
782
|
+
positions.push(0);
|
|
783
|
+
}
|
|
784
|
+
return new Tree(this.type, children, positions, this.to - this.from);
|
|
785
|
+
}
|
|
786
|
+
resolve(pos, side = 0) {
|
|
787
|
+
return resolveNode(this, pos, side, false);
|
|
788
|
+
}
|
|
789
|
+
resolveInner(pos, side = 0) {
|
|
790
|
+
return resolveNode(this, pos, side, true);
|
|
791
|
+
}
|
|
792
|
+
enterUnfinishedNodesBefore(pos) {
|
|
793
|
+
return enterUnfinishedNodesBefore(this, pos);
|
|
794
|
+
}
|
|
795
|
+
/// @internal
|
|
796
|
+
toString() {
|
|
797
|
+
return this.context.buffer.childString(this.index);
|
|
798
|
+
}
|
|
799
|
+
getChild(type, before = null, after = null) {
|
|
800
|
+
let r = getChildren(this, type, before, after);
|
|
801
|
+
return r.length ? r[0] : null;
|
|
802
|
+
}
|
|
803
|
+
getChildren(type, before = null, after = null) {
|
|
804
|
+
return getChildren(this, type, before, after);
|
|
805
|
+
}
|
|
806
|
+
get node() {
|
|
807
|
+
return this;
|
|
808
|
+
}
|
|
809
|
+
matchContext(context) {
|
|
810
|
+
return matchNodeContext(this, context);
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
/// A tree cursor object focuses on a given node in a syntax tree, and
|
|
814
|
+
/// allows you to move to adjacent nodes.
|
|
815
|
+
class TreeCursor {
|
|
816
|
+
/// Shorthand for `.type.name`.
|
|
817
|
+
get name() {
|
|
818
|
+
return this.type.name;
|
|
819
|
+
}
|
|
820
|
+
/// @internal
|
|
821
|
+
constructor(node,
|
|
822
|
+
/// @internal
|
|
823
|
+
mode = 0) {
|
|
824
|
+
this.mode = mode;
|
|
825
|
+
/// @internal
|
|
826
|
+
this.buffer = null;
|
|
827
|
+
this.stack = [];
|
|
828
|
+
/// @internal
|
|
829
|
+
this.index = 0;
|
|
830
|
+
this.bufferNode = null;
|
|
831
|
+
if (node instanceof TreeNode) {
|
|
832
|
+
this.yieldNode(node);
|
|
833
|
+
} else {
|
|
834
|
+
this._tree = node.context.parent;
|
|
835
|
+
this.buffer = node.context;
|
|
836
|
+
for (let n = node._parent; n; n = n._parent) this.stack.unshift(n.index);
|
|
837
|
+
this.bufferNode = node;
|
|
838
|
+
this.yieldBuf(node.index);
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
yieldNode(node) {
|
|
842
|
+
if (!node) return false;
|
|
843
|
+
this._tree = node;
|
|
844
|
+
this.type = node.type;
|
|
845
|
+
this.from = node.from;
|
|
846
|
+
this.to = node.to;
|
|
847
|
+
return true;
|
|
848
|
+
}
|
|
849
|
+
yieldBuf(index, type) {
|
|
850
|
+
this.index = index;
|
|
851
|
+
let {
|
|
852
|
+
start,
|
|
853
|
+
buffer
|
|
854
|
+
} = this.buffer;
|
|
855
|
+
this.type = type || buffer.set.types[buffer.buffer[index]];
|
|
856
|
+
this.from = start + buffer.buffer[index + 1];
|
|
857
|
+
this.to = start + buffer.buffer[index + 2];
|
|
858
|
+
return true;
|
|
859
|
+
}
|
|
860
|
+
yield(node) {
|
|
861
|
+
if (!node) return false;
|
|
862
|
+
if (node instanceof TreeNode) {
|
|
863
|
+
this.buffer = null;
|
|
864
|
+
return this.yieldNode(node);
|
|
865
|
+
}
|
|
866
|
+
this.buffer = node.context;
|
|
867
|
+
return this.yieldBuf(node.index, node.type);
|
|
868
|
+
}
|
|
869
|
+
/// @internal
|
|
870
|
+
toString() {
|
|
871
|
+
return this.buffer ? this.buffer.buffer.childString(this.index) : this._tree.toString();
|
|
872
|
+
}
|
|
873
|
+
/// @internal
|
|
874
|
+
enterChild(dir, pos, side) {
|
|
875
|
+
if (!this.buffer) return this.yield(this._tree.nextChild(dir < 0 ? this._tree._tree.children.length - 1 : 0, dir, pos, side, this.mode));
|
|
876
|
+
let {
|
|
877
|
+
buffer
|
|
878
|
+
} = this.buffer;
|
|
879
|
+
let index = buffer.findChild(this.index + 4, buffer.buffer[this.index + 3], dir, pos - this.buffer.start, side);
|
|
880
|
+
if (index < 0) return false;
|
|
881
|
+
this.stack.push(this.index);
|
|
882
|
+
return this.yieldBuf(index);
|
|
883
|
+
}
|
|
884
|
+
/// Move the cursor to this node's first child. When this returns
|
|
885
|
+
/// false, the node has no child, and the cursor has not been moved.
|
|
886
|
+
firstChild() {
|
|
887
|
+
return this.enterChild(1, 0, 4 /* Side.DontCare */);
|
|
888
|
+
}
|
|
889
|
+
/// Move the cursor to this node's last child.
|
|
890
|
+
lastChild() {
|
|
891
|
+
return this.enterChild(-1, 0, 4 /* Side.DontCare */);
|
|
892
|
+
}
|
|
893
|
+
/// Move the cursor to the first child that ends after `pos`.
|
|
894
|
+
childAfter(pos) {
|
|
895
|
+
return this.enterChild(1, pos, 2 /* Side.After */);
|
|
896
|
+
}
|
|
897
|
+
/// Move to the last child that starts before `pos`.
|
|
898
|
+
childBefore(pos) {
|
|
899
|
+
return this.enterChild(-1, pos, -2 /* Side.Before */);
|
|
900
|
+
}
|
|
901
|
+
/// Move the cursor to the child around `pos`. If side is -1 the
|
|
902
|
+
/// child may end at that position, when 1 it may start there. This
|
|
903
|
+
/// will also enter [overlaid](#common.MountedTree.overlay)
|
|
904
|
+
/// [mounted](#common.NodeProp^mounted) trees unless `overlays` is
|
|
905
|
+
/// set to false.
|
|
906
|
+
enter(pos, side, mode = this.mode) {
|
|
907
|
+
if (!this.buffer) return this.yield(this._tree.enter(pos, side, mode));
|
|
908
|
+
return mode & IterMode.ExcludeBuffers ? false : this.enterChild(1, pos, side);
|
|
909
|
+
}
|
|
910
|
+
/// Move to the node's parent node, if this isn't the top node.
|
|
911
|
+
parent() {
|
|
912
|
+
if (!this.buffer) return this.yieldNode(this.mode & IterMode.IncludeAnonymous ? this._tree._parent : this._tree.parent);
|
|
913
|
+
if (this.stack.length) return this.yieldBuf(this.stack.pop());
|
|
914
|
+
let parent = this.mode & IterMode.IncludeAnonymous ? this.buffer.parent : this.buffer.parent.nextSignificantParent();
|
|
915
|
+
this.buffer = null;
|
|
916
|
+
return this.yieldNode(parent);
|
|
917
|
+
}
|
|
918
|
+
/// @internal
|
|
919
|
+
sibling(dir) {
|
|
920
|
+
if (!this.buffer) return !this._tree._parent ? false : this.yield(this._tree.index < 0 ? null : this._tree._parent.nextChild(this._tree.index + dir, dir, 0, 4 /* Side.DontCare */, this.mode));
|
|
921
|
+
let {
|
|
922
|
+
buffer
|
|
923
|
+
} = this.buffer,
|
|
924
|
+
d = this.stack.length - 1;
|
|
925
|
+
if (dir < 0) {
|
|
926
|
+
let parentStart = d < 0 ? 0 : this.stack[d] + 4;
|
|
927
|
+
if (this.index != parentStart) return this.yieldBuf(buffer.findChild(parentStart, this.index, -1, 0, 4 /* Side.DontCare */));
|
|
928
|
+
} else {
|
|
929
|
+
let after = buffer.buffer[this.index + 3];
|
|
930
|
+
if (after < (d < 0 ? buffer.buffer.length : buffer.buffer[this.stack[d] + 3])) return this.yieldBuf(after);
|
|
931
|
+
}
|
|
932
|
+
return d < 0 ? this.yield(this.buffer.parent.nextChild(this.buffer.index + dir, dir, 0, 4 /* Side.DontCare */, this.mode)) : false;
|
|
933
|
+
}
|
|
934
|
+
/// Move to this node's next sibling, if any.
|
|
935
|
+
nextSibling() {
|
|
936
|
+
return this.sibling(1);
|
|
937
|
+
}
|
|
938
|
+
/// Move to this node's previous sibling, if any.
|
|
939
|
+
prevSibling() {
|
|
940
|
+
return this.sibling(-1);
|
|
941
|
+
}
|
|
942
|
+
atLastNode(dir) {
|
|
943
|
+
let index,
|
|
944
|
+
parent,
|
|
945
|
+
{
|
|
946
|
+
buffer
|
|
947
|
+
} = this;
|
|
948
|
+
if (buffer) {
|
|
949
|
+
if (dir > 0) {
|
|
950
|
+
if (this.index < buffer.buffer.buffer.length) return false;
|
|
951
|
+
} else {
|
|
952
|
+
for (let i = 0; i < this.index; i++) if (buffer.buffer.buffer[i + 3] < this.index) return false;
|
|
953
|
+
}
|
|
954
|
+
({
|
|
955
|
+
index,
|
|
956
|
+
parent
|
|
957
|
+
} = buffer);
|
|
958
|
+
} else {
|
|
959
|
+
({
|
|
960
|
+
index,
|
|
961
|
+
_parent: parent
|
|
962
|
+
} = this._tree);
|
|
963
|
+
}
|
|
964
|
+
for (; parent; ({
|
|
965
|
+
index,
|
|
966
|
+
_parent: parent
|
|
967
|
+
} = parent)) {
|
|
968
|
+
if (index > -1) for (let i = index + dir, e = dir < 0 ? -1 : parent._tree.children.length; i != e; i += dir) {
|
|
969
|
+
let child = parent._tree.children[i];
|
|
970
|
+
if (this.mode & IterMode.IncludeAnonymous || child instanceof TreeBuffer || !child.type.isAnonymous || hasChild(child)) return false;
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
return true;
|
|
974
|
+
}
|
|
975
|
+
move(dir, enter) {
|
|
976
|
+
if (enter && this.enterChild(dir, 0, 4 /* Side.DontCare */)) return true;
|
|
977
|
+
for (;;) {
|
|
978
|
+
if (this.sibling(dir)) return true;
|
|
979
|
+
if (this.atLastNode(dir) || !this.parent()) return false;
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
/// Move to the next node in a
|
|
983
|
+
/// [pre-order](https://en.wikipedia.org/wiki/Tree_traversal#Pre-order,_NLR)
|
|
984
|
+
/// traversal, going from a node to its first child or, if the
|
|
985
|
+
/// current node is empty or `enter` is false, its next sibling or
|
|
986
|
+
/// the next sibling of the first parent node that has one.
|
|
987
|
+
next(enter = true) {
|
|
988
|
+
return this.move(1, enter);
|
|
989
|
+
}
|
|
990
|
+
/// Move to the next node in a last-to-first pre-order traveral. A
|
|
991
|
+
/// node is followed by its last child or, if it has none, its
|
|
992
|
+
/// previous sibling or the previous sibling of the first parent
|
|
993
|
+
/// node that has one.
|
|
994
|
+
prev(enter = true) {
|
|
995
|
+
return this.move(-1, enter);
|
|
996
|
+
}
|
|
997
|
+
/// Move the cursor to the innermost node that covers `pos`. If
|
|
998
|
+
/// `side` is -1, it will enter nodes that end at `pos`. If it is 1,
|
|
999
|
+
/// it will enter nodes that start at `pos`.
|
|
1000
|
+
moveTo(pos, side = 0) {
|
|
1001
|
+
// Move up to a node that actually holds the position, if possible
|
|
1002
|
+
while (this.from == this.to || (side < 1 ? this.from >= pos : this.from > pos) || (side > -1 ? this.to <= pos : this.to < pos)) if (!this.parent()) break;
|
|
1003
|
+
// Then scan down into child nodes as far as possible
|
|
1004
|
+
while (this.enterChild(1, pos, side)) {}
|
|
1005
|
+
return this;
|
|
1006
|
+
}
|
|
1007
|
+
/// Get a [syntax node](#common.SyntaxNode) at the cursor's current
|
|
1008
|
+
/// position.
|
|
1009
|
+
get node() {
|
|
1010
|
+
if (!this.buffer) return this._tree;
|
|
1011
|
+
let cache = this.bufferNode,
|
|
1012
|
+
result = null,
|
|
1013
|
+
depth = 0;
|
|
1014
|
+
if (cache && cache.context == this.buffer) {
|
|
1015
|
+
scan: for (let index = this.index, d = this.stack.length; d >= 0;) {
|
|
1016
|
+
for (let c = cache; c; c = c._parent) if (c.index == index) {
|
|
1017
|
+
if (index == this.index) return c;
|
|
1018
|
+
result = c;
|
|
1019
|
+
depth = d + 1;
|
|
1020
|
+
break scan;
|
|
1021
|
+
}
|
|
1022
|
+
index = this.stack[--d];
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
for (let i = depth; i < this.stack.length; i++) result = new BufferNode(this.buffer, result, this.stack[i]);
|
|
1026
|
+
return this.bufferNode = new BufferNode(this.buffer, result, this.index);
|
|
1027
|
+
}
|
|
1028
|
+
/// Get the [tree](#common.Tree) that represents the current node, if
|
|
1029
|
+
/// any. Will return null when the node is in a [tree
|
|
1030
|
+
/// buffer](#common.TreeBuffer).
|
|
1031
|
+
get tree() {
|
|
1032
|
+
return this.buffer ? null : this._tree._tree;
|
|
1033
|
+
}
|
|
1034
|
+
/// Iterate over the current node and all its descendants, calling
|
|
1035
|
+
/// `enter` when entering a node and `leave`, if given, when leaving
|
|
1036
|
+
/// one. When `enter` returns `false`, any children of that node are
|
|
1037
|
+
/// skipped, and `leave` isn't called for it.
|
|
1038
|
+
iterate(enter, leave) {
|
|
1039
|
+
for (let depth = 0;;) {
|
|
1040
|
+
let mustLeave = false;
|
|
1041
|
+
if (this.type.isAnonymous || enter(this) !== false) {
|
|
1042
|
+
if (this.firstChild()) {
|
|
1043
|
+
depth++;
|
|
1044
|
+
continue;
|
|
1045
|
+
}
|
|
1046
|
+
if (!this.type.isAnonymous) mustLeave = true;
|
|
1047
|
+
}
|
|
1048
|
+
for (;;) {
|
|
1049
|
+
if (mustLeave && leave) leave(this);
|
|
1050
|
+
mustLeave = this.type.isAnonymous;
|
|
1051
|
+
if (this.nextSibling()) break;
|
|
1052
|
+
if (!depth) return;
|
|
1053
|
+
this.parent();
|
|
1054
|
+
depth--;
|
|
1055
|
+
mustLeave = true;
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
/// Test whether the current node matches a given context—a sequence
|
|
1060
|
+
/// of direct parent node names. Empty strings in the context array
|
|
1061
|
+
/// are treated as wildcards.
|
|
1062
|
+
matchContext(context) {
|
|
1063
|
+
if (!this.buffer) return matchNodeContext(this.node, context);
|
|
1064
|
+
let {
|
|
1065
|
+
buffer
|
|
1066
|
+
} = this.buffer,
|
|
1067
|
+
{
|
|
1068
|
+
types
|
|
1069
|
+
} = buffer.set;
|
|
1070
|
+
for (let i = context.length - 1, d = this.stack.length - 1; i >= 0; d--) {
|
|
1071
|
+
if (d < 0) return matchNodeContext(this.node, context, i);
|
|
1072
|
+
let type = types[buffer.buffer[this.stack[d]]];
|
|
1073
|
+
if (!type.isAnonymous) {
|
|
1074
|
+
if (context[i] && context[i] != type.name) return false;
|
|
1075
|
+
i--;
|
|
1076
|
+
}
|
|
1077
|
+
}
|
|
1078
|
+
return true;
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
function hasChild(tree) {
|
|
1082
|
+
return tree.children.some(ch => ch instanceof TreeBuffer || !ch.type.isAnonymous || hasChild(ch));
|
|
1083
|
+
}
|
|
1084
|
+
function buildTree(data) {
|
|
1085
|
+
var _a;
|
|
1086
|
+
let {
|
|
1087
|
+
buffer,
|
|
1088
|
+
nodeSet,
|
|
1089
|
+
maxBufferLength = DefaultBufferLength,
|
|
1090
|
+
reused = [],
|
|
1091
|
+
minRepeatType = nodeSet.types.length
|
|
1092
|
+
} = data;
|
|
1093
|
+
let cursor = Array.isArray(buffer) ? new FlatBufferCursor(buffer, buffer.length) : buffer;
|
|
1094
|
+
let types = nodeSet.types;
|
|
1095
|
+
let contextHash = 0,
|
|
1096
|
+
lookAhead = 0;
|
|
1097
|
+
function takeNode(parentStart, minPos, children, positions, inRepeat) {
|
|
1098
|
+
let {
|
|
1099
|
+
id,
|
|
1100
|
+
start,
|
|
1101
|
+
end,
|
|
1102
|
+
size
|
|
1103
|
+
} = cursor;
|
|
1104
|
+
let lookAheadAtStart = lookAhead;
|
|
1105
|
+
while (size < 0) {
|
|
1106
|
+
cursor.next();
|
|
1107
|
+
if (size == -1 /* SpecialRecord.Reuse */) {
|
|
1108
|
+
let node = reused[id];
|
|
1109
|
+
children.push(node);
|
|
1110
|
+
positions.push(start - parentStart);
|
|
1111
|
+
return;
|
|
1112
|
+
} else if (size == -3 /* SpecialRecord.ContextChange */) {
|
|
1113
|
+
// Context change
|
|
1114
|
+
contextHash = id;
|
|
1115
|
+
return;
|
|
1116
|
+
} else if (size == -4 /* SpecialRecord.LookAhead */) {
|
|
1117
|
+
lookAhead = id;
|
|
1118
|
+
return;
|
|
1119
|
+
} else {
|
|
1120
|
+
throw new RangeError(`Unrecognized record size: ${size}`);
|
|
1121
|
+
}
|
|
1122
|
+
}
|
|
1123
|
+
let type = types[id],
|
|
1124
|
+
node,
|
|
1125
|
+
buffer;
|
|
1126
|
+
let startPos = start - parentStart;
|
|
1127
|
+
if (end - start <= maxBufferLength && (buffer = findBufferSize(cursor.pos - minPos, inRepeat))) {
|
|
1128
|
+
// Small enough for a buffer, and no reused nodes inside
|
|
1129
|
+
let data = new Uint16Array(buffer.size - buffer.skip);
|
|
1130
|
+
let endPos = cursor.pos - buffer.size,
|
|
1131
|
+
index = data.length;
|
|
1132
|
+
while (cursor.pos > endPos) index = copyToBuffer(buffer.start, data, index);
|
|
1133
|
+
node = new TreeBuffer(data, end - buffer.start, nodeSet);
|
|
1134
|
+
startPos = buffer.start - parentStart;
|
|
1135
|
+
} else {
|
|
1136
|
+
// Make it a node
|
|
1137
|
+
let endPos = cursor.pos - size;
|
|
1138
|
+
cursor.next();
|
|
1139
|
+
let localChildren = [],
|
|
1140
|
+
localPositions = [];
|
|
1141
|
+
let localInRepeat = id >= minRepeatType ? id : -1;
|
|
1142
|
+
let lastGroup = 0,
|
|
1143
|
+
lastEnd = end;
|
|
1144
|
+
while (cursor.pos > endPos) {
|
|
1145
|
+
if (localInRepeat >= 0 && cursor.id == localInRepeat && cursor.size >= 0) {
|
|
1146
|
+
if (cursor.end <= lastEnd - maxBufferLength) {
|
|
1147
|
+
makeRepeatLeaf(localChildren, localPositions, start, lastGroup, cursor.end, lastEnd, localInRepeat, lookAheadAtStart);
|
|
1148
|
+
lastGroup = localChildren.length;
|
|
1149
|
+
lastEnd = cursor.end;
|
|
1150
|
+
}
|
|
1151
|
+
cursor.next();
|
|
1152
|
+
} else {
|
|
1153
|
+
takeNode(start, endPos, localChildren, localPositions, localInRepeat);
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
if (localInRepeat >= 0 && lastGroup > 0 && lastGroup < localChildren.length) makeRepeatLeaf(localChildren, localPositions, start, lastGroup, start, lastEnd, localInRepeat, lookAheadAtStart);
|
|
1157
|
+
localChildren.reverse();
|
|
1158
|
+
localPositions.reverse();
|
|
1159
|
+
if (localInRepeat > -1 && lastGroup > 0) {
|
|
1160
|
+
let make = makeBalanced(type);
|
|
1161
|
+
node = balanceRange(type, localChildren, localPositions, 0, localChildren.length, 0, end - start, make, make);
|
|
1162
|
+
} else {
|
|
1163
|
+
node = makeTree(type, localChildren, localPositions, end - start, lookAheadAtStart - end);
|
|
1164
|
+
}
|
|
1165
|
+
}
|
|
1166
|
+
children.push(node);
|
|
1167
|
+
positions.push(startPos);
|
|
1168
|
+
}
|
|
1169
|
+
function makeBalanced(type) {
|
|
1170
|
+
return (children, positions, length) => {
|
|
1171
|
+
let lookAhead = 0,
|
|
1172
|
+
lastI = children.length - 1,
|
|
1173
|
+
last,
|
|
1174
|
+
lookAheadProp;
|
|
1175
|
+
if (lastI >= 0 && (last = children[lastI]) instanceof Tree) {
|
|
1176
|
+
if (!lastI && last.type == type && last.length == length) return last;
|
|
1177
|
+
if (lookAheadProp = last.prop(NodeProp.lookAhead)) lookAhead = positions[lastI] + last.length + lookAheadProp;
|
|
1178
|
+
}
|
|
1179
|
+
return makeTree(type, children, positions, length, lookAhead);
|
|
1180
|
+
};
|
|
1181
|
+
}
|
|
1182
|
+
function makeRepeatLeaf(children, positions, base, i, from, to, type, lookAhead) {
|
|
1183
|
+
let localChildren = [],
|
|
1184
|
+
localPositions = [];
|
|
1185
|
+
while (children.length > i) {
|
|
1186
|
+
localChildren.push(children.pop());
|
|
1187
|
+
localPositions.push(positions.pop() + base - from);
|
|
1188
|
+
}
|
|
1189
|
+
children.push(makeTree(nodeSet.types[type], localChildren, localPositions, to - from, lookAhead - to));
|
|
1190
|
+
positions.push(from - base);
|
|
1191
|
+
}
|
|
1192
|
+
function makeTree(type, children, positions, length, lookAhead = 0, props) {
|
|
1193
|
+
if (contextHash) {
|
|
1194
|
+
let pair = [NodeProp.contextHash, contextHash];
|
|
1195
|
+
props = props ? [pair].concat(props) : [pair];
|
|
1196
|
+
}
|
|
1197
|
+
if (lookAhead > 25) {
|
|
1198
|
+
let pair = [NodeProp.lookAhead, lookAhead];
|
|
1199
|
+
props = props ? [pair].concat(props) : [pair];
|
|
1200
|
+
}
|
|
1201
|
+
return new Tree(type, children, positions, length, props);
|
|
1202
|
+
}
|
|
1203
|
+
function findBufferSize(maxSize, inRepeat) {
|
|
1204
|
+
// Scan through the buffer to find previous siblings that fit
|
|
1205
|
+
// together in a TreeBuffer, and don't contain any reused nodes
|
|
1206
|
+
// (which can't be stored in a buffer).
|
|
1207
|
+
// If `inRepeat` is > -1, ignore node boundaries of that type for
|
|
1208
|
+
// nesting, but make sure the end falls either at the start
|
|
1209
|
+
// (`maxSize`) or before such a node.
|
|
1210
|
+
let fork = cursor.fork();
|
|
1211
|
+
let size = 0,
|
|
1212
|
+
start = 0,
|
|
1213
|
+
skip = 0,
|
|
1214
|
+
minStart = fork.end - maxBufferLength;
|
|
1215
|
+
let result = {
|
|
1216
|
+
size: 0,
|
|
1217
|
+
start: 0,
|
|
1218
|
+
skip: 0
|
|
1219
|
+
};
|
|
1220
|
+
scan: for (let minPos = fork.pos - maxSize; fork.pos > minPos;) {
|
|
1221
|
+
let nodeSize = fork.size;
|
|
1222
|
+
// Pretend nested repeat nodes of the same type don't exist
|
|
1223
|
+
if (fork.id == inRepeat && nodeSize >= 0) {
|
|
1224
|
+
// Except that we store the current state as a valid return
|
|
1225
|
+
// value.
|
|
1226
|
+
result.size = size;
|
|
1227
|
+
result.start = start;
|
|
1228
|
+
result.skip = skip;
|
|
1229
|
+
skip += 4;
|
|
1230
|
+
size += 4;
|
|
1231
|
+
fork.next();
|
|
1232
|
+
continue;
|
|
1233
|
+
}
|
|
1234
|
+
let startPos = fork.pos - nodeSize;
|
|
1235
|
+
if (nodeSize < 0 || startPos < minPos || fork.start < minStart) break;
|
|
1236
|
+
let localSkipped = fork.id >= minRepeatType ? 4 : 0;
|
|
1237
|
+
let nodeStart = fork.start;
|
|
1238
|
+
fork.next();
|
|
1239
|
+
while (fork.pos > startPos) {
|
|
1240
|
+
if (fork.size < 0) {
|
|
1241
|
+
if (fork.size == -3 /* SpecialRecord.ContextChange */) localSkipped += 4;else break scan;
|
|
1242
|
+
} else if (fork.id >= minRepeatType) {
|
|
1243
|
+
localSkipped += 4;
|
|
1244
|
+
}
|
|
1245
|
+
fork.next();
|
|
1246
|
+
}
|
|
1247
|
+
start = nodeStart;
|
|
1248
|
+
size += nodeSize;
|
|
1249
|
+
skip += localSkipped;
|
|
1250
|
+
}
|
|
1251
|
+
if (inRepeat < 0 || size == maxSize) {
|
|
1252
|
+
result.size = size;
|
|
1253
|
+
result.start = start;
|
|
1254
|
+
result.skip = skip;
|
|
1255
|
+
}
|
|
1256
|
+
return result.size > 4 ? result : undefined;
|
|
1257
|
+
}
|
|
1258
|
+
function copyToBuffer(bufferStart, buffer, index) {
|
|
1259
|
+
let {
|
|
1260
|
+
id,
|
|
1261
|
+
start,
|
|
1262
|
+
end,
|
|
1263
|
+
size
|
|
1264
|
+
} = cursor;
|
|
1265
|
+
cursor.next();
|
|
1266
|
+
if (size >= 0 && id < minRepeatType) {
|
|
1267
|
+
let startIndex = index;
|
|
1268
|
+
if (size > 4) {
|
|
1269
|
+
let endPos = cursor.pos - (size - 4);
|
|
1270
|
+
while (cursor.pos > endPos) index = copyToBuffer(bufferStart, buffer, index);
|
|
1271
|
+
}
|
|
1272
|
+
buffer[--index] = startIndex;
|
|
1273
|
+
buffer[--index] = end - bufferStart;
|
|
1274
|
+
buffer[--index] = start - bufferStart;
|
|
1275
|
+
buffer[--index] = id;
|
|
1276
|
+
} else if (size == -3 /* SpecialRecord.ContextChange */) {
|
|
1277
|
+
contextHash = id;
|
|
1278
|
+
} else if (size == -4 /* SpecialRecord.LookAhead */) {
|
|
1279
|
+
lookAhead = id;
|
|
1280
|
+
}
|
|
1281
|
+
return index;
|
|
1282
|
+
}
|
|
1283
|
+
let children = [],
|
|
1284
|
+
positions = [];
|
|
1285
|
+
while (cursor.pos > 0) takeNode(data.start || 0, data.bufferStart || 0, children, positions, -1);
|
|
1286
|
+
let length = (_a = data.length) !== null && _a !== void 0 ? _a : children.length ? positions[0] + children[0].length : 0;
|
|
1287
|
+
return new Tree(types[data.topID], children.reverse(), positions.reverse(), length);
|
|
1288
|
+
}
|
|
1289
|
+
const nodeSizeCache = new WeakMap();
|
|
1290
|
+
function nodeSize(balanceType, node) {
|
|
1291
|
+
if (!balanceType.isAnonymous || node instanceof TreeBuffer || node.type != balanceType) return 1;
|
|
1292
|
+
let size = nodeSizeCache.get(node);
|
|
1293
|
+
if (size == null) {
|
|
1294
|
+
size = 1;
|
|
1295
|
+
for (let child of node.children) {
|
|
1296
|
+
if (child.type != balanceType || !(child instanceof Tree)) {
|
|
1297
|
+
size = 1;
|
|
1298
|
+
break;
|
|
1299
|
+
}
|
|
1300
|
+
size += nodeSize(balanceType, child);
|
|
1301
|
+
}
|
|
1302
|
+
nodeSizeCache.set(node, size);
|
|
1303
|
+
}
|
|
1304
|
+
return size;
|
|
1305
|
+
}
|
|
1306
|
+
function balanceRange(
|
|
1307
|
+
// The type the balanced tree's inner nodes.
|
|
1308
|
+
balanceType,
|
|
1309
|
+
// The direct children and their positions
|
|
1310
|
+
children, positions,
|
|
1311
|
+
// The index range in children/positions to use
|
|
1312
|
+
from, to,
|
|
1313
|
+
// The start position of the nodes, relative to their parent.
|
|
1314
|
+
start,
|
|
1315
|
+
// Length of the outer node
|
|
1316
|
+
length,
|
|
1317
|
+
// Function to build the top node of the balanced tree
|
|
1318
|
+
mkTop,
|
|
1319
|
+
// Function to build internal nodes for the balanced tree
|
|
1320
|
+
mkTree) {
|
|
1321
|
+
let total = 0;
|
|
1322
|
+
for (let i = from; i < to; i++) total += nodeSize(balanceType, children[i]);
|
|
1323
|
+
let maxChild = Math.ceil(total * 1.5 / 8 /* Balance.BranchFactor */);
|
|
1324
|
+
let localChildren = [],
|
|
1325
|
+
localPositions = [];
|
|
1326
|
+
function divide(children, positions, from, to, offset) {
|
|
1327
|
+
for (let i = from; i < to;) {
|
|
1328
|
+
let groupFrom = i,
|
|
1329
|
+
groupStart = positions[i],
|
|
1330
|
+
groupSize = nodeSize(balanceType, children[i]);
|
|
1331
|
+
i++;
|
|
1332
|
+
for (; i < to; i++) {
|
|
1333
|
+
let nextSize = nodeSize(balanceType, children[i]);
|
|
1334
|
+
if (groupSize + nextSize >= maxChild) break;
|
|
1335
|
+
groupSize += nextSize;
|
|
1336
|
+
}
|
|
1337
|
+
if (i == groupFrom + 1) {
|
|
1338
|
+
if (groupSize > maxChild) {
|
|
1339
|
+
let only = children[groupFrom]; // Only trees can have a size > 1
|
|
1340
|
+
divide(only.children, only.positions, 0, only.children.length, positions[groupFrom] + offset);
|
|
1341
|
+
continue;
|
|
1342
|
+
}
|
|
1343
|
+
localChildren.push(children[groupFrom]);
|
|
1344
|
+
} else {
|
|
1345
|
+
let length = positions[i - 1] + children[i - 1].length - groupStart;
|
|
1346
|
+
localChildren.push(balanceRange(balanceType, children, positions, groupFrom, i, groupStart, length, null, mkTree));
|
|
1347
|
+
}
|
|
1348
|
+
localPositions.push(groupStart + offset - start);
|
|
1349
|
+
}
|
|
1350
|
+
}
|
|
1351
|
+
divide(children, positions, from, to, 0);
|
|
1352
|
+
return (mkTop || mkTree)(localChildren, localPositions, length);
|
|
1353
|
+
}
|
|
1354
|
+
/// Provides a way to associate values with pieces of trees. As long
|
|
1355
|
+
/// as that part of the tree is reused, the associated values can be
|
|
1356
|
+
/// retrieved from an updated tree.
|
|
1357
|
+
class NodeWeakMap {
|
|
1358
|
+
constructor() {
|
|
1359
|
+
this.map = new WeakMap();
|
|
1360
|
+
}
|
|
1361
|
+
setBuffer(buffer, index, value) {
|
|
1362
|
+
let inner = this.map.get(buffer);
|
|
1363
|
+
if (!inner) this.map.set(buffer, inner = new Map());
|
|
1364
|
+
inner.set(index, value);
|
|
1365
|
+
}
|
|
1366
|
+
getBuffer(buffer, index) {
|
|
1367
|
+
let inner = this.map.get(buffer);
|
|
1368
|
+
return inner && inner.get(index);
|
|
1369
|
+
}
|
|
1370
|
+
/// Set the value for this syntax node.
|
|
1371
|
+
set(node, value) {
|
|
1372
|
+
if (node instanceof BufferNode) this.setBuffer(node.context.buffer, node.index, value);else if (node instanceof TreeNode) this.map.set(node.tree, value);
|
|
1373
|
+
}
|
|
1374
|
+
/// Retrieve value for this syntax node, if it exists in the map.
|
|
1375
|
+
get(node) {
|
|
1376
|
+
return node instanceof BufferNode ? this.getBuffer(node.context.buffer, node.index) : node instanceof TreeNode ? this.map.get(node.tree) : undefined;
|
|
1377
|
+
}
|
|
1378
|
+
/// Set the value for the node that a cursor currently points to.
|
|
1379
|
+
cursorSet(cursor, value) {
|
|
1380
|
+
if (cursor.buffer) this.setBuffer(cursor.buffer.buffer, cursor.index, value);else this.map.set(cursor.tree, value);
|
|
1381
|
+
}
|
|
1382
|
+
/// Retrieve the value for the node that a cursor currently points
|
|
1383
|
+
/// to.
|
|
1384
|
+
cursorGet(cursor) {
|
|
1385
|
+
return cursor.buffer ? this.getBuffer(cursor.buffer.buffer, cursor.index) : this.map.get(cursor.tree);
|
|
1386
|
+
}
|
|
1387
|
+
}
|
|
1388
|
+
|
|
1389
|
+
/// Tree fragments are used during [incremental
|
|
1390
|
+
/// parsing](#common.Parser.startParse) to track parts of old trees
|
|
1391
|
+
/// that can be reused in a new parse. An array of fragments is used
|
|
1392
|
+
/// to track regions of an old tree whose nodes might be reused in new
|
|
1393
|
+
/// parses. Use the static
|
|
1394
|
+
/// [`applyChanges`](#common.TreeFragment^applyChanges) method to
|
|
1395
|
+
/// update fragments for document changes.
|
|
1396
|
+
class TreeFragment {
|
|
1397
|
+
/// Construct a tree fragment. You'll usually want to use
|
|
1398
|
+
/// [`addTree`](#common.TreeFragment^addTree) and
|
|
1399
|
+
/// [`applyChanges`](#common.TreeFragment^applyChanges) instead of
|
|
1400
|
+
/// calling this directly.
|
|
1401
|
+
constructor(
|
|
1402
|
+
/// The start of the unchanged range pointed to by this fragment.
|
|
1403
|
+
/// This refers to an offset in the _updated_ document (as opposed
|
|
1404
|
+
/// to the original tree).
|
|
1405
|
+
from,
|
|
1406
|
+
/// The end of the unchanged range.
|
|
1407
|
+
to,
|
|
1408
|
+
/// The tree that this fragment is based on.
|
|
1409
|
+
tree,
|
|
1410
|
+
/// The offset between the fragment's tree and the document that
|
|
1411
|
+
/// this fragment can be used against. Add this when going from
|
|
1412
|
+
/// document to tree positions, subtract it to go from tree to
|
|
1413
|
+
/// document positions.
|
|
1414
|
+
offset, openStart = false, openEnd = false) {
|
|
1415
|
+
this.from = from;
|
|
1416
|
+
this.to = to;
|
|
1417
|
+
this.tree = tree;
|
|
1418
|
+
this.offset = offset;
|
|
1419
|
+
this.open = (openStart ? 1 /* Open.Start */ : 0) | (openEnd ? 2 /* Open.End */ : 0);
|
|
1420
|
+
}
|
|
1421
|
+
/// Whether the start of the fragment represents the start of a
|
|
1422
|
+
/// parse, or the end of a change. (In the second case, it may not
|
|
1423
|
+
/// be safe to reuse some nodes at the start, depending on the
|
|
1424
|
+
/// parsing algorithm.)
|
|
1425
|
+
get openStart() {
|
|
1426
|
+
return (this.open & 1 /* Open.Start */) > 0;
|
|
1427
|
+
}
|
|
1428
|
+
/// Whether the end of the fragment represents the end of a
|
|
1429
|
+
/// full-document parse, or the start of a change.
|
|
1430
|
+
get openEnd() {
|
|
1431
|
+
return (this.open & 2 /* Open.End */) > 0;
|
|
1432
|
+
}
|
|
1433
|
+
/// Create a set of fragments from a freshly parsed tree, or update
|
|
1434
|
+
/// an existing set of fragments by replacing the ones that overlap
|
|
1435
|
+
/// with a tree with content from the new tree. When `partial` is
|
|
1436
|
+
/// true, the parse is treated as incomplete, and the resulting
|
|
1437
|
+
/// fragment has [`openEnd`](#common.TreeFragment.openEnd) set to
|
|
1438
|
+
/// true.
|
|
1439
|
+
static addTree(tree, fragments = [], partial = false) {
|
|
1440
|
+
let result = [new TreeFragment(0, tree.length, tree, 0, false, partial)];
|
|
1441
|
+
for (let f of fragments) if (f.to > tree.length) result.push(f);
|
|
1442
|
+
return result;
|
|
1443
|
+
}
|
|
1444
|
+
/// Apply a set of edits to an array of fragments, removing or
|
|
1445
|
+
/// splitting fragments as necessary to remove edited ranges, and
|
|
1446
|
+
/// adjusting offsets for fragments that moved.
|
|
1447
|
+
static applyChanges(fragments, changes, minGap = 128) {
|
|
1448
|
+
if (!changes.length) return fragments;
|
|
1449
|
+
let result = [];
|
|
1450
|
+
let fI = 1,
|
|
1451
|
+
nextF = fragments.length ? fragments[0] : null;
|
|
1452
|
+
for (let cI = 0, pos = 0, off = 0;; cI++) {
|
|
1453
|
+
let nextC = cI < changes.length ? changes[cI] : null;
|
|
1454
|
+
let nextPos = nextC ? nextC.fromA : 1e9;
|
|
1455
|
+
if (nextPos - pos >= minGap) while (nextF && nextF.from < nextPos) {
|
|
1456
|
+
let cut = nextF;
|
|
1457
|
+
if (pos >= cut.from || nextPos <= cut.to || off) {
|
|
1458
|
+
let fFrom = Math.max(cut.from, pos) - off,
|
|
1459
|
+
fTo = Math.min(cut.to, nextPos) - off;
|
|
1460
|
+
cut = fFrom >= fTo ? null : new TreeFragment(fFrom, fTo, cut.tree, cut.offset + off, cI > 0, !!nextC);
|
|
1461
|
+
}
|
|
1462
|
+
if (cut) result.push(cut);
|
|
1463
|
+
if (nextF.to > nextPos) break;
|
|
1464
|
+
nextF = fI < fragments.length ? fragments[fI++] : null;
|
|
1465
|
+
}
|
|
1466
|
+
if (!nextC) break;
|
|
1467
|
+
pos = nextC.toA;
|
|
1468
|
+
off = nextC.toA - nextC.toB;
|
|
1469
|
+
}
|
|
1470
|
+
return result;
|
|
1471
|
+
}
|
|
1472
|
+
}
|
|
1473
|
+
/// A superclass that parsers should extend.
|
|
1474
|
+
class Parser {
|
|
1475
|
+
/// Start a parse, returning a [partial parse](#common.PartialParse)
|
|
1476
|
+
/// object. [`fragments`](#common.TreeFragment) can be passed in to
|
|
1477
|
+
/// make the parse incremental.
|
|
1478
|
+
///
|
|
1479
|
+
/// By default, the entire input is parsed. You can pass `ranges`,
|
|
1480
|
+
/// which should be a sorted array of non-empty, non-overlapping
|
|
1481
|
+
/// ranges, to parse only those ranges. The tree returned in that
|
|
1482
|
+
/// case will start at `ranges[0].from`.
|
|
1483
|
+
startParse(input, fragments, ranges) {
|
|
1484
|
+
if (typeof input == "string") input = new StringInput(input);
|
|
1485
|
+
ranges = !ranges ? [new Range(0, input.length)] : ranges.length ? ranges.map(r => new Range(r.from, r.to)) : [new Range(0, 0)];
|
|
1486
|
+
return this.createParse(input, fragments || [], ranges);
|
|
1487
|
+
}
|
|
1488
|
+
/// Run a full parse, returning the resulting tree.
|
|
1489
|
+
parse(input, fragments, ranges) {
|
|
1490
|
+
let parse = this.startParse(input, fragments, ranges);
|
|
1491
|
+
for (;;) {
|
|
1492
|
+
let done = parse.advance();
|
|
1493
|
+
if (done) return done;
|
|
1494
|
+
}
|
|
1495
|
+
}
|
|
1496
|
+
}
|
|
1497
|
+
class StringInput {
|
|
1498
|
+
constructor(string) {
|
|
1499
|
+
this.string = string;
|
|
1500
|
+
}
|
|
1501
|
+
get length() {
|
|
1502
|
+
return this.string.length;
|
|
1503
|
+
}
|
|
1504
|
+
chunk(from) {
|
|
1505
|
+
return this.string.slice(from);
|
|
1506
|
+
}
|
|
1507
|
+
get lineChunks() {
|
|
1508
|
+
return false;
|
|
1509
|
+
}
|
|
1510
|
+
read(from, to) {
|
|
1511
|
+
return this.string.slice(from, to);
|
|
1512
|
+
}
|
|
1513
|
+
}
|
|
1514
|
+
|
|
1515
|
+
/// Create a parse wrapper that, after the inner parse completes,
|
|
1516
|
+
/// scans its tree for mixed language regions with the `nest`
|
|
1517
|
+
/// function, runs the resulting [inner parses](#common.NestedParse),
|
|
1518
|
+
/// and then [mounts](#common.NodeProp^mounted) their results onto the
|
|
1519
|
+
/// tree.
|
|
1520
|
+
function parseMixed(nest) {
|
|
1521
|
+
return (parse, input, fragments, ranges) => new MixedParse(parse, nest, input, fragments, ranges);
|
|
1522
|
+
}
|
|
1523
|
+
class InnerParse {
|
|
1524
|
+
constructor(parser, parse, overlay, target, ranges) {
|
|
1525
|
+
this.parser = parser;
|
|
1526
|
+
this.parse = parse;
|
|
1527
|
+
this.overlay = overlay;
|
|
1528
|
+
this.target = target;
|
|
1529
|
+
this.ranges = ranges;
|
|
1530
|
+
}
|
|
1531
|
+
}
|
|
1532
|
+
class ActiveOverlay {
|
|
1533
|
+
constructor(parser, predicate, mounts, index, start, target, prev) {
|
|
1534
|
+
this.parser = parser;
|
|
1535
|
+
this.predicate = predicate;
|
|
1536
|
+
this.mounts = mounts;
|
|
1537
|
+
this.index = index;
|
|
1538
|
+
this.start = start;
|
|
1539
|
+
this.target = target;
|
|
1540
|
+
this.prev = prev;
|
|
1541
|
+
this.depth = 0;
|
|
1542
|
+
this.ranges = [];
|
|
1543
|
+
}
|
|
1544
|
+
}
|
|
1545
|
+
const stoppedInner = new NodeProp({
|
|
1546
|
+
perNode: true
|
|
1547
|
+
});
|
|
1548
|
+
class MixedParse {
|
|
1549
|
+
constructor(base, nest, input, fragments, ranges) {
|
|
1550
|
+
this.nest = nest;
|
|
1551
|
+
this.input = input;
|
|
1552
|
+
this.fragments = fragments;
|
|
1553
|
+
this.ranges = ranges;
|
|
1554
|
+
this.inner = [];
|
|
1555
|
+
this.innerDone = 0;
|
|
1556
|
+
this.baseTree = null;
|
|
1557
|
+
this.stoppedAt = null;
|
|
1558
|
+
this.baseParse = base;
|
|
1559
|
+
}
|
|
1560
|
+
advance() {
|
|
1561
|
+
if (this.baseParse) {
|
|
1562
|
+
let done = this.baseParse.advance();
|
|
1563
|
+
if (!done) return null;
|
|
1564
|
+
this.baseParse = null;
|
|
1565
|
+
this.baseTree = done;
|
|
1566
|
+
this.startInner();
|
|
1567
|
+
if (this.stoppedAt != null) for (let inner of this.inner) inner.parse.stopAt(this.stoppedAt);
|
|
1568
|
+
}
|
|
1569
|
+
if (this.innerDone == this.inner.length) {
|
|
1570
|
+
let result = this.baseTree;
|
|
1571
|
+
if (this.stoppedAt != null) result = new Tree(result.type, result.children, result.positions, result.length, result.propValues.concat([[stoppedInner, this.stoppedAt]]));
|
|
1572
|
+
return result;
|
|
1573
|
+
}
|
|
1574
|
+
let inner = this.inner[this.innerDone],
|
|
1575
|
+
done = inner.parse.advance();
|
|
1576
|
+
if (done) {
|
|
1577
|
+
this.innerDone++;
|
|
1578
|
+
// This is a somewhat dodgy but super helpful hack where we
|
|
1579
|
+
// patch up nodes created by the inner parse (and thus
|
|
1580
|
+
// presumably not aliased anywhere else) to hold the information
|
|
1581
|
+
// about the inner parse.
|
|
1582
|
+
let props = Object.assign(Object.create(null), inner.target.props);
|
|
1583
|
+
props[NodeProp.mounted.id] = new MountedTree(done, inner.overlay, inner.parser);
|
|
1584
|
+
inner.target.props = props;
|
|
1585
|
+
}
|
|
1586
|
+
return null;
|
|
1587
|
+
}
|
|
1588
|
+
get parsedPos() {
|
|
1589
|
+
if (this.baseParse) return 0;
|
|
1590
|
+
let pos = this.input.length;
|
|
1591
|
+
for (let i = this.innerDone; i < this.inner.length; i++) {
|
|
1592
|
+
if (this.inner[i].ranges[0].from < pos) pos = Math.min(pos, this.inner[i].parse.parsedPos);
|
|
1593
|
+
}
|
|
1594
|
+
return pos;
|
|
1595
|
+
}
|
|
1596
|
+
stopAt(pos) {
|
|
1597
|
+
this.stoppedAt = pos;
|
|
1598
|
+
if (this.baseParse) this.baseParse.stopAt(pos);else for (let i = this.innerDone; i < this.inner.length; i++) this.inner[i].parse.stopAt(pos);
|
|
1599
|
+
}
|
|
1600
|
+
startInner() {
|
|
1601
|
+
let fragmentCursor = new FragmentCursor(this.fragments);
|
|
1602
|
+
let overlay = null;
|
|
1603
|
+
let covered = null;
|
|
1604
|
+
let cursor = new TreeCursor(new TreeNode(this.baseTree, this.ranges[0].from, 0, null), IterMode.IncludeAnonymous | IterMode.IgnoreMounts);
|
|
1605
|
+
scan: for (let nest, isCovered; this.stoppedAt == null || cursor.from < this.stoppedAt;) {
|
|
1606
|
+
let enter = true,
|
|
1607
|
+
range;
|
|
1608
|
+
if (fragmentCursor.hasNode(cursor)) {
|
|
1609
|
+
if (overlay) {
|
|
1610
|
+
let match = overlay.mounts.find(m => m.frag.from <= cursor.from && m.frag.to >= cursor.to && m.mount.overlay);
|
|
1611
|
+
if (match) for (let r of match.mount.overlay) {
|
|
1612
|
+
let from = r.from + match.pos,
|
|
1613
|
+
to = r.to + match.pos;
|
|
1614
|
+
if (from >= cursor.from && to <= cursor.to && !overlay.ranges.some(r => r.from < to && r.to > from)) overlay.ranges.push({
|
|
1615
|
+
from,
|
|
1616
|
+
to
|
|
1617
|
+
});
|
|
1618
|
+
}
|
|
1619
|
+
}
|
|
1620
|
+
enter = false;
|
|
1621
|
+
} else if (covered && (isCovered = checkCover(covered.ranges, cursor.from, cursor.to))) {
|
|
1622
|
+
enter = isCovered != 2 /* Cover.Full */;
|
|
1623
|
+
} else if (!cursor.type.isAnonymous && cursor.from < cursor.to && (nest = this.nest(cursor, this.input))) {
|
|
1624
|
+
if (!cursor.tree) materialize(cursor);
|
|
1625
|
+
let oldMounts = fragmentCursor.findMounts(cursor.from, nest.parser);
|
|
1626
|
+
if (typeof nest.overlay == "function") {
|
|
1627
|
+
overlay = new ActiveOverlay(nest.parser, nest.overlay, oldMounts, this.inner.length, cursor.from, cursor.tree, overlay);
|
|
1628
|
+
} else {
|
|
1629
|
+
let ranges = punchRanges(this.ranges, nest.overlay || [new Range(cursor.from, cursor.to)]);
|
|
1630
|
+
if (ranges.length) this.inner.push(new InnerParse(nest.parser, nest.parser.startParse(this.input, enterFragments(oldMounts, ranges), ranges), nest.overlay ? nest.overlay.map(r => new Range(r.from - cursor.from, r.to - cursor.from)) : null, cursor.tree, ranges));
|
|
1631
|
+
if (!nest.overlay) enter = false;else if (ranges.length) covered = {
|
|
1632
|
+
ranges,
|
|
1633
|
+
depth: 0,
|
|
1634
|
+
prev: covered
|
|
1635
|
+
};
|
|
1636
|
+
}
|
|
1637
|
+
} else if (overlay && (range = overlay.predicate(cursor))) {
|
|
1638
|
+
if (range === true) range = new Range(cursor.from, cursor.to);
|
|
1639
|
+
if (range.from < range.to) overlay.ranges.push(range);
|
|
1640
|
+
}
|
|
1641
|
+
if (enter && cursor.firstChild()) {
|
|
1642
|
+
if (overlay) overlay.depth++;
|
|
1643
|
+
if (covered) covered.depth++;
|
|
1644
|
+
} else {
|
|
1645
|
+
for (;;) {
|
|
1646
|
+
if (cursor.nextSibling()) break;
|
|
1647
|
+
if (!cursor.parent()) break scan;
|
|
1648
|
+
if (overlay && ! --overlay.depth) {
|
|
1649
|
+
let ranges = punchRanges(this.ranges, overlay.ranges);
|
|
1650
|
+
if (ranges.length) this.inner.splice(overlay.index, 0, new InnerParse(overlay.parser, overlay.parser.startParse(this.input, enterFragments(overlay.mounts, ranges), ranges), overlay.ranges.map(r => new Range(r.from - overlay.start, r.to - overlay.start)), overlay.target, ranges));
|
|
1651
|
+
overlay = overlay.prev;
|
|
1652
|
+
}
|
|
1653
|
+
if (covered && ! --covered.depth) covered = covered.prev;
|
|
1654
|
+
}
|
|
1655
|
+
}
|
|
1656
|
+
}
|
|
1657
|
+
}
|
|
1658
|
+
}
|
|
1659
|
+
function checkCover(covered, from, to) {
|
|
1660
|
+
for (let range of covered) {
|
|
1661
|
+
if (range.from >= to) break;
|
|
1662
|
+
if (range.to > from) return range.from <= from && range.to >= to ? 2 /* Cover.Full */ : 1 /* Cover.Partial */;
|
|
1663
|
+
}
|
|
1664
|
+
|
|
1665
|
+
return 0 /* Cover.None */;
|
|
1666
|
+
}
|
|
1667
|
+
// Take a piece of buffer and convert it into a stand-alone
|
|
1668
|
+
// TreeBuffer.
|
|
1669
|
+
function sliceBuf(buf, startI, endI, nodes, positions, off) {
|
|
1670
|
+
if (startI < endI) {
|
|
1671
|
+
let from = buf.buffer[startI + 1];
|
|
1672
|
+
nodes.push(buf.slice(startI, endI, from));
|
|
1673
|
+
positions.push(from - off);
|
|
1674
|
+
}
|
|
1675
|
+
}
|
|
1676
|
+
// This function takes a node that's in a buffer, and converts it, and
|
|
1677
|
+
// its parent buffer nodes, into a Tree. This is again acting on the
|
|
1678
|
+
// assumption that the trees and buffers have been constructed by the
|
|
1679
|
+
// parse that was ran via the mix parser, and thus aren't shared with
|
|
1680
|
+
// any other code, making violations of the immutability safe.
|
|
1681
|
+
function materialize(cursor) {
|
|
1682
|
+
let {
|
|
1683
|
+
node
|
|
1684
|
+
} = cursor,
|
|
1685
|
+
depth = 0;
|
|
1686
|
+
// Scan up to the nearest tree
|
|
1687
|
+
do {
|
|
1688
|
+
cursor.parent();
|
|
1689
|
+
depth++;
|
|
1690
|
+
} while (!cursor.tree);
|
|
1691
|
+
// Find the index of the buffer in that tree
|
|
1692
|
+
let i = 0,
|
|
1693
|
+
base = cursor.tree,
|
|
1694
|
+
off = 0;
|
|
1695
|
+
for (;; i++) {
|
|
1696
|
+
off = base.positions[i] + cursor.from;
|
|
1697
|
+
if (off <= node.from && off + base.children[i].length >= node.to) break;
|
|
1698
|
+
}
|
|
1699
|
+
let buf = base.children[i],
|
|
1700
|
+
b = buf.buffer;
|
|
1701
|
+
// Split a level in the buffer, putting the nodes before and after
|
|
1702
|
+
// the child that contains `node` into new buffers.
|
|
1703
|
+
function split(startI, endI, type, innerOffset, length) {
|
|
1704
|
+
let i = startI;
|
|
1705
|
+
while (b[i + 2] + off <= node.from) i = b[i + 3];
|
|
1706
|
+
let children = [],
|
|
1707
|
+
positions = [];
|
|
1708
|
+
sliceBuf(buf, startI, i, children, positions, innerOffset);
|
|
1709
|
+
let from = b[i + 1],
|
|
1710
|
+
to = b[i + 2];
|
|
1711
|
+
let isTarget = from + off == node.from && to + off == node.to && b[i] == node.type.id;
|
|
1712
|
+
children.push(isTarget ? node.toTree() : split(i + 4, b[i + 3], buf.set.types[b[i]], from, to - from));
|
|
1713
|
+
positions.push(from - innerOffset);
|
|
1714
|
+
sliceBuf(buf, b[i + 3], endI, children, positions, innerOffset);
|
|
1715
|
+
return new Tree(type, children, positions, length);
|
|
1716
|
+
}
|
|
1717
|
+
base.children[i] = split(0, b.length, NodeType.none, 0, buf.length);
|
|
1718
|
+
// Move the cursor back to the target node
|
|
1719
|
+
for (let d = 0; d <= depth; d++) cursor.childAfter(node.from);
|
|
1720
|
+
}
|
|
1721
|
+
class StructureCursor {
|
|
1722
|
+
constructor(root, offset) {
|
|
1723
|
+
this.offset = offset;
|
|
1724
|
+
this.done = false;
|
|
1725
|
+
this.cursor = root.cursor(IterMode.IncludeAnonymous | IterMode.IgnoreMounts);
|
|
1726
|
+
}
|
|
1727
|
+
// Move to the first node (in pre-order) that starts at or after `pos`.
|
|
1728
|
+
moveTo(pos) {
|
|
1729
|
+
let {
|
|
1730
|
+
cursor
|
|
1731
|
+
} = this,
|
|
1732
|
+
p = pos - this.offset;
|
|
1733
|
+
while (!this.done && cursor.from < p) {
|
|
1734
|
+
if (cursor.to >= pos && cursor.enter(p, 1, IterMode.IgnoreOverlays | IterMode.ExcludeBuffers)) ;else if (!cursor.next(false)) this.done = true;
|
|
1735
|
+
}
|
|
1736
|
+
}
|
|
1737
|
+
hasNode(cursor) {
|
|
1738
|
+
this.moveTo(cursor.from);
|
|
1739
|
+
if (!this.done && this.cursor.from + this.offset == cursor.from && this.cursor.tree) {
|
|
1740
|
+
for (let tree = this.cursor.tree;;) {
|
|
1741
|
+
if (tree == cursor.tree) return true;
|
|
1742
|
+
if (tree.children.length && tree.positions[0] == 0 && tree.children[0] instanceof Tree) tree = tree.children[0];else break;
|
|
1743
|
+
}
|
|
1744
|
+
}
|
|
1745
|
+
return false;
|
|
1746
|
+
}
|
|
1747
|
+
}
|
|
1748
|
+
class FragmentCursor {
|
|
1749
|
+
constructor(fragments) {
|
|
1750
|
+
var _a;
|
|
1751
|
+
this.fragments = fragments;
|
|
1752
|
+
this.curTo = 0;
|
|
1753
|
+
this.fragI = 0;
|
|
1754
|
+
if (fragments.length) {
|
|
1755
|
+
let first = this.curFrag = fragments[0];
|
|
1756
|
+
this.curTo = (_a = first.tree.prop(stoppedInner)) !== null && _a !== void 0 ? _a : first.to;
|
|
1757
|
+
this.inner = new StructureCursor(first.tree, -first.offset);
|
|
1758
|
+
} else {
|
|
1759
|
+
this.curFrag = this.inner = null;
|
|
1760
|
+
}
|
|
1761
|
+
}
|
|
1762
|
+
hasNode(node) {
|
|
1763
|
+
while (this.curFrag && node.from >= this.curTo) this.nextFrag();
|
|
1764
|
+
return this.curFrag && this.curFrag.from <= node.from && this.curTo >= node.to && this.inner.hasNode(node);
|
|
1765
|
+
}
|
|
1766
|
+
nextFrag() {
|
|
1767
|
+
var _a;
|
|
1768
|
+
this.fragI++;
|
|
1769
|
+
if (this.fragI == this.fragments.length) {
|
|
1770
|
+
this.curFrag = this.inner = null;
|
|
1771
|
+
} else {
|
|
1772
|
+
let frag = this.curFrag = this.fragments[this.fragI];
|
|
1773
|
+
this.curTo = (_a = frag.tree.prop(stoppedInner)) !== null && _a !== void 0 ? _a : frag.to;
|
|
1774
|
+
this.inner = new StructureCursor(frag.tree, -frag.offset);
|
|
1775
|
+
}
|
|
1776
|
+
}
|
|
1777
|
+
findMounts(pos, parser) {
|
|
1778
|
+
var _a;
|
|
1779
|
+
let result = [];
|
|
1780
|
+
if (this.inner) {
|
|
1781
|
+
this.inner.cursor.moveTo(pos, 1);
|
|
1782
|
+
for (let pos = this.inner.cursor.node; pos; pos = pos.parent) {
|
|
1783
|
+
let mount = (_a = pos.tree) === null || _a === void 0 ? void 0 : _a.prop(NodeProp.mounted);
|
|
1784
|
+
if (mount && mount.parser == parser) {
|
|
1785
|
+
for (let i = this.fragI; i < this.fragments.length; i++) {
|
|
1786
|
+
let frag = this.fragments[i];
|
|
1787
|
+
if (frag.from >= pos.to) break;
|
|
1788
|
+
if (frag.tree == this.curFrag.tree) result.push({
|
|
1789
|
+
frag,
|
|
1790
|
+
pos: pos.from - frag.offset,
|
|
1791
|
+
mount
|
|
1792
|
+
});
|
|
1793
|
+
}
|
|
1794
|
+
}
|
|
1795
|
+
}
|
|
1796
|
+
}
|
|
1797
|
+
return result;
|
|
1798
|
+
}
|
|
1799
|
+
}
|
|
1800
|
+
function punchRanges(outer, ranges) {
|
|
1801
|
+
let copy = null,
|
|
1802
|
+
current = ranges;
|
|
1803
|
+
for (let i = 1, j = 0; i < outer.length; i++) {
|
|
1804
|
+
let gapFrom = outer[i - 1].to,
|
|
1805
|
+
gapTo = outer[i].from;
|
|
1806
|
+
for (; j < current.length; j++) {
|
|
1807
|
+
let r = current[j];
|
|
1808
|
+
if (r.from >= gapTo) break;
|
|
1809
|
+
if (r.to <= gapFrom) continue;
|
|
1810
|
+
if (!copy) current = copy = ranges.slice();
|
|
1811
|
+
if (r.from < gapFrom) {
|
|
1812
|
+
copy[j] = new Range(r.from, gapFrom);
|
|
1813
|
+
if (r.to > gapTo) copy.splice(j + 1, 0, new Range(gapTo, r.to));
|
|
1814
|
+
} else if (r.to > gapTo) {
|
|
1815
|
+
copy[j--] = new Range(gapTo, r.to);
|
|
1816
|
+
} else {
|
|
1817
|
+
copy.splice(j--, 1);
|
|
1818
|
+
}
|
|
1819
|
+
}
|
|
1820
|
+
}
|
|
1821
|
+
return current;
|
|
1822
|
+
}
|
|
1823
|
+
function findCoverChanges(a, b, from, to) {
|
|
1824
|
+
let iA = 0,
|
|
1825
|
+
iB = 0,
|
|
1826
|
+
inA = false,
|
|
1827
|
+
inB = false,
|
|
1828
|
+
pos = -1e9;
|
|
1829
|
+
let result = [];
|
|
1830
|
+
for (;;) {
|
|
1831
|
+
let nextA = iA == a.length ? 1e9 : inA ? a[iA].to : a[iA].from;
|
|
1832
|
+
let nextB = iB == b.length ? 1e9 : inB ? b[iB].to : b[iB].from;
|
|
1833
|
+
if (inA != inB) {
|
|
1834
|
+
let start = Math.max(pos, from),
|
|
1835
|
+
end = Math.min(nextA, nextB, to);
|
|
1836
|
+
if (start < end) result.push(new Range(start, end));
|
|
1837
|
+
}
|
|
1838
|
+
pos = Math.min(nextA, nextB);
|
|
1839
|
+
if (pos == 1e9) break;
|
|
1840
|
+
if (nextA == pos) {
|
|
1841
|
+
if (!inA) inA = true;else {
|
|
1842
|
+
inA = false;
|
|
1843
|
+
iA++;
|
|
1844
|
+
}
|
|
1845
|
+
}
|
|
1846
|
+
if (nextB == pos) {
|
|
1847
|
+
if (!inB) inB = true;else {
|
|
1848
|
+
inB = false;
|
|
1849
|
+
iB++;
|
|
1850
|
+
}
|
|
1851
|
+
}
|
|
1852
|
+
}
|
|
1853
|
+
return result;
|
|
1854
|
+
}
|
|
1855
|
+
// Given a number of fragments for the outer tree, and a set of ranges
|
|
1856
|
+
// to parse, find fragments for inner trees mounted around those
|
|
1857
|
+
// ranges, if any.
|
|
1858
|
+
function enterFragments(mounts, ranges) {
|
|
1859
|
+
let result = [];
|
|
1860
|
+
for (let {
|
|
1861
|
+
pos,
|
|
1862
|
+
mount,
|
|
1863
|
+
frag
|
|
1864
|
+
} of mounts) {
|
|
1865
|
+
let startPos = pos + (mount.overlay ? mount.overlay[0].from : 0),
|
|
1866
|
+
endPos = startPos + mount.tree.length;
|
|
1867
|
+
let from = Math.max(frag.from, startPos),
|
|
1868
|
+
to = Math.min(frag.to, endPos);
|
|
1869
|
+
if (mount.overlay) {
|
|
1870
|
+
let overlay = mount.overlay.map(r => new Range(r.from + pos, r.to + pos));
|
|
1871
|
+
let changes = findCoverChanges(ranges, overlay, from, to);
|
|
1872
|
+
for (let i = 0, pos = from;; i++) {
|
|
1873
|
+
let last = i == changes.length,
|
|
1874
|
+
end = last ? to : changes[i].from;
|
|
1875
|
+
if (end > pos) result.push(new TreeFragment(pos, end, mount.tree, -startPos, frag.from >= pos || frag.openStart, frag.to <= end || frag.openEnd));
|
|
1876
|
+
if (last) break;
|
|
1877
|
+
pos = changes[i].to;
|
|
1878
|
+
}
|
|
1879
|
+
} else {
|
|
1880
|
+
result.push(new TreeFragment(from, to, mount.tree, -startPos, frag.from >= startPos || frag.openStart, frag.to <= endPos || frag.openEnd));
|
|
1881
|
+
}
|
|
1882
|
+
}
|
|
1883
|
+
return result;
|
|
1884
|
+
}
|
|
1885
|
+
|
|
1886
|
+
let nextTagID = 0;
|
|
1887
|
+
/// Highlighting tags are markers that denote a highlighting category.
|
|
1888
|
+
/// They are [associated](#highlight.styleTags) with parts of a syntax
|
|
1889
|
+
/// tree by a language mode, and then mapped to an actual CSS style by
|
|
1890
|
+
/// a [highlighter](#highlight.Highlighter).
|
|
1891
|
+
///
|
|
1892
|
+
/// Because syntax tree node types and highlight styles have to be
|
|
1893
|
+
/// able to talk the same language, CodeMirror uses a mostly _closed_
|
|
1894
|
+
/// [vocabulary](#highlight.tags) of syntax tags (as opposed to
|
|
1895
|
+
/// traditional open string-based systems, which make it hard for
|
|
1896
|
+
/// highlighting themes to cover all the tokens produced by the
|
|
1897
|
+
/// various languages).
|
|
1898
|
+
///
|
|
1899
|
+
/// It _is_ possible to [define](#highlight.Tag^define) your own
|
|
1900
|
+
/// highlighting tags for system-internal use (where you control both
|
|
1901
|
+
/// the language package and the highlighter), but such tags will not
|
|
1902
|
+
/// be picked up by regular highlighters (though you can derive them
|
|
1903
|
+
/// from standard tags to allow highlighters to fall back to those).
|
|
1904
|
+
class Tag {
|
|
1905
|
+
/// @internal
|
|
1906
|
+
constructor(
|
|
1907
|
+
/// The set of this tag and all its parent tags, starting with
|
|
1908
|
+
/// this one itself and sorted in order of decreasing specificity.
|
|
1909
|
+
set,
|
|
1910
|
+
/// The base unmodified tag that this one is based on, if it's
|
|
1911
|
+
/// modified @internal
|
|
1912
|
+
base,
|
|
1913
|
+
/// The modifiers applied to this.base @internal
|
|
1914
|
+
modified) {
|
|
1915
|
+
this.set = set;
|
|
1916
|
+
this.base = base;
|
|
1917
|
+
this.modified = modified;
|
|
1918
|
+
/// @internal
|
|
1919
|
+
this.id = nextTagID++;
|
|
1920
|
+
}
|
|
1921
|
+
/// Define a new tag. If `parent` is given, the tag is treated as a
|
|
1922
|
+
/// sub-tag of that parent, and
|
|
1923
|
+
/// [highlighters](#highlight.tagHighlighter) that don't mention
|
|
1924
|
+
/// this tag will try to fall back to the parent tag (or grandparent
|
|
1925
|
+
/// tag, etc).
|
|
1926
|
+
static define(parent) {
|
|
1927
|
+
if (parent === null || parent === void 0 ? void 0 : parent.base) throw new Error("Can not derive from a modified tag");
|
|
1928
|
+
let tag = new Tag([], null, []);
|
|
1929
|
+
tag.set.push(tag);
|
|
1930
|
+
if (parent) for (let t of parent.set) tag.set.push(t);
|
|
1931
|
+
return tag;
|
|
1932
|
+
}
|
|
1933
|
+
/// Define a tag _modifier_, which is a function that, given a tag,
|
|
1934
|
+
/// will return a tag that is a subtag of the original. Applying the
|
|
1935
|
+
/// same modifier to a twice tag will return the same value (`m1(t1)
|
|
1936
|
+
/// == m1(t1)`) and applying multiple modifiers will, regardless or
|
|
1937
|
+
/// order, produce the same tag (`m1(m2(t1)) == m2(m1(t1))`).
|
|
1938
|
+
///
|
|
1939
|
+
/// When multiple modifiers are applied to a given base tag, each
|
|
1940
|
+
/// smaller set of modifiers is registered as a parent, so that for
|
|
1941
|
+
/// example `m1(m2(m3(t1)))` is a subtype of `m1(m2(t1))`,
|
|
1942
|
+
/// `m1(m3(t1)`, and so on.
|
|
1943
|
+
static defineModifier() {
|
|
1944
|
+
let mod = new Modifier();
|
|
1945
|
+
return tag => {
|
|
1946
|
+
if (tag.modified.indexOf(mod) > -1) return tag;
|
|
1947
|
+
return Modifier.get(tag.base || tag, tag.modified.concat(mod).sort((a, b) => a.id - b.id));
|
|
1948
|
+
};
|
|
1949
|
+
}
|
|
1950
|
+
}
|
|
1951
|
+
let nextModifierID = 0;
|
|
1952
|
+
class Modifier {
|
|
1953
|
+
constructor() {
|
|
1954
|
+
this.instances = [];
|
|
1955
|
+
this.id = nextModifierID++;
|
|
1956
|
+
}
|
|
1957
|
+
static get(base, mods) {
|
|
1958
|
+
if (!mods.length) return base;
|
|
1959
|
+
let exists = mods[0].instances.find(t => t.base == base && sameArray(mods, t.modified));
|
|
1960
|
+
if (exists) return exists;
|
|
1961
|
+
let set = [],
|
|
1962
|
+
tag = new Tag(set, base, mods);
|
|
1963
|
+
for (let m of mods) m.instances.push(tag);
|
|
1964
|
+
let configs = powerSet(mods);
|
|
1965
|
+
for (let parent of base.set) if (!parent.modified.length) for (let config of configs) set.push(Modifier.get(parent, config));
|
|
1966
|
+
return tag;
|
|
1967
|
+
}
|
|
1968
|
+
}
|
|
1969
|
+
function sameArray(a, b) {
|
|
1970
|
+
return a.length == b.length && a.every((x, i) => x == b[i]);
|
|
1971
|
+
}
|
|
1972
|
+
function powerSet(array) {
|
|
1973
|
+
let sets = [[]];
|
|
1974
|
+
for (let i = 0; i < array.length; i++) {
|
|
1975
|
+
for (let j = 0, e = sets.length; j < e; j++) {
|
|
1976
|
+
sets.push(sets[j].concat(array[i]));
|
|
1977
|
+
}
|
|
1978
|
+
}
|
|
1979
|
+
return sets.sort((a, b) => b.length - a.length);
|
|
1980
|
+
}
|
|
1981
|
+
/// This function is used to add a set of tags to a language syntax
|
|
1982
|
+
/// via [`NodeSet.extend`](#common.NodeSet.extend) or
|
|
1983
|
+
/// [`LRParser.configure`](#lr.LRParser.configure).
|
|
1984
|
+
///
|
|
1985
|
+
/// The argument object maps node selectors to [highlighting
|
|
1986
|
+
/// tags](#highlight.Tag) or arrays of tags.
|
|
1987
|
+
///
|
|
1988
|
+
/// Node selectors may hold one or more (space-separated) node paths.
|
|
1989
|
+
/// Such a path can be a [node name](#common.NodeType.name), or
|
|
1990
|
+
/// multiple node names (or `*` wildcards) separated by slash
|
|
1991
|
+
/// characters, as in `"Block/Declaration/VariableName"`. Such a path
|
|
1992
|
+
/// matches the final node but only if its direct parent nodes are the
|
|
1993
|
+
/// other nodes mentioned. A `*` in such a path matches any parent,
|
|
1994
|
+
/// but only a single level—wildcards that match multiple parents
|
|
1995
|
+
/// aren't supported, both for efficiency reasons and because Lezer
|
|
1996
|
+
/// trees make it rather hard to reason about what they would match.)
|
|
1997
|
+
///
|
|
1998
|
+
/// A path can be ended with `/...` to indicate that the tag assigned
|
|
1999
|
+
/// to the node should also apply to all child nodes, even if they
|
|
2000
|
+
/// match their own style (by default, only the innermost style is
|
|
2001
|
+
/// used).
|
|
2002
|
+
///
|
|
2003
|
+
/// When a path ends in `!`, as in `Attribute!`, no further matching
|
|
2004
|
+
/// happens for the node's child nodes, and the entire node gets the
|
|
2005
|
+
/// given style.
|
|
2006
|
+
///
|
|
2007
|
+
/// In this notation, node names that contain `/`, `!`, `*`, or `...`
|
|
2008
|
+
/// must be quoted as JSON strings.
|
|
2009
|
+
///
|
|
2010
|
+
/// For example:
|
|
2011
|
+
///
|
|
2012
|
+
/// ```javascript
|
|
2013
|
+
/// parser.withProps(
|
|
2014
|
+
/// styleTags({
|
|
2015
|
+
/// // Style Number and BigNumber nodes
|
|
2016
|
+
/// "Number BigNumber": tags.number,
|
|
2017
|
+
/// // Style Escape nodes whose parent is String
|
|
2018
|
+
/// "String/Escape": tags.escape,
|
|
2019
|
+
/// // Style anything inside Attributes nodes
|
|
2020
|
+
/// "Attributes!": tags.meta,
|
|
2021
|
+
/// // Add a style to all content inside Italic nodes
|
|
2022
|
+
/// "Italic/...": tags.emphasis,
|
|
2023
|
+
/// // Style InvalidString nodes as both `string` and `invalid`
|
|
2024
|
+
/// "InvalidString": [tags.string, tags.invalid],
|
|
2025
|
+
/// // Style the node named "/" as punctuation
|
|
2026
|
+
/// '"/"': tags.punctuation
|
|
2027
|
+
/// })
|
|
2028
|
+
/// )
|
|
2029
|
+
/// ```
|
|
2030
|
+
function styleTags(spec) {
|
|
2031
|
+
let byName = Object.create(null);
|
|
2032
|
+
for (let prop in spec) {
|
|
2033
|
+
let tags = spec[prop];
|
|
2034
|
+
if (!Array.isArray(tags)) tags = [tags];
|
|
2035
|
+
for (let part of prop.split(" ")) if (part) {
|
|
2036
|
+
let pieces = [],
|
|
2037
|
+
mode = 2 /* Mode.Normal */,
|
|
2038
|
+
rest = part;
|
|
2039
|
+
for (let pos = 0;;) {
|
|
2040
|
+
if (rest == "..." && pos > 0 && pos + 3 == part.length) {
|
|
2041
|
+
mode = 1 /* Mode.Inherit */;
|
|
2042
|
+
break;
|
|
2043
|
+
}
|
|
2044
|
+
let m = /^"(?:[^"\\]|\\.)*?"|[^\/!]+/.exec(rest);
|
|
2045
|
+
if (!m) throw new RangeError("Invalid path: " + part);
|
|
2046
|
+
pieces.push(m[0] == "*" ? "" : m[0][0] == '"' ? JSON.parse(m[0]) : m[0]);
|
|
2047
|
+
pos += m[0].length;
|
|
2048
|
+
if (pos == part.length) break;
|
|
2049
|
+
let next = part[pos++];
|
|
2050
|
+
if (pos == part.length && next == "!") {
|
|
2051
|
+
mode = 0 /* Mode.Opaque */;
|
|
2052
|
+
break;
|
|
2053
|
+
}
|
|
2054
|
+
if (next != "/") throw new RangeError("Invalid path: " + part);
|
|
2055
|
+
rest = part.slice(pos);
|
|
2056
|
+
}
|
|
2057
|
+
let last = pieces.length - 1,
|
|
2058
|
+
inner = pieces[last];
|
|
2059
|
+
if (!inner) throw new RangeError("Invalid path: " + part);
|
|
2060
|
+
let rule = new Rule(tags, mode, last > 0 ? pieces.slice(0, last) : null);
|
|
2061
|
+
byName[inner] = rule.sort(byName[inner]);
|
|
2062
|
+
}
|
|
2063
|
+
}
|
|
2064
|
+
return ruleNodeProp.add(byName);
|
|
2065
|
+
}
|
|
2066
|
+
const ruleNodeProp = new NodeProp();
|
|
2067
|
+
class Rule {
|
|
2068
|
+
constructor(tags, mode, context, next) {
|
|
2069
|
+
this.tags = tags;
|
|
2070
|
+
this.mode = mode;
|
|
2071
|
+
this.context = context;
|
|
2072
|
+
this.next = next;
|
|
2073
|
+
}
|
|
2074
|
+
get opaque() {
|
|
2075
|
+
return this.mode == 0 /* Mode.Opaque */;
|
|
2076
|
+
}
|
|
2077
|
+
get inherit() {
|
|
2078
|
+
return this.mode == 1 /* Mode.Inherit */;
|
|
2079
|
+
}
|
|
2080
|
+
sort(other) {
|
|
2081
|
+
if (!other || other.depth < this.depth) {
|
|
2082
|
+
this.next = other;
|
|
2083
|
+
return this;
|
|
2084
|
+
}
|
|
2085
|
+
other.next = this.sort(other.next);
|
|
2086
|
+
return other;
|
|
2087
|
+
}
|
|
2088
|
+
get depth() {
|
|
2089
|
+
return this.context ? this.context.length : 0;
|
|
2090
|
+
}
|
|
2091
|
+
}
|
|
2092
|
+
Rule.empty = new Rule([], 2 /* Mode.Normal */, null);
|
|
2093
|
+
/// Define a [highlighter](#highlight.Highlighter) from an array of
|
|
2094
|
+
/// tag/class pairs. Classes associated with more specific tags will
|
|
2095
|
+
/// take precedence.
|
|
2096
|
+
function tagHighlighter(tags, options) {
|
|
2097
|
+
let map = Object.create(null);
|
|
2098
|
+
for (let style of tags) {
|
|
2099
|
+
if (!Array.isArray(style.tag)) map[style.tag.id] = style.class;else for (let tag of style.tag) map[tag.id] = style.class;
|
|
2100
|
+
}
|
|
2101
|
+
let {
|
|
2102
|
+
scope,
|
|
2103
|
+
all = null
|
|
2104
|
+
} = options || {};
|
|
2105
|
+
return {
|
|
2106
|
+
style: tags => {
|
|
2107
|
+
let cls = all;
|
|
2108
|
+
for (let tag of tags) {
|
|
2109
|
+
for (let sub of tag.set) {
|
|
2110
|
+
let tagClass = map[sub.id];
|
|
2111
|
+
if (tagClass) {
|
|
2112
|
+
cls = cls ? cls + " " + tagClass : tagClass;
|
|
2113
|
+
break;
|
|
2114
|
+
}
|
|
2115
|
+
}
|
|
2116
|
+
}
|
|
2117
|
+
return cls;
|
|
2118
|
+
},
|
|
2119
|
+
scope
|
|
2120
|
+
};
|
|
2121
|
+
}
|
|
2122
|
+
function highlightTags(highlighters, tags) {
|
|
2123
|
+
let result = null;
|
|
2124
|
+
for (let highlighter of highlighters) {
|
|
2125
|
+
let value = highlighter.style(tags);
|
|
2126
|
+
if (value) result = result ? result + " " + value : value;
|
|
2127
|
+
}
|
|
2128
|
+
return result;
|
|
2129
|
+
}
|
|
2130
|
+
/// Highlight the given [tree](#common.Tree) with the given
|
|
2131
|
+
/// [highlighter](#highlight.Highlighter).
|
|
2132
|
+
function highlightTree(tree, highlighter,
|
|
2133
|
+
/// Assign styling to a region of the text. Will be called, in order
|
|
2134
|
+
/// of position, for any ranges where more than zero classes apply.
|
|
2135
|
+
/// `classes` is a space separated string of CSS classes.
|
|
2136
|
+
putStyle,
|
|
2137
|
+
/// The start of the range to highlight.
|
|
2138
|
+
from = 0,
|
|
2139
|
+
/// The end of the range.
|
|
2140
|
+
to = tree.length) {
|
|
2141
|
+
let builder = new HighlightBuilder(from, Array.isArray(highlighter) ? highlighter : [highlighter], putStyle);
|
|
2142
|
+
builder.highlightRange(tree.cursor(), from, to, "", builder.highlighters);
|
|
2143
|
+
builder.flush(to);
|
|
2144
|
+
}
|
|
2145
|
+
class HighlightBuilder {
|
|
2146
|
+
constructor(at, highlighters, span) {
|
|
2147
|
+
this.at = at;
|
|
2148
|
+
this.highlighters = highlighters;
|
|
2149
|
+
this.span = span;
|
|
2150
|
+
this.class = "";
|
|
2151
|
+
}
|
|
2152
|
+
startSpan(at, cls) {
|
|
2153
|
+
if (cls != this.class) {
|
|
2154
|
+
this.flush(at);
|
|
2155
|
+
if (at > this.at) this.at = at;
|
|
2156
|
+
this.class = cls;
|
|
2157
|
+
}
|
|
2158
|
+
}
|
|
2159
|
+
flush(to) {
|
|
2160
|
+
if (to > this.at && this.class) this.span(this.at, to, this.class);
|
|
2161
|
+
}
|
|
2162
|
+
highlightRange(cursor, from, to, inheritedClass, highlighters) {
|
|
2163
|
+
let {
|
|
2164
|
+
type,
|
|
2165
|
+
from: start,
|
|
2166
|
+
to: end
|
|
2167
|
+
} = cursor;
|
|
2168
|
+
if (start >= to || end <= from) return;
|
|
2169
|
+
if (type.isTop) highlighters = this.highlighters.filter(h => !h.scope || h.scope(type));
|
|
2170
|
+
let cls = inheritedClass;
|
|
2171
|
+
let rule = getStyleTags(cursor) || Rule.empty;
|
|
2172
|
+
let tagCls = highlightTags(highlighters, rule.tags);
|
|
2173
|
+
if (tagCls) {
|
|
2174
|
+
if (cls) cls += " ";
|
|
2175
|
+
cls += tagCls;
|
|
2176
|
+
if (rule.mode == 1 /* Mode.Inherit */) inheritedClass += (inheritedClass ? " " : "") + tagCls;
|
|
2177
|
+
}
|
|
2178
|
+
this.startSpan(cursor.from, cls);
|
|
2179
|
+
if (rule.opaque) return;
|
|
2180
|
+
let mounted = cursor.tree && cursor.tree.prop(NodeProp.mounted);
|
|
2181
|
+
if (mounted && mounted.overlay) {
|
|
2182
|
+
let inner = cursor.node.enter(mounted.overlay[0].from + start, 1);
|
|
2183
|
+
let innerHighlighters = this.highlighters.filter(h => !h.scope || h.scope(mounted.tree.type));
|
|
2184
|
+
let hasChild = cursor.firstChild();
|
|
2185
|
+
for (let i = 0, pos = start;; i++) {
|
|
2186
|
+
let next = i < mounted.overlay.length ? mounted.overlay[i] : null;
|
|
2187
|
+
let nextPos = next ? next.from + start : end;
|
|
2188
|
+
let rangeFrom = Math.max(from, pos),
|
|
2189
|
+
rangeTo = Math.min(to, nextPos);
|
|
2190
|
+
if (rangeFrom < rangeTo && hasChild) {
|
|
2191
|
+
while (cursor.from < rangeTo) {
|
|
2192
|
+
this.highlightRange(cursor, rangeFrom, rangeTo, inheritedClass, highlighters);
|
|
2193
|
+
this.startSpan(Math.min(rangeTo, cursor.to), cls);
|
|
2194
|
+
if (cursor.to >= nextPos || !cursor.nextSibling()) break;
|
|
2195
|
+
}
|
|
2196
|
+
}
|
|
2197
|
+
if (!next || nextPos > to) break;
|
|
2198
|
+
pos = next.to + start;
|
|
2199
|
+
if (pos > from) {
|
|
2200
|
+
this.highlightRange(inner.cursor(), Math.max(from, next.from + start), Math.min(to, pos), inheritedClass, innerHighlighters);
|
|
2201
|
+
this.startSpan(pos, cls);
|
|
2202
|
+
}
|
|
2203
|
+
}
|
|
2204
|
+
if (hasChild) cursor.parent();
|
|
2205
|
+
} else if (cursor.firstChild()) {
|
|
2206
|
+
do {
|
|
2207
|
+
if (cursor.to <= from) continue;
|
|
2208
|
+
if (cursor.from >= to) break;
|
|
2209
|
+
this.highlightRange(cursor, from, to, inheritedClass, highlighters);
|
|
2210
|
+
this.startSpan(Math.min(to, cursor.to), cls);
|
|
2211
|
+
} while (cursor.nextSibling());
|
|
2212
|
+
cursor.parent();
|
|
2213
|
+
}
|
|
2214
|
+
}
|
|
2215
|
+
}
|
|
2216
|
+
/// Match a syntax node's [highlight rules](#highlight.styleTags). If
|
|
2217
|
+
/// there's a match, return its set of tags, and whether it is
|
|
2218
|
+
/// opaque (uses a `!`) or applies to all child nodes (`/...`).
|
|
2219
|
+
function getStyleTags(node) {
|
|
2220
|
+
let rule = node.type.prop(ruleNodeProp);
|
|
2221
|
+
while (rule && rule.context && !node.matchContext(rule.context)) rule = rule.next;
|
|
2222
|
+
return rule || null;
|
|
2223
|
+
}
|
|
2224
|
+
const t = Tag.define;
|
|
2225
|
+
const comment = t(),
|
|
2226
|
+
name = t(),
|
|
2227
|
+
typeName = t(name),
|
|
2228
|
+
propertyName = t(name),
|
|
2229
|
+
literal = t(),
|
|
2230
|
+
string = t(literal),
|
|
2231
|
+
number = t(literal),
|
|
2232
|
+
content = t(),
|
|
2233
|
+
heading = t(content),
|
|
2234
|
+
keyword = t(),
|
|
2235
|
+
operator = t(),
|
|
2236
|
+
punctuation = t(),
|
|
2237
|
+
bracket = t(punctuation),
|
|
2238
|
+
meta = t();
|
|
2239
|
+
/// The default set of highlighting [tags](#highlight.Tag).
|
|
2240
|
+
///
|
|
2241
|
+
/// This collection is heavily biased towards programming languages,
|
|
2242
|
+
/// and necessarily incomplete. A full ontology of syntactic
|
|
2243
|
+
/// constructs would fill a stack of books, and be impractical to
|
|
2244
|
+
/// write themes for. So try to make do with this set. If all else
|
|
2245
|
+
/// fails, [open an
|
|
2246
|
+
/// issue](https://github.com/codemirror/codemirror.next) to propose a
|
|
2247
|
+
/// new tag, or [define](#highlight.Tag^define) a local custom tag for
|
|
2248
|
+
/// your use case.
|
|
2249
|
+
///
|
|
2250
|
+
/// Note that it is not obligatory to always attach the most specific
|
|
2251
|
+
/// tag possible to an element—if your grammar can't easily
|
|
2252
|
+
/// distinguish a certain type of element (such as a local variable),
|
|
2253
|
+
/// it is okay to style it as its more general variant (a variable).
|
|
2254
|
+
///
|
|
2255
|
+
/// For tags that extend some parent tag, the documentation links to
|
|
2256
|
+
/// the parent.
|
|
2257
|
+
const tags = {
|
|
2258
|
+
/// A comment.
|
|
2259
|
+
comment,
|
|
2260
|
+
/// A line [comment](#highlight.tags.comment).
|
|
2261
|
+
lineComment: t(comment),
|
|
2262
|
+
/// A block [comment](#highlight.tags.comment).
|
|
2263
|
+
blockComment: t(comment),
|
|
2264
|
+
/// A documentation [comment](#highlight.tags.comment).
|
|
2265
|
+
docComment: t(comment),
|
|
2266
|
+
/// Any kind of identifier.
|
|
2267
|
+
name,
|
|
2268
|
+
/// The [name](#highlight.tags.name) of a variable.
|
|
2269
|
+
variableName: t(name),
|
|
2270
|
+
/// A type [name](#highlight.tags.name).
|
|
2271
|
+
typeName: typeName,
|
|
2272
|
+
/// A tag name (subtag of [`typeName`](#highlight.tags.typeName)).
|
|
2273
|
+
tagName: t(typeName),
|
|
2274
|
+
/// A property or field [name](#highlight.tags.name).
|
|
2275
|
+
propertyName: propertyName,
|
|
2276
|
+
/// An attribute name (subtag of [`propertyName`](#highlight.tags.propertyName)).
|
|
2277
|
+
attributeName: t(propertyName),
|
|
2278
|
+
/// The [name](#highlight.tags.name) of a class.
|
|
2279
|
+
className: t(name),
|
|
2280
|
+
/// A label [name](#highlight.tags.name).
|
|
2281
|
+
labelName: t(name),
|
|
2282
|
+
/// A namespace [name](#highlight.tags.name).
|
|
2283
|
+
namespace: t(name),
|
|
2284
|
+
/// The [name](#highlight.tags.name) of a macro.
|
|
2285
|
+
macroName: t(name),
|
|
2286
|
+
/// A literal value.
|
|
2287
|
+
literal,
|
|
2288
|
+
/// A string [literal](#highlight.tags.literal).
|
|
2289
|
+
string,
|
|
2290
|
+
/// A documentation [string](#highlight.tags.string).
|
|
2291
|
+
docString: t(string),
|
|
2292
|
+
/// A character literal (subtag of [string](#highlight.tags.string)).
|
|
2293
|
+
character: t(string),
|
|
2294
|
+
/// An attribute value (subtag of [string](#highlight.tags.string)).
|
|
2295
|
+
attributeValue: t(string),
|
|
2296
|
+
/// A number [literal](#highlight.tags.literal).
|
|
2297
|
+
number,
|
|
2298
|
+
/// An integer [number](#highlight.tags.number) literal.
|
|
2299
|
+
integer: t(number),
|
|
2300
|
+
/// A floating-point [number](#highlight.tags.number) literal.
|
|
2301
|
+
float: t(number),
|
|
2302
|
+
/// A boolean [literal](#highlight.tags.literal).
|
|
2303
|
+
bool: t(literal),
|
|
2304
|
+
/// Regular expression [literal](#highlight.tags.literal).
|
|
2305
|
+
regexp: t(literal),
|
|
2306
|
+
/// An escape [literal](#highlight.tags.literal), for example a
|
|
2307
|
+
/// backslash escape in a string.
|
|
2308
|
+
escape: t(literal),
|
|
2309
|
+
/// A color [literal](#highlight.tags.literal).
|
|
2310
|
+
color: t(literal),
|
|
2311
|
+
/// A URL [literal](#highlight.tags.literal).
|
|
2312
|
+
url: t(literal),
|
|
2313
|
+
/// A language keyword.
|
|
2314
|
+
keyword,
|
|
2315
|
+
/// The [keyword](#highlight.tags.keyword) for the self or this
|
|
2316
|
+
/// object.
|
|
2317
|
+
self: t(keyword),
|
|
2318
|
+
/// The [keyword](#highlight.tags.keyword) for null.
|
|
2319
|
+
null: t(keyword),
|
|
2320
|
+
/// A [keyword](#highlight.tags.keyword) denoting some atomic value.
|
|
2321
|
+
atom: t(keyword),
|
|
2322
|
+
/// A [keyword](#highlight.tags.keyword) that represents a unit.
|
|
2323
|
+
unit: t(keyword),
|
|
2324
|
+
/// A modifier [keyword](#highlight.tags.keyword).
|
|
2325
|
+
modifier: t(keyword),
|
|
2326
|
+
/// A [keyword](#highlight.tags.keyword) that acts as an operator.
|
|
2327
|
+
operatorKeyword: t(keyword),
|
|
2328
|
+
/// A control-flow related [keyword](#highlight.tags.keyword).
|
|
2329
|
+
controlKeyword: t(keyword),
|
|
2330
|
+
/// A [keyword](#highlight.tags.keyword) that defines something.
|
|
2331
|
+
definitionKeyword: t(keyword),
|
|
2332
|
+
/// A [keyword](#highlight.tags.keyword) related to defining or
|
|
2333
|
+
/// interfacing with modules.
|
|
2334
|
+
moduleKeyword: t(keyword),
|
|
2335
|
+
/// An operator.
|
|
2336
|
+
operator,
|
|
2337
|
+
/// An [operator](#highlight.tags.operator) that dereferences something.
|
|
2338
|
+
derefOperator: t(operator),
|
|
2339
|
+
/// Arithmetic-related [operator](#highlight.tags.operator).
|
|
2340
|
+
arithmeticOperator: t(operator),
|
|
2341
|
+
/// Logical [operator](#highlight.tags.operator).
|
|
2342
|
+
logicOperator: t(operator),
|
|
2343
|
+
/// Bit [operator](#highlight.tags.operator).
|
|
2344
|
+
bitwiseOperator: t(operator),
|
|
2345
|
+
/// Comparison [operator](#highlight.tags.operator).
|
|
2346
|
+
compareOperator: t(operator),
|
|
2347
|
+
/// [Operator](#highlight.tags.operator) that updates its operand.
|
|
2348
|
+
updateOperator: t(operator),
|
|
2349
|
+
/// [Operator](#highlight.tags.operator) that defines something.
|
|
2350
|
+
definitionOperator: t(operator),
|
|
2351
|
+
/// Type-related [operator](#highlight.tags.operator).
|
|
2352
|
+
typeOperator: t(operator),
|
|
2353
|
+
/// Control-flow [operator](#highlight.tags.operator).
|
|
2354
|
+
controlOperator: t(operator),
|
|
2355
|
+
/// Program or markup punctuation.
|
|
2356
|
+
punctuation,
|
|
2357
|
+
/// [Punctuation](#highlight.tags.punctuation) that separates
|
|
2358
|
+
/// things.
|
|
2359
|
+
separator: t(punctuation),
|
|
2360
|
+
/// Bracket-style [punctuation](#highlight.tags.punctuation).
|
|
2361
|
+
bracket,
|
|
2362
|
+
/// Angle [brackets](#highlight.tags.bracket) (usually `<` and `>`
|
|
2363
|
+
/// tokens).
|
|
2364
|
+
angleBracket: t(bracket),
|
|
2365
|
+
/// Square [brackets](#highlight.tags.bracket) (usually `[` and `]`
|
|
2366
|
+
/// tokens).
|
|
2367
|
+
squareBracket: t(bracket),
|
|
2368
|
+
/// Parentheses (usually `(` and `)` tokens). Subtag of
|
|
2369
|
+
/// [bracket](#highlight.tags.bracket).
|
|
2370
|
+
paren: t(bracket),
|
|
2371
|
+
/// Braces (usually `{` and `}` tokens). Subtag of
|
|
2372
|
+
/// [bracket](#highlight.tags.bracket).
|
|
2373
|
+
brace: t(bracket),
|
|
2374
|
+
/// Content, for example plain text in XML or markup documents.
|
|
2375
|
+
content,
|
|
2376
|
+
/// [Content](#highlight.tags.content) that represents a heading.
|
|
2377
|
+
heading,
|
|
2378
|
+
/// A level 1 [heading](#highlight.tags.heading).
|
|
2379
|
+
heading1: t(heading),
|
|
2380
|
+
/// A level 2 [heading](#highlight.tags.heading).
|
|
2381
|
+
heading2: t(heading),
|
|
2382
|
+
/// A level 3 [heading](#highlight.tags.heading).
|
|
2383
|
+
heading3: t(heading),
|
|
2384
|
+
/// A level 4 [heading](#highlight.tags.heading).
|
|
2385
|
+
heading4: t(heading),
|
|
2386
|
+
/// A level 5 [heading](#highlight.tags.heading).
|
|
2387
|
+
heading5: t(heading),
|
|
2388
|
+
/// A level 6 [heading](#highlight.tags.heading).
|
|
2389
|
+
heading6: t(heading),
|
|
2390
|
+
/// A prose separator (such as a horizontal rule).
|
|
2391
|
+
contentSeparator: t(content),
|
|
2392
|
+
/// [Content](#highlight.tags.content) that represents a list.
|
|
2393
|
+
list: t(content),
|
|
2394
|
+
/// [Content](#highlight.tags.content) that represents a quote.
|
|
2395
|
+
quote: t(content),
|
|
2396
|
+
/// [Content](#highlight.tags.content) that is emphasized.
|
|
2397
|
+
emphasis: t(content),
|
|
2398
|
+
/// [Content](#highlight.tags.content) that is styled strong.
|
|
2399
|
+
strong: t(content),
|
|
2400
|
+
/// [Content](#highlight.tags.content) that is part of a link.
|
|
2401
|
+
link: t(content),
|
|
2402
|
+
/// [Content](#highlight.tags.content) that is styled as code or
|
|
2403
|
+
/// monospace.
|
|
2404
|
+
monospace: t(content),
|
|
2405
|
+
/// [Content](#highlight.tags.content) that has a strike-through
|
|
2406
|
+
/// style.
|
|
2407
|
+
strikethrough: t(content),
|
|
2408
|
+
/// Inserted text in a change-tracking format.
|
|
2409
|
+
inserted: t(),
|
|
2410
|
+
/// Deleted text.
|
|
2411
|
+
deleted: t(),
|
|
2412
|
+
/// Changed text.
|
|
2413
|
+
changed: t(),
|
|
2414
|
+
/// An invalid or unsyntactic element.
|
|
2415
|
+
invalid: t(),
|
|
2416
|
+
/// Metadata or meta-instruction.
|
|
2417
|
+
meta,
|
|
2418
|
+
/// [Metadata](#highlight.tags.meta) that applies to the entire
|
|
2419
|
+
/// document.
|
|
2420
|
+
documentMeta: t(meta),
|
|
2421
|
+
/// [Metadata](#highlight.tags.meta) that annotates or adds
|
|
2422
|
+
/// attributes to a given syntactic element.
|
|
2423
|
+
annotation: t(meta),
|
|
2424
|
+
/// Processing instruction or preprocessor directive. Subtag of
|
|
2425
|
+
/// [meta](#highlight.tags.meta).
|
|
2426
|
+
processingInstruction: t(meta),
|
|
2427
|
+
/// [Modifier](#highlight.Tag^defineModifier) that indicates that a
|
|
2428
|
+
/// given element is being defined. Expected to be used with the
|
|
2429
|
+
/// various [name](#highlight.tags.name) tags.
|
|
2430
|
+
definition: Tag.defineModifier(),
|
|
2431
|
+
/// [Modifier](#highlight.Tag^defineModifier) that indicates that
|
|
2432
|
+
/// something is constant. Mostly expected to be used with
|
|
2433
|
+
/// [variable names](#highlight.tags.variableName).
|
|
2434
|
+
constant: Tag.defineModifier(),
|
|
2435
|
+
/// [Modifier](#highlight.Tag^defineModifier) used to indicate that
|
|
2436
|
+
/// a [variable](#highlight.tags.variableName) or [property
|
|
2437
|
+
/// name](#highlight.tags.propertyName) is being called or defined
|
|
2438
|
+
/// as a function.
|
|
2439
|
+
function: Tag.defineModifier(),
|
|
2440
|
+
/// [Modifier](#highlight.Tag^defineModifier) that can be applied to
|
|
2441
|
+
/// [names](#highlight.tags.name) to indicate that they belong to
|
|
2442
|
+
/// the language's standard environment.
|
|
2443
|
+
standard: Tag.defineModifier(),
|
|
2444
|
+
/// [Modifier](#highlight.Tag^defineModifier) that indicates a given
|
|
2445
|
+
/// [names](#highlight.tags.name) is local to some scope.
|
|
2446
|
+
local: Tag.defineModifier(),
|
|
2447
|
+
/// A generic variant [modifier](#highlight.Tag^defineModifier) that
|
|
2448
|
+
/// can be used to tag language-specific alternative variants of
|
|
2449
|
+
/// some common tag. It is recommended for themes to define special
|
|
2450
|
+
/// forms of at least the [string](#highlight.tags.string) and
|
|
2451
|
+
/// [variable name](#highlight.tags.variableName) tags, since those
|
|
2452
|
+
/// come up a lot.
|
|
2453
|
+
special: Tag.defineModifier()
|
|
2454
|
+
};
|
|
2455
|
+
/// This is a highlighter that adds stable, predictable classes to
|
|
2456
|
+
/// tokens, for styling with external CSS.
|
|
2457
|
+
///
|
|
2458
|
+
/// The following tags are mapped to their name prefixed with `"tok-"`
|
|
2459
|
+
/// (for example `"tok-comment"`):
|
|
2460
|
+
///
|
|
2461
|
+
/// * [`link`](#highlight.tags.link)
|
|
2462
|
+
/// * [`heading`](#highlight.tags.heading)
|
|
2463
|
+
/// * [`emphasis`](#highlight.tags.emphasis)
|
|
2464
|
+
/// * [`strong`](#highlight.tags.strong)
|
|
2465
|
+
/// * [`keyword`](#highlight.tags.keyword)
|
|
2466
|
+
/// * [`atom`](#highlight.tags.atom)
|
|
2467
|
+
/// * [`bool`](#highlight.tags.bool)
|
|
2468
|
+
/// * [`url`](#highlight.tags.url)
|
|
2469
|
+
/// * [`labelName`](#highlight.tags.labelName)
|
|
2470
|
+
/// * [`inserted`](#highlight.tags.inserted)
|
|
2471
|
+
/// * [`deleted`](#highlight.tags.deleted)
|
|
2472
|
+
/// * [`literal`](#highlight.tags.literal)
|
|
2473
|
+
/// * [`string`](#highlight.tags.string)
|
|
2474
|
+
/// * [`number`](#highlight.tags.number)
|
|
2475
|
+
/// * [`variableName`](#highlight.tags.variableName)
|
|
2476
|
+
/// * [`typeName`](#highlight.tags.typeName)
|
|
2477
|
+
/// * [`namespace`](#highlight.tags.namespace)
|
|
2478
|
+
/// * [`className`](#highlight.tags.className)
|
|
2479
|
+
/// * [`macroName`](#highlight.tags.macroName)
|
|
2480
|
+
/// * [`propertyName`](#highlight.tags.propertyName)
|
|
2481
|
+
/// * [`operator`](#highlight.tags.operator)
|
|
2482
|
+
/// * [`comment`](#highlight.tags.comment)
|
|
2483
|
+
/// * [`meta`](#highlight.tags.meta)
|
|
2484
|
+
/// * [`punctuation`](#highlight.tags.punctuation)
|
|
2485
|
+
/// * [`invalid`](#highlight.tags.invalid)
|
|
2486
|
+
///
|
|
2487
|
+
/// In addition, these mappings are provided:
|
|
2488
|
+
///
|
|
2489
|
+
/// * [`regexp`](#highlight.tags.regexp),
|
|
2490
|
+
/// [`escape`](#highlight.tags.escape), and
|
|
2491
|
+
/// [`special`](#highlight.tags.special)[`(string)`](#highlight.tags.string)
|
|
2492
|
+
/// are mapped to `"tok-string2"`
|
|
2493
|
+
/// * [`special`](#highlight.tags.special)[`(variableName)`](#highlight.tags.variableName)
|
|
2494
|
+
/// to `"tok-variableName2"`
|
|
2495
|
+
/// * [`local`](#highlight.tags.local)[`(variableName)`](#highlight.tags.variableName)
|
|
2496
|
+
/// to `"tok-variableName tok-local"`
|
|
2497
|
+
/// * [`definition`](#highlight.tags.definition)[`(variableName)`](#highlight.tags.variableName)
|
|
2498
|
+
/// to `"tok-variableName tok-definition"`
|
|
2499
|
+
/// * [`definition`](#highlight.tags.definition)[`(propertyName)`](#highlight.tags.propertyName)
|
|
2500
|
+
/// to `"tok-propertyName tok-definition"`
|
|
2501
|
+
tagHighlighter([{
|
|
2502
|
+
tag: tags.link,
|
|
2503
|
+
class: "tok-link"
|
|
2504
|
+
}, {
|
|
2505
|
+
tag: tags.heading,
|
|
2506
|
+
class: "tok-heading"
|
|
2507
|
+
}, {
|
|
2508
|
+
tag: tags.emphasis,
|
|
2509
|
+
class: "tok-emphasis"
|
|
2510
|
+
}, {
|
|
2511
|
+
tag: tags.strong,
|
|
2512
|
+
class: "tok-strong"
|
|
2513
|
+
}, {
|
|
2514
|
+
tag: tags.keyword,
|
|
2515
|
+
class: "tok-keyword"
|
|
2516
|
+
}, {
|
|
2517
|
+
tag: tags.atom,
|
|
2518
|
+
class: "tok-atom"
|
|
2519
|
+
}, {
|
|
2520
|
+
tag: tags.bool,
|
|
2521
|
+
class: "tok-bool"
|
|
2522
|
+
}, {
|
|
2523
|
+
tag: tags.url,
|
|
2524
|
+
class: "tok-url"
|
|
2525
|
+
}, {
|
|
2526
|
+
tag: tags.labelName,
|
|
2527
|
+
class: "tok-labelName"
|
|
2528
|
+
}, {
|
|
2529
|
+
tag: tags.inserted,
|
|
2530
|
+
class: "tok-inserted"
|
|
2531
|
+
}, {
|
|
2532
|
+
tag: tags.deleted,
|
|
2533
|
+
class: "tok-deleted"
|
|
2534
|
+
}, {
|
|
2535
|
+
tag: tags.literal,
|
|
2536
|
+
class: "tok-literal"
|
|
2537
|
+
}, {
|
|
2538
|
+
tag: tags.string,
|
|
2539
|
+
class: "tok-string"
|
|
2540
|
+
}, {
|
|
2541
|
+
tag: tags.number,
|
|
2542
|
+
class: "tok-number"
|
|
2543
|
+
}, {
|
|
2544
|
+
tag: [tags.regexp, tags.escape, tags.special(tags.string)],
|
|
2545
|
+
class: "tok-string2"
|
|
2546
|
+
}, {
|
|
2547
|
+
tag: tags.variableName,
|
|
2548
|
+
class: "tok-variableName"
|
|
2549
|
+
}, {
|
|
2550
|
+
tag: tags.local(tags.variableName),
|
|
2551
|
+
class: "tok-variableName tok-local"
|
|
2552
|
+
}, {
|
|
2553
|
+
tag: tags.definition(tags.variableName),
|
|
2554
|
+
class: "tok-variableName tok-definition"
|
|
2555
|
+
}, {
|
|
2556
|
+
tag: tags.special(tags.variableName),
|
|
2557
|
+
class: "tok-variableName2"
|
|
2558
|
+
}, {
|
|
2559
|
+
tag: tags.definition(tags.propertyName),
|
|
2560
|
+
class: "tok-propertyName tok-definition"
|
|
2561
|
+
}, {
|
|
2562
|
+
tag: tags.typeName,
|
|
2563
|
+
class: "tok-typeName"
|
|
2564
|
+
}, {
|
|
2565
|
+
tag: tags.namespace,
|
|
2566
|
+
class: "tok-namespace"
|
|
2567
|
+
}, {
|
|
2568
|
+
tag: tags.className,
|
|
2569
|
+
class: "tok-className"
|
|
2570
|
+
}, {
|
|
2571
|
+
tag: tags.macroName,
|
|
2572
|
+
class: "tok-macroName"
|
|
2573
|
+
}, {
|
|
2574
|
+
tag: tags.propertyName,
|
|
2575
|
+
class: "tok-propertyName"
|
|
2576
|
+
}, {
|
|
2577
|
+
tag: tags.operator,
|
|
2578
|
+
class: "tok-operator"
|
|
2579
|
+
}, {
|
|
2580
|
+
tag: tags.comment,
|
|
2581
|
+
class: "tok-comment"
|
|
2582
|
+
}, {
|
|
2583
|
+
tag: tags.meta,
|
|
2584
|
+
class: "tok-meta"
|
|
2585
|
+
}, {
|
|
2586
|
+
tag: tags.invalid,
|
|
2587
|
+
class: "tok-invalid"
|
|
2588
|
+
}, {
|
|
2589
|
+
tag: tags.punctuation,
|
|
2590
|
+
class: "tok-punctuation"
|
|
2591
|
+
}]);
|
|
2592
|
+
|
|
2593
|
+
var _a;
|
|
2594
|
+
/**
|
|
2595
|
+
Node prop stored in a parser's top syntax node to provide the
|
|
2596
|
+
facet that stores language-specific data for that language.
|
|
2597
|
+
*/
|
|
2598
|
+
const languageDataProp = /*@__PURE__*/new NodeProp();
|
|
2599
|
+
/**
|
|
2600
|
+
Helper function to define a facet (to be added to the top syntax
|
|
2601
|
+
node(s) for a language via
|
|
2602
|
+
[`languageDataProp`](https://codemirror.net/6/docs/ref/#language.languageDataProp)), that will be
|
|
2603
|
+
used to associate language data with the language. You
|
|
2604
|
+
probably only need this when subclassing
|
|
2605
|
+
[`Language`](https://codemirror.net/6/docs/ref/#language.Language).
|
|
2606
|
+
*/
|
|
2607
|
+
function defineLanguageFacet(baseData) {
|
|
2608
|
+
return Facet.define({
|
|
2609
|
+
combine: baseData ? values => values.concat(baseData) : undefined
|
|
2610
|
+
});
|
|
2611
|
+
}
|
|
2612
|
+
/**
|
|
2613
|
+
A language object manages parsing and per-language
|
|
2614
|
+
[metadata](https://codemirror.net/6/docs/ref/#state.EditorState.languageDataAt). Parse data is
|
|
2615
|
+
managed as a [Lezer](https://lezer.codemirror.net) tree. The class
|
|
2616
|
+
can be used directly, via the [`LRLanguage`](https://codemirror.net/6/docs/ref/#language.LRLanguage)
|
|
2617
|
+
subclass for [Lezer](https://lezer.codemirror.net/) LR parsers, or
|
|
2618
|
+
via the [`StreamLanguage`](https://codemirror.net/6/docs/ref/#language.StreamLanguage) subclass
|
|
2619
|
+
for stream parsers.
|
|
2620
|
+
*/
|
|
2621
|
+
class Language {
|
|
2622
|
+
/**
|
|
2623
|
+
Construct a language object. If you need to invoke this
|
|
2624
|
+
directly, first define a data facet with
|
|
2625
|
+
[`defineLanguageFacet`](https://codemirror.net/6/docs/ref/#language.defineLanguageFacet), and then
|
|
2626
|
+
configure your parser to [attach](https://codemirror.net/6/docs/ref/#language.languageDataProp) it
|
|
2627
|
+
to the language's outer syntax node.
|
|
2628
|
+
*/
|
|
2629
|
+
constructor(
|
|
2630
|
+
/**
|
|
2631
|
+
The [language data](https://codemirror.net/6/docs/ref/#state.EditorState.languageDataAt) facet
|
|
2632
|
+
used for this language.
|
|
2633
|
+
*/
|
|
2634
|
+
data, parser, extraExtensions = [],
|
|
2635
|
+
/**
|
|
2636
|
+
A language name.
|
|
2637
|
+
*/
|
|
2638
|
+
name = "") {
|
|
2639
|
+
this.data = data;
|
|
2640
|
+
this.name = name;
|
|
2641
|
+
// Kludge to define EditorState.tree as a debugging helper,
|
|
2642
|
+
// without the EditorState package actually knowing about
|
|
2643
|
+
// languages and lezer trees.
|
|
2644
|
+
if (!EditorState.prototype.hasOwnProperty("tree")) Object.defineProperty(EditorState.prototype, "tree", {
|
|
2645
|
+
get() {
|
|
2646
|
+
return syntaxTree(this);
|
|
2647
|
+
}
|
|
2648
|
+
});
|
|
2649
|
+
this.parser = parser;
|
|
2650
|
+
this.extension = [language.of(this), EditorState.languageData.of((state, pos, side) => state.facet(languageDataFacetAt(state, pos, side)))].concat(extraExtensions);
|
|
2651
|
+
}
|
|
2652
|
+
/**
|
|
2653
|
+
Query whether this language is active at the given position.
|
|
2654
|
+
*/
|
|
2655
|
+
isActiveAt(state, pos, side = -1) {
|
|
2656
|
+
return languageDataFacetAt(state, pos, side) == this.data;
|
|
2657
|
+
}
|
|
2658
|
+
/**
|
|
2659
|
+
Find the document regions that were parsed using this language.
|
|
2660
|
+
The returned regions will _include_ any nested languages rooted
|
|
2661
|
+
in this language, when those exist.
|
|
2662
|
+
*/
|
|
2663
|
+
findRegions(state) {
|
|
2664
|
+
let lang = state.facet(language);
|
|
2665
|
+
if ((lang === null || lang === void 0 ? void 0 : lang.data) == this.data) return [{
|
|
2666
|
+
from: 0,
|
|
2667
|
+
to: state.doc.length
|
|
2668
|
+
}];
|
|
2669
|
+
if (!lang || !lang.allowsNesting) return [];
|
|
2670
|
+
let result = [];
|
|
2671
|
+
let explore = (tree, from) => {
|
|
2672
|
+
if (tree.prop(languageDataProp) == this.data) {
|
|
2673
|
+
result.push({
|
|
2674
|
+
from,
|
|
2675
|
+
to: from + tree.length
|
|
2676
|
+
});
|
|
2677
|
+
return;
|
|
2678
|
+
}
|
|
2679
|
+
let mount = tree.prop(NodeProp.mounted);
|
|
2680
|
+
if (mount) {
|
|
2681
|
+
if (mount.tree.prop(languageDataProp) == this.data) {
|
|
2682
|
+
if (mount.overlay) for (let r of mount.overlay) result.push({
|
|
2683
|
+
from: r.from + from,
|
|
2684
|
+
to: r.to + from
|
|
2685
|
+
});else result.push({
|
|
2686
|
+
from: from,
|
|
2687
|
+
to: from + tree.length
|
|
2688
|
+
});
|
|
2689
|
+
return;
|
|
2690
|
+
} else if (mount.overlay) {
|
|
2691
|
+
let size = result.length;
|
|
2692
|
+
explore(mount.tree, mount.overlay[0].from + from);
|
|
2693
|
+
if (result.length > size) return;
|
|
2694
|
+
}
|
|
2695
|
+
}
|
|
2696
|
+
for (let i = 0; i < tree.children.length; i++) {
|
|
2697
|
+
let ch = tree.children[i];
|
|
2698
|
+
if (ch instanceof Tree) explore(ch, tree.positions[i] + from);
|
|
2699
|
+
}
|
|
2700
|
+
};
|
|
2701
|
+
explore(syntaxTree(state), 0);
|
|
2702
|
+
return result;
|
|
2703
|
+
}
|
|
2704
|
+
/**
|
|
2705
|
+
Indicates whether this language allows nested languages. The
|
|
2706
|
+
default implementation returns true.
|
|
2707
|
+
*/
|
|
2708
|
+
get allowsNesting() {
|
|
2709
|
+
return true;
|
|
2710
|
+
}
|
|
2711
|
+
}
|
|
2712
|
+
/**
|
|
2713
|
+
@internal
|
|
2714
|
+
*/
|
|
2715
|
+
Language.setState = /*@__PURE__*/StateEffect.define();
|
|
2716
|
+
function languageDataFacetAt(state, pos, side) {
|
|
2717
|
+
let topLang = state.facet(language);
|
|
2718
|
+
if (!topLang) return null;
|
|
2719
|
+
let facet = topLang.data;
|
|
2720
|
+
if (topLang.allowsNesting) {
|
|
2721
|
+
for (let node = syntaxTree(state).topNode; node; node = node.enter(pos, side, IterMode.ExcludeBuffers)) facet = node.type.prop(languageDataProp) || facet;
|
|
2722
|
+
}
|
|
2723
|
+
return facet;
|
|
2724
|
+
}
|
|
2725
|
+
/**
|
|
2726
|
+
A subclass of [`Language`](https://codemirror.net/6/docs/ref/#language.Language) for use with Lezer
|
|
2727
|
+
[LR parsers](https://lezer.codemirror.net/docs/ref#lr.LRParser)
|
|
2728
|
+
parsers.
|
|
2729
|
+
*/
|
|
2730
|
+
class LRLanguage extends Language {
|
|
2731
|
+
constructor(data, parser, name) {
|
|
2732
|
+
super(data, parser, [], name);
|
|
2733
|
+
this.parser = parser;
|
|
2734
|
+
}
|
|
2735
|
+
/**
|
|
2736
|
+
Define a language from a parser.
|
|
2737
|
+
*/
|
|
2738
|
+
static define(spec) {
|
|
2739
|
+
let data = defineLanguageFacet(spec.languageData);
|
|
2740
|
+
return new LRLanguage(data, spec.parser.configure({
|
|
2741
|
+
props: [languageDataProp.add(type => type.isTop ? data : undefined)]
|
|
2742
|
+
}), spec.name);
|
|
2743
|
+
}
|
|
2744
|
+
/**
|
|
2745
|
+
Create a new instance of this language with a reconfigured
|
|
2746
|
+
version of its parser and optionally a new name.
|
|
2747
|
+
*/
|
|
2748
|
+
configure(options, name) {
|
|
2749
|
+
return new LRLanguage(this.data, this.parser.configure(options), name || this.name);
|
|
2750
|
+
}
|
|
2751
|
+
get allowsNesting() {
|
|
2752
|
+
return this.parser.hasWrappers();
|
|
2753
|
+
}
|
|
2754
|
+
}
|
|
2755
|
+
/**
|
|
2756
|
+
Get the syntax tree for a state, which is the current (possibly
|
|
2757
|
+
incomplete) parse tree of the active
|
|
2758
|
+
[language](https://codemirror.net/6/docs/ref/#language.Language), or the empty tree if there is no
|
|
2759
|
+
language available.
|
|
2760
|
+
*/
|
|
2761
|
+
function syntaxTree(state) {
|
|
2762
|
+
let field = state.field(Language.state, false);
|
|
2763
|
+
return field ? field.tree : Tree.empty;
|
|
2764
|
+
}
|
|
2765
|
+
// Lezer-style Input object for a Text document.
|
|
2766
|
+
class DocInput {
|
|
2767
|
+
constructor(doc, length = doc.length) {
|
|
2768
|
+
this.doc = doc;
|
|
2769
|
+
this.length = length;
|
|
2770
|
+
this.cursorPos = 0;
|
|
2771
|
+
this.string = "";
|
|
2772
|
+
this.cursor = doc.iter();
|
|
2773
|
+
}
|
|
2774
|
+
syncTo(pos) {
|
|
2775
|
+
this.string = this.cursor.next(pos - this.cursorPos).value;
|
|
2776
|
+
this.cursorPos = pos + this.string.length;
|
|
2777
|
+
return this.cursorPos - this.string.length;
|
|
2778
|
+
}
|
|
2779
|
+
chunk(pos) {
|
|
2780
|
+
this.syncTo(pos);
|
|
2781
|
+
return this.string;
|
|
2782
|
+
}
|
|
2783
|
+
get lineChunks() {
|
|
2784
|
+
return true;
|
|
2785
|
+
}
|
|
2786
|
+
read(from, to) {
|
|
2787
|
+
let stringStart = this.cursorPos - this.string.length;
|
|
2788
|
+
if (from < stringStart || to >= this.cursorPos) return this.doc.sliceString(from, to);else return this.string.slice(from - stringStart, to - stringStart);
|
|
2789
|
+
}
|
|
2790
|
+
}
|
|
2791
|
+
let currentContext = null;
|
|
2792
|
+
/**
|
|
2793
|
+
A parse context provided to parsers working on the editor content.
|
|
2794
|
+
*/
|
|
2795
|
+
class ParseContext {
|
|
2796
|
+
constructor(parser,
|
|
2797
|
+
/**
|
|
2798
|
+
The current editor state.
|
|
2799
|
+
*/
|
|
2800
|
+
state,
|
|
2801
|
+
/**
|
|
2802
|
+
Tree fragments that can be reused by incremental re-parses.
|
|
2803
|
+
*/
|
|
2804
|
+
fragments = [],
|
|
2805
|
+
/**
|
|
2806
|
+
@internal
|
|
2807
|
+
*/
|
|
2808
|
+
tree,
|
|
2809
|
+
/**
|
|
2810
|
+
@internal
|
|
2811
|
+
*/
|
|
2812
|
+
treeLen,
|
|
2813
|
+
/**
|
|
2814
|
+
The current editor viewport (or some overapproximation
|
|
2815
|
+
thereof). Intended to be used for opportunistically avoiding
|
|
2816
|
+
work (in which case
|
|
2817
|
+
[`skipUntilInView`](https://codemirror.net/6/docs/ref/#language.ParseContext.skipUntilInView)
|
|
2818
|
+
should be called to make sure the parser is restarted when the
|
|
2819
|
+
skipped region becomes visible).
|
|
2820
|
+
*/
|
|
2821
|
+
viewport,
|
|
2822
|
+
/**
|
|
2823
|
+
@internal
|
|
2824
|
+
*/
|
|
2825
|
+
skipped,
|
|
2826
|
+
/**
|
|
2827
|
+
This is where skipping parsers can register a promise that,
|
|
2828
|
+
when resolved, will schedule a new parse. It is cleared when
|
|
2829
|
+
the parse worker picks up the promise. @internal
|
|
2830
|
+
*/
|
|
2831
|
+
scheduleOn) {
|
|
2832
|
+
this.parser = parser;
|
|
2833
|
+
this.state = state;
|
|
2834
|
+
this.fragments = fragments;
|
|
2835
|
+
this.tree = tree;
|
|
2836
|
+
this.treeLen = treeLen;
|
|
2837
|
+
this.viewport = viewport;
|
|
2838
|
+
this.skipped = skipped;
|
|
2839
|
+
this.scheduleOn = scheduleOn;
|
|
2840
|
+
this.parse = null;
|
|
2841
|
+
/**
|
|
2842
|
+
@internal
|
|
2843
|
+
*/
|
|
2844
|
+
this.tempSkipped = [];
|
|
2845
|
+
}
|
|
2846
|
+
/**
|
|
2847
|
+
@internal
|
|
2848
|
+
*/
|
|
2849
|
+
static create(parser, state, viewport) {
|
|
2850
|
+
return new ParseContext(parser, state, [], Tree.empty, 0, viewport, [], null);
|
|
2851
|
+
}
|
|
2852
|
+
startParse() {
|
|
2853
|
+
return this.parser.startParse(new DocInput(this.state.doc), this.fragments);
|
|
2854
|
+
}
|
|
2855
|
+
/**
|
|
2856
|
+
@internal
|
|
2857
|
+
*/
|
|
2858
|
+
work(until, upto) {
|
|
2859
|
+
if (upto != null && upto >= this.state.doc.length) upto = undefined;
|
|
2860
|
+
if (this.tree != Tree.empty && this.isDone(upto !== null && upto !== void 0 ? upto : this.state.doc.length)) {
|
|
2861
|
+
this.takeTree();
|
|
2862
|
+
return true;
|
|
2863
|
+
}
|
|
2864
|
+
return this.withContext(() => {
|
|
2865
|
+
var _a;
|
|
2866
|
+
if (typeof until == "number") {
|
|
2867
|
+
let endTime = Date.now() + until;
|
|
2868
|
+
until = () => Date.now() > endTime;
|
|
2869
|
+
}
|
|
2870
|
+
if (!this.parse) this.parse = this.startParse();
|
|
2871
|
+
if (upto != null && (this.parse.stoppedAt == null || this.parse.stoppedAt > upto) && upto < this.state.doc.length) this.parse.stopAt(upto);
|
|
2872
|
+
for (;;) {
|
|
2873
|
+
let done = this.parse.advance();
|
|
2874
|
+
if (done) {
|
|
2875
|
+
this.fragments = this.withoutTempSkipped(TreeFragment.addTree(done, this.fragments, this.parse.stoppedAt != null));
|
|
2876
|
+
this.treeLen = (_a = this.parse.stoppedAt) !== null && _a !== void 0 ? _a : this.state.doc.length;
|
|
2877
|
+
this.tree = done;
|
|
2878
|
+
this.parse = null;
|
|
2879
|
+
if (this.treeLen < (upto !== null && upto !== void 0 ? upto : this.state.doc.length)) this.parse = this.startParse();else return true;
|
|
2880
|
+
}
|
|
2881
|
+
if (until()) return false;
|
|
2882
|
+
}
|
|
2883
|
+
});
|
|
2884
|
+
}
|
|
2885
|
+
/**
|
|
2886
|
+
@internal
|
|
2887
|
+
*/
|
|
2888
|
+
takeTree() {
|
|
2889
|
+
let pos, tree;
|
|
2890
|
+
if (this.parse && (pos = this.parse.parsedPos) >= this.treeLen) {
|
|
2891
|
+
if (this.parse.stoppedAt == null || this.parse.stoppedAt > pos) this.parse.stopAt(pos);
|
|
2892
|
+
this.withContext(() => {
|
|
2893
|
+
while (!(tree = this.parse.advance())) {}
|
|
2894
|
+
});
|
|
2895
|
+
this.treeLen = pos;
|
|
2896
|
+
this.tree = tree;
|
|
2897
|
+
this.fragments = this.withoutTempSkipped(TreeFragment.addTree(this.tree, this.fragments, true));
|
|
2898
|
+
this.parse = null;
|
|
2899
|
+
}
|
|
2900
|
+
}
|
|
2901
|
+
withContext(f) {
|
|
2902
|
+
let prev = currentContext;
|
|
2903
|
+
currentContext = this;
|
|
2904
|
+
try {
|
|
2905
|
+
return f();
|
|
2906
|
+
} finally {
|
|
2907
|
+
currentContext = prev;
|
|
2908
|
+
}
|
|
2909
|
+
}
|
|
2910
|
+
withoutTempSkipped(fragments) {
|
|
2911
|
+
for (let r; r = this.tempSkipped.pop();) fragments = cutFragments(fragments, r.from, r.to);
|
|
2912
|
+
return fragments;
|
|
2913
|
+
}
|
|
2914
|
+
/**
|
|
2915
|
+
@internal
|
|
2916
|
+
*/
|
|
2917
|
+
changes(changes, newState) {
|
|
2918
|
+
let {
|
|
2919
|
+
fragments,
|
|
2920
|
+
tree,
|
|
2921
|
+
treeLen,
|
|
2922
|
+
viewport,
|
|
2923
|
+
skipped
|
|
2924
|
+
} = this;
|
|
2925
|
+
this.takeTree();
|
|
2926
|
+
if (!changes.empty) {
|
|
2927
|
+
let ranges = [];
|
|
2928
|
+
changes.iterChangedRanges((fromA, toA, fromB, toB) => ranges.push({
|
|
2929
|
+
fromA,
|
|
2930
|
+
toA,
|
|
2931
|
+
fromB,
|
|
2932
|
+
toB
|
|
2933
|
+
}));
|
|
2934
|
+
fragments = TreeFragment.applyChanges(fragments, ranges);
|
|
2935
|
+
tree = Tree.empty;
|
|
2936
|
+
treeLen = 0;
|
|
2937
|
+
viewport = {
|
|
2938
|
+
from: changes.mapPos(viewport.from, -1),
|
|
2939
|
+
to: changes.mapPos(viewport.to, 1)
|
|
2940
|
+
};
|
|
2941
|
+
if (this.skipped.length) {
|
|
2942
|
+
skipped = [];
|
|
2943
|
+
for (let r of this.skipped) {
|
|
2944
|
+
let from = changes.mapPos(r.from, 1),
|
|
2945
|
+
to = changes.mapPos(r.to, -1);
|
|
2946
|
+
if (from < to) skipped.push({
|
|
2947
|
+
from,
|
|
2948
|
+
to
|
|
2949
|
+
});
|
|
2950
|
+
}
|
|
2951
|
+
}
|
|
2952
|
+
}
|
|
2953
|
+
return new ParseContext(this.parser, newState, fragments, tree, treeLen, viewport, skipped, this.scheduleOn);
|
|
2954
|
+
}
|
|
2955
|
+
/**
|
|
2956
|
+
@internal
|
|
2957
|
+
*/
|
|
2958
|
+
updateViewport(viewport) {
|
|
2959
|
+
if (this.viewport.from == viewport.from && this.viewport.to == viewport.to) return false;
|
|
2960
|
+
this.viewport = viewport;
|
|
2961
|
+
let startLen = this.skipped.length;
|
|
2962
|
+
for (let i = 0; i < this.skipped.length; i++) {
|
|
2963
|
+
let {
|
|
2964
|
+
from,
|
|
2965
|
+
to
|
|
2966
|
+
} = this.skipped[i];
|
|
2967
|
+
if (from < viewport.to && to > viewport.from) {
|
|
2968
|
+
this.fragments = cutFragments(this.fragments, from, to);
|
|
2969
|
+
this.skipped.splice(i--, 1);
|
|
2970
|
+
}
|
|
2971
|
+
}
|
|
2972
|
+
if (this.skipped.length >= startLen) return false;
|
|
2973
|
+
this.reset();
|
|
2974
|
+
return true;
|
|
2975
|
+
}
|
|
2976
|
+
/**
|
|
2977
|
+
@internal
|
|
2978
|
+
*/
|
|
2979
|
+
reset() {
|
|
2980
|
+
if (this.parse) {
|
|
2981
|
+
this.takeTree();
|
|
2982
|
+
this.parse = null;
|
|
2983
|
+
}
|
|
2984
|
+
}
|
|
2985
|
+
/**
|
|
2986
|
+
Notify the parse scheduler that the given region was skipped
|
|
2987
|
+
because it wasn't in view, and the parse should be restarted
|
|
2988
|
+
when it comes into view.
|
|
2989
|
+
*/
|
|
2990
|
+
skipUntilInView(from, to) {
|
|
2991
|
+
this.skipped.push({
|
|
2992
|
+
from,
|
|
2993
|
+
to
|
|
2994
|
+
});
|
|
2995
|
+
}
|
|
2996
|
+
/**
|
|
2997
|
+
Returns a parser intended to be used as placeholder when
|
|
2998
|
+
asynchronously loading a nested parser. It'll skip its input and
|
|
2999
|
+
mark it as not-really-parsed, so that the next update will parse
|
|
3000
|
+
it again.
|
|
3001
|
+
|
|
3002
|
+
When `until` is given, a reparse will be scheduled when that
|
|
3003
|
+
promise resolves.
|
|
3004
|
+
*/
|
|
3005
|
+
static getSkippingParser(until) {
|
|
3006
|
+
return new class extends Parser {
|
|
3007
|
+
createParse(input, fragments, ranges) {
|
|
3008
|
+
let from = ranges[0].from,
|
|
3009
|
+
to = ranges[ranges.length - 1].to;
|
|
3010
|
+
let parser = {
|
|
3011
|
+
parsedPos: from,
|
|
3012
|
+
advance() {
|
|
3013
|
+
let cx = currentContext;
|
|
3014
|
+
if (cx) {
|
|
3015
|
+
for (let r of ranges) cx.tempSkipped.push(r);
|
|
3016
|
+
if (until) cx.scheduleOn = cx.scheduleOn ? Promise.all([cx.scheduleOn, until]) : until;
|
|
3017
|
+
}
|
|
3018
|
+
this.parsedPos = to;
|
|
3019
|
+
return new Tree(NodeType.none, [], [], to - from);
|
|
3020
|
+
},
|
|
3021
|
+
stoppedAt: null,
|
|
3022
|
+
stopAt() {}
|
|
3023
|
+
};
|
|
3024
|
+
return parser;
|
|
3025
|
+
}
|
|
3026
|
+
}();
|
|
3027
|
+
}
|
|
3028
|
+
/**
|
|
3029
|
+
@internal
|
|
3030
|
+
*/
|
|
3031
|
+
isDone(upto) {
|
|
3032
|
+
upto = Math.min(upto, this.state.doc.length);
|
|
3033
|
+
let frags = this.fragments;
|
|
3034
|
+
return this.treeLen >= upto && frags.length && frags[0].from == 0 && frags[0].to >= upto;
|
|
3035
|
+
}
|
|
3036
|
+
/**
|
|
3037
|
+
Get the context for the current parse, or `null` if no editor
|
|
3038
|
+
parse is in progress.
|
|
3039
|
+
*/
|
|
3040
|
+
static get() {
|
|
3041
|
+
return currentContext;
|
|
3042
|
+
}
|
|
3043
|
+
}
|
|
3044
|
+
function cutFragments(fragments, from, to) {
|
|
3045
|
+
return TreeFragment.applyChanges(fragments, [{
|
|
3046
|
+
fromA: from,
|
|
3047
|
+
toA: to,
|
|
3048
|
+
fromB: from,
|
|
3049
|
+
toB: to
|
|
3050
|
+
}]);
|
|
3051
|
+
}
|
|
3052
|
+
class LanguageState {
|
|
3053
|
+
constructor(
|
|
3054
|
+
// A mutable parse state that is used to preserve work done during
|
|
3055
|
+
// the lifetime of a state when moving to the next state.
|
|
3056
|
+
context) {
|
|
3057
|
+
this.context = context;
|
|
3058
|
+
this.tree = context.tree;
|
|
3059
|
+
}
|
|
3060
|
+
apply(tr) {
|
|
3061
|
+
if (!tr.docChanged && this.tree == this.context.tree) return this;
|
|
3062
|
+
let newCx = this.context.changes(tr.changes, tr.state);
|
|
3063
|
+
// If the previous parse wasn't done, go forward only up to its
|
|
3064
|
+
// end position or the end of the viewport, to avoid slowing down
|
|
3065
|
+
// state updates with parse work beyond the viewport.
|
|
3066
|
+
let upto = this.context.treeLen == tr.startState.doc.length ? undefined : Math.max(tr.changes.mapPos(this.context.treeLen), newCx.viewport.to);
|
|
3067
|
+
if (!newCx.work(20 /* Work.Apply */, upto)) newCx.takeTree();
|
|
3068
|
+
return new LanguageState(newCx);
|
|
3069
|
+
}
|
|
3070
|
+
static init(state) {
|
|
3071
|
+
let vpTo = Math.min(3000 /* Work.InitViewport */, state.doc.length);
|
|
3072
|
+
let parseState = ParseContext.create(state.facet(language).parser, state, {
|
|
3073
|
+
from: 0,
|
|
3074
|
+
to: vpTo
|
|
3075
|
+
});
|
|
3076
|
+
if (!parseState.work(20 /* Work.Apply */, vpTo)) parseState.takeTree();
|
|
3077
|
+
return new LanguageState(parseState);
|
|
3078
|
+
}
|
|
3079
|
+
}
|
|
3080
|
+
Language.state = /*@__PURE__*/StateField.define({
|
|
3081
|
+
create: LanguageState.init,
|
|
3082
|
+
update(value, tr) {
|
|
3083
|
+
for (let e of tr.effects) if (e.is(Language.setState)) return e.value;
|
|
3084
|
+
if (tr.startState.facet(language) != tr.state.facet(language)) return LanguageState.init(tr.state);
|
|
3085
|
+
return value.apply(tr);
|
|
3086
|
+
}
|
|
3087
|
+
});
|
|
3088
|
+
let requestIdle = callback => {
|
|
3089
|
+
let timeout = setTimeout(() => callback(), 500 /* Work.MaxPause */);
|
|
3090
|
+
return () => clearTimeout(timeout);
|
|
3091
|
+
};
|
|
3092
|
+
if (typeof requestIdleCallback != "undefined") requestIdle = callback => {
|
|
3093
|
+
let idle = -1,
|
|
3094
|
+
timeout = setTimeout(() => {
|
|
3095
|
+
idle = requestIdleCallback(callback, {
|
|
3096
|
+
timeout: 500 /* Work.MaxPause */ - 100 /* Work.MinPause */
|
|
3097
|
+
});
|
|
3098
|
+
}, 100 /* Work.MinPause */);
|
|
3099
|
+
return () => idle < 0 ? clearTimeout(timeout) : cancelIdleCallback(idle);
|
|
3100
|
+
};
|
|
3101
|
+
const isInputPending = typeof navigator != "undefined" && ((_a = navigator.scheduling) === null || _a === void 0 ? void 0 : _a.isInputPending) ? () => navigator.scheduling.isInputPending() : null;
|
|
3102
|
+
const parseWorker = /*@__PURE__*/ViewPlugin.fromClass(class ParseWorker {
|
|
3103
|
+
constructor(view) {
|
|
3104
|
+
this.view = view;
|
|
3105
|
+
this.working = null;
|
|
3106
|
+
this.workScheduled = 0;
|
|
3107
|
+
// End of the current time chunk
|
|
3108
|
+
this.chunkEnd = -1;
|
|
3109
|
+
// Milliseconds of budget left for this chunk
|
|
3110
|
+
this.chunkBudget = -1;
|
|
3111
|
+
this.work = this.work.bind(this);
|
|
3112
|
+
this.scheduleWork();
|
|
3113
|
+
}
|
|
3114
|
+
update(update) {
|
|
3115
|
+
let cx = this.view.state.field(Language.state).context;
|
|
3116
|
+
if (cx.updateViewport(update.view.viewport) || this.view.viewport.to > cx.treeLen) this.scheduleWork();
|
|
3117
|
+
if (update.docChanged) {
|
|
3118
|
+
if (this.view.hasFocus) this.chunkBudget += 50 /* Work.ChangeBonus */;
|
|
3119
|
+
this.scheduleWork();
|
|
3120
|
+
}
|
|
3121
|
+
this.checkAsyncSchedule(cx);
|
|
3122
|
+
}
|
|
3123
|
+
scheduleWork() {
|
|
3124
|
+
if (this.working) return;
|
|
3125
|
+
let {
|
|
3126
|
+
state
|
|
3127
|
+
} = this.view,
|
|
3128
|
+
field = state.field(Language.state);
|
|
3129
|
+
if (field.tree != field.context.tree || !field.context.isDone(state.doc.length)) this.working = requestIdle(this.work);
|
|
3130
|
+
}
|
|
3131
|
+
work(deadline) {
|
|
3132
|
+
this.working = null;
|
|
3133
|
+
let now = Date.now();
|
|
3134
|
+
if (this.chunkEnd < now && (this.chunkEnd < 0 || this.view.hasFocus)) {
|
|
3135
|
+
// Start a new chunk
|
|
3136
|
+
this.chunkEnd = now + 30000 /* Work.ChunkTime */;
|
|
3137
|
+
this.chunkBudget = 3000 /* Work.ChunkBudget */;
|
|
3138
|
+
}
|
|
3139
|
+
|
|
3140
|
+
if (this.chunkBudget <= 0) return; // No more budget
|
|
3141
|
+
let {
|
|
3142
|
+
state,
|
|
3143
|
+
viewport: {
|
|
3144
|
+
to: vpTo
|
|
3145
|
+
}
|
|
3146
|
+
} = this.view,
|
|
3147
|
+
field = state.field(Language.state);
|
|
3148
|
+
if (field.tree == field.context.tree && field.context.isDone(vpTo + 100000 /* Work.MaxParseAhead */)) return;
|
|
3149
|
+
let endTime = Date.now() + Math.min(this.chunkBudget, 100 /* Work.Slice */, deadline && !isInputPending ? Math.max(25 /* Work.MinSlice */, deadline.timeRemaining() - 5) : 1e9);
|
|
3150
|
+
let viewportFirst = field.context.treeLen < vpTo && state.doc.length > vpTo + 1000;
|
|
3151
|
+
let done = field.context.work(() => {
|
|
3152
|
+
return isInputPending && isInputPending() || Date.now() > endTime;
|
|
3153
|
+
}, vpTo + (viewportFirst ? 0 : 100000 /* Work.MaxParseAhead */));
|
|
3154
|
+
this.chunkBudget -= Date.now() - now;
|
|
3155
|
+
if (done || this.chunkBudget <= 0) {
|
|
3156
|
+
field.context.takeTree();
|
|
3157
|
+
this.view.dispatch({
|
|
3158
|
+
effects: Language.setState.of(new LanguageState(field.context))
|
|
3159
|
+
});
|
|
3160
|
+
}
|
|
3161
|
+
if (this.chunkBudget > 0 && !(done && !viewportFirst)) this.scheduleWork();
|
|
3162
|
+
this.checkAsyncSchedule(field.context);
|
|
3163
|
+
}
|
|
3164
|
+
checkAsyncSchedule(cx) {
|
|
3165
|
+
if (cx.scheduleOn) {
|
|
3166
|
+
this.workScheduled++;
|
|
3167
|
+
cx.scheduleOn.then(() => this.scheduleWork()).catch(err => logException(this.view.state, err)).then(() => this.workScheduled--);
|
|
3168
|
+
cx.scheduleOn = null;
|
|
3169
|
+
}
|
|
3170
|
+
}
|
|
3171
|
+
destroy() {
|
|
3172
|
+
if (this.working) this.working();
|
|
3173
|
+
}
|
|
3174
|
+
isWorking() {
|
|
3175
|
+
return !!(this.working || this.workScheduled > 0);
|
|
3176
|
+
}
|
|
3177
|
+
}, {
|
|
3178
|
+
eventHandlers: {
|
|
3179
|
+
focus() {
|
|
3180
|
+
this.scheduleWork();
|
|
3181
|
+
}
|
|
3182
|
+
}
|
|
3183
|
+
});
|
|
3184
|
+
/**
|
|
3185
|
+
The facet used to associate a language with an editor state. Used
|
|
3186
|
+
by `Language` object's `extension` property (so you don't need to
|
|
3187
|
+
manually wrap your languages in this). Can be used to access the
|
|
3188
|
+
current language on a state.
|
|
3189
|
+
*/
|
|
3190
|
+
const language = /*@__PURE__*/Facet.define({
|
|
3191
|
+
combine(languages) {
|
|
3192
|
+
return languages.length ? languages[0] : null;
|
|
3193
|
+
},
|
|
3194
|
+
enables: language => [Language.state, parseWorker, EditorView.contentAttributes.compute([language], state => {
|
|
3195
|
+
let lang = state.facet(language);
|
|
3196
|
+
return lang && lang.name ? {
|
|
3197
|
+
"data-language": lang.name
|
|
3198
|
+
} : {};
|
|
3199
|
+
})]
|
|
3200
|
+
});
|
|
3201
|
+
/**
|
|
3202
|
+
This class bundles a [language](https://codemirror.net/6/docs/ref/#language.Language) with an
|
|
3203
|
+
optional set of supporting extensions. Language packages are
|
|
3204
|
+
encouraged to export a function that optionally takes a
|
|
3205
|
+
configuration object and returns a `LanguageSupport` instance, as
|
|
3206
|
+
the main way for client code to use the package.
|
|
3207
|
+
*/
|
|
3208
|
+
class LanguageSupport {
|
|
3209
|
+
/**
|
|
3210
|
+
Create a language support object.
|
|
3211
|
+
*/
|
|
3212
|
+
constructor(
|
|
3213
|
+
/**
|
|
3214
|
+
The language object.
|
|
3215
|
+
*/
|
|
3216
|
+
language,
|
|
3217
|
+
/**
|
|
3218
|
+
An optional set of supporting extensions. When nesting a
|
|
3219
|
+
language in another language, the outer language is encouraged
|
|
3220
|
+
to include the supporting extensions for its inner languages
|
|
3221
|
+
in its own set of support extensions.
|
|
3222
|
+
*/
|
|
3223
|
+
support = []) {
|
|
3224
|
+
this.language = language;
|
|
3225
|
+
this.support = support;
|
|
3226
|
+
this.extension = [language, support];
|
|
3227
|
+
}
|
|
3228
|
+
}
|
|
3229
|
+
/**
|
|
3230
|
+
Language descriptions are used to store metadata about languages
|
|
3231
|
+
and to dynamically load them. Their main role is finding the
|
|
3232
|
+
appropriate language for a filename or dynamically loading nested
|
|
3233
|
+
parsers.
|
|
3234
|
+
*/
|
|
3235
|
+
class LanguageDescription {
|
|
3236
|
+
constructor(
|
|
3237
|
+
/**
|
|
3238
|
+
The name of this language.
|
|
3239
|
+
*/
|
|
3240
|
+
name,
|
|
3241
|
+
/**
|
|
3242
|
+
Alternative names for the mode (lowercased, includes `this.name`).
|
|
3243
|
+
*/
|
|
3244
|
+
alias,
|
|
3245
|
+
/**
|
|
3246
|
+
File extensions associated with this language.
|
|
3247
|
+
*/
|
|
3248
|
+
extensions,
|
|
3249
|
+
/**
|
|
3250
|
+
Optional filename pattern that should be associated with this
|
|
3251
|
+
language.
|
|
3252
|
+
*/
|
|
3253
|
+
filename, loadFunc,
|
|
3254
|
+
/**
|
|
3255
|
+
If the language has been loaded, this will hold its value.
|
|
3256
|
+
*/
|
|
3257
|
+
support = undefined) {
|
|
3258
|
+
this.name = name;
|
|
3259
|
+
this.alias = alias;
|
|
3260
|
+
this.extensions = extensions;
|
|
3261
|
+
this.filename = filename;
|
|
3262
|
+
this.loadFunc = loadFunc;
|
|
3263
|
+
this.support = support;
|
|
3264
|
+
this.loading = null;
|
|
3265
|
+
}
|
|
3266
|
+
/**
|
|
3267
|
+
Start loading the the language. Will return a promise that
|
|
3268
|
+
resolves to a [`LanguageSupport`](https://codemirror.net/6/docs/ref/#language.LanguageSupport)
|
|
3269
|
+
object when the language successfully loads.
|
|
3270
|
+
*/
|
|
3271
|
+
load() {
|
|
3272
|
+
return this.loading || (this.loading = this.loadFunc().then(support => this.support = support, err => {
|
|
3273
|
+
this.loading = null;
|
|
3274
|
+
throw err;
|
|
3275
|
+
}));
|
|
3276
|
+
}
|
|
3277
|
+
/**
|
|
3278
|
+
Create a language description.
|
|
3279
|
+
*/
|
|
3280
|
+
static of(spec) {
|
|
3281
|
+
let {
|
|
3282
|
+
load,
|
|
3283
|
+
support
|
|
3284
|
+
} = spec;
|
|
3285
|
+
if (!load) {
|
|
3286
|
+
if (!support) throw new RangeError("Must pass either 'load' or 'support' to LanguageDescription.of");
|
|
3287
|
+
load = () => Promise.resolve(support);
|
|
3288
|
+
}
|
|
3289
|
+
return new LanguageDescription(spec.name, (spec.alias || []).concat(spec.name).map(s => s.toLowerCase()), spec.extensions || [], spec.filename, load, support);
|
|
3290
|
+
}
|
|
3291
|
+
/**
|
|
3292
|
+
Look for a language in the given array of descriptions that
|
|
3293
|
+
matches the filename. Will first match
|
|
3294
|
+
[`filename`](https://codemirror.net/6/docs/ref/#language.LanguageDescription.filename) patterns,
|
|
3295
|
+
and then [extensions](https://codemirror.net/6/docs/ref/#language.LanguageDescription.extensions),
|
|
3296
|
+
and return the first language that matches.
|
|
3297
|
+
*/
|
|
3298
|
+
static matchFilename(descs, filename) {
|
|
3299
|
+
for (let d of descs) if (d.filename && d.filename.test(filename)) return d;
|
|
3300
|
+
let ext = /\.([^.]+)$/.exec(filename);
|
|
3301
|
+
if (ext) for (let d of descs) if (d.extensions.indexOf(ext[1]) > -1) return d;
|
|
3302
|
+
return null;
|
|
3303
|
+
}
|
|
3304
|
+
/**
|
|
3305
|
+
Look for a language whose name or alias matches the the given
|
|
3306
|
+
name (case-insensitively). If `fuzzy` is true, and no direct
|
|
3307
|
+
matchs is found, this'll also search for a language whose name
|
|
3308
|
+
or alias occurs in the string (for names shorter than three
|
|
3309
|
+
characters, only when surrounded by non-word characters).
|
|
3310
|
+
*/
|
|
3311
|
+
static matchLanguageName(descs, name, fuzzy = true) {
|
|
3312
|
+
name = name.toLowerCase();
|
|
3313
|
+
for (let d of descs) if (d.alias.some(a => a == name)) return d;
|
|
3314
|
+
if (fuzzy) for (let d of descs) for (let a of d.alias) {
|
|
3315
|
+
let found = name.indexOf(a);
|
|
3316
|
+
if (found > -1 && (a.length > 2 || !/\w/.test(name[found - 1]) && !/\w/.test(name[found + a.length]))) return d;
|
|
3317
|
+
}
|
|
3318
|
+
return null;
|
|
3319
|
+
}
|
|
3320
|
+
}
|
|
3321
|
+
|
|
3322
|
+
/**
|
|
3323
|
+
Facet that defines a way to provide a function that computes the
|
|
3324
|
+
appropriate indentation depth, as a column number (see
|
|
3325
|
+
[`indentString`](https://codemirror.net/6/docs/ref/#language.indentString)), at the start of a given
|
|
3326
|
+
line. A return value of `null` indicates no indentation can be
|
|
3327
|
+
determined, and the line should inherit the indentation of the one
|
|
3328
|
+
above it. A return value of `undefined` defers to the next indent
|
|
3329
|
+
service.
|
|
3330
|
+
*/
|
|
3331
|
+
const indentService = /*@__PURE__*/Facet.define();
|
|
3332
|
+
/**
|
|
3333
|
+
Facet for overriding the unit by which indentation happens.
|
|
3334
|
+
Should be a string consisting either entirely of spaces or
|
|
3335
|
+
entirely of tabs. When not set, this defaults to 2 spaces.
|
|
3336
|
+
*/
|
|
3337
|
+
const indentUnit = /*@__PURE__*/Facet.define({
|
|
3338
|
+
combine: values => {
|
|
3339
|
+
if (!values.length) return " ";
|
|
3340
|
+
if (!/^(?: +|\t+)$/.test(values[0])) throw new Error("Invalid indent unit: " + JSON.stringify(values[0]));
|
|
3341
|
+
return values[0];
|
|
3342
|
+
}
|
|
3343
|
+
});
|
|
3344
|
+
/**
|
|
3345
|
+
Return the _column width_ of an indent unit in the state.
|
|
3346
|
+
Determined by the [`indentUnit`](https://codemirror.net/6/docs/ref/#language.indentUnit)
|
|
3347
|
+
facet, and [`tabSize`](https://codemirror.net/6/docs/ref/#state.EditorState^tabSize) when that
|
|
3348
|
+
contains tabs.
|
|
3349
|
+
*/
|
|
3350
|
+
function getIndentUnit(state) {
|
|
3351
|
+
let unit = state.facet(indentUnit);
|
|
3352
|
+
return unit.charCodeAt(0) == 9 ? state.tabSize * unit.length : unit.length;
|
|
3353
|
+
}
|
|
3354
|
+
/**
|
|
3355
|
+
Create an indentation string that covers columns 0 to `cols`.
|
|
3356
|
+
Will use tabs for as much of the columns as possible when the
|
|
3357
|
+
[`indentUnit`](https://codemirror.net/6/docs/ref/#language.indentUnit) facet contains
|
|
3358
|
+
tabs.
|
|
3359
|
+
*/
|
|
3360
|
+
function indentString(state, cols) {
|
|
3361
|
+
let result = "",
|
|
3362
|
+
ts = state.tabSize;
|
|
3363
|
+
if (state.facet(indentUnit).charCodeAt(0) == 9) while (cols >= ts) {
|
|
3364
|
+
result += "\t";
|
|
3365
|
+
cols -= ts;
|
|
3366
|
+
}
|
|
3367
|
+
for (let i = 0; i < cols; i++) result += " ";
|
|
3368
|
+
return result;
|
|
3369
|
+
}
|
|
3370
|
+
/**
|
|
3371
|
+
Get the indentation, as a column number, at the given position.
|
|
3372
|
+
Will first consult any [indent services](https://codemirror.net/6/docs/ref/#language.indentService)
|
|
3373
|
+
that are registered, and if none of those return an indentation,
|
|
3374
|
+
this will check the syntax tree for the [indent node
|
|
3375
|
+
prop](https://codemirror.net/6/docs/ref/#language.indentNodeProp) and use that if found. Returns a
|
|
3376
|
+
number when an indentation could be determined, and null
|
|
3377
|
+
otherwise.
|
|
3378
|
+
*/
|
|
3379
|
+
function getIndentation(context, pos) {
|
|
3380
|
+
if (context instanceof EditorState) context = new IndentContext(context);
|
|
3381
|
+
for (let service of context.state.facet(indentService)) {
|
|
3382
|
+
let result = service(context, pos);
|
|
3383
|
+
if (result !== undefined) return result;
|
|
3384
|
+
}
|
|
3385
|
+
let tree = syntaxTree(context.state);
|
|
3386
|
+
return tree ? syntaxIndentation(context, tree, pos) : null;
|
|
3387
|
+
}
|
|
3388
|
+
/**
|
|
3389
|
+
Indentation contexts are used when calling [indentation
|
|
3390
|
+
services](https://codemirror.net/6/docs/ref/#language.indentService). They provide helper utilities
|
|
3391
|
+
useful in indentation logic, and can selectively override the
|
|
3392
|
+
indentation reported for some lines.
|
|
3393
|
+
*/
|
|
3394
|
+
class IndentContext {
|
|
3395
|
+
/**
|
|
3396
|
+
Create an indent context.
|
|
3397
|
+
*/
|
|
3398
|
+
constructor(
|
|
3399
|
+
/**
|
|
3400
|
+
The editor state.
|
|
3401
|
+
*/
|
|
3402
|
+
state,
|
|
3403
|
+
/**
|
|
3404
|
+
@internal
|
|
3405
|
+
*/
|
|
3406
|
+
options = {}) {
|
|
3407
|
+
this.state = state;
|
|
3408
|
+
this.options = options;
|
|
3409
|
+
this.unit = getIndentUnit(state);
|
|
3410
|
+
}
|
|
3411
|
+
/**
|
|
3412
|
+
Get a description of the line at the given position, taking
|
|
3413
|
+
[simulated line
|
|
3414
|
+
breaks](https://codemirror.net/6/docs/ref/#language.IndentContext.constructor^options.simulateBreak)
|
|
3415
|
+
into account. If there is such a break at `pos`, the `bias`
|
|
3416
|
+
argument determines whether the part of the line line before or
|
|
3417
|
+
after the break is used.
|
|
3418
|
+
*/
|
|
3419
|
+
lineAt(pos, bias = 1) {
|
|
3420
|
+
let line = this.state.doc.lineAt(pos);
|
|
3421
|
+
let {
|
|
3422
|
+
simulateBreak,
|
|
3423
|
+
simulateDoubleBreak
|
|
3424
|
+
} = this.options;
|
|
3425
|
+
if (simulateBreak != null && simulateBreak >= line.from && simulateBreak <= line.to) {
|
|
3426
|
+
if (simulateDoubleBreak && simulateBreak == pos) return {
|
|
3427
|
+
text: "",
|
|
3428
|
+
from: pos
|
|
3429
|
+
};else if (bias < 0 ? simulateBreak < pos : simulateBreak <= pos) return {
|
|
3430
|
+
text: line.text.slice(simulateBreak - line.from),
|
|
3431
|
+
from: simulateBreak
|
|
3432
|
+
};else return {
|
|
3433
|
+
text: line.text.slice(0, simulateBreak - line.from),
|
|
3434
|
+
from: line.from
|
|
3435
|
+
};
|
|
3436
|
+
}
|
|
3437
|
+
return line;
|
|
3438
|
+
}
|
|
3439
|
+
/**
|
|
3440
|
+
Get the text directly after `pos`, either the entire line
|
|
3441
|
+
or the next 100 characters, whichever is shorter.
|
|
3442
|
+
*/
|
|
3443
|
+
textAfterPos(pos, bias = 1) {
|
|
3444
|
+
if (this.options.simulateDoubleBreak && pos == this.options.simulateBreak) return "";
|
|
3445
|
+
let {
|
|
3446
|
+
text,
|
|
3447
|
+
from
|
|
3448
|
+
} = this.lineAt(pos, bias);
|
|
3449
|
+
return text.slice(pos - from, Math.min(text.length, pos + 100 - from));
|
|
3450
|
+
}
|
|
3451
|
+
/**
|
|
3452
|
+
Find the column for the given position.
|
|
3453
|
+
*/
|
|
3454
|
+
column(pos, bias = 1) {
|
|
3455
|
+
let {
|
|
3456
|
+
text,
|
|
3457
|
+
from
|
|
3458
|
+
} = this.lineAt(pos, bias);
|
|
3459
|
+
let result = this.countColumn(text, pos - from);
|
|
3460
|
+
let override = this.options.overrideIndentation ? this.options.overrideIndentation(from) : -1;
|
|
3461
|
+
if (override > -1) result += override - this.countColumn(text, text.search(/\S|$/));
|
|
3462
|
+
return result;
|
|
3463
|
+
}
|
|
3464
|
+
/**
|
|
3465
|
+
Find the column position (taking tabs into account) of the given
|
|
3466
|
+
position in the given string.
|
|
3467
|
+
*/
|
|
3468
|
+
countColumn(line, pos = line.length) {
|
|
3469
|
+
return countColumn(line, this.state.tabSize, pos);
|
|
3470
|
+
}
|
|
3471
|
+
/**
|
|
3472
|
+
Find the indentation column of the line at the given point.
|
|
3473
|
+
*/
|
|
3474
|
+
lineIndent(pos, bias = 1) {
|
|
3475
|
+
let {
|
|
3476
|
+
text,
|
|
3477
|
+
from
|
|
3478
|
+
} = this.lineAt(pos, bias);
|
|
3479
|
+
let override = this.options.overrideIndentation;
|
|
3480
|
+
if (override) {
|
|
3481
|
+
let overriden = override(from);
|
|
3482
|
+
if (overriden > -1) return overriden;
|
|
3483
|
+
}
|
|
3484
|
+
return this.countColumn(text, text.search(/\S|$/));
|
|
3485
|
+
}
|
|
3486
|
+
/**
|
|
3487
|
+
Returns the [simulated line
|
|
3488
|
+
break](https://codemirror.net/6/docs/ref/#language.IndentContext.constructor^options.simulateBreak)
|
|
3489
|
+
for this context, if any.
|
|
3490
|
+
*/
|
|
3491
|
+
get simulatedBreak() {
|
|
3492
|
+
return this.options.simulateBreak || null;
|
|
3493
|
+
}
|
|
3494
|
+
}
|
|
3495
|
+
/**
|
|
3496
|
+
A syntax tree node prop used to associate indentation strategies
|
|
3497
|
+
with node types. Such a strategy is a function from an indentation
|
|
3498
|
+
context to a column number (see also
|
|
3499
|
+
[`indentString`](https://codemirror.net/6/docs/ref/#language.indentString)) or null, where null
|
|
3500
|
+
indicates that no definitive indentation can be determined.
|
|
3501
|
+
*/
|
|
3502
|
+
const indentNodeProp = /*@__PURE__*/new NodeProp();
|
|
3503
|
+
// Compute the indentation for a given position from the syntax tree.
|
|
3504
|
+
function syntaxIndentation(cx, ast, pos) {
|
|
3505
|
+
return indentFrom(ast.resolveInner(pos).enterUnfinishedNodesBefore(pos), pos, cx);
|
|
3506
|
+
}
|
|
3507
|
+
function ignoreClosed(cx) {
|
|
3508
|
+
return cx.pos == cx.options.simulateBreak && cx.options.simulateDoubleBreak;
|
|
3509
|
+
}
|
|
3510
|
+
function indentStrategy(tree) {
|
|
3511
|
+
let strategy = tree.type.prop(indentNodeProp);
|
|
3512
|
+
if (strategy) return strategy;
|
|
3513
|
+
let first = tree.firstChild,
|
|
3514
|
+
close;
|
|
3515
|
+
if (first && (close = first.type.prop(NodeProp.closedBy))) {
|
|
3516
|
+
let last = tree.lastChild,
|
|
3517
|
+
closed = last && close.indexOf(last.name) > -1;
|
|
3518
|
+
return cx => delimitedStrategy(cx, true, 1, undefined, closed && !ignoreClosed(cx) ? last.from : undefined);
|
|
3519
|
+
}
|
|
3520
|
+
return tree.parent == null ? topIndent : null;
|
|
3521
|
+
}
|
|
3522
|
+
function indentFrom(node, pos, base) {
|
|
3523
|
+
for (; node; node = node.parent) {
|
|
3524
|
+
let strategy = indentStrategy(node);
|
|
3525
|
+
if (strategy) return strategy(TreeIndentContext.create(base, pos, node));
|
|
3526
|
+
}
|
|
3527
|
+
return null;
|
|
3528
|
+
}
|
|
3529
|
+
function topIndent() {
|
|
3530
|
+
return 0;
|
|
3531
|
+
}
|
|
3532
|
+
/**
|
|
3533
|
+
Objects of this type provide context information and helper
|
|
3534
|
+
methods to indentation functions registered on syntax nodes.
|
|
3535
|
+
*/
|
|
3536
|
+
class TreeIndentContext extends IndentContext {
|
|
3537
|
+
constructor(base,
|
|
3538
|
+
/**
|
|
3539
|
+
The position at which indentation is being computed.
|
|
3540
|
+
*/
|
|
3541
|
+
pos,
|
|
3542
|
+
/**
|
|
3543
|
+
The syntax tree node to which the indentation strategy
|
|
3544
|
+
applies.
|
|
3545
|
+
*/
|
|
3546
|
+
node) {
|
|
3547
|
+
super(base.state, base.options);
|
|
3548
|
+
this.base = base;
|
|
3549
|
+
this.pos = pos;
|
|
3550
|
+
this.node = node;
|
|
3551
|
+
}
|
|
3552
|
+
/**
|
|
3553
|
+
@internal
|
|
3554
|
+
*/
|
|
3555
|
+
static create(base, pos, node) {
|
|
3556
|
+
return new TreeIndentContext(base, pos, node);
|
|
3557
|
+
}
|
|
3558
|
+
/**
|
|
3559
|
+
Get the text directly after `this.pos`, either the entire line
|
|
3560
|
+
or the next 100 characters, whichever is shorter.
|
|
3561
|
+
*/
|
|
3562
|
+
get textAfter() {
|
|
3563
|
+
return this.textAfterPos(this.pos);
|
|
3564
|
+
}
|
|
3565
|
+
/**
|
|
3566
|
+
Get the indentation at the reference line for `this.node`, which
|
|
3567
|
+
is the line on which it starts, unless there is a node that is
|
|
3568
|
+
_not_ a parent of this node covering the start of that line. If
|
|
3569
|
+
so, the line at the start of that node is tried, again skipping
|
|
3570
|
+
on if it is covered by another such node.
|
|
3571
|
+
*/
|
|
3572
|
+
get baseIndent() {
|
|
3573
|
+
let line = this.state.doc.lineAt(this.node.from);
|
|
3574
|
+
// Skip line starts that are covered by a sibling (or cousin, etc)
|
|
3575
|
+
for (;;) {
|
|
3576
|
+
let atBreak = this.node.resolve(line.from);
|
|
3577
|
+
while (atBreak.parent && atBreak.parent.from == atBreak.from) atBreak = atBreak.parent;
|
|
3578
|
+
if (isParent(atBreak, this.node)) break;
|
|
3579
|
+
line = this.state.doc.lineAt(atBreak.from);
|
|
3580
|
+
}
|
|
3581
|
+
return this.lineIndent(line.from);
|
|
3582
|
+
}
|
|
3583
|
+
/**
|
|
3584
|
+
Continue looking for indentations in the node's parent nodes,
|
|
3585
|
+
and return the result of that.
|
|
3586
|
+
*/
|
|
3587
|
+
continue() {
|
|
3588
|
+
let parent = this.node.parent;
|
|
3589
|
+
return parent ? indentFrom(parent, this.pos, this.base) : 0;
|
|
3590
|
+
}
|
|
3591
|
+
}
|
|
3592
|
+
function isParent(parent, of) {
|
|
3593
|
+
for (let cur = of; cur; cur = cur.parent) if (parent == cur) return true;
|
|
3594
|
+
return false;
|
|
3595
|
+
}
|
|
3596
|
+
// Check whether a delimited node is aligned (meaning there are
|
|
3597
|
+
// non-skipped nodes on the same line as the opening delimiter). And
|
|
3598
|
+
// if so, return the opening token.
|
|
3599
|
+
function bracketedAligned(context) {
|
|
3600
|
+
let tree = context.node;
|
|
3601
|
+
let openToken = tree.childAfter(tree.from),
|
|
3602
|
+
last = tree.lastChild;
|
|
3603
|
+
if (!openToken) return null;
|
|
3604
|
+
let sim = context.options.simulateBreak;
|
|
3605
|
+
let openLine = context.state.doc.lineAt(openToken.from);
|
|
3606
|
+
let lineEnd = sim == null || sim <= openLine.from ? openLine.to : Math.min(openLine.to, sim);
|
|
3607
|
+
for (let pos = openToken.to;;) {
|
|
3608
|
+
let next = tree.childAfter(pos);
|
|
3609
|
+
if (!next || next == last) return null;
|
|
3610
|
+
if (!next.type.isSkipped) return next.from < lineEnd ? openToken : null;
|
|
3611
|
+
pos = next.to;
|
|
3612
|
+
}
|
|
3613
|
+
}
|
|
3614
|
+
/**
|
|
3615
|
+
An indentation strategy for delimited (usually bracketed) nodes.
|
|
3616
|
+
Will, by default, indent one unit more than the parent's base
|
|
3617
|
+
indent unless the line starts with a closing token. When `align`
|
|
3618
|
+
is true and there are non-skipped nodes on the node's opening
|
|
3619
|
+
line, the content of the node will be aligned with the end of the
|
|
3620
|
+
opening node, like this:
|
|
3621
|
+
|
|
3622
|
+
foo(bar,
|
|
3623
|
+
baz)
|
|
3624
|
+
*/
|
|
3625
|
+
function delimitedIndent({
|
|
3626
|
+
closing,
|
|
3627
|
+
align = true,
|
|
3628
|
+
units = 1
|
|
3629
|
+
}) {
|
|
3630
|
+
return context => delimitedStrategy(context, align, units, closing);
|
|
3631
|
+
}
|
|
3632
|
+
function delimitedStrategy(context, align, units, closing, closedAt) {
|
|
3633
|
+
let after = context.textAfter,
|
|
3634
|
+
space = after.match(/^\s*/)[0].length;
|
|
3635
|
+
let closed = closing && after.slice(space, space + closing.length) == closing || closedAt == context.pos + space;
|
|
3636
|
+
let aligned = align ? bracketedAligned(context) : null;
|
|
3637
|
+
if (aligned) return closed ? context.column(aligned.from) : context.column(aligned.to);
|
|
3638
|
+
return context.baseIndent + (closed ? 0 : context.unit * units);
|
|
3639
|
+
}
|
|
3640
|
+
/**
|
|
3641
|
+
An indentation strategy that aligns a node's content to its base
|
|
3642
|
+
indentation.
|
|
3643
|
+
*/
|
|
3644
|
+
const flatIndent = context => context.baseIndent;
|
|
3645
|
+
/**
|
|
3646
|
+
Creates an indentation strategy that, by default, indents
|
|
3647
|
+
continued lines one unit more than the node's base indentation.
|
|
3648
|
+
You can provide `except` to prevent indentation of lines that
|
|
3649
|
+
match a pattern (for example `/^else\b/` in `if`/`else`
|
|
3650
|
+
constructs), and you can change the amount of units used with the
|
|
3651
|
+
`units` option.
|
|
3652
|
+
*/
|
|
3653
|
+
function continuedIndent({
|
|
3654
|
+
except,
|
|
3655
|
+
units = 1
|
|
3656
|
+
} = {}) {
|
|
3657
|
+
return context => {
|
|
3658
|
+
let matchExcept = except && except.test(context.textAfter);
|
|
3659
|
+
return context.baseIndent + (matchExcept ? 0 : units * context.unit);
|
|
3660
|
+
};
|
|
3661
|
+
}
|
|
3662
|
+
const DontIndentBeyond = 200;
|
|
3663
|
+
/**
|
|
3664
|
+
Enables reindentation on input. When a language defines an
|
|
3665
|
+
`indentOnInput` field in its [language
|
|
3666
|
+
data](https://codemirror.net/6/docs/ref/#state.EditorState.languageDataAt), which must hold a regular
|
|
3667
|
+
expression, the line at the cursor will be reindented whenever new
|
|
3668
|
+
text is typed and the input from the start of the line up to the
|
|
3669
|
+
cursor matches that regexp.
|
|
3670
|
+
|
|
3671
|
+
To avoid unneccesary reindents, it is recommended to start the
|
|
3672
|
+
regexp with `^` (usually followed by `\s*`), and end it with `$`.
|
|
3673
|
+
For example, `/^\s*\}$/` will reindent when a closing brace is
|
|
3674
|
+
added at the start of a line.
|
|
3675
|
+
*/
|
|
3676
|
+
function indentOnInput() {
|
|
3677
|
+
return EditorState.transactionFilter.of(tr => {
|
|
3678
|
+
if (!tr.docChanged || !tr.isUserEvent("input.type") && !tr.isUserEvent("input.complete")) return tr;
|
|
3679
|
+
let rules = tr.startState.languageDataAt("indentOnInput", tr.startState.selection.main.head);
|
|
3680
|
+
if (!rules.length) return tr;
|
|
3681
|
+
let doc = tr.newDoc,
|
|
3682
|
+
{
|
|
3683
|
+
head
|
|
3684
|
+
} = tr.newSelection.main,
|
|
3685
|
+
line = doc.lineAt(head);
|
|
3686
|
+
if (head > line.from + DontIndentBeyond) return tr;
|
|
3687
|
+
let lineStart = doc.sliceString(line.from, head);
|
|
3688
|
+
if (!rules.some(r => r.test(lineStart))) return tr;
|
|
3689
|
+
let {
|
|
3690
|
+
state
|
|
3691
|
+
} = tr,
|
|
3692
|
+
last = -1,
|
|
3693
|
+
changes = [];
|
|
3694
|
+
for (let {
|
|
3695
|
+
head
|
|
3696
|
+
} of state.selection.ranges) {
|
|
3697
|
+
let line = state.doc.lineAt(head);
|
|
3698
|
+
if (line.from == last) continue;
|
|
3699
|
+
last = line.from;
|
|
3700
|
+
let indent = getIndentation(state, line.from);
|
|
3701
|
+
if (indent == null) continue;
|
|
3702
|
+
let cur = /^\s*/.exec(line.text)[0];
|
|
3703
|
+
let norm = indentString(state, indent);
|
|
3704
|
+
if (cur != norm) changes.push({
|
|
3705
|
+
from: line.from,
|
|
3706
|
+
to: line.from + cur.length,
|
|
3707
|
+
insert: norm
|
|
3708
|
+
});
|
|
3709
|
+
}
|
|
3710
|
+
return changes.length ? [tr, {
|
|
3711
|
+
changes,
|
|
3712
|
+
sequential: true
|
|
3713
|
+
}] : tr;
|
|
3714
|
+
});
|
|
3715
|
+
}
|
|
3716
|
+
|
|
3717
|
+
/**
|
|
3718
|
+
A facet that registers a code folding service. When called with
|
|
3719
|
+
the extent of a line, such a function should return a foldable
|
|
3720
|
+
range that starts on that line (but continues beyond it), if one
|
|
3721
|
+
can be found.
|
|
3722
|
+
*/
|
|
3723
|
+
const foldService = /*@__PURE__*/Facet.define();
|
|
3724
|
+
/**
|
|
3725
|
+
This node prop is used to associate folding information with
|
|
3726
|
+
syntax node types. Given a syntax node, it should check whether
|
|
3727
|
+
that tree is foldable and return the range that can be collapsed
|
|
3728
|
+
when it is.
|
|
3729
|
+
*/
|
|
3730
|
+
const foldNodeProp = /*@__PURE__*/new NodeProp();
|
|
3731
|
+
/**
|
|
3732
|
+
[Fold](https://codemirror.net/6/docs/ref/#language.foldNodeProp) function that folds everything but
|
|
3733
|
+
the first and the last child of a syntax node. Useful for nodes
|
|
3734
|
+
that start and end with delimiters.
|
|
3735
|
+
*/
|
|
3736
|
+
function foldInside(node) {
|
|
3737
|
+
let first = node.firstChild,
|
|
3738
|
+
last = node.lastChild;
|
|
3739
|
+
return first && first.to < last.from ? {
|
|
3740
|
+
from: first.to,
|
|
3741
|
+
to: last.type.isError ? node.to : last.from
|
|
3742
|
+
} : null;
|
|
3743
|
+
}
|
|
3744
|
+
function syntaxFolding(state, start, end) {
|
|
3745
|
+
let tree = syntaxTree(state);
|
|
3746
|
+
if (tree.length < end) return null;
|
|
3747
|
+
let inner = tree.resolveInner(end, 1);
|
|
3748
|
+
let found = null;
|
|
3749
|
+
for (let cur = inner; cur; cur = cur.parent) {
|
|
3750
|
+
if (cur.to <= end || cur.from > end) continue;
|
|
3751
|
+
if (found && cur.from < start) break;
|
|
3752
|
+
let prop = cur.type.prop(foldNodeProp);
|
|
3753
|
+
if (prop && (cur.to < tree.length - 50 || tree.length == state.doc.length || !isUnfinished(cur))) {
|
|
3754
|
+
let value = prop(cur, state);
|
|
3755
|
+
if (value && value.from <= end && value.from >= start && value.to > end) found = value;
|
|
3756
|
+
}
|
|
3757
|
+
}
|
|
3758
|
+
return found;
|
|
3759
|
+
}
|
|
3760
|
+
function isUnfinished(node) {
|
|
3761
|
+
let ch = node.lastChild;
|
|
3762
|
+
return ch && ch.to == node.to && ch.type.isError;
|
|
3763
|
+
}
|
|
3764
|
+
/**
|
|
3765
|
+
Check whether the given line is foldable. First asks any fold
|
|
3766
|
+
services registered through
|
|
3767
|
+
[`foldService`](https://codemirror.net/6/docs/ref/#language.foldService), and if none of them return
|
|
3768
|
+
a result, tries to query the [fold node
|
|
3769
|
+
prop](https://codemirror.net/6/docs/ref/#language.foldNodeProp) of syntax nodes that cover the end
|
|
3770
|
+
of the line.
|
|
3771
|
+
*/
|
|
3772
|
+
function foldable(state, lineStart, lineEnd) {
|
|
3773
|
+
for (let service of state.facet(foldService)) {
|
|
3774
|
+
let result = service(state, lineStart, lineEnd);
|
|
3775
|
+
if (result) return result;
|
|
3776
|
+
}
|
|
3777
|
+
return syntaxFolding(state, lineStart, lineEnd);
|
|
3778
|
+
}
|
|
3779
|
+
function mapRange(range, mapping) {
|
|
3780
|
+
let from = mapping.mapPos(range.from, 1),
|
|
3781
|
+
to = mapping.mapPos(range.to, -1);
|
|
3782
|
+
return from >= to ? undefined : {
|
|
3783
|
+
from,
|
|
3784
|
+
to
|
|
3785
|
+
};
|
|
3786
|
+
}
|
|
3787
|
+
/**
|
|
3788
|
+
State effect that can be attached to a transaction to fold the
|
|
3789
|
+
given range. (You probably only need this in exceptional
|
|
3790
|
+
circumstances—usually you'll just want to let
|
|
3791
|
+
[`foldCode`](https://codemirror.net/6/docs/ref/#language.foldCode) and the [fold
|
|
3792
|
+
gutter](https://codemirror.net/6/docs/ref/#language.foldGutter) create the transactions.)
|
|
3793
|
+
*/
|
|
3794
|
+
const foldEffect = /*@__PURE__*/StateEffect.define({
|
|
3795
|
+
map: mapRange
|
|
3796
|
+
});
|
|
3797
|
+
/**
|
|
3798
|
+
State effect that unfolds the given range (if it was folded).
|
|
3799
|
+
*/
|
|
3800
|
+
const unfoldEffect = /*@__PURE__*/StateEffect.define({
|
|
3801
|
+
map: mapRange
|
|
3802
|
+
});
|
|
3803
|
+
function selectedLines(view) {
|
|
3804
|
+
let lines = [];
|
|
3805
|
+
for (let {
|
|
3806
|
+
head
|
|
3807
|
+
} of view.state.selection.ranges) {
|
|
3808
|
+
if (lines.some(l => l.from <= head && l.to >= head)) continue;
|
|
3809
|
+
lines.push(view.lineBlockAt(head));
|
|
3810
|
+
}
|
|
3811
|
+
return lines;
|
|
3812
|
+
}
|
|
3813
|
+
/**
|
|
3814
|
+
The state field that stores the folded ranges (as a [decoration
|
|
3815
|
+
set](https://codemirror.net/6/docs/ref/#view.DecorationSet)). Can be passed to
|
|
3816
|
+
[`EditorState.toJSON`](https://codemirror.net/6/docs/ref/#state.EditorState.toJSON) and
|
|
3817
|
+
[`fromJSON`](https://codemirror.net/6/docs/ref/#state.EditorState^fromJSON) to serialize the fold
|
|
3818
|
+
state.
|
|
3819
|
+
*/
|
|
3820
|
+
const foldState = /*@__PURE__*/StateField.define({
|
|
3821
|
+
create() {
|
|
3822
|
+
return Decoration.none;
|
|
3823
|
+
},
|
|
3824
|
+
update(folded, tr) {
|
|
3825
|
+
folded = folded.map(tr.changes);
|
|
3826
|
+
for (let e of tr.effects) {
|
|
3827
|
+
if (e.is(foldEffect) && !foldExists(folded, e.value.from, e.value.to)) folded = folded.update({
|
|
3828
|
+
add: [foldWidget.range(e.value.from, e.value.to)]
|
|
3829
|
+
});else if (e.is(unfoldEffect)) folded = folded.update({
|
|
3830
|
+
filter: (from, to) => e.value.from != from || e.value.to != to,
|
|
3831
|
+
filterFrom: e.value.from,
|
|
3832
|
+
filterTo: e.value.to
|
|
3833
|
+
});
|
|
3834
|
+
}
|
|
3835
|
+
// Clear folded ranges that cover the selection head
|
|
3836
|
+
if (tr.selection) {
|
|
3837
|
+
let onSelection = false,
|
|
3838
|
+
{
|
|
3839
|
+
head
|
|
3840
|
+
} = tr.selection.main;
|
|
3841
|
+
folded.between(head, head, (a, b) => {
|
|
3842
|
+
if (a < head && b > head) onSelection = true;
|
|
3843
|
+
});
|
|
3844
|
+
if (onSelection) folded = folded.update({
|
|
3845
|
+
filterFrom: head,
|
|
3846
|
+
filterTo: head,
|
|
3847
|
+
filter: (a, b) => b <= head || a >= head
|
|
3848
|
+
});
|
|
3849
|
+
}
|
|
3850
|
+
return folded;
|
|
3851
|
+
},
|
|
3852
|
+
provide: f => EditorView.decorations.from(f),
|
|
3853
|
+
toJSON(folded, state) {
|
|
3854
|
+
let ranges = [];
|
|
3855
|
+
folded.between(0, state.doc.length, (from, to) => {
|
|
3856
|
+
ranges.push(from, to);
|
|
3857
|
+
});
|
|
3858
|
+
return ranges;
|
|
3859
|
+
},
|
|
3860
|
+
fromJSON(value) {
|
|
3861
|
+
if (!Array.isArray(value) || value.length % 2) throw new RangeError("Invalid JSON for fold state");
|
|
3862
|
+
let ranges = [];
|
|
3863
|
+
for (let i = 0; i < value.length;) {
|
|
3864
|
+
let from = value[i++],
|
|
3865
|
+
to = value[i++];
|
|
3866
|
+
if (typeof from != "number" || typeof to != "number") throw new RangeError("Invalid JSON for fold state");
|
|
3867
|
+
ranges.push(foldWidget.range(from, to));
|
|
3868
|
+
}
|
|
3869
|
+
return Decoration.set(ranges, true);
|
|
3870
|
+
}
|
|
3871
|
+
});
|
|
3872
|
+
function findFold(state, from, to) {
|
|
3873
|
+
var _a;
|
|
3874
|
+
let found = null;
|
|
3875
|
+
(_a = state.field(foldState, false)) === null || _a === void 0 ? void 0 : _a.between(from, to, (from, to) => {
|
|
3876
|
+
if (!found || found.from > from) found = {
|
|
3877
|
+
from,
|
|
3878
|
+
to
|
|
3879
|
+
};
|
|
3880
|
+
});
|
|
3881
|
+
return found;
|
|
3882
|
+
}
|
|
3883
|
+
function foldExists(folded, from, to) {
|
|
3884
|
+
let found = false;
|
|
3885
|
+
folded.between(from, from, (a, b) => {
|
|
3886
|
+
if (a == from && b == to) found = true;
|
|
3887
|
+
});
|
|
3888
|
+
return found;
|
|
3889
|
+
}
|
|
3890
|
+
function maybeEnable(state, other) {
|
|
3891
|
+
return state.field(foldState, false) ? other : other.concat(StateEffect.appendConfig.of(codeFolding()));
|
|
3892
|
+
}
|
|
3893
|
+
/**
|
|
3894
|
+
Fold the lines that are selected, if possible.
|
|
3895
|
+
*/
|
|
3896
|
+
const foldCode = view => {
|
|
3897
|
+
for (let line of selectedLines(view)) {
|
|
3898
|
+
let range = foldable(view.state, line.from, line.to);
|
|
3899
|
+
if (range) {
|
|
3900
|
+
view.dispatch({
|
|
3901
|
+
effects: maybeEnable(view.state, [foldEffect.of(range), announceFold(view, range)])
|
|
3902
|
+
});
|
|
3903
|
+
return true;
|
|
3904
|
+
}
|
|
3905
|
+
}
|
|
3906
|
+
return false;
|
|
3907
|
+
};
|
|
3908
|
+
/**
|
|
3909
|
+
Unfold folded ranges on selected lines.
|
|
3910
|
+
*/
|
|
3911
|
+
const unfoldCode = view => {
|
|
3912
|
+
if (!view.state.field(foldState, false)) return false;
|
|
3913
|
+
let effects = [];
|
|
3914
|
+
for (let line of selectedLines(view)) {
|
|
3915
|
+
let folded = findFold(view.state, line.from, line.to);
|
|
3916
|
+
if (folded) effects.push(unfoldEffect.of(folded), announceFold(view, folded, false));
|
|
3917
|
+
}
|
|
3918
|
+
if (effects.length) view.dispatch({
|
|
3919
|
+
effects
|
|
3920
|
+
});
|
|
3921
|
+
return effects.length > 0;
|
|
3922
|
+
};
|
|
3923
|
+
function announceFold(view, range, fold = true) {
|
|
3924
|
+
let lineFrom = view.state.doc.lineAt(range.from).number,
|
|
3925
|
+
lineTo = view.state.doc.lineAt(range.to).number;
|
|
3926
|
+
return EditorView.announce.of(`${view.state.phrase(fold ? "Folded lines" : "Unfolded lines")} ${lineFrom} ${view.state.phrase("to")} ${lineTo}.`);
|
|
3927
|
+
}
|
|
3928
|
+
/**
|
|
3929
|
+
Fold all top-level foldable ranges. Note that, in most cases,
|
|
3930
|
+
folding information will depend on the [syntax
|
|
3931
|
+
tree](https://codemirror.net/6/docs/ref/#language.syntaxTree), and folding everything may not work
|
|
3932
|
+
reliably when the document hasn't been fully parsed (either
|
|
3933
|
+
because the editor state was only just initialized, or because the
|
|
3934
|
+
document is so big that the parser decided not to parse it
|
|
3935
|
+
entirely).
|
|
3936
|
+
*/
|
|
3937
|
+
const foldAll = view => {
|
|
3938
|
+
let {
|
|
3939
|
+
state
|
|
3940
|
+
} = view,
|
|
3941
|
+
effects = [];
|
|
3942
|
+
for (let pos = 0; pos < state.doc.length;) {
|
|
3943
|
+
let line = view.lineBlockAt(pos),
|
|
3944
|
+
range = foldable(state, line.from, line.to);
|
|
3945
|
+
if (range) effects.push(foldEffect.of(range));
|
|
3946
|
+
pos = (range ? view.lineBlockAt(range.to) : line).to + 1;
|
|
3947
|
+
}
|
|
3948
|
+
if (effects.length) view.dispatch({
|
|
3949
|
+
effects: maybeEnable(view.state, effects)
|
|
3950
|
+
});
|
|
3951
|
+
return !!effects.length;
|
|
3952
|
+
};
|
|
3953
|
+
/**
|
|
3954
|
+
Unfold all folded code.
|
|
3955
|
+
*/
|
|
3956
|
+
const unfoldAll = view => {
|
|
3957
|
+
let field = view.state.field(foldState, false);
|
|
3958
|
+
if (!field || !field.size) return false;
|
|
3959
|
+
let effects = [];
|
|
3960
|
+
field.between(0, view.state.doc.length, (from, to) => {
|
|
3961
|
+
effects.push(unfoldEffect.of({
|
|
3962
|
+
from,
|
|
3963
|
+
to
|
|
3964
|
+
}));
|
|
3965
|
+
});
|
|
3966
|
+
view.dispatch({
|
|
3967
|
+
effects
|
|
3968
|
+
});
|
|
3969
|
+
return true;
|
|
3970
|
+
};
|
|
3971
|
+
/**
|
|
3972
|
+
Default fold-related key bindings.
|
|
3973
|
+
|
|
3974
|
+
- Ctrl-Shift-[ (Cmd-Alt-[ on macOS): [`foldCode`](https://codemirror.net/6/docs/ref/#language.foldCode).
|
|
3975
|
+
- Ctrl-Shift-] (Cmd-Alt-] on macOS): [`unfoldCode`](https://codemirror.net/6/docs/ref/#language.unfoldCode).
|
|
3976
|
+
- Ctrl-Alt-[: [`foldAll`](https://codemirror.net/6/docs/ref/#language.foldAll).
|
|
3977
|
+
- Ctrl-Alt-]: [`unfoldAll`](https://codemirror.net/6/docs/ref/#language.unfoldAll).
|
|
3978
|
+
*/
|
|
3979
|
+
const foldKeymap = [{
|
|
3980
|
+
key: "Ctrl-Shift-[",
|
|
3981
|
+
mac: "Cmd-Alt-[",
|
|
3982
|
+
run: foldCode
|
|
3983
|
+
}, {
|
|
3984
|
+
key: "Ctrl-Shift-]",
|
|
3985
|
+
mac: "Cmd-Alt-]",
|
|
3986
|
+
run: unfoldCode
|
|
3987
|
+
}, {
|
|
3988
|
+
key: "Ctrl-Alt-[",
|
|
3989
|
+
run: foldAll
|
|
3990
|
+
}, {
|
|
3991
|
+
key: "Ctrl-Alt-]",
|
|
3992
|
+
run: unfoldAll
|
|
3993
|
+
}];
|
|
3994
|
+
const defaultConfig = {
|
|
3995
|
+
placeholderDOM: null,
|
|
3996
|
+
placeholderText: "…"
|
|
3997
|
+
};
|
|
3998
|
+
const foldConfig = /*@__PURE__*/Facet.define({
|
|
3999
|
+
combine(values) {
|
|
4000
|
+
return combineConfig(values, defaultConfig);
|
|
4001
|
+
}
|
|
4002
|
+
});
|
|
4003
|
+
/**
|
|
4004
|
+
Create an extension that configures code folding.
|
|
4005
|
+
*/
|
|
4006
|
+
function codeFolding(config) {
|
|
4007
|
+
let result = [foldState, baseTheme$1];
|
|
4008
|
+
if (config) result.push(foldConfig.of(config));
|
|
4009
|
+
return result;
|
|
4010
|
+
}
|
|
4011
|
+
const foldWidget = /*@__PURE__*/Decoration.replace({
|
|
4012
|
+
widget: /*@__PURE__*/new class extends WidgetType {
|
|
4013
|
+
toDOM(view) {
|
|
4014
|
+
let {
|
|
4015
|
+
state
|
|
4016
|
+
} = view,
|
|
4017
|
+
conf = state.facet(foldConfig);
|
|
4018
|
+
let onclick = event => {
|
|
4019
|
+
let line = view.lineBlockAt(view.posAtDOM(event.target));
|
|
4020
|
+
let folded = findFold(view.state, line.from, line.to);
|
|
4021
|
+
if (folded) view.dispatch({
|
|
4022
|
+
effects: unfoldEffect.of(folded)
|
|
4023
|
+
});
|
|
4024
|
+
event.preventDefault();
|
|
4025
|
+
};
|
|
4026
|
+
if (conf.placeholderDOM) return conf.placeholderDOM(view, onclick);
|
|
4027
|
+
let element = document.createElement("span");
|
|
4028
|
+
element.textContent = conf.placeholderText;
|
|
4029
|
+
element.setAttribute("aria-label", state.phrase("folded code"));
|
|
4030
|
+
element.title = state.phrase("unfold");
|
|
4031
|
+
element.className = "cm-foldPlaceholder";
|
|
4032
|
+
element.onclick = onclick;
|
|
4033
|
+
return element;
|
|
4034
|
+
}
|
|
4035
|
+
}()
|
|
4036
|
+
});
|
|
4037
|
+
const foldGutterDefaults = {
|
|
4038
|
+
openText: "⌄",
|
|
4039
|
+
closedText: "›",
|
|
4040
|
+
markerDOM: null,
|
|
4041
|
+
domEventHandlers: {},
|
|
4042
|
+
foldingChanged: () => false
|
|
4043
|
+
};
|
|
4044
|
+
class FoldMarker extends GutterMarker {
|
|
4045
|
+
constructor(config, open) {
|
|
4046
|
+
super();
|
|
4047
|
+
this.config = config;
|
|
4048
|
+
this.open = open;
|
|
4049
|
+
}
|
|
4050
|
+
eq(other) {
|
|
4051
|
+
return this.config == other.config && this.open == other.open;
|
|
4052
|
+
}
|
|
4053
|
+
toDOM(view) {
|
|
4054
|
+
if (this.config.markerDOM) return this.config.markerDOM(this.open);
|
|
4055
|
+
let span = document.createElement("span");
|
|
4056
|
+
span.textContent = this.open ? this.config.openText : this.config.closedText;
|
|
4057
|
+
span.title = view.state.phrase(this.open ? "Fold line" : "Unfold line");
|
|
4058
|
+
return span;
|
|
4059
|
+
}
|
|
4060
|
+
}
|
|
4061
|
+
/**
|
|
4062
|
+
Create an extension that registers a fold gutter, which shows a
|
|
4063
|
+
fold status indicator before foldable lines (which can be clicked
|
|
4064
|
+
to fold or unfold the line).
|
|
4065
|
+
*/
|
|
4066
|
+
function foldGutter(config = {}) {
|
|
4067
|
+
let fullConfig = Object.assign(Object.assign({}, foldGutterDefaults), config);
|
|
4068
|
+
let canFold = new FoldMarker(fullConfig, true),
|
|
4069
|
+
canUnfold = new FoldMarker(fullConfig, false);
|
|
4070
|
+
let markers = ViewPlugin.fromClass(class {
|
|
4071
|
+
constructor(view) {
|
|
4072
|
+
this.from = view.viewport.from;
|
|
4073
|
+
this.markers = this.buildMarkers(view);
|
|
4074
|
+
}
|
|
4075
|
+
update(update) {
|
|
4076
|
+
if (update.docChanged || update.viewportChanged || update.startState.facet(language) != update.state.facet(language) || update.startState.field(foldState, false) != update.state.field(foldState, false) || syntaxTree(update.startState) != syntaxTree(update.state) || fullConfig.foldingChanged(update)) this.markers = this.buildMarkers(update.view);
|
|
4077
|
+
}
|
|
4078
|
+
buildMarkers(view) {
|
|
4079
|
+
let builder = new RangeSetBuilder();
|
|
4080
|
+
for (let line of view.viewportLineBlocks) {
|
|
4081
|
+
let mark = findFold(view.state, line.from, line.to) ? canUnfold : foldable(view.state, line.from, line.to) ? canFold : null;
|
|
4082
|
+
if (mark) builder.add(line.from, line.from, mark);
|
|
4083
|
+
}
|
|
4084
|
+
return builder.finish();
|
|
4085
|
+
}
|
|
4086
|
+
});
|
|
4087
|
+
let {
|
|
4088
|
+
domEventHandlers
|
|
4089
|
+
} = fullConfig;
|
|
4090
|
+
return [markers, gutter({
|
|
4091
|
+
class: "cm-foldGutter",
|
|
4092
|
+
markers(view) {
|
|
4093
|
+
var _a;
|
|
4094
|
+
return ((_a = view.plugin(markers)) === null || _a === void 0 ? void 0 : _a.markers) || RangeSet.empty;
|
|
4095
|
+
},
|
|
4096
|
+
initialSpacer() {
|
|
4097
|
+
return new FoldMarker(fullConfig, false);
|
|
4098
|
+
},
|
|
4099
|
+
domEventHandlers: Object.assign(Object.assign({}, domEventHandlers), {
|
|
4100
|
+
click: (view, line, event) => {
|
|
4101
|
+
if (domEventHandlers.click && domEventHandlers.click(view, line, event)) return true;
|
|
4102
|
+
let folded = findFold(view.state, line.from, line.to);
|
|
4103
|
+
if (folded) {
|
|
4104
|
+
view.dispatch({
|
|
4105
|
+
effects: unfoldEffect.of(folded)
|
|
4106
|
+
});
|
|
4107
|
+
return true;
|
|
4108
|
+
}
|
|
4109
|
+
let range = foldable(view.state, line.from, line.to);
|
|
4110
|
+
if (range) {
|
|
4111
|
+
view.dispatch({
|
|
4112
|
+
effects: foldEffect.of(range)
|
|
4113
|
+
});
|
|
4114
|
+
return true;
|
|
4115
|
+
}
|
|
4116
|
+
return false;
|
|
4117
|
+
}
|
|
4118
|
+
})
|
|
4119
|
+
}), codeFolding()];
|
|
4120
|
+
}
|
|
4121
|
+
const baseTheme$1 = /*@__PURE__*/EditorView.baseTheme({
|
|
4122
|
+
".cm-foldPlaceholder": {
|
|
4123
|
+
backgroundColor: "#eee",
|
|
4124
|
+
border: "1px solid #ddd",
|
|
4125
|
+
color: "#888",
|
|
4126
|
+
borderRadius: ".2em",
|
|
4127
|
+
margin: "0 1px",
|
|
4128
|
+
padding: "0 1px",
|
|
4129
|
+
cursor: "pointer"
|
|
4130
|
+
},
|
|
4131
|
+
".cm-foldGutter span": {
|
|
4132
|
+
padding: "0 1px",
|
|
4133
|
+
cursor: "pointer"
|
|
4134
|
+
}
|
|
4135
|
+
});
|
|
4136
|
+
|
|
4137
|
+
/**
|
|
4138
|
+
A highlight style associates CSS styles with higlighting
|
|
4139
|
+
[tags](https://lezer.codemirror.net/docs/ref#highlight.Tag).
|
|
4140
|
+
*/
|
|
4141
|
+
class HighlightStyle {
|
|
4142
|
+
constructor(
|
|
4143
|
+
/**
|
|
4144
|
+
The tag styles used to create this highlight style.
|
|
4145
|
+
*/
|
|
4146
|
+
specs, options) {
|
|
4147
|
+
this.specs = specs;
|
|
4148
|
+
let modSpec;
|
|
4149
|
+
function def(spec) {
|
|
4150
|
+
let cls = StyleModule.newName();
|
|
4151
|
+
(modSpec || (modSpec = Object.create(null)))["." + cls] = spec;
|
|
4152
|
+
return cls;
|
|
4153
|
+
}
|
|
4154
|
+
const all = typeof options.all == "string" ? options.all : options.all ? def(options.all) : undefined;
|
|
4155
|
+
const scopeOpt = options.scope;
|
|
4156
|
+
this.scope = scopeOpt instanceof Language ? type => type.prop(languageDataProp) == scopeOpt.data : scopeOpt ? type => type == scopeOpt : undefined;
|
|
4157
|
+
this.style = tagHighlighter(specs.map(style => ({
|
|
4158
|
+
tag: style.tag,
|
|
4159
|
+
class: style.class || def(Object.assign({}, style, {
|
|
4160
|
+
tag: null
|
|
4161
|
+
}))
|
|
4162
|
+
})), {
|
|
4163
|
+
all
|
|
4164
|
+
}).style;
|
|
4165
|
+
this.module = modSpec ? new StyleModule(modSpec) : null;
|
|
4166
|
+
this.themeType = options.themeType;
|
|
4167
|
+
}
|
|
4168
|
+
/**
|
|
4169
|
+
Create a highlighter style that associates the given styles to
|
|
4170
|
+
the given tags. The specs must be objects that hold a style tag
|
|
4171
|
+
or array of tags in their `tag` property, and either a single
|
|
4172
|
+
`class` property providing a static CSS class (for highlighter
|
|
4173
|
+
that rely on external styling), or a
|
|
4174
|
+
[`style-mod`](https://github.com/marijnh/style-mod#documentation)-style
|
|
4175
|
+
set of CSS properties (which define the styling for those tags).
|
|
4176
|
+
|
|
4177
|
+
The CSS rules created for a highlighter will be emitted in the
|
|
4178
|
+
order of the spec's properties. That means that for elements that
|
|
4179
|
+
have multiple tags associated with them, styles defined further
|
|
4180
|
+
down in the list will have a higher CSS precedence than styles
|
|
4181
|
+
defined earlier.
|
|
4182
|
+
*/
|
|
4183
|
+
static define(specs, options) {
|
|
4184
|
+
return new HighlightStyle(specs, options || {});
|
|
4185
|
+
}
|
|
4186
|
+
}
|
|
4187
|
+
const highlighterFacet = /*@__PURE__*/Facet.define();
|
|
4188
|
+
const fallbackHighlighter = /*@__PURE__*/Facet.define({
|
|
4189
|
+
combine(values) {
|
|
4190
|
+
return values.length ? [values[0]] : null;
|
|
4191
|
+
}
|
|
4192
|
+
});
|
|
4193
|
+
function getHighlighters(state) {
|
|
4194
|
+
let main = state.facet(highlighterFacet);
|
|
4195
|
+
return main.length ? main : state.facet(fallbackHighlighter);
|
|
4196
|
+
}
|
|
4197
|
+
/**
|
|
4198
|
+
Wrap a highlighter in an editor extension that uses it to apply
|
|
4199
|
+
syntax highlighting to the editor content.
|
|
4200
|
+
|
|
4201
|
+
When multiple (non-fallback) styles are provided, the styling
|
|
4202
|
+
applied is the union of the classes they emit.
|
|
4203
|
+
*/
|
|
4204
|
+
function syntaxHighlighting(highlighter, options) {
|
|
4205
|
+
let ext = [treeHighlighter],
|
|
4206
|
+
themeType;
|
|
4207
|
+
if (highlighter instanceof HighlightStyle) {
|
|
4208
|
+
if (highlighter.module) ext.push(EditorView.styleModule.of(highlighter.module));
|
|
4209
|
+
themeType = highlighter.themeType;
|
|
4210
|
+
}
|
|
4211
|
+
if (options === null || options === void 0 ? void 0 : options.fallback) ext.push(fallbackHighlighter.of(highlighter));else if (themeType) ext.push(highlighterFacet.computeN([EditorView.darkTheme], state => {
|
|
4212
|
+
return state.facet(EditorView.darkTheme) == (themeType == "dark") ? [highlighter] : [];
|
|
4213
|
+
}));else ext.push(highlighterFacet.of(highlighter));
|
|
4214
|
+
return ext;
|
|
4215
|
+
}
|
|
4216
|
+
class TreeHighlighter {
|
|
4217
|
+
constructor(view) {
|
|
4218
|
+
this.markCache = Object.create(null);
|
|
4219
|
+
this.tree = syntaxTree(view.state);
|
|
4220
|
+
this.decorations = this.buildDeco(view, getHighlighters(view.state));
|
|
4221
|
+
}
|
|
4222
|
+
update(update) {
|
|
4223
|
+
let tree = syntaxTree(update.state),
|
|
4224
|
+
highlighters = getHighlighters(update.state);
|
|
4225
|
+
let styleChange = highlighters != getHighlighters(update.startState);
|
|
4226
|
+
if (tree.length < update.view.viewport.to && !styleChange && tree.type == this.tree.type) {
|
|
4227
|
+
this.decorations = this.decorations.map(update.changes);
|
|
4228
|
+
} else if (tree != this.tree || update.viewportChanged || styleChange) {
|
|
4229
|
+
this.tree = tree;
|
|
4230
|
+
this.decorations = this.buildDeco(update.view, highlighters);
|
|
4231
|
+
}
|
|
4232
|
+
}
|
|
4233
|
+
buildDeco(view, highlighters) {
|
|
4234
|
+
if (!highlighters || !this.tree.length) return Decoration.none;
|
|
4235
|
+
let builder = new RangeSetBuilder();
|
|
4236
|
+
for (let {
|
|
4237
|
+
from,
|
|
4238
|
+
to
|
|
4239
|
+
} of view.visibleRanges) {
|
|
4240
|
+
highlightTree(this.tree, highlighters, (from, to, style) => {
|
|
4241
|
+
builder.add(from, to, this.markCache[style] || (this.markCache[style] = Decoration.mark({
|
|
4242
|
+
class: style
|
|
4243
|
+
})));
|
|
4244
|
+
}, from, to);
|
|
4245
|
+
}
|
|
4246
|
+
return builder.finish();
|
|
4247
|
+
}
|
|
4248
|
+
}
|
|
4249
|
+
const treeHighlighter = /*@__PURE__*/Prec.high( /*@__PURE__*/ViewPlugin.fromClass(TreeHighlighter, {
|
|
4250
|
+
decorations: v => v.decorations
|
|
4251
|
+
}));
|
|
4252
|
+
/**
|
|
4253
|
+
A default highlight style (works well with light themes).
|
|
4254
|
+
*/
|
|
4255
|
+
const defaultHighlightStyle = /*@__PURE__*/HighlightStyle.define([{
|
|
4256
|
+
tag: tags.meta,
|
|
4257
|
+
color: "#404740"
|
|
4258
|
+
}, {
|
|
4259
|
+
tag: tags.link,
|
|
4260
|
+
textDecoration: "underline"
|
|
4261
|
+
}, {
|
|
4262
|
+
tag: tags.heading,
|
|
4263
|
+
textDecoration: "underline",
|
|
4264
|
+
fontWeight: "bold"
|
|
4265
|
+
}, {
|
|
4266
|
+
tag: tags.emphasis,
|
|
4267
|
+
fontStyle: "italic"
|
|
4268
|
+
}, {
|
|
4269
|
+
tag: tags.strong,
|
|
4270
|
+
fontWeight: "bold"
|
|
4271
|
+
}, {
|
|
4272
|
+
tag: tags.strikethrough,
|
|
4273
|
+
textDecoration: "line-through"
|
|
4274
|
+
}, {
|
|
4275
|
+
tag: tags.keyword,
|
|
4276
|
+
color: "#708"
|
|
4277
|
+
}, {
|
|
4278
|
+
tag: [tags.atom, tags.bool, tags.url, tags.contentSeparator, tags.labelName],
|
|
4279
|
+
color: "#219"
|
|
4280
|
+
}, {
|
|
4281
|
+
tag: [tags.literal, tags.inserted],
|
|
4282
|
+
color: "#164"
|
|
4283
|
+
}, {
|
|
4284
|
+
tag: [tags.string, tags.deleted],
|
|
4285
|
+
color: "#a11"
|
|
4286
|
+
}, {
|
|
4287
|
+
tag: [tags.regexp, tags.escape, /*@__PURE__*/tags.special(tags.string)],
|
|
4288
|
+
color: "#e40"
|
|
4289
|
+
}, {
|
|
4290
|
+
tag: /*@__PURE__*/tags.definition(tags.variableName),
|
|
4291
|
+
color: "#00f"
|
|
4292
|
+
}, {
|
|
4293
|
+
tag: /*@__PURE__*/tags.local(tags.variableName),
|
|
4294
|
+
color: "#30a"
|
|
4295
|
+
}, {
|
|
4296
|
+
tag: [tags.typeName, tags.namespace],
|
|
4297
|
+
color: "#085"
|
|
4298
|
+
}, {
|
|
4299
|
+
tag: tags.className,
|
|
4300
|
+
color: "#167"
|
|
4301
|
+
}, {
|
|
4302
|
+
tag: [/*@__PURE__*/tags.special(tags.variableName), tags.macroName],
|
|
4303
|
+
color: "#256"
|
|
4304
|
+
}, {
|
|
4305
|
+
tag: /*@__PURE__*/tags.definition(tags.propertyName),
|
|
4306
|
+
color: "#00c"
|
|
4307
|
+
}, {
|
|
4308
|
+
tag: tags.comment,
|
|
4309
|
+
color: "#940"
|
|
4310
|
+
}, {
|
|
4311
|
+
tag: tags.invalid,
|
|
4312
|
+
color: "#f00"
|
|
4313
|
+
}]);
|
|
4314
|
+
const baseTheme = /*@__PURE__*/EditorView.baseTheme({
|
|
4315
|
+
"&.cm-focused .cm-matchingBracket": {
|
|
4316
|
+
backgroundColor: "#328c8252"
|
|
4317
|
+
},
|
|
4318
|
+
"&.cm-focused .cm-nonmatchingBracket": {
|
|
4319
|
+
backgroundColor: "#bb555544"
|
|
4320
|
+
}
|
|
4321
|
+
});
|
|
4322
|
+
const DefaultScanDist = 10000,
|
|
4323
|
+
DefaultBrackets = "()[]{}";
|
|
4324
|
+
const bracketMatchingConfig = /*@__PURE__*/Facet.define({
|
|
4325
|
+
combine(configs) {
|
|
4326
|
+
return combineConfig(configs, {
|
|
4327
|
+
afterCursor: true,
|
|
4328
|
+
brackets: DefaultBrackets,
|
|
4329
|
+
maxScanDistance: DefaultScanDist,
|
|
4330
|
+
renderMatch: defaultRenderMatch
|
|
4331
|
+
});
|
|
4332
|
+
}
|
|
4333
|
+
});
|
|
4334
|
+
const matchingMark = /*@__PURE__*/Decoration.mark({
|
|
4335
|
+
class: "cm-matchingBracket"
|
|
4336
|
+
}),
|
|
4337
|
+
nonmatchingMark = /*@__PURE__*/Decoration.mark({
|
|
4338
|
+
class: "cm-nonmatchingBracket"
|
|
4339
|
+
});
|
|
4340
|
+
function defaultRenderMatch(match) {
|
|
4341
|
+
let decorations = [];
|
|
4342
|
+
let mark = match.matched ? matchingMark : nonmatchingMark;
|
|
4343
|
+
decorations.push(mark.range(match.start.from, match.start.to));
|
|
4344
|
+
if (match.end) decorations.push(mark.range(match.end.from, match.end.to));
|
|
4345
|
+
return decorations;
|
|
4346
|
+
}
|
|
4347
|
+
const bracketMatchingState = /*@__PURE__*/StateField.define({
|
|
4348
|
+
create() {
|
|
4349
|
+
return Decoration.none;
|
|
4350
|
+
},
|
|
4351
|
+
update(deco, tr) {
|
|
4352
|
+
if (!tr.docChanged && !tr.selection) return deco;
|
|
4353
|
+
let decorations = [];
|
|
4354
|
+
let config = tr.state.facet(bracketMatchingConfig);
|
|
4355
|
+
for (let range of tr.state.selection.ranges) {
|
|
4356
|
+
if (!range.empty) continue;
|
|
4357
|
+
let match = matchBrackets(tr.state, range.head, -1, config) || range.head > 0 && matchBrackets(tr.state, range.head - 1, 1, config) || config.afterCursor && (matchBrackets(tr.state, range.head, 1, config) || range.head < tr.state.doc.length && matchBrackets(tr.state, range.head + 1, -1, config));
|
|
4358
|
+
if (match) decorations = decorations.concat(config.renderMatch(match, tr.state));
|
|
4359
|
+
}
|
|
4360
|
+
return Decoration.set(decorations, true);
|
|
4361
|
+
},
|
|
4362
|
+
provide: f => EditorView.decorations.from(f)
|
|
4363
|
+
});
|
|
4364
|
+
const bracketMatchingUnique = [bracketMatchingState, baseTheme];
|
|
4365
|
+
/**
|
|
4366
|
+
Create an extension that enables bracket matching. Whenever the
|
|
4367
|
+
cursor is next to a bracket, that bracket and the one it matches
|
|
4368
|
+
are highlighted. Or, when no matching bracket is found, another
|
|
4369
|
+
highlighting style is used to indicate this.
|
|
4370
|
+
*/
|
|
4371
|
+
function bracketMatching(config = {}) {
|
|
4372
|
+
return [bracketMatchingConfig.of(config), bracketMatchingUnique];
|
|
4373
|
+
}
|
|
4374
|
+
/**
|
|
4375
|
+
When larger syntax nodes, such as HTML tags, are marked as
|
|
4376
|
+
opening/closing, it can be a bit messy to treat the whole node as
|
|
4377
|
+
a matchable bracket. This node prop allows you to define, for such
|
|
4378
|
+
a node, a ‘handle’—the part of the node that is highlighted, and
|
|
4379
|
+
that the cursor must be on to activate highlighting in the first
|
|
4380
|
+
place.
|
|
4381
|
+
*/
|
|
4382
|
+
const bracketMatchingHandle = /*@__PURE__*/new NodeProp();
|
|
4383
|
+
function matchingNodes(node, dir, brackets) {
|
|
4384
|
+
let byProp = node.prop(dir < 0 ? NodeProp.openedBy : NodeProp.closedBy);
|
|
4385
|
+
if (byProp) return byProp;
|
|
4386
|
+
if (node.name.length == 1) {
|
|
4387
|
+
let index = brackets.indexOf(node.name);
|
|
4388
|
+
if (index > -1 && index % 2 == (dir < 0 ? 1 : 0)) return [brackets[index + dir]];
|
|
4389
|
+
}
|
|
4390
|
+
return null;
|
|
4391
|
+
}
|
|
4392
|
+
function findHandle(node) {
|
|
4393
|
+
let hasHandle = node.type.prop(bracketMatchingHandle);
|
|
4394
|
+
return hasHandle ? hasHandle(node.node) : node;
|
|
4395
|
+
}
|
|
4396
|
+
/**
|
|
4397
|
+
Find the matching bracket for the token at `pos`, scanning
|
|
4398
|
+
direction `dir`. Only the `brackets` and `maxScanDistance`
|
|
4399
|
+
properties are used from `config`, if given. Returns null if no
|
|
4400
|
+
bracket was found at `pos`, or a match result otherwise.
|
|
4401
|
+
*/
|
|
4402
|
+
function matchBrackets(state, pos, dir, config = {}) {
|
|
4403
|
+
let maxScanDistance = config.maxScanDistance || DefaultScanDist,
|
|
4404
|
+
brackets = config.brackets || DefaultBrackets;
|
|
4405
|
+
let tree = syntaxTree(state),
|
|
4406
|
+
node = tree.resolveInner(pos, dir);
|
|
4407
|
+
for (let cur = node; cur; cur = cur.parent) {
|
|
4408
|
+
let matches = matchingNodes(cur.type, dir, brackets);
|
|
4409
|
+
if (matches && cur.from < cur.to) {
|
|
4410
|
+
let handle = findHandle(cur);
|
|
4411
|
+
if (handle && (dir > 0 ? pos >= handle.from && pos < handle.to : pos > handle.from && pos <= handle.to)) return matchMarkedBrackets(state, pos, dir, cur, handle, matches, brackets);
|
|
4412
|
+
}
|
|
4413
|
+
}
|
|
4414
|
+
return matchPlainBrackets(state, pos, dir, tree, node.type, maxScanDistance, brackets);
|
|
4415
|
+
}
|
|
4416
|
+
function matchMarkedBrackets(_state, _pos, dir, token, handle, matching, brackets) {
|
|
4417
|
+
let parent = token.parent,
|
|
4418
|
+
firstToken = {
|
|
4419
|
+
from: handle.from,
|
|
4420
|
+
to: handle.to
|
|
4421
|
+
};
|
|
4422
|
+
let depth = 0,
|
|
4423
|
+
cursor = parent === null || parent === void 0 ? void 0 : parent.cursor();
|
|
4424
|
+
if (cursor && (dir < 0 ? cursor.childBefore(token.from) : cursor.childAfter(token.to))) do {
|
|
4425
|
+
if (dir < 0 ? cursor.to <= token.from : cursor.from >= token.to) {
|
|
4426
|
+
if (depth == 0 && matching.indexOf(cursor.type.name) > -1 && cursor.from < cursor.to) {
|
|
4427
|
+
let endHandle = findHandle(cursor);
|
|
4428
|
+
return {
|
|
4429
|
+
start: firstToken,
|
|
4430
|
+
end: endHandle ? {
|
|
4431
|
+
from: endHandle.from,
|
|
4432
|
+
to: endHandle.to
|
|
4433
|
+
} : undefined,
|
|
4434
|
+
matched: true
|
|
4435
|
+
};
|
|
4436
|
+
} else if (matchingNodes(cursor.type, dir, brackets)) {
|
|
4437
|
+
depth++;
|
|
4438
|
+
} else if (matchingNodes(cursor.type, -dir, brackets)) {
|
|
4439
|
+
if (depth == 0) {
|
|
4440
|
+
let endHandle = findHandle(cursor);
|
|
4441
|
+
return {
|
|
4442
|
+
start: firstToken,
|
|
4443
|
+
end: endHandle && endHandle.from < endHandle.to ? {
|
|
4444
|
+
from: endHandle.from,
|
|
4445
|
+
to: endHandle.to
|
|
4446
|
+
} : undefined,
|
|
4447
|
+
matched: false
|
|
4448
|
+
};
|
|
4449
|
+
}
|
|
4450
|
+
depth--;
|
|
4451
|
+
}
|
|
4452
|
+
}
|
|
4453
|
+
} while (dir < 0 ? cursor.prevSibling() : cursor.nextSibling());
|
|
4454
|
+
return {
|
|
4455
|
+
start: firstToken,
|
|
4456
|
+
matched: false
|
|
4457
|
+
};
|
|
4458
|
+
}
|
|
4459
|
+
function matchPlainBrackets(state, pos, dir, tree, tokenType, maxScanDistance, brackets) {
|
|
4460
|
+
let startCh = dir < 0 ? state.sliceDoc(pos - 1, pos) : state.sliceDoc(pos, pos + 1);
|
|
4461
|
+
let bracket = brackets.indexOf(startCh);
|
|
4462
|
+
if (bracket < 0 || bracket % 2 == 0 != dir > 0) return null;
|
|
4463
|
+
let startToken = {
|
|
4464
|
+
from: dir < 0 ? pos - 1 : pos,
|
|
4465
|
+
to: dir > 0 ? pos + 1 : pos
|
|
4466
|
+
};
|
|
4467
|
+
let iter = state.doc.iterRange(pos, dir > 0 ? state.doc.length : 0),
|
|
4468
|
+
depth = 0;
|
|
4469
|
+
for (let distance = 0; !iter.next().done && distance <= maxScanDistance;) {
|
|
4470
|
+
let text = iter.value;
|
|
4471
|
+
if (dir < 0) distance += text.length;
|
|
4472
|
+
let basePos = pos + distance * dir;
|
|
4473
|
+
for (let pos = dir > 0 ? 0 : text.length - 1, end = dir > 0 ? text.length : -1; pos != end; pos += dir) {
|
|
4474
|
+
let found = brackets.indexOf(text[pos]);
|
|
4475
|
+
if (found < 0 || tree.resolveInner(basePos + pos, 1).type != tokenType) continue;
|
|
4476
|
+
if (found % 2 == 0 == dir > 0) {
|
|
4477
|
+
depth++;
|
|
4478
|
+
} else if (depth == 1) {
|
|
4479
|
+
// Closing
|
|
4480
|
+
return {
|
|
4481
|
+
start: startToken,
|
|
4482
|
+
end: {
|
|
4483
|
+
from: basePos + pos,
|
|
4484
|
+
to: basePos + pos + 1
|
|
4485
|
+
},
|
|
4486
|
+
matched: found >> 1 == bracket >> 1
|
|
4487
|
+
};
|
|
4488
|
+
} else {
|
|
4489
|
+
depth--;
|
|
4490
|
+
}
|
|
4491
|
+
}
|
|
4492
|
+
if (dir > 0) distance += text.length;
|
|
4493
|
+
}
|
|
4494
|
+
return iter.done ? {
|
|
4495
|
+
start: startToken,
|
|
4496
|
+
matched: false
|
|
4497
|
+
} : null;
|
|
4498
|
+
}
|
|
4499
|
+
const noTokens = /*@__PURE__*/Object.create(null);
|
|
4500
|
+
const typeArray = [NodeType.none];
|
|
4501
|
+
const warned = [];
|
|
4502
|
+
const defaultTable = /*@__PURE__*/Object.create(null);
|
|
4503
|
+
for (let [legacyName, name] of [["variable", "variableName"], ["variable-2", "variableName.special"], ["string-2", "string.special"], ["def", "variableName.definition"], ["tag", "tagName"], ["attribute", "attributeName"], ["type", "typeName"], ["builtin", "variableName.standard"], ["qualifier", "modifier"], ["error", "invalid"], ["header", "heading"], ["property", "propertyName"]]) defaultTable[legacyName] = /*@__PURE__*/createTokenType(noTokens, name);
|
|
4504
|
+
function warnForPart(part, msg) {
|
|
4505
|
+
if (warned.indexOf(part) > -1) return;
|
|
4506
|
+
warned.push(part);
|
|
4507
|
+
console.warn(msg);
|
|
4508
|
+
}
|
|
4509
|
+
function createTokenType(extra, tagStr) {
|
|
4510
|
+
let tag = null;
|
|
4511
|
+
for (let part of tagStr.split(".")) {
|
|
4512
|
+
let value = extra[part] || tags[part];
|
|
4513
|
+
if (!value) {
|
|
4514
|
+
warnForPart(part, `Unknown highlighting tag ${part}`);
|
|
4515
|
+
} else if (typeof value == "function") {
|
|
4516
|
+
if (!tag) warnForPart(part, `Modifier ${part} used at start of tag`);else tag = value(tag);
|
|
4517
|
+
} else {
|
|
4518
|
+
if (tag) warnForPart(part, `Tag ${part} used as modifier`);else tag = value;
|
|
4519
|
+
}
|
|
4520
|
+
}
|
|
4521
|
+
if (!tag) return 0;
|
|
4522
|
+
let name = tagStr.replace(/ /g, "_"),
|
|
4523
|
+
type = NodeType.define({
|
|
4524
|
+
id: typeArray.length,
|
|
4525
|
+
name,
|
|
4526
|
+
props: [styleTags({
|
|
4527
|
+
[name]: tag
|
|
4528
|
+
})]
|
|
4529
|
+
});
|
|
4530
|
+
typeArray.push(type);
|
|
4531
|
+
return type.id;
|
|
4532
|
+
}
|
|
4533
|
+
|
|
4534
|
+
export { LanguageDescription as A, ParseContext as B, IterMode as C, DefaultBufferLength as D, flatIndent as E, delimitedIndent as F, NodeWeakMap as G, HighlightStyle as H, IndentContext as I, LanguageSupport as L, NodeProp as N, Parser as P, Tag as T, indentUnit as a, getIndentUnit as b, indentOnInput as c, syntaxHighlighting as d, defaultHighlightStyle as e, foldGutter as f, getIndentation as g, bracketMatching as h, indentString as i, foldKeymap as j, styleTags as k, LRLanguage as l, matchBrackets as m, indentNodeProp as n, continuedIndent as o, parseMixed as p, foldNodeProp as q, foldInside as r, syntaxTree as s, tags as t, NodeType as u, NodeSet as v, Tree as w, Language as x, defineLanguageFacet as y, languageDataProp as z };
|