-
Notifications
You must be signed in to change notification settings - Fork 60
/
cst-utils.ts
338 lines (310 loc) · 11.1 KB
/
cst-utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/******************************************************************************
* Copyright 2021 TypeFox GmbH
* This program and the accompanying materials are made available under the
* terms of the MIT License, which is available in the project root.
******************************************************************************/
import type { IToken } from '@chevrotain/types';
import type { Range } from 'vscode-languageserver-types';
import type { CstNode, CompositeCstNode, LeafCstNode } from '../syntax-tree.js';
import type { DocumentSegment } from '../workspace/documents.js';
import type { Stream, TreeStream } from './stream.js';
import { isCompositeCstNode, isLeafCstNode, isRootCstNode } from '../syntax-tree.js';
import { TreeStreamImpl } from './stream.js';
/**
* Create a stream of all CST nodes that are directly and indirectly contained in the given root node,
* including the root node itself.
*/
export function streamCst(node: CstNode): TreeStream<CstNode> {
return new TreeStreamImpl(node, element => {
if (isCompositeCstNode(element)) {
return element.content;
} else {
return [];
}
}, { includeRoot: true });
}
/**
* Create a stream of all leaf nodes that are directly and indirectly contained in the given root node.
*/
export function flattenCst(node: CstNode): Stream<LeafCstNode> {
return streamCst(node).filter(isLeafCstNode);
}
/**
* Determines whether the specified cst node is a child of the specified parent node.
*/
export function isChildNode(child: CstNode, parent: CstNode): boolean {
while (child.container) {
child = child.container;
if (child === parent) {
return true;
}
}
return false;
}
export function tokenToRange(token: IToken): Range {
// Chevrotain uses 1-based indices everywhere
// So we subtract 1 from every value to align with the LSP
return {
start: {
character: token.startColumn! - 1,
line: token.startLine! - 1
},
end: {
character: token.endColumn!, // endColumn uses the correct index
line: token.endLine! - 1
}
};
}
export function toDocumentSegment(node: CstNode): DocumentSegment;
export function toDocumentSegment(node?: CstNode): DocumentSegment | undefined;
export function toDocumentSegment(node?: CstNode): DocumentSegment | undefined {
if (!node) {
return undefined;
}
const { offset, end, range } = node;
return {
range,
offset,
end,
length: end - offset
};
}
export enum RangeComparison {
Before = 0,
After = 1,
OverlapFront = 2,
OverlapBack = 3,
Inside = 4
}
export function compareRange(range: Range, to: Range): RangeComparison {
if (range.end.line < to.start.line || (range.end.line === to.start.line && range.end.character < range.start.character)) {
return RangeComparison.Before;
} else if (range.start.line > to.end.line || (range.start.line === to.end.line && range.start.character > to.end.character)) {
return RangeComparison.After;
}
const startInside = range.start.line > to.start.line || (range.start.line === to.start.line && range.start.character >= to.start.character);
const endInside = range.end.line < to.end.line || (range.end.line === to.end.line && range.end.character <= to.end.character);
if (startInside && endInside) {
return RangeComparison.Inside;
} else if (startInside) {
return RangeComparison.OverlapBack;
} else {
return RangeComparison.OverlapFront;
}
}
export function inRange(range: Range, to: Range): boolean {
const comparison = compareRange(range, to);
return comparison > RangeComparison.After;
}
// The \p{L} regex matches any unicode letter character, i.e. characters from non-english alphabets
// Together with \w it matches any kind of character which can commonly appear in IDs
export const DefaultNameRegexp = /^[\w\p{L}]$/u;
/**
* Performs `findLeafNodeAtOffset` with a minor difference: When encountering a character that matches the `nameRegexp` argument,
* it will instead return the leaf node at the `offset - 1` position.
*
* For LSP services, users expect that the declaration of an element is available if the cursor is directly after the element.
*/
export function findDeclarationNodeAtOffset(cstNode: CstNode | undefined, offset: number, nameRegexp = DefaultNameRegexp): LeafCstNode | undefined {
if (cstNode) {
if (offset > 0) {
const localOffset = offset - cstNode.offset;
const textAtOffset = cstNode.text.charAt(localOffset);
if (!nameRegexp.test(textAtOffset)) {
offset--;
}
}
return findLeafNodeAtOffset(cstNode, offset);
}
return undefined;
}
export function findCommentNode(cstNode: CstNode | undefined, commentNames: string[]): CstNode | undefined {
if (cstNode) {
const previous = getPreviousNode(cstNode, true);
if (previous && isCommentNode(previous, commentNames)) {
return previous;
}
if (isRootCstNode(cstNode)) {
// Go from the first non-hidden node through all nodes in reverse order
// We do this to find the comment node which directly precedes the root node
const endIndex = cstNode.content.findIndex(e => !e.hidden);
for (let i = endIndex - 1; i >= 0; i--) {
const child = cstNode.content[i];
if (isCommentNode(child, commentNames)) {
return child;
}
}
}
}
return undefined;
}
export function isCommentNode(cstNode: CstNode, commentNames: string[]): boolean {
return isLeafCstNode(cstNode) && commentNames.includes(cstNode.tokenType.name);
}
/**
* Finds the leaf CST node at the specified 0-based string offset.
* Note that the given offset will be within the range of the returned leaf node.
*
* If the offset does not point to a CST node (but just white space), this method will return `undefined`.
*
* @param node The CST node to search through.
* @param offset The specified offset.
* @returns The CST node at the specified offset.
*/
export function findLeafNodeAtOffset(node: CstNode, offset: number): LeafCstNode | undefined {
if (isLeafCstNode(node)) {
return node;
} else if (isCompositeCstNode(node)) {
const searchResult = binarySearch(node, offset, false);
if (searchResult) {
return findLeafNodeAtOffset(searchResult, offset);
}
}
return undefined;
}
/**
* Finds the leaf CST node at the specified 0-based string offset.
* If no CST node exists at the specified position, it will return the leaf node before it.
*
* If there is no leaf node before the specified offset, this method will return `undefined`.
*
* @param node The CST node to search through.
* @param offset The specified offset.
* @returns The CST node closest to the specified offset.
*/
export function findLeafNodeBeforeOffset(node: CstNode, offset: number): LeafCstNode | undefined {
if (isLeafCstNode(node)) {
return node;
} else if (isCompositeCstNode(node)) {
const searchResult = binarySearch(node, offset, true);
if (searchResult) {
return findLeafNodeBeforeOffset(searchResult, offset);
}
}
return undefined;
}
function binarySearch(node: CompositeCstNode, offset: number, closest: boolean): CstNode | undefined {
let left = 0;
let right = node.content.length - 1;
let closestNode: CstNode | undefined = undefined;
while (left <= right) {
const middle = Math.floor((left + right) / 2);
const middleNode = node.content[middle];
if (middleNode.offset <= offset && middleNode.end > offset) {
// Found an exact match
return middleNode;
}
if (middleNode.end <= offset) {
// Update the closest node (less than offset) and move to the right half
closestNode = closest ? middleNode : undefined;
left = middle + 1;
} else {
// Move to the left half
right = middle - 1;
}
}
return closestNode;
}
export function getPreviousNode(node: CstNode, hidden = true): CstNode | undefined {
while (node.container) {
const parent = node.container;
let index = parent.content.indexOf(node);
while (index > 0) {
index--;
const previous = parent.content[index];
if (hidden || !previous.hidden) {
return previous;
}
}
node = parent;
}
return undefined;
}
export function getNextNode(node: CstNode, hidden = true): CstNode | undefined {
while (node.container) {
const parent = node.container;
let index = parent.content.indexOf(node);
const last = parent.content.length - 1;
while (index < last) {
index++;
const next = parent.content[index];
if (hidden || !next.hidden) {
return next;
}
}
node = parent;
}
return undefined;
}
export function getStartlineNode(node: CstNode): CstNode {
if (node.range.start.character === 0) {
return node;
}
const line = node.range.start.line;
let last = node;
let index: number | undefined;
while (node.container) {
const parent = node.container;
const selfIndex = index ?? parent.content.indexOf(node);
if (selfIndex === 0) {
node = parent;
index = undefined;
} else {
index = selfIndex - 1;
node = parent.content[index];
}
if (node.range.start.line !== line) {
break;
}
last = node;
}
return last;
}
export function getInteriorNodes(start: CstNode, end: CstNode): CstNode[] {
const commonParent = getCommonParent(start, end);
if (!commonParent) {
return [];
}
return commonParent.parent.content.slice(commonParent.a + 1, commonParent.b);
}
function getCommonParent(a: CstNode, b: CstNode): CommonParent | undefined {
const aParents = getParentChain(a);
const bParents = getParentChain(b);
let current: CommonParent | undefined;
for (let i = 0; i < aParents.length && i < bParents.length; i++) {
const aParent = aParents[i];
const bParent = bParents[i];
if (aParent.parent === bParent.parent) {
current = {
parent: aParent.parent,
a: aParent.index,
b: bParent.index
};
} else {
break;
}
}
return current;
}
interface CommonParent {
parent: CompositeCstNode
a: number
b: number
}
function getParentChain(node: CstNode): ParentLink[] {
const chain: ParentLink[] = [];
while (node.container) {
const parent = node.container;
const index = parent.content.indexOf(node);
chain.push({
parent,
index
});
node = parent;
}
return chain.reverse();
}
interface ParentLink {
parent: CompositeCstNode
index: number
}