@maketribe/ms-app 3.0.36 → 3.0.37

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.
Files changed (88) hide show
  1. package/dist/cjs/dataview/member/MemberTable.js +2 -2
  2. package/dist/cjs/dataview/member/MemberTable.js.map +1 -1
  3. package/dist/cjs/dataview/menu/MenuTable.js +33 -0
  4. package/dist/cjs/dataview/menu/MenuTable.js.map +1 -1
  5. package/dist/cjs/layouts/components/header/tools.js +7 -2
  6. package/dist/cjs/layouts/components/header/tools.js.map +1 -1
  7. package/dist/cjs/node_modules/.pnpm/{@codemirror_autocomplete@6.11.1_@codemirror_language@6.9.3_@codemirror_state@6.3.2_@codemirro_dop6xooh6ucho44vivbvrw3b2e → @codemirror_autocomplete@6.15.0_@codemirror_language@6.10.1_@codemirror_state@6.4.1_@codemirr_djjyqdn7bhwopzrz2h2s2bwhzm}/node_modules/@codemirror/autocomplete/dist/index.js +77 -25
  8. package/dist/cjs/node_modules/.pnpm/@codemirror_autocomplete@6.15.0_@codemirror_language@6.10.1_@codemirror_state@6.4.1_@codemirr_djjyqdn7bhwopzrz2h2s2bwhzm/node_modules/@codemirror/autocomplete/dist/index.js.map +1 -0
  9. package/dist/cjs/node_modules/.pnpm/@codemirror_commands@6.3.2/node_modules/@codemirror/commands/dist/index.js +7 -1433
  10. package/dist/cjs/node_modules/.pnpm/@codemirror_commands@6.3.2/node_modules/@codemirror/commands/dist/index.js.map +1 -1
  11. package/dist/cjs/node_modules/.pnpm/@codemirror_commands@6.3.3/node_modules/@codemirror/commands/dist/index.js +1484 -0
  12. package/dist/cjs/node_modules/.pnpm/@codemirror_commands@6.3.3/node_modules/@codemirror/commands/dist/index.js.map +1 -0
  13. package/dist/cjs/node_modules/.pnpm/@codemirror_language@6.10.1/node_modules/@codemirror/language/dist/index.js +1751 -0
  14. package/dist/cjs/node_modules/.pnpm/@codemirror_language@6.10.1/node_modules/@codemirror/language/dist/index.js.map +1 -0
  15. package/dist/cjs/node_modules/.pnpm/@codemirror_language@6.9.3/node_modules/@codemirror/language/dist/index.js +0 -1030
  16. package/dist/cjs/node_modules/.pnpm/@codemirror_language@6.9.3/node_modules/@codemirror/language/dist/index.js.map +1 -1
  17. package/dist/cjs/node_modules/.pnpm/{@codemirror_lint@6.4.2 → @codemirror_lint@6.5.0}/node_modules/@codemirror/lint/dist/index.js +8 -7
  18. package/dist/cjs/node_modules/.pnpm/@codemirror_lint@6.5.0/node_modules/@codemirror/lint/dist/index.js.map +1 -0
  19. package/dist/cjs/node_modules/.pnpm/{@codemirror_search@6.5.5 → @codemirror_search@6.5.6}/node_modules/@codemirror/search/dist/index.js +9 -9
  20. package/dist/cjs/node_modules/.pnpm/@codemirror_search@6.5.6/node_modules/@codemirror/search/dist/index.js.map +1 -0
  21. package/dist/cjs/node_modules/.pnpm/@codemirror_state@6.3.2/node_modules/@codemirror/state/dist/index.js +0 -42
  22. package/dist/cjs/node_modules/.pnpm/@codemirror_state@6.3.2/node_modules/@codemirror/state/dist/index.js.map +1 -1
  23. package/dist/cjs/node_modules/.pnpm/@codemirror_state@6.4.1/node_modules/@codemirror/state/dist/index.js +3958 -0
  24. package/dist/cjs/node_modules/.pnpm/@codemirror_state@6.4.1/node_modules/@codemirror/state/dist/index.js.map +1 -0
  25. package/dist/cjs/node_modules/.pnpm/@codemirror_view@6.22.1/node_modules/@codemirror/view/dist/index.js +3 -2161
  26. package/dist/cjs/node_modules/.pnpm/@codemirror_view@6.22.1/node_modules/@codemirror/view/dist/index.js.map +1 -1
  27. package/dist/cjs/node_modules/.pnpm/@codemirror_view@6.26.1/node_modules/@codemirror/view/dist/index.js +10580 -0
  28. package/dist/cjs/node_modules/.pnpm/@codemirror_view@6.26.1/node_modules/@codemirror/view/dist/index.js.map +1 -0
  29. package/dist/cjs/node_modules/.pnpm/@lezer_common@1.2.1/node_modules/@lezer/common/dist/index.js +1668 -0
  30. package/dist/cjs/node_modules/.pnpm/@lezer_common@1.2.1/node_modules/@lezer/common/dist/index.js.map +1 -0
  31. package/dist/cjs/node_modules/.pnpm/{codemirror@6.0.1_@lezer_common@1.1.1 → codemirror@6.0.1_@lezer_common@1.2.1}/node_modules/codemirror/dist/index.js +7 -7
  32. package/dist/cjs/node_modules/.pnpm/{codemirror@6.0.1_@lezer_common@1.1.1 → codemirror@6.0.1_@lezer_common@1.2.1}/node_modules/codemirror/dist/index.js.map +1 -1
  33. package/dist/cjs/node_modules/.pnpm/style-mod@4.1.2/node_modules/style-mod/src/style-mod.js +177 -0
  34. package/dist/cjs/node_modules/.pnpm/style-mod@4.1.2/node_modules/style-mod/src/style-mod.js.map +1 -0
  35. package/dist/cjs/node_modules/.pnpm/vue-codemirror@6.1.1_codemirror@6.0.1_vue@3.3.4/node_modules/vue-codemirror/dist/vue-codemirror.esm.js +1 -1
  36. package/dist/esm/dataview/member/MemberTable.js +2 -2
  37. package/dist/esm/dataview/member/MemberTable.js.map +1 -1
  38. package/dist/esm/dataview/menu/MenuTable.js +34 -1
  39. package/dist/esm/dataview/menu/MenuTable.js.map +1 -1
  40. package/dist/esm/layouts/components/header/tools.js +7 -2
  41. package/dist/esm/layouts/components/header/tools.js.map +1 -1
  42. package/dist/esm/node_modules/.pnpm/{@codemirror_autocomplete@6.11.1_@codemirror_language@6.9.3_@codemirror_state@6.3.2_@codemirro_dop6xooh6ucho44vivbvrw3b2e → @codemirror_autocomplete@6.15.0_@codemirror_language@6.10.1_@codemirror_state@6.4.1_@codemirr_djjyqdn7bhwopzrz2h2s2bwhzm}/node_modules/@codemirror/autocomplete/dist/index.js +77 -25
  43. package/dist/esm/node_modules/.pnpm/@codemirror_autocomplete@6.15.0_@codemirror_language@6.10.1_@codemirror_state@6.4.1_@codemirr_djjyqdn7bhwopzrz2h2s2bwhzm/node_modules/@codemirror/autocomplete/dist/index.js.map +1 -0
  44. package/dist/esm/node_modules/.pnpm/@codemirror_commands@6.3.2/node_modules/@codemirror/commands/dist/index.js +4 -1357
  45. package/dist/esm/node_modules/.pnpm/@codemirror_commands@6.3.2/node_modules/@codemirror/commands/dist/index.js.map +1 -1
  46. package/dist/esm/node_modules/.pnpm/@codemirror_commands@6.3.3/node_modules/@codemirror/commands/dist/index.js +1408 -0
  47. package/dist/esm/node_modules/.pnpm/@codemirror_commands@6.3.3/node_modules/@codemirror/commands/dist/index.js.map +1 -0
  48. package/dist/esm/node_modules/.pnpm/@codemirror_language@6.10.1/node_modules/@codemirror/language/dist/index.js +1715 -0
  49. package/dist/esm/node_modules/.pnpm/@codemirror_language@6.10.1/node_modules/@codemirror/language/dist/index.js.map +1 -0
  50. package/dist/esm/node_modules/.pnpm/@codemirror_language@6.9.3/node_modules/@codemirror/language/dist/index.js +4 -1011
  51. package/dist/esm/node_modules/.pnpm/@codemirror_language@6.9.3/node_modules/@codemirror/language/dist/index.js.map +1 -1
  52. package/dist/esm/node_modules/.pnpm/{@codemirror_lint@6.4.2 → @codemirror_lint@6.5.0}/node_modules/@codemirror/lint/dist/index.js +8 -7
  53. package/dist/esm/node_modules/.pnpm/@codemirror_lint@6.5.0/node_modules/@codemirror/lint/dist/index.js.map +1 -0
  54. package/dist/esm/node_modules/.pnpm/{@codemirror_search@6.5.5 → @codemirror_search@6.5.6}/node_modules/@codemirror/search/dist/index.js +9 -9
  55. package/dist/esm/node_modules/.pnpm/@codemirror_search@6.5.6/node_modules/@codemirror/search/dist/index.js.map +1 -0
  56. package/dist/esm/node_modules/.pnpm/@codemirror_state@6.3.2/node_modules/@codemirror/state/dist/index.js +1 -41
  57. package/dist/esm/node_modules/.pnpm/@codemirror_state@6.3.2/node_modules/@codemirror/state/dist/index.js.map +1 -1
  58. package/dist/esm/node_modules/.pnpm/@codemirror_state@6.4.1/node_modules/@codemirror/state/dist/index.js +3930 -0
  59. package/dist/esm/node_modules/.pnpm/@codemirror_state@6.4.1/node_modules/@codemirror/state/dist/index.js.map +1 -0
  60. package/dist/esm/node_modules/.pnpm/@codemirror_view@6.22.1/node_modules/@codemirror/view/dist/index.js +5 -2142
  61. package/dist/esm/node_modules/.pnpm/@codemirror_view@6.22.1/node_modules/@codemirror/view/dist/index.js.map +1 -1
  62. package/dist/esm/node_modules/.pnpm/@codemirror_view@6.26.1/node_modules/@codemirror/view/dist/index.js +10548 -0
  63. package/dist/esm/node_modules/.pnpm/@codemirror_view@6.26.1/node_modules/@codemirror/view/dist/index.js.map +1 -0
  64. package/dist/esm/node_modules/.pnpm/@lezer_common@1.2.1/node_modules/@lezer/common/dist/index.js +1658 -0
  65. package/dist/esm/node_modules/.pnpm/@lezer_common@1.2.1/node_modules/@lezer/common/dist/index.js.map +1 -0
  66. package/dist/esm/node_modules/.pnpm/{codemirror@6.0.1_@lezer_common@1.1.1 → codemirror@6.0.1_@lezer_common@1.2.1}/node_modules/codemirror/dist/index.js +8 -8
  67. package/dist/esm/node_modules/.pnpm/{codemirror@6.0.1_@lezer_common@1.1.1 → codemirror@6.0.1_@lezer_common@1.2.1}/node_modules/codemirror/dist/index.js.map +1 -1
  68. package/dist/esm/node_modules/.pnpm/style-mod@4.1.2/node_modules/style-mod/src/style-mod.js +175 -0
  69. package/dist/esm/node_modules/.pnpm/style-mod@4.1.2/node_modules/style-mod/src/style-mod.js.map +1 -0
  70. package/dist/esm/node_modules/.pnpm/vue-codemirror@6.1.1_codemirror@6.0.1_vue@3.3.4/node_modules/vue-codemirror/dist/vue-codemirror.esm.js +1 -1
  71. package/dist/style/components/classify-search.css +1 -1
  72. package/dist/style/components/index.css +1 -1
  73. package/dist/style/index.css +2 -2
  74. package/dist/style/layouts/default/header.css +1 -1
  75. package/dist/style/layouts/default/index.css +1 -1
  76. package/dist/style/layouts/index.css +1 -1
  77. package/dist/style/src/components/classify-search.scss +5 -2
  78. package/dist/style/src/layouts/default/header.scss +4 -0
  79. package/package.json +4 -4
  80. package/src/dataview/member/MemberTable.tsx +2 -2
  81. package/src/dataview/menu/MenuTable.ts +34 -1
  82. package/src/layouts/components/header/tools.tsx +6 -1
  83. package/dist/cjs/node_modules/.pnpm/@codemirror_autocomplete@6.11.1_@codemirror_language@6.9.3_@codemirror_state@6.3.2_@codemirro_dop6xooh6ucho44vivbvrw3b2e/node_modules/@codemirror/autocomplete/dist/index.js.map +0 -1
  84. package/dist/cjs/node_modules/.pnpm/@codemirror_lint@6.4.2/node_modules/@codemirror/lint/dist/index.js.map +0 -1
  85. package/dist/cjs/node_modules/.pnpm/@codemirror_search@6.5.5/node_modules/@codemirror/search/dist/index.js.map +0 -1
  86. package/dist/esm/node_modules/.pnpm/@codemirror_autocomplete@6.11.1_@codemirror_language@6.9.3_@codemirror_state@6.3.2_@codemirro_dop6xooh6ucho44vivbvrw3b2e/node_modules/@codemirror/autocomplete/dist/index.js.map +0 -1
  87. package/dist/esm/node_modules/.pnpm/@codemirror_lint@6.4.2/node_modules/@codemirror/lint/dist/index.js.map +0 -1
  88. package/dist/esm/node_modules/.pnpm/@codemirror_search@6.5.5/node_modules/@codemirror/search/dist/index.js.map +0 -1
@@ -0,0 +1,1668 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ The default maximum length of a `TreeBuffer` node.
5
+ */
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
+ /**
15
+ Each [node type](#common.NodeType) or [individual tree](#common.Tree)
16
+ can have metadata associated with it in props. Instances of this
17
+ class represent prop names.
18
+ */
19
+ class NodeProp {
20
+ /**
21
+ Create a new node prop type.
22
+ */
23
+ constructor(config = {}) {
24
+ this.id = nextPropID++;
25
+ this.perNode = !!config.perNode;
26
+ this.deserialize = config.deserialize || (() => {
27
+ throw new Error("This node type doesn't define a deserialize function");
28
+ });
29
+ }
30
+ /**
31
+ This is meant to be used with
32
+ [`NodeSet.extend`](#common.NodeSet.extend) or
33
+ [`LRParser.configure`](#lr.ParserConfig.props) to compute
34
+ prop values for each node type in the set. Takes a [match
35
+ object](#common.NodeType^match) or function that returns undefined
36
+ if the node type doesn't get this prop, and the prop's value if
37
+ it does.
38
+ */
39
+ add(match) {
40
+ if (this.perNode)
41
+ throw new RangeError("Can't add per-node props to node types");
42
+ if (typeof match != "function")
43
+ match = NodeType.match(match);
44
+ return (type) => {
45
+ let result = match(type);
46
+ return result === undefined ? null : [this, result];
47
+ };
48
+ }
49
+ }
50
+ /**
51
+ Prop that is used to describe matching delimiters. For opening
52
+ delimiters, this holds an array of node names (written as a
53
+ space-separated string when declaring this prop in a grammar)
54
+ for the node types of closing delimiters that match it.
55
+ */
56
+ NodeProp.closedBy = new NodeProp({ deserialize: str => str.split(" ") });
57
+ /**
58
+ The inverse of [`closedBy`](#common.NodeProp^closedBy). This is
59
+ attached to closing delimiters, holding an array of node names
60
+ of types of matching opening delimiters.
61
+ */
62
+ NodeProp.openedBy = new NodeProp({ deserialize: str => str.split(" ") });
63
+ /**
64
+ Used to assign node types to groups (for example, all node
65
+ types that represent an expression could be tagged with an
66
+ `"Expression"` group).
67
+ */
68
+ NodeProp.group = new NodeProp({ deserialize: str => str.split(" ") });
69
+ /**
70
+ Attached to nodes to indicate these should be
71
+ [displayed](https://codemirror.net/docs/ref/#language.syntaxTree)
72
+ in a bidirectional text isolate, so that direction-neutral
73
+ characters on their sides don't incorrectly get associated with
74
+ surrounding text. You'll generally want to set this for nodes
75
+ that contain arbitrary text, like strings and comments, and for
76
+ nodes that appear _inside_ arbitrary text, like HTML tags. When
77
+ not given a value, in a grammar declaration, defaults to
78
+ `"auto"`.
79
+ */
80
+ NodeProp.isolate = new NodeProp({ deserialize: value => {
81
+ if (value && value != "rtl" && value != "ltr" && value != "auto")
82
+ throw new RangeError("Invalid value for isolate: " + value);
83
+ return value || "auto";
84
+ } });
85
+ /**
86
+ The hash of the [context](#lr.ContextTracker.constructor)
87
+ that the node was parsed in, if any. Used to limit reuse of
88
+ contextual nodes.
89
+ */
90
+ NodeProp.contextHash = new NodeProp({ perNode: true });
91
+ /**
92
+ The distance beyond the end of the node that the tokenizer
93
+ looked ahead for any of the tokens inside the node. (The LR
94
+ parser only stores this when it is larger than 25, for
95
+ efficiency reasons.)
96
+ */
97
+ NodeProp.lookAhead = new NodeProp({ perNode: true });
98
+ /**
99
+ This per-node prop is used to replace a given node, or part of a
100
+ node, with another tree. This is useful to include trees from
101
+ different languages in mixed-language parsers.
102
+ */
103
+ NodeProp.mounted = new NodeProp({ perNode: true });
104
+ /**
105
+ A mounted tree, which can be [stored](#common.NodeProp^mounted) on
106
+ a tree node to indicate that parts of its content are
107
+ represented by another tree.
108
+ */
109
+ class MountedTree {
110
+ constructor(
111
+ /**
112
+ The inner tree.
113
+ */
114
+ tree,
115
+ /**
116
+ If this is null, this tree replaces the entire node (it will
117
+ be included in the regular iteration instead of its host
118
+ node). If not, only the given ranges are considered to be
119
+ covered by this tree. This is used for trees that are mixed in
120
+ a way that isn't strictly hierarchical. Such mounted trees are
121
+ only entered by [`resolveInner`](#common.Tree.resolveInner)
122
+ and [`enter`](#common.SyntaxNode.enter).
123
+ */
124
+ overlay,
125
+ /**
126
+ The parser used to create this subtree.
127
+ */
128
+ parser) {
129
+ this.tree = tree;
130
+ this.overlay = overlay;
131
+ this.parser = parser;
132
+ }
133
+ /**
134
+ @internal
135
+ */
136
+ static get(tree) {
137
+ return tree && tree.props && tree.props[NodeProp.mounted.id];
138
+ }
139
+ }
140
+ const noProps = Object.create(null);
141
+ /**
142
+ Each node in a syntax tree has a node type associated with it.
143
+ */
144
+ class NodeType {
145
+ /**
146
+ @internal
147
+ */
148
+ constructor(
149
+ /**
150
+ The name of the node type. Not necessarily unique, but if the
151
+ grammar was written properly, different node types with the
152
+ same name within a node set should play the same semantic
153
+ role.
154
+ */
155
+ name,
156
+ /**
157
+ @internal
158
+ */
159
+ props,
160
+ /**
161
+ The id of this node in its set. Corresponds to the term ids
162
+ used in the parser.
163
+ */
164
+ id,
165
+ /**
166
+ @internal
167
+ */
168
+ flags = 0) {
169
+ this.name = name;
170
+ this.props = props;
171
+ this.id = id;
172
+ this.flags = flags;
173
+ }
174
+ /**
175
+ Define a node type.
176
+ */
177
+ static define(spec) {
178
+ let props = spec.props && spec.props.length ? Object.create(null) : noProps;
179
+ let flags = (spec.top ? 1 /* NodeFlag.Top */ : 0) | (spec.skipped ? 2 /* NodeFlag.Skipped */ : 0) |
180
+ (spec.error ? 4 /* NodeFlag.Error */ : 0) | (spec.name == null ? 8 /* NodeFlag.Anonymous */ : 0);
181
+ let type = new NodeType(spec.name || "", props, spec.id, flags);
182
+ if (spec.props)
183
+ for (let src of spec.props) {
184
+ if (!Array.isArray(src))
185
+ src = src(type);
186
+ if (src) {
187
+ if (src[0].perNode)
188
+ throw new RangeError("Can't store a per-node prop on a node type");
189
+ props[src[0].id] = src[1];
190
+ }
191
+ }
192
+ return type;
193
+ }
194
+ /**
195
+ Retrieves a node prop for this type. Will return `undefined` if
196
+ the prop isn't present on this node.
197
+ */
198
+ prop(prop) { return this.props[prop.id]; }
199
+ /**
200
+ True when this is the top node of a grammar.
201
+ */
202
+ get isTop() { return (this.flags & 1 /* NodeFlag.Top */) > 0; }
203
+ /**
204
+ True when this node is produced by a skip rule.
205
+ */
206
+ get isSkipped() { return (this.flags & 2 /* NodeFlag.Skipped */) > 0; }
207
+ /**
208
+ Indicates whether this is an error node.
209
+ */
210
+ get isError() { return (this.flags & 4 /* NodeFlag.Error */) > 0; }
211
+ /**
212
+ When true, this node type doesn't correspond to a user-declared
213
+ named node, for example because it is used to cache repetition.
214
+ */
215
+ get isAnonymous() { return (this.flags & 8 /* NodeFlag.Anonymous */) > 0; }
216
+ /**
217
+ Returns true when this node's name or one of its
218
+ [groups](#common.NodeProp^group) matches the given string.
219
+ */
220
+ is(name) {
221
+ if (typeof name == 'string') {
222
+ if (this.name == name)
223
+ return true;
224
+ let group = this.prop(NodeProp.group);
225
+ return group ? group.indexOf(name) > -1 : false;
226
+ }
227
+ return this.id == name;
228
+ }
229
+ /**
230
+ Create a function from node types to arbitrary values by
231
+ specifying an object whose property names are node or
232
+ [group](#common.NodeProp^group) names. Often useful with
233
+ [`NodeProp.add`](#common.NodeProp.add). You can put multiple
234
+ names, separated by spaces, in a single property name to map
235
+ multiple node names to a single value.
236
+ */
237
+ static match(map) {
238
+ let direct = Object.create(null);
239
+ for (let prop in map)
240
+ for (let name of prop.split(" "))
241
+ direct[name] = map[prop];
242
+ return (node) => {
243
+ for (let groups = node.prop(NodeProp.group), i = -1; i < (groups ? groups.length : 0); i++) {
244
+ let found = direct[i < 0 ? node.name : groups[i]];
245
+ if (found)
246
+ return found;
247
+ }
248
+ };
249
+ }
250
+ }
251
+ /**
252
+ An empty dummy node type to use when no actual type is available.
253
+ */
254
+ NodeType.none = new NodeType("", Object.create(null), 0, 8 /* NodeFlag.Anonymous */);
255
+ const CachedNode = new WeakMap(), CachedInnerNode = new WeakMap();
256
+ /**
257
+ Options that control iteration. Can be combined with the `|`
258
+ operator to enable multiple ones.
259
+ */
260
+ exports.IterMode = void 0;
261
+ (function (IterMode) {
262
+ /**
263
+ When enabled, iteration will only visit [`Tree`](#common.Tree)
264
+ objects, not nodes packed into
265
+ [`TreeBuffer`](#common.TreeBuffer)s.
266
+ */
267
+ IterMode[IterMode["ExcludeBuffers"] = 1] = "ExcludeBuffers";
268
+ /**
269
+ Enable this to make iteration include anonymous nodes (such as
270
+ the nodes that wrap repeated grammar constructs into a balanced
271
+ tree).
272
+ */
273
+ IterMode[IterMode["IncludeAnonymous"] = 2] = "IncludeAnonymous";
274
+ /**
275
+ By default, regular [mounted](#common.NodeProp^mounted) nodes
276
+ replace their base node in iteration. Enable this to ignore them
277
+ instead.
278
+ */
279
+ IterMode[IterMode["IgnoreMounts"] = 4] = "IgnoreMounts";
280
+ /**
281
+ This option only applies in
282
+ [`enter`](#common.SyntaxNode.enter)-style methods. It tells the
283
+ library to not enter mounted overlays if one covers the given
284
+ position.
285
+ */
286
+ IterMode[IterMode["IgnoreOverlays"] = 8] = "IgnoreOverlays";
287
+ })(exports.IterMode || (exports.IterMode = {}));
288
+ /**
289
+ A piece of syntax tree. There are two ways to approach these
290
+ trees: the way they are actually stored in memory, and the
291
+ convenient way.
292
+
293
+ Syntax trees are stored as a tree of `Tree` and `TreeBuffer`
294
+ objects. By packing detail information into `TreeBuffer` leaf
295
+ nodes, the representation is made a lot more memory-efficient.
296
+
297
+ However, when you want to actually work with tree nodes, this
298
+ representation is very awkward, so most client code will want to
299
+ use the [`TreeCursor`](#common.TreeCursor) or
300
+ [`SyntaxNode`](#common.SyntaxNode) interface instead, which provides
301
+ a view on some part of this data structure, and can be used to
302
+ move around to adjacent nodes.
303
+ */
304
+ class Tree {
305
+ /**
306
+ Construct a new tree. See also [`Tree.build`](#common.Tree^build).
307
+ */
308
+ constructor(
309
+ /**
310
+ The type of the top node.
311
+ */
312
+ type,
313
+ /**
314
+ This node's child nodes.
315
+ */
316
+ children,
317
+ /**
318
+ The positions (offsets relative to the start of this tree) of
319
+ the children.
320
+ */
321
+ positions,
322
+ /**
323
+ The total length of this tree
324
+ */
325
+ length,
326
+ /**
327
+ Per-node [node props](#common.NodeProp) to associate with this node.
328
+ */
329
+ props) {
330
+ this.type = type;
331
+ this.children = children;
332
+ this.positions = positions;
333
+ this.length = length;
334
+ /**
335
+ @internal
336
+ */
337
+ this.props = null;
338
+ if (props && props.length) {
339
+ this.props = Object.create(null);
340
+ for (let [prop, value] of props)
341
+ this.props[typeof prop == "number" ? prop : prop.id] = value;
342
+ }
343
+ }
344
+ /**
345
+ @internal
346
+ */
347
+ toString() {
348
+ let mounted = MountedTree.get(this);
349
+ if (mounted && !mounted.overlay)
350
+ return mounted.tree.toString();
351
+ let children = "";
352
+ for (let ch of this.children) {
353
+ let str = ch.toString();
354
+ if (str) {
355
+ if (children)
356
+ children += ",";
357
+ children += str;
358
+ }
359
+ }
360
+ return !this.type.name ? children :
361
+ (/\W/.test(this.type.name) && !this.type.isError ? JSON.stringify(this.type.name) : this.type.name) +
362
+ (children.length ? "(" + children + ")" : "");
363
+ }
364
+ /**
365
+ Get a [tree cursor](#common.TreeCursor) positioned at the top of
366
+ the tree. Mode can be used to [control](#common.IterMode) which
367
+ nodes the cursor visits.
368
+ */
369
+ cursor(mode = 0) {
370
+ return new TreeCursor(this.topNode, mode);
371
+ }
372
+ /**
373
+ Get a [tree cursor](#common.TreeCursor) pointing into this tree
374
+ at the given position and side (see
375
+ [`moveTo`](#common.TreeCursor.moveTo).
376
+ */
377
+ cursorAt(pos, side = 0, mode = 0) {
378
+ let scope = CachedNode.get(this) || this.topNode;
379
+ let cursor = new TreeCursor(scope);
380
+ cursor.moveTo(pos, side);
381
+ CachedNode.set(this, cursor._tree);
382
+ return cursor;
383
+ }
384
+ /**
385
+ Get a [syntax node](#common.SyntaxNode) object for the top of the
386
+ tree.
387
+ */
388
+ get topNode() {
389
+ return new TreeNode(this, 0, 0, null);
390
+ }
391
+ /**
392
+ Get the [syntax node](#common.SyntaxNode) at the given position.
393
+ If `side` is -1, this will move into nodes that end at the
394
+ position. If 1, it'll move into nodes that start at the
395
+ position. With 0, it'll only enter nodes that cover the position
396
+ from both sides.
397
+
398
+ Note that this will not enter
399
+ [overlays](#common.MountedTree.overlay), and you often want
400
+ [`resolveInner`](#common.Tree.resolveInner) instead.
401
+ */
402
+ resolve(pos, side = 0) {
403
+ let node = resolveNode(CachedNode.get(this) || this.topNode, pos, side, false);
404
+ CachedNode.set(this, node);
405
+ return node;
406
+ }
407
+ /**
408
+ Like [`resolve`](#common.Tree.resolve), but will enter
409
+ [overlaid](#common.MountedTree.overlay) nodes, producing a syntax node
410
+ pointing into the innermost overlaid tree at the given position
411
+ (with parent links going through all parent structure, including
412
+ the host trees).
413
+ */
414
+ resolveInner(pos, side = 0) {
415
+ let node = resolveNode(CachedInnerNode.get(this) || this.topNode, pos, side, true);
416
+ CachedInnerNode.set(this, node);
417
+ return node;
418
+ }
419
+ /**
420
+ In some situations, it can be useful to iterate through all
421
+ nodes around a position, including those in overlays that don't
422
+ directly cover the position. This method gives you an iterator
423
+ that will produce all nodes, from small to big, around the given
424
+ position.
425
+ */
426
+ resolveStack(pos, side = 0) {
427
+ return stackIterator(this, pos, side);
428
+ }
429
+ /**
430
+ Iterate over the tree and its children, calling `enter` for any
431
+ node that touches the `from`/`to` region (if given) before
432
+ running over such a node's children, and `leave` (if given) when
433
+ leaving the node. When `enter` returns `false`, that node will
434
+ not have its children iterated over (or `leave` called).
435
+ */
436
+ iterate(spec) {
437
+ let { enter, leave, from = 0, to = this.length } = spec;
438
+ let mode = spec.mode || 0, anon = (mode & exports.IterMode.IncludeAnonymous) > 0;
439
+ for (let c = this.cursor(mode | exports.IterMode.IncludeAnonymous);;) {
440
+ let entered = false;
441
+ if (c.from <= to && c.to >= from && (!anon && c.type.isAnonymous || enter(c) !== false)) {
442
+ if (c.firstChild())
443
+ continue;
444
+ entered = true;
445
+ }
446
+ for (;;) {
447
+ if (entered && leave && (anon || !c.type.isAnonymous))
448
+ leave(c);
449
+ if (c.nextSibling())
450
+ break;
451
+ if (!c.parent())
452
+ return;
453
+ entered = true;
454
+ }
455
+ }
456
+ }
457
+ /**
458
+ Get the value of the given [node prop](#common.NodeProp) for this
459
+ node. Works with both per-node and per-type props.
460
+ */
461
+ prop(prop) {
462
+ return !prop.perNode ? this.type.prop(prop) : this.props ? this.props[prop.id] : undefined;
463
+ }
464
+ /**
465
+ Returns the node's [per-node props](#common.NodeProp.perNode) in a
466
+ format that can be passed to the [`Tree`](#common.Tree)
467
+ constructor.
468
+ */
469
+ get propValues() {
470
+ let result = [];
471
+ if (this.props)
472
+ for (let id in this.props)
473
+ result.push([+id, this.props[id]]);
474
+ return result;
475
+ }
476
+ /**
477
+ Balance the direct children of this tree, producing a copy of
478
+ which may have children grouped into subtrees with type
479
+ [`NodeType.none`](#common.NodeType^none).
480
+ */
481
+ balance(config = {}) {
482
+ return this.children.length <= 8 /* Balance.BranchFactor */ ? this :
483
+ 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)));
484
+ }
485
+ /**
486
+ Build a tree from a postfix-ordered buffer of node information,
487
+ or a cursor over such a buffer.
488
+ */
489
+ static build(data) { return buildTree(data); }
490
+ }
491
+ /**
492
+ The empty tree
493
+ */
494
+ Tree.empty = new Tree(NodeType.none, [], [], 0);
495
+ class FlatBufferCursor {
496
+ constructor(buffer, index) {
497
+ this.buffer = buffer;
498
+ this.index = index;
499
+ }
500
+ get id() { return this.buffer[this.index - 4]; }
501
+ get start() { return this.buffer[this.index - 3]; }
502
+ get end() { return this.buffer[this.index - 2]; }
503
+ get size() { return this.buffer[this.index - 1]; }
504
+ get pos() { return this.index; }
505
+ next() { this.index -= 4; }
506
+ fork() { return new FlatBufferCursor(this.buffer, this.index); }
507
+ }
508
+ /**
509
+ Tree buffers contain (type, start, end, endIndex) quads for each
510
+ node. In such a buffer, nodes are stored in prefix order (parents
511
+ before children, with the endIndex of the parent indicating which
512
+ children belong to it).
513
+ */
514
+ class TreeBuffer {
515
+ /**
516
+ Create a tree buffer.
517
+ */
518
+ constructor(
519
+ /**
520
+ The buffer's content.
521
+ */
522
+ buffer,
523
+ /**
524
+ The total length of the group of nodes in the buffer.
525
+ */
526
+ length,
527
+ /**
528
+ The node set used in this buffer.
529
+ */
530
+ set) {
531
+ this.buffer = buffer;
532
+ this.length = length;
533
+ this.set = set;
534
+ }
535
+ /**
536
+ @internal
537
+ */
538
+ get type() { return NodeType.none; }
539
+ /**
540
+ @internal
541
+ */
542
+ toString() {
543
+ let result = [];
544
+ for (let index = 0; index < this.buffer.length;) {
545
+ result.push(this.childString(index));
546
+ index = this.buffer[index + 3];
547
+ }
548
+ return result.join(",");
549
+ }
550
+ /**
551
+ @internal
552
+ */
553
+ childString(index) {
554
+ let id = this.buffer[index], endIndex = this.buffer[index + 3];
555
+ let type = this.set.types[id], result = type.name;
556
+ if (/\W/.test(result) && !type.isError)
557
+ result = JSON.stringify(result);
558
+ index += 4;
559
+ if (endIndex == index)
560
+ return result;
561
+ let children = [];
562
+ while (index < endIndex) {
563
+ children.push(this.childString(index));
564
+ index = this.buffer[index + 3];
565
+ }
566
+ return result + "(" + children.join(",") + ")";
567
+ }
568
+ /**
569
+ @internal
570
+ */
571
+ findChild(startIndex, endIndex, dir, pos, side) {
572
+ let { buffer } = this, pick = -1;
573
+ for (let i = startIndex; i != endIndex; i = buffer[i + 3]) {
574
+ if (checkSide(side, pos, buffer[i + 1], buffer[i + 2])) {
575
+ pick = i;
576
+ if (dir > 0)
577
+ break;
578
+ }
579
+ }
580
+ return pick;
581
+ }
582
+ /**
583
+ @internal
584
+ */
585
+ slice(startI, endI, from) {
586
+ let b = this.buffer;
587
+ let copy = new Uint16Array(endI - startI), len = 0;
588
+ for (let i = startI, j = 0; i < endI;) {
589
+ copy[j++] = b[i++];
590
+ copy[j++] = b[i++] - from;
591
+ let to = copy[j++] = b[i++] - from;
592
+ copy[j++] = b[i++] - startI;
593
+ len = Math.max(len, to);
594
+ }
595
+ return new TreeBuffer(copy, len, this.set);
596
+ }
597
+ }
598
+ function checkSide(side, pos, from, to) {
599
+ switch (side) {
600
+ case -2 /* Side.Before */: return from < pos;
601
+ case -1 /* Side.AtOrBefore */: return to >= pos && from < pos;
602
+ case 0 /* Side.Around */: return from < pos && to > pos;
603
+ case 1 /* Side.AtOrAfter */: return from <= pos && to > pos;
604
+ case 2 /* Side.After */: return to > pos;
605
+ case 4 /* Side.DontCare */: return true;
606
+ }
607
+ }
608
+ function resolveNode(node, pos, side, overlays) {
609
+ var _a;
610
+ // Move up to a node that actually holds the position, if possible
611
+ while (node.from == node.to ||
612
+ (side < 1 ? node.from >= pos : node.from > pos) ||
613
+ (side > -1 ? node.to <= pos : node.to < pos)) {
614
+ let parent = !overlays && node instanceof TreeNode && node.index < 0 ? null : node.parent;
615
+ if (!parent)
616
+ return node;
617
+ node = parent;
618
+ }
619
+ let mode = overlays ? 0 : exports.IterMode.IgnoreOverlays;
620
+ // Must go up out of overlays when those do not overlap with pos
621
+ if (overlays)
622
+ for (let scan = node, parent = scan.parent; parent; scan = parent, parent = scan.parent) {
623
+ if (scan instanceof TreeNode && scan.index < 0 && ((_a = parent.enter(pos, side, mode)) === null || _a === void 0 ? void 0 : _a.from) != scan.from)
624
+ node = parent;
625
+ }
626
+ for (;;) {
627
+ let inner = node.enter(pos, side, mode);
628
+ if (!inner)
629
+ return node;
630
+ node = inner;
631
+ }
632
+ }
633
+ class BaseNode {
634
+ cursor(mode = 0) { return new TreeCursor(this, mode); }
635
+ getChild(type, before = null, after = null) {
636
+ let r = getChildren(this, type, before, after);
637
+ return r.length ? r[0] : null;
638
+ }
639
+ getChildren(type, before = null, after = null) {
640
+ return getChildren(this, type, before, after);
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
+ matchContext(context) {
649
+ return matchNodeContext(this, context);
650
+ }
651
+ enterUnfinishedNodesBefore(pos) {
652
+ let scan = this.childBefore(pos), node = this;
653
+ while (scan) {
654
+ let last = scan.lastChild;
655
+ if (!last || last.to != scan.to)
656
+ break;
657
+ if (last.type.isError && last.from == last.to) {
658
+ node = scan;
659
+ scan = last.prevSibling;
660
+ }
661
+ else {
662
+ scan = last;
663
+ }
664
+ }
665
+ return node;
666
+ }
667
+ get node() { return this; }
668
+ get next() { return this.parent; }
669
+ }
670
+ class TreeNode extends BaseNode {
671
+ constructor(_tree, from,
672
+ // Index in parent node, set to -1 if the node is not a direct child of _parent.node (overlay)
673
+ index, _parent) {
674
+ super();
675
+ this._tree = _tree;
676
+ this.from = from;
677
+ this.index = index;
678
+ this._parent = _parent;
679
+ }
680
+ get type() { return this._tree.type; }
681
+ get name() { return this._tree.type.name; }
682
+ get to() { return this.from + this._tree.length; }
683
+ nextChild(i, dir, pos, side, mode = 0) {
684
+ for (let parent = this;;) {
685
+ for (let { children, positions } = parent._tree, e = dir > 0 ? children.length : -1; i != e; i += dir) {
686
+ let next = children[i], start = positions[i] + parent.from;
687
+ if (!checkSide(side, pos, start, start + next.length))
688
+ continue;
689
+ if (next instanceof TreeBuffer) {
690
+ if (mode & exports.IterMode.ExcludeBuffers)
691
+ continue;
692
+ let index = next.findChild(0, next.buffer.length, dir, pos - start, side);
693
+ if (index > -1)
694
+ return new BufferNode(new BufferContext(parent, next, i, start), null, index);
695
+ }
696
+ else if ((mode & exports.IterMode.IncludeAnonymous) || (!next.type.isAnonymous || hasChild(next))) {
697
+ let mounted;
698
+ if (!(mode & exports.IterMode.IgnoreMounts) && (mounted = MountedTree.get(next)) && !mounted.overlay)
699
+ return new TreeNode(mounted.tree, start, i, parent);
700
+ let inner = new TreeNode(next, start, i, parent);
701
+ return (mode & exports.IterMode.IncludeAnonymous) || !inner.type.isAnonymous ? inner
702
+ : inner.nextChild(dir < 0 ? next.children.length - 1 : 0, dir, pos, side);
703
+ }
704
+ }
705
+ if ((mode & exports.IterMode.IncludeAnonymous) || !parent.type.isAnonymous)
706
+ return null;
707
+ if (parent.index >= 0)
708
+ i = parent.index + dir;
709
+ else
710
+ i = dir < 0 ? -1 : parent._parent._tree.children.length;
711
+ parent = parent._parent;
712
+ if (!parent)
713
+ return null;
714
+ }
715
+ }
716
+ get firstChild() { return this.nextChild(0, 1, 0, 4 /* Side.DontCare */); }
717
+ get lastChild() { return this.nextChild(this._tree.children.length - 1, -1, 0, 4 /* Side.DontCare */); }
718
+ childAfter(pos) { return this.nextChild(0, 1, pos, 2 /* Side.After */); }
719
+ childBefore(pos) { return this.nextChild(this._tree.children.length - 1, -1, pos, -2 /* Side.Before */); }
720
+ enter(pos, side, mode = 0) {
721
+ let mounted;
722
+ if (!(mode & exports.IterMode.IgnoreOverlays) && (mounted = MountedTree.get(this._tree)) && mounted.overlay) {
723
+ let rPos = pos - this.from;
724
+ for (let { from, to } of mounted.overlay) {
725
+ if ((side > 0 ? from <= rPos : from < rPos) &&
726
+ (side < 0 ? to >= rPos : to > rPos))
727
+ return new TreeNode(mounted.tree, mounted.overlay[0].from + this.from, -1, this);
728
+ }
729
+ }
730
+ return this.nextChild(0, 1, pos, side, mode);
731
+ }
732
+ nextSignificantParent() {
733
+ let val = this;
734
+ while (val.type.isAnonymous && val._parent)
735
+ val = val._parent;
736
+ return val;
737
+ }
738
+ get parent() {
739
+ return this._parent ? this._parent.nextSignificantParent() : null;
740
+ }
741
+ get nextSibling() {
742
+ return this._parent && this.index >= 0 ? this._parent.nextChild(this.index + 1, 1, 0, 4 /* Side.DontCare */) : null;
743
+ }
744
+ get prevSibling() {
745
+ return this._parent && this.index >= 0 ? this._parent.nextChild(this.index - 1, -1, 0, 4 /* Side.DontCare */) : null;
746
+ }
747
+ get tree() { return this._tree; }
748
+ toTree() { return this._tree; }
749
+ /**
750
+ @internal
751
+ */
752
+ toString() { return this._tree.toString(); }
753
+ }
754
+ function getChildren(node, type, before, after) {
755
+ let cur = node.cursor(), result = [];
756
+ if (!cur.firstChild())
757
+ return result;
758
+ if (before != null)
759
+ for (let found = false; !found;) {
760
+ found = cur.type.is(before);
761
+ if (!cur.nextSibling())
762
+ return result;
763
+ }
764
+ for (;;) {
765
+ if (after != null && cur.type.is(after))
766
+ return result;
767
+ if (cur.type.is(type))
768
+ result.push(cur.node);
769
+ if (!cur.nextSibling())
770
+ return after == null ? result : [];
771
+ }
772
+ }
773
+ function matchNodeContext(node, context, i = context.length - 1) {
774
+ for (let p = node.parent; i >= 0; p = p.parent) {
775
+ if (!p)
776
+ return false;
777
+ if (!p.type.isAnonymous) {
778
+ if (context[i] && context[i] != p.name)
779
+ return false;
780
+ i--;
781
+ }
782
+ }
783
+ return true;
784
+ }
785
+ class BufferContext {
786
+ constructor(parent, buffer, index, start) {
787
+ this.parent = parent;
788
+ this.buffer = buffer;
789
+ this.index = index;
790
+ this.start = start;
791
+ }
792
+ }
793
+ class BufferNode extends BaseNode {
794
+ get name() { return this.type.name; }
795
+ get from() { return this.context.start + this.context.buffer.buffer[this.index + 1]; }
796
+ get to() { return this.context.start + this.context.buffer.buffer[this.index + 2]; }
797
+ constructor(context, _parent, index) {
798
+ super();
799
+ this.context = context;
800
+ this._parent = _parent;
801
+ this.index = index;
802
+ this.type = context.buffer.set.types[context.buffer.buffer[index]];
803
+ }
804
+ child(dir, pos, side) {
805
+ let { buffer } = this.context;
806
+ let index = buffer.findChild(this.index + 4, buffer.buffer[this.index + 3], dir, pos - this.context.start, side);
807
+ return index < 0 ? null : new BufferNode(this.context, this, index);
808
+ }
809
+ get firstChild() { return this.child(1, 0, 4 /* Side.DontCare */); }
810
+ get lastChild() { return this.child(-1, 0, 4 /* Side.DontCare */); }
811
+ childAfter(pos) { return this.child(1, pos, 2 /* Side.After */); }
812
+ childBefore(pos) { return this.child(-1, pos, -2 /* Side.Before */); }
813
+ enter(pos, side, mode = 0) {
814
+ if (mode & exports.IterMode.ExcludeBuffers)
815
+ return null;
816
+ let { buffer } = this.context;
817
+ let index = buffer.findChild(this.index + 4, buffer.buffer[this.index + 3], side > 0 ? 1 : -1, pos - this.context.start, side);
818
+ return index < 0 ? null : new BufferNode(this.context, this, index);
819
+ }
820
+ get parent() {
821
+ return this._parent || this.context.parent.nextSignificantParent();
822
+ }
823
+ externalSibling(dir) {
824
+ return this._parent ? null : this.context.parent.nextChild(this.context.index + dir, dir, 0, 4 /* Side.DontCare */);
825
+ }
826
+ get nextSibling() {
827
+ let { buffer } = this.context;
828
+ let after = buffer.buffer[this.index + 3];
829
+ if (after < (this._parent ? buffer.buffer[this._parent.index + 3] : buffer.buffer.length))
830
+ return new BufferNode(this.context, this._parent, after);
831
+ return this.externalSibling(1);
832
+ }
833
+ get prevSibling() {
834
+ let { buffer } = this.context;
835
+ let parentStart = this._parent ? this._parent.index + 4 : 0;
836
+ if (this.index == parentStart)
837
+ return this.externalSibling(-1);
838
+ return new BufferNode(this.context, this._parent, buffer.findChild(parentStart, this.index, -1, 0, 4 /* Side.DontCare */));
839
+ }
840
+ get tree() { return null; }
841
+ toTree() {
842
+ let children = [], positions = [];
843
+ let { buffer } = this.context;
844
+ let startI = this.index + 4, endI = buffer.buffer[this.index + 3];
845
+ if (endI > startI) {
846
+ let from = buffer.buffer[this.index + 1];
847
+ children.push(buffer.slice(startI, endI, from));
848
+ positions.push(0);
849
+ }
850
+ return new Tree(this.type, children, positions, this.to - this.from);
851
+ }
852
+ /**
853
+ @internal
854
+ */
855
+ toString() { return this.context.buffer.childString(this.index); }
856
+ }
857
+ function iterStack(heads) {
858
+ if (!heads.length)
859
+ return null;
860
+ let pick = 0, picked = heads[0];
861
+ for (let i = 1; i < heads.length; i++) {
862
+ let node = heads[i];
863
+ if (node.from > picked.from || node.to < picked.to) {
864
+ picked = node;
865
+ pick = i;
866
+ }
867
+ }
868
+ let next = picked instanceof TreeNode && picked.index < 0 ? null : picked.parent;
869
+ let newHeads = heads.slice();
870
+ if (next)
871
+ newHeads[pick] = next;
872
+ else
873
+ newHeads.splice(pick, 1);
874
+ return new StackIterator(newHeads, picked);
875
+ }
876
+ class StackIterator {
877
+ constructor(heads, node) {
878
+ this.heads = heads;
879
+ this.node = node;
880
+ }
881
+ get next() { return iterStack(this.heads); }
882
+ }
883
+ function stackIterator(tree, pos, side) {
884
+ let inner = tree.resolveInner(pos, side), layers = null;
885
+ for (let scan = inner instanceof TreeNode ? inner : inner.context.parent; scan; scan = scan.parent) {
886
+ if (scan.index < 0) { // This is an overlay root
887
+ let parent = scan.parent;
888
+ (layers || (layers = [inner])).push(parent.resolve(pos, side));
889
+ scan = parent;
890
+ }
891
+ else {
892
+ let mount = MountedTree.get(scan.tree);
893
+ // Relevant overlay branching off
894
+ if (mount && mount.overlay && mount.overlay[0].from <= pos && mount.overlay[mount.overlay.length - 1].to >= pos) {
895
+ let root = new TreeNode(mount.tree, mount.overlay[0].from + scan.from, -1, scan);
896
+ (layers || (layers = [inner])).push(resolveNode(root, pos, side, false));
897
+ }
898
+ }
899
+ }
900
+ return layers ? iterStack(layers) : inner;
901
+ }
902
+ /**
903
+ A tree cursor object focuses on a given node in a syntax tree, and
904
+ allows you to move to adjacent nodes.
905
+ */
906
+ class TreeCursor {
907
+ /**
908
+ Shorthand for `.type.name`.
909
+ */
910
+ get name() { return this.type.name; }
911
+ /**
912
+ @internal
913
+ */
914
+ constructor(node,
915
+ /**
916
+ @internal
917
+ */
918
+ mode = 0) {
919
+ this.mode = mode;
920
+ /**
921
+ @internal
922
+ */
923
+ this.buffer = null;
924
+ this.stack = [];
925
+ /**
926
+ @internal
927
+ */
928
+ this.index = 0;
929
+ this.bufferNode = null;
930
+ if (node instanceof TreeNode) {
931
+ this.yieldNode(node);
932
+ }
933
+ else {
934
+ this._tree = node.context.parent;
935
+ this.buffer = node.context;
936
+ for (let n = node._parent; n; n = n._parent)
937
+ this.stack.unshift(n.index);
938
+ this.bufferNode = node;
939
+ this.yieldBuf(node.index);
940
+ }
941
+ }
942
+ yieldNode(node) {
943
+ if (!node)
944
+ return false;
945
+ this._tree = node;
946
+ this.type = node.type;
947
+ this.from = node.from;
948
+ this.to = node.to;
949
+ return true;
950
+ }
951
+ yieldBuf(index, type) {
952
+ this.index = index;
953
+ let { start, buffer } = this.buffer;
954
+ this.type = type || buffer.set.types[buffer.buffer[index]];
955
+ this.from = start + buffer.buffer[index + 1];
956
+ this.to = start + buffer.buffer[index + 2];
957
+ return true;
958
+ }
959
+ /**
960
+ @internal
961
+ */
962
+ yield(node) {
963
+ if (!node)
964
+ return false;
965
+ if (node instanceof TreeNode) {
966
+ this.buffer = null;
967
+ return this.yieldNode(node);
968
+ }
969
+ this.buffer = node.context;
970
+ return this.yieldBuf(node.index, node.type);
971
+ }
972
+ /**
973
+ @internal
974
+ */
975
+ toString() {
976
+ return this.buffer ? this.buffer.buffer.childString(this.index) : this._tree.toString();
977
+ }
978
+ /**
979
+ @internal
980
+ */
981
+ enterChild(dir, pos, side) {
982
+ if (!this.buffer)
983
+ return this.yield(this._tree.nextChild(dir < 0 ? this._tree._tree.children.length - 1 : 0, dir, pos, side, this.mode));
984
+ let { buffer } = this.buffer;
985
+ let index = buffer.findChild(this.index + 4, buffer.buffer[this.index + 3], dir, pos - this.buffer.start, side);
986
+ if (index < 0)
987
+ return false;
988
+ this.stack.push(this.index);
989
+ return this.yieldBuf(index);
990
+ }
991
+ /**
992
+ Move the cursor to this node's first child. When this returns
993
+ false, the node has no child, and the cursor has not been moved.
994
+ */
995
+ firstChild() { return this.enterChild(1, 0, 4 /* Side.DontCare */); }
996
+ /**
997
+ Move the cursor to this node's last child.
998
+ */
999
+ lastChild() { return this.enterChild(-1, 0, 4 /* Side.DontCare */); }
1000
+ /**
1001
+ Move the cursor to the first child that ends after `pos`.
1002
+ */
1003
+ childAfter(pos) { return this.enterChild(1, pos, 2 /* Side.After */); }
1004
+ /**
1005
+ Move to the last child that starts before `pos`.
1006
+ */
1007
+ childBefore(pos) { return this.enterChild(-1, pos, -2 /* Side.Before */); }
1008
+ /**
1009
+ Move the cursor to the child around `pos`. If side is -1 the
1010
+ child may end at that position, when 1 it may start there. This
1011
+ will also enter [overlaid](#common.MountedTree.overlay)
1012
+ [mounted](#common.NodeProp^mounted) trees unless `overlays` is
1013
+ set to false.
1014
+ */
1015
+ enter(pos, side, mode = this.mode) {
1016
+ if (!this.buffer)
1017
+ return this.yield(this._tree.enter(pos, side, mode));
1018
+ return mode & exports.IterMode.ExcludeBuffers ? false : this.enterChild(1, pos, side);
1019
+ }
1020
+ /**
1021
+ Move to the node's parent node, if this isn't the top node.
1022
+ */
1023
+ parent() {
1024
+ if (!this.buffer)
1025
+ return this.yieldNode((this.mode & exports.IterMode.IncludeAnonymous) ? this._tree._parent : this._tree.parent);
1026
+ if (this.stack.length)
1027
+ return this.yieldBuf(this.stack.pop());
1028
+ let parent = (this.mode & exports.IterMode.IncludeAnonymous) ? this.buffer.parent : this.buffer.parent.nextSignificantParent();
1029
+ this.buffer = null;
1030
+ return this.yieldNode(parent);
1031
+ }
1032
+ /**
1033
+ @internal
1034
+ */
1035
+ sibling(dir) {
1036
+ if (!this.buffer)
1037
+ return !this._tree._parent ? false
1038
+ : this.yield(this._tree.index < 0 ? null
1039
+ : this._tree._parent.nextChild(this._tree.index + dir, dir, 0, 4 /* Side.DontCare */, this.mode));
1040
+ let { buffer } = this.buffer, d = this.stack.length - 1;
1041
+ if (dir < 0) {
1042
+ let parentStart = d < 0 ? 0 : this.stack[d] + 4;
1043
+ if (this.index != parentStart)
1044
+ return this.yieldBuf(buffer.findChild(parentStart, this.index, -1, 0, 4 /* Side.DontCare */));
1045
+ }
1046
+ else {
1047
+ let after = buffer.buffer[this.index + 3];
1048
+ if (after < (d < 0 ? buffer.buffer.length : buffer.buffer[this.stack[d] + 3]))
1049
+ return this.yieldBuf(after);
1050
+ }
1051
+ return d < 0 ? this.yield(this.buffer.parent.nextChild(this.buffer.index + dir, dir, 0, 4 /* Side.DontCare */, this.mode)) : false;
1052
+ }
1053
+ /**
1054
+ Move to this node's next sibling, if any.
1055
+ */
1056
+ nextSibling() { return this.sibling(1); }
1057
+ /**
1058
+ Move to this node's previous sibling, if any.
1059
+ */
1060
+ prevSibling() { return this.sibling(-1); }
1061
+ atLastNode(dir) {
1062
+ let index, parent, { buffer } = this;
1063
+ if (buffer) {
1064
+ if (dir > 0) {
1065
+ if (this.index < buffer.buffer.buffer.length)
1066
+ return false;
1067
+ }
1068
+ else {
1069
+ for (let i = 0; i < this.index; i++)
1070
+ if (buffer.buffer.buffer[i + 3] < this.index)
1071
+ return false;
1072
+ }
1073
+ ({ index, parent } = buffer);
1074
+ }
1075
+ else {
1076
+ ({ index, _parent: parent } = this._tree);
1077
+ }
1078
+ for (; parent; { index, _parent: parent } = parent) {
1079
+ if (index > -1)
1080
+ for (let i = index + dir, e = dir < 0 ? -1 : parent._tree.children.length; i != e; i += dir) {
1081
+ let child = parent._tree.children[i];
1082
+ if ((this.mode & exports.IterMode.IncludeAnonymous) ||
1083
+ child instanceof TreeBuffer ||
1084
+ !child.type.isAnonymous ||
1085
+ hasChild(child))
1086
+ return false;
1087
+ }
1088
+ }
1089
+ return true;
1090
+ }
1091
+ move(dir, enter) {
1092
+ if (enter && this.enterChild(dir, 0, 4 /* Side.DontCare */))
1093
+ return true;
1094
+ for (;;) {
1095
+ if (this.sibling(dir))
1096
+ return true;
1097
+ if (this.atLastNode(dir) || !this.parent())
1098
+ return false;
1099
+ }
1100
+ }
1101
+ /**
1102
+ Move to the next node in a
1103
+ [pre-order](https://en.wikipedia.org/wiki/Tree_traversal#Pre-order,_NLR)
1104
+ traversal, going from a node to its first child or, if the
1105
+ current node is empty or `enter` is false, its next sibling or
1106
+ the next sibling of the first parent node that has one.
1107
+ */
1108
+ next(enter = true) { return this.move(1, enter); }
1109
+ /**
1110
+ Move to the next node in a last-to-first pre-order traveral. A
1111
+ node is followed by its last child or, if it has none, its
1112
+ previous sibling or the previous sibling of the first parent
1113
+ node that has one.
1114
+ */
1115
+ prev(enter = true) { return this.move(-1, enter); }
1116
+ /**
1117
+ Move the cursor to the innermost node that covers `pos`. If
1118
+ `side` is -1, it will enter nodes that end at `pos`. If it is 1,
1119
+ it will enter nodes that start at `pos`.
1120
+ */
1121
+ moveTo(pos, side = 0) {
1122
+ // Move up to a node that actually holds the position, if possible
1123
+ while (this.from == this.to ||
1124
+ (side < 1 ? this.from >= pos : this.from > pos) ||
1125
+ (side > -1 ? this.to <= pos : this.to < pos))
1126
+ if (!this.parent())
1127
+ break;
1128
+ // Then scan down into child nodes as far as possible
1129
+ while (this.enterChild(1, pos, side)) { }
1130
+ return this;
1131
+ }
1132
+ /**
1133
+ Get a [syntax node](#common.SyntaxNode) at the cursor's current
1134
+ position.
1135
+ */
1136
+ get node() {
1137
+ if (!this.buffer)
1138
+ return this._tree;
1139
+ let cache = this.bufferNode, result = null, depth = 0;
1140
+ if (cache && cache.context == this.buffer) {
1141
+ scan: for (let index = this.index, d = this.stack.length; d >= 0;) {
1142
+ for (let c = cache; c; c = c._parent)
1143
+ if (c.index == index) {
1144
+ if (index == this.index)
1145
+ return c;
1146
+ result = c;
1147
+ depth = d + 1;
1148
+ break scan;
1149
+ }
1150
+ index = this.stack[--d];
1151
+ }
1152
+ }
1153
+ for (let i = depth; i < this.stack.length; i++)
1154
+ result = new BufferNode(this.buffer, result, this.stack[i]);
1155
+ return this.bufferNode = new BufferNode(this.buffer, result, this.index);
1156
+ }
1157
+ /**
1158
+ Get the [tree](#common.Tree) that represents the current node, if
1159
+ any. Will return null when the node is in a [tree
1160
+ buffer](#common.TreeBuffer).
1161
+ */
1162
+ get tree() {
1163
+ return this.buffer ? null : this._tree._tree;
1164
+ }
1165
+ /**
1166
+ Iterate over the current node and all its descendants, calling
1167
+ `enter` when entering a node and `leave`, if given, when leaving
1168
+ one. When `enter` returns `false`, any children of that node are
1169
+ skipped, and `leave` isn't called for it.
1170
+ */
1171
+ iterate(enter, leave) {
1172
+ for (let depth = 0;;) {
1173
+ let mustLeave = false;
1174
+ if (this.type.isAnonymous || enter(this) !== false) {
1175
+ if (this.firstChild()) {
1176
+ depth++;
1177
+ continue;
1178
+ }
1179
+ if (!this.type.isAnonymous)
1180
+ mustLeave = true;
1181
+ }
1182
+ for (;;) {
1183
+ if (mustLeave && leave)
1184
+ leave(this);
1185
+ mustLeave = this.type.isAnonymous;
1186
+ if (this.nextSibling())
1187
+ break;
1188
+ if (!depth)
1189
+ return;
1190
+ this.parent();
1191
+ depth--;
1192
+ mustLeave = true;
1193
+ }
1194
+ }
1195
+ }
1196
+ /**
1197
+ Test whether the current node matches a given context—a sequence
1198
+ of direct parent node names. Empty strings in the context array
1199
+ are treated as wildcards.
1200
+ */
1201
+ matchContext(context) {
1202
+ if (!this.buffer)
1203
+ return matchNodeContext(this.node, context);
1204
+ let { buffer } = this.buffer, { types } = buffer.set;
1205
+ for (let i = context.length - 1, d = this.stack.length - 1; i >= 0; d--) {
1206
+ if (d < 0)
1207
+ return matchNodeContext(this.node, context, i);
1208
+ let type = types[buffer.buffer[this.stack[d]]];
1209
+ if (!type.isAnonymous) {
1210
+ if (context[i] && context[i] != type.name)
1211
+ return false;
1212
+ i--;
1213
+ }
1214
+ }
1215
+ return true;
1216
+ }
1217
+ }
1218
+ function hasChild(tree) {
1219
+ return tree.children.some(ch => ch instanceof TreeBuffer || !ch.type.isAnonymous || hasChild(ch));
1220
+ }
1221
+ function buildTree(data) {
1222
+ var _a;
1223
+ let { buffer, nodeSet, maxBufferLength = DefaultBufferLength, reused = [], minRepeatType = nodeSet.types.length } = data;
1224
+ let cursor = Array.isArray(buffer) ? new FlatBufferCursor(buffer, buffer.length) : buffer;
1225
+ let types = nodeSet.types;
1226
+ let contextHash = 0, lookAhead = 0;
1227
+ function takeNode(parentStart, minPos, children, positions, inRepeat, depth) {
1228
+ let { id, start, end, size } = cursor;
1229
+ let lookAheadAtStart = lookAhead;
1230
+ while (size < 0) {
1231
+ cursor.next();
1232
+ if (size == -1 /* SpecialRecord.Reuse */) {
1233
+ let node = reused[id];
1234
+ children.push(node);
1235
+ positions.push(start - parentStart);
1236
+ return;
1237
+ }
1238
+ else if (size == -3 /* SpecialRecord.ContextChange */) { // Context change
1239
+ contextHash = id;
1240
+ return;
1241
+ }
1242
+ else if (size == -4 /* SpecialRecord.LookAhead */) {
1243
+ lookAhead = id;
1244
+ return;
1245
+ }
1246
+ else {
1247
+ throw new RangeError(`Unrecognized record size: ${size}`);
1248
+ }
1249
+ }
1250
+ let type = types[id], node, buffer;
1251
+ let startPos = start - parentStart;
1252
+ if (end - start <= maxBufferLength && (buffer = findBufferSize(cursor.pos - minPos, inRepeat))) {
1253
+ // Small enough for a buffer, and no reused nodes inside
1254
+ let data = new Uint16Array(buffer.size - buffer.skip);
1255
+ let endPos = cursor.pos - buffer.size, index = data.length;
1256
+ while (cursor.pos > endPos)
1257
+ index = copyToBuffer(buffer.start, data, index);
1258
+ node = new TreeBuffer(data, end - buffer.start, nodeSet);
1259
+ startPos = buffer.start - parentStart;
1260
+ }
1261
+ else { // Make it a node
1262
+ let endPos = cursor.pos - size;
1263
+ cursor.next();
1264
+ let localChildren = [], localPositions = [];
1265
+ let localInRepeat = id >= minRepeatType ? id : -1;
1266
+ let lastGroup = 0, lastEnd = end;
1267
+ while (cursor.pos > endPos) {
1268
+ if (localInRepeat >= 0 && cursor.id == localInRepeat && cursor.size >= 0) {
1269
+ if (cursor.end <= lastEnd - maxBufferLength) {
1270
+ makeRepeatLeaf(localChildren, localPositions, start, lastGroup, cursor.end, lastEnd, localInRepeat, lookAheadAtStart);
1271
+ lastGroup = localChildren.length;
1272
+ lastEnd = cursor.end;
1273
+ }
1274
+ cursor.next();
1275
+ }
1276
+ else if (depth > 2500 /* CutOff.Depth */) {
1277
+ takeFlatNode(start, endPos, localChildren, localPositions);
1278
+ }
1279
+ else {
1280
+ takeNode(start, endPos, localChildren, localPositions, localInRepeat, depth + 1);
1281
+ }
1282
+ }
1283
+ if (localInRepeat >= 0 && lastGroup > 0 && lastGroup < localChildren.length)
1284
+ makeRepeatLeaf(localChildren, localPositions, start, lastGroup, start, lastEnd, localInRepeat, lookAheadAtStart);
1285
+ localChildren.reverse();
1286
+ localPositions.reverse();
1287
+ if (localInRepeat > -1 && lastGroup > 0) {
1288
+ let make = makeBalanced(type);
1289
+ node = balanceRange(type, localChildren, localPositions, 0, localChildren.length, 0, end - start, make, make);
1290
+ }
1291
+ else {
1292
+ node = makeTree(type, localChildren, localPositions, end - start, lookAheadAtStart - end);
1293
+ }
1294
+ }
1295
+ children.push(node);
1296
+ positions.push(startPos);
1297
+ }
1298
+ function takeFlatNode(parentStart, minPos, children, positions) {
1299
+ let nodes = []; // Temporary, inverted array of leaf nodes found, with absolute positions
1300
+ let nodeCount = 0, stopAt = -1;
1301
+ while (cursor.pos > minPos) {
1302
+ let { id, start, end, size } = cursor;
1303
+ if (size > 4) { // Not a leaf
1304
+ cursor.next();
1305
+ }
1306
+ else if (stopAt > -1 && start < stopAt) {
1307
+ break;
1308
+ }
1309
+ else {
1310
+ if (stopAt < 0)
1311
+ stopAt = end - maxBufferLength;
1312
+ nodes.push(id, start, end);
1313
+ nodeCount++;
1314
+ cursor.next();
1315
+ }
1316
+ }
1317
+ if (nodeCount) {
1318
+ let buffer = new Uint16Array(nodeCount * 4);
1319
+ let start = nodes[nodes.length - 2];
1320
+ for (let i = nodes.length - 3, j = 0; i >= 0; i -= 3) {
1321
+ buffer[j++] = nodes[i];
1322
+ buffer[j++] = nodes[i + 1] - start;
1323
+ buffer[j++] = nodes[i + 2] - start;
1324
+ buffer[j++] = j;
1325
+ }
1326
+ children.push(new TreeBuffer(buffer, nodes[2] - start, nodeSet));
1327
+ positions.push(start - parentStart);
1328
+ }
1329
+ }
1330
+ function makeBalanced(type) {
1331
+ return (children, positions, length) => {
1332
+ let lookAhead = 0, lastI = children.length - 1, last, lookAheadProp;
1333
+ if (lastI >= 0 && (last = children[lastI]) instanceof Tree) {
1334
+ if (!lastI && last.type == type && last.length == length)
1335
+ return last;
1336
+ if (lookAheadProp = last.prop(NodeProp.lookAhead))
1337
+ lookAhead = positions[lastI] + last.length + lookAheadProp;
1338
+ }
1339
+ return makeTree(type, children, positions, length, lookAhead);
1340
+ };
1341
+ }
1342
+ function makeRepeatLeaf(children, positions, base, i, from, to, type, lookAhead) {
1343
+ let localChildren = [], localPositions = [];
1344
+ while (children.length > i) {
1345
+ localChildren.push(children.pop());
1346
+ localPositions.push(positions.pop() + base - from);
1347
+ }
1348
+ children.push(makeTree(nodeSet.types[type], localChildren, localPositions, to - from, lookAhead - to));
1349
+ positions.push(from - base);
1350
+ }
1351
+ function makeTree(type, children, positions, length, lookAhead = 0, props) {
1352
+ if (contextHash) {
1353
+ let pair = [NodeProp.contextHash, contextHash];
1354
+ props = props ? [pair].concat(props) : [pair];
1355
+ }
1356
+ if (lookAhead > 25) {
1357
+ let pair = [NodeProp.lookAhead, lookAhead];
1358
+ props = props ? [pair].concat(props) : [pair];
1359
+ }
1360
+ return new Tree(type, children, positions, length, props);
1361
+ }
1362
+ function findBufferSize(maxSize, inRepeat) {
1363
+ // Scan through the buffer to find previous siblings that fit
1364
+ // together in a TreeBuffer, and don't contain any reused nodes
1365
+ // (which can't be stored in a buffer).
1366
+ // If `inRepeat` is > -1, ignore node boundaries of that type for
1367
+ // nesting, but make sure the end falls either at the start
1368
+ // (`maxSize`) or before such a node.
1369
+ let fork = cursor.fork();
1370
+ let size = 0, start = 0, skip = 0, minStart = fork.end - maxBufferLength;
1371
+ let result = { size: 0, start: 0, skip: 0 };
1372
+ scan: for (let minPos = fork.pos - maxSize; fork.pos > minPos;) {
1373
+ let nodeSize = fork.size;
1374
+ // Pretend nested repeat nodes of the same type don't exist
1375
+ if (fork.id == inRepeat && nodeSize >= 0) {
1376
+ // Except that we store the current state as a valid return
1377
+ // value.
1378
+ result.size = size;
1379
+ result.start = start;
1380
+ result.skip = skip;
1381
+ skip += 4;
1382
+ size += 4;
1383
+ fork.next();
1384
+ continue;
1385
+ }
1386
+ let startPos = fork.pos - nodeSize;
1387
+ if (nodeSize < 0 || startPos < minPos || fork.start < minStart)
1388
+ break;
1389
+ let localSkipped = fork.id >= minRepeatType ? 4 : 0;
1390
+ let nodeStart = fork.start;
1391
+ fork.next();
1392
+ while (fork.pos > startPos) {
1393
+ if (fork.size < 0) {
1394
+ if (fork.size == -3 /* SpecialRecord.ContextChange */)
1395
+ localSkipped += 4;
1396
+ else
1397
+ break scan;
1398
+ }
1399
+ else if (fork.id >= minRepeatType) {
1400
+ localSkipped += 4;
1401
+ }
1402
+ fork.next();
1403
+ }
1404
+ start = nodeStart;
1405
+ size += nodeSize;
1406
+ skip += localSkipped;
1407
+ }
1408
+ if (inRepeat < 0 || size == maxSize) {
1409
+ result.size = size;
1410
+ result.start = start;
1411
+ result.skip = skip;
1412
+ }
1413
+ return result.size > 4 ? result : undefined;
1414
+ }
1415
+ function copyToBuffer(bufferStart, buffer, index) {
1416
+ let { id, start, end, size } = cursor;
1417
+ cursor.next();
1418
+ if (size >= 0 && id < minRepeatType) {
1419
+ let startIndex = index;
1420
+ if (size > 4) {
1421
+ let endPos = cursor.pos - (size - 4);
1422
+ while (cursor.pos > endPos)
1423
+ index = copyToBuffer(bufferStart, buffer, index);
1424
+ }
1425
+ buffer[--index] = startIndex;
1426
+ buffer[--index] = end - bufferStart;
1427
+ buffer[--index] = start - bufferStart;
1428
+ buffer[--index] = id;
1429
+ }
1430
+ else if (size == -3 /* SpecialRecord.ContextChange */) {
1431
+ contextHash = id;
1432
+ }
1433
+ else if (size == -4 /* SpecialRecord.LookAhead */) {
1434
+ lookAhead = id;
1435
+ }
1436
+ return index;
1437
+ }
1438
+ let children = [], positions = [];
1439
+ while (cursor.pos > 0)
1440
+ takeNode(data.start || 0, data.bufferStart || 0, children, positions, -1, 0);
1441
+ let length = (_a = data.length) !== null && _a !== void 0 ? _a : (children.length ? positions[0] + children[0].length : 0);
1442
+ return new Tree(types[data.topID], children.reverse(), positions.reverse(), length);
1443
+ }
1444
+ const nodeSizeCache = new WeakMap;
1445
+ function nodeSize(balanceType, node) {
1446
+ if (!balanceType.isAnonymous || node instanceof TreeBuffer || node.type != balanceType)
1447
+ return 1;
1448
+ let size = nodeSizeCache.get(node);
1449
+ if (size == null) {
1450
+ size = 1;
1451
+ for (let child of node.children) {
1452
+ if (child.type != balanceType || !(child instanceof Tree)) {
1453
+ size = 1;
1454
+ break;
1455
+ }
1456
+ size += nodeSize(balanceType, child);
1457
+ }
1458
+ nodeSizeCache.set(node, size);
1459
+ }
1460
+ return size;
1461
+ }
1462
+ function balanceRange(
1463
+ // The type the balanced tree's inner nodes.
1464
+ balanceType,
1465
+ // The direct children and their positions
1466
+ children, positions,
1467
+ // The index range in children/positions to use
1468
+ from, to,
1469
+ // The start position of the nodes, relative to their parent.
1470
+ start,
1471
+ // Length of the outer node
1472
+ length,
1473
+ // Function to build the top node of the balanced tree
1474
+ mkTop,
1475
+ // Function to build internal nodes for the balanced tree
1476
+ mkTree) {
1477
+ let total = 0;
1478
+ for (let i = from; i < to; i++)
1479
+ total += nodeSize(balanceType, children[i]);
1480
+ let maxChild = Math.ceil((total * 1.5) / 8 /* Balance.BranchFactor */);
1481
+ let localChildren = [], localPositions = [];
1482
+ function divide(children, positions, from, to, offset) {
1483
+ for (let i = from; i < to;) {
1484
+ let groupFrom = i, groupStart = positions[i], groupSize = nodeSize(balanceType, children[i]);
1485
+ i++;
1486
+ for (; i < to; i++) {
1487
+ let nextSize = nodeSize(balanceType, children[i]);
1488
+ if (groupSize + nextSize >= maxChild)
1489
+ break;
1490
+ groupSize += nextSize;
1491
+ }
1492
+ if (i == groupFrom + 1) {
1493
+ if (groupSize > maxChild) {
1494
+ let only = children[groupFrom]; // Only trees can have a size > 1
1495
+ divide(only.children, only.positions, 0, only.children.length, positions[groupFrom] + offset);
1496
+ continue;
1497
+ }
1498
+ localChildren.push(children[groupFrom]);
1499
+ }
1500
+ else {
1501
+ let length = positions[i - 1] + children[i - 1].length - groupStart;
1502
+ localChildren.push(balanceRange(balanceType, children, positions, groupFrom, i, groupStart, length, null, mkTree));
1503
+ }
1504
+ localPositions.push(groupStart + offset - start);
1505
+ }
1506
+ }
1507
+ divide(children, positions, from, to, 0);
1508
+ return (mkTop || mkTree)(localChildren, localPositions, length);
1509
+ }
1510
+
1511
+ /**
1512
+ Tree fragments are used during [incremental
1513
+ parsing](#common.Parser.startParse) to track parts of old trees
1514
+ that can be reused in a new parse. An array of fragments is used
1515
+ to track regions of an old tree whose nodes might be reused in new
1516
+ parses. Use the static
1517
+ [`applyChanges`](#common.TreeFragment^applyChanges) method to
1518
+ update fragments for document changes.
1519
+ */
1520
+ class TreeFragment {
1521
+ /**
1522
+ Construct a tree fragment. You'll usually want to use
1523
+ [`addTree`](#common.TreeFragment^addTree) and
1524
+ [`applyChanges`](#common.TreeFragment^applyChanges) instead of
1525
+ calling this directly.
1526
+ */
1527
+ constructor(
1528
+ /**
1529
+ The start of the unchanged range pointed to by this fragment.
1530
+ This refers to an offset in the _updated_ document (as opposed
1531
+ to the original tree).
1532
+ */
1533
+ from,
1534
+ /**
1535
+ The end of the unchanged range.
1536
+ */
1537
+ to,
1538
+ /**
1539
+ The tree that this fragment is based on.
1540
+ */
1541
+ tree,
1542
+ /**
1543
+ The offset between the fragment's tree and the document that
1544
+ this fragment can be used against. Add this when going from
1545
+ document to tree positions, subtract it to go from tree to
1546
+ document positions.
1547
+ */
1548
+ offset, openStart = false, openEnd = false) {
1549
+ this.from = from;
1550
+ this.to = to;
1551
+ this.tree = tree;
1552
+ this.offset = offset;
1553
+ this.open = (openStart ? 1 /* Open.Start */ : 0) | (openEnd ? 2 /* Open.End */ : 0);
1554
+ }
1555
+ /**
1556
+ Whether the start of the fragment represents the start of a
1557
+ parse, or the end of a change. (In the second case, it may not
1558
+ be safe to reuse some nodes at the start, depending on the
1559
+ parsing algorithm.)
1560
+ */
1561
+ get openStart() { return (this.open & 1 /* Open.Start */) > 0; }
1562
+ /**
1563
+ Whether the end of the fragment represents the end of a
1564
+ full-document parse, or the start of a change.
1565
+ */
1566
+ get openEnd() { return (this.open & 2 /* Open.End */) > 0; }
1567
+ /**
1568
+ Create a set of fragments from a freshly parsed tree, or update
1569
+ an existing set of fragments by replacing the ones that overlap
1570
+ with a tree with content from the new tree. When `partial` is
1571
+ true, the parse is treated as incomplete, and the resulting
1572
+ fragment has [`openEnd`](#common.TreeFragment.openEnd) set to
1573
+ true.
1574
+ */
1575
+ static addTree(tree, fragments = [], partial = false) {
1576
+ let result = [new TreeFragment(0, tree.length, tree, 0, false, partial)];
1577
+ for (let f of fragments)
1578
+ if (f.to > tree.length)
1579
+ result.push(f);
1580
+ return result;
1581
+ }
1582
+ /**
1583
+ Apply a set of edits to an array of fragments, removing or
1584
+ splitting fragments as necessary to remove edited ranges, and
1585
+ adjusting offsets for fragments that moved.
1586
+ */
1587
+ static applyChanges(fragments, changes, minGap = 128) {
1588
+ if (!changes.length)
1589
+ return fragments;
1590
+ let result = [];
1591
+ let fI = 1, nextF = fragments.length ? fragments[0] : null;
1592
+ for (let cI = 0, pos = 0, off = 0;; cI++) {
1593
+ let nextC = cI < changes.length ? changes[cI] : null;
1594
+ let nextPos = nextC ? nextC.fromA : 1e9;
1595
+ if (nextPos - pos >= minGap)
1596
+ while (nextF && nextF.from < nextPos) {
1597
+ let cut = nextF;
1598
+ if (pos >= cut.from || nextPos <= cut.to || off) {
1599
+ let fFrom = Math.max(cut.from, pos) - off, fTo = Math.min(cut.to, nextPos) - off;
1600
+ cut = fFrom >= fTo ? null : new TreeFragment(fFrom, fTo, cut.tree, cut.offset + off, cI > 0, !!nextC);
1601
+ }
1602
+ if (cut)
1603
+ result.push(cut);
1604
+ if (nextF.to > nextPos)
1605
+ break;
1606
+ nextF = fI < fragments.length ? fragments[fI++] : null;
1607
+ }
1608
+ if (!nextC)
1609
+ break;
1610
+ pos = nextC.toA;
1611
+ off = nextC.toA - nextC.toB;
1612
+ }
1613
+ return result;
1614
+ }
1615
+ }
1616
+ /**
1617
+ A superclass that parsers should extend.
1618
+ */
1619
+ class Parser {
1620
+ /**
1621
+ Start a parse, returning a [partial parse](#common.PartialParse)
1622
+ object. [`fragments`](#common.TreeFragment) can be passed in to
1623
+ make the parse incremental.
1624
+
1625
+ By default, the entire input is parsed. You can pass `ranges`,
1626
+ which should be a sorted array of non-empty, non-overlapping
1627
+ ranges, to parse only those ranges. The tree returned in that
1628
+ case will start at `ranges[0].from`.
1629
+ */
1630
+ startParse(input, fragments, ranges) {
1631
+ if (typeof input == "string")
1632
+ input = new StringInput(input);
1633
+ ranges = !ranges ? [new Range(0, input.length)] : ranges.length ? ranges.map(r => new Range(r.from, r.to)) : [new Range(0, 0)];
1634
+ return this.createParse(input, fragments || [], ranges);
1635
+ }
1636
+ /**
1637
+ Run a full parse, returning the resulting tree.
1638
+ */
1639
+ parse(input, fragments, ranges) {
1640
+ let parse = this.startParse(input, fragments, ranges);
1641
+ for (;;) {
1642
+ let done = parse.advance();
1643
+ if (done)
1644
+ return done;
1645
+ }
1646
+ }
1647
+ }
1648
+ class StringInput {
1649
+ constructor(string) {
1650
+ this.string = string;
1651
+ }
1652
+ get length() { return this.string.length; }
1653
+ chunk(from) { return this.string.slice(from); }
1654
+ get lineChunks() { return false; }
1655
+ read(from, to) { return this.string.slice(from, to); }
1656
+ }
1657
+ new NodeProp({ perNode: true });
1658
+
1659
+ exports.DefaultBufferLength = DefaultBufferLength;
1660
+ exports.MountedTree = MountedTree;
1661
+ exports.NodeProp = NodeProp;
1662
+ exports.NodeType = NodeType;
1663
+ exports.Parser = Parser;
1664
+ exports.Tree = Tree;
1665
+ exports.TreeBuffer = TreeBuffer;
1666
+ exports.TreeCursor = TreeCursor;
1667
+ exports.TreeFragment = TreeFragment;
1668
+ //# sourceMappingURL=index.js.map