-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathcljParser.ts
296 lines (252 loc) · 10.6 KB
/
cljParser.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
import * as vscode from 'vscode';
interface ExpressionInfo {
functionName: string;
parameterPosition: number;
}
interface RelativeExpressionInfo {
startPosition: number;
parameterPosition: number;
}
const CLJ_TEXT_DELIMITER = `"`;
const CLJ_TEXT_ESCAPE = `\\`;
const CLJ_COMMENT_DELIMITER = `;`;
const R_CLJ_WHITE_SPACE = /\s|,/;
const R_CLJ_OPERATOR_DELIMITERS = /\s|,|\(|{|\[/;
const OPEN_CLJ_BLOCK_BRACKET = `(`;
const CLOSE_CLJ_BLOCK_BRACKET = `)`;
/** { close_char open_char } */
const CLJ_EXPRESSION_DELIMITERS: Map<string, string> = new Map<string, string>([
[`}`, `{`],
[CLOSE_CLJ_BLOCK_BRACKET, OPEN_CLJ_BLOCK_BRACKET],
[`]`, `[`],
[CLJ_TEXT_DELIMITER, CLJ_TEXT_DELIMITER],
]);
const getExpressionInfo = (text: string): ExpressionInfo | undefined => {
text = removeCljComments(text);
const relativeExpressionInfo = getRelativeExpressionInfo(text);
if (!relativeExpressionInfo)
return;
let functionName = text.substring(relativeExpressionInfo.startPosition + 1); // expression openning ignored
functionName = functionName.substring(functionName.search(/[^,\s]/)); // trim left
functionName = functionName.substring(0, functionName.search(R_CLJ_OPERATOR_DELIMITERS)); // trim right according to operator delimiter
if (!functionName.length)
return;
return {
functionName,
parameterPosition: relativeExpressionInfo.parameterPosition,
};
};
const removeCljComments = (text: string): string => {
const lines = text.match(/[^\r\n]+/g) || [] // split string by line
if (lines.length > 1) {
return lines.map(line => removeCljComments(line)).join(`\n`); // remove comments from each line and concat them again after
}
const line = lines[0];
let uncommentedIndex = line.length;
let insideString = false;
for (let i = 0; i < line.length; i++) {
if (line[i] === CLJ_TEXT_DELIMITER) {
insideString = !insideString || line[i - 1] === CLJ_TEXT_ESCAPE;
continue;
}
if (line[i] === CLJ_COMMENT_DELIMITER && !insideString) { // ignore comment delimiter inside a string
uncommentedIndex = i;
break;
}
}
return line.substring(0, uncommentedIndex);
};
const getRelativeExpressionInfo = (text: string, openChar: string = `(`): RelativeExpressionInfo | undefined => {
const relativeExpressionInfo: RelativeExpressionInfo = {
startPosition: text.length - 1,
parameterPosition: -1,
};
let newParameterFound = false;
while (relativeExpressionInfo.startPosition >= 0) {
const char = text[relativeExpressionInfo.startPosition];
// check if found the beginning of the expression (string escape taken care of)
if (char === openChar && (openChar !== CLJ_TEXT_DELIMITER || (text[relativeExpressionInfo.startPosition - 1] !== CLJ_TEXT_ESCAPE))) {
if (newParameterFound) // ignore one parameter found if it's actually the function we're looking for
relativeExpressionInfo.parameterPosition--;
return relativeExpressionInfo;
}
// ignore everything if searching inside a string
if (openChar === CLJ_TEXT_DELIMITER) {
relativeExpressionInfo.startPosition--;
continue;
}
// invalid code if a beginning of an expression is found without being searched for
if (char !== CLJ_TEXT_DELIMITER && containsValue(CLJ_EXPRESSION_DELIMITERS, char))
return;
// keep searching if it's white space
if (R_CLJ_WHITE_SPACE.test(char)) {
if (!newParameterFound) {
relativeExpressionInfo.parameterPosition++;
newParameterFound = true;
}
relativeExpressionInfo.startPosition--;
continue;
}
// check for new expressions
const expressionDelimiter = CLJ_EXPRESSION_DELIMITERS.get(char);
if (!!expressionDelimiter) {
const innerExpressionInfo = getRelativeExpressionInfo(text.substring(0, relativeExpressionInfo.startPosition), expressionDelimiter);
if (!innerExpressionInfo)
return;
relativeExpressionInfo.startPosition = innerExpressionInfo.startPosition - 1;
relativeExpressionInfo.parameterPosition++;
newParameterFound = true;
continue;
}
newParameterFound = false;
relativeExpressionInfo.startPosition--;
}
return; // reached the beginning of the text without finding the start of the expression
};
const containsValue = (map: Map<any, any>, checkValue: any): boolean => {
for (let value of map.values()) {
if (value === checkValue)
return true;
}
return false;
};
const getNamespace = (text: string): string => {
const m = text.match(/^[;\s\t\n]*\((?:[\s\t\n]*(?:in-){0,1}ns)[\s\t\n]+'?([\w\-.]+)[\s\S]*\)[\s\S]*/);
return m ? m[1] : 'user';
};
/** A range of numbers between `start` and `end`.
* The `end` index is not included and `end` could be before `start`.
* Whereas default `vscode.Range` requires that `start` should be
* before or equal than `end`. */
const range = (start: number, end: number): Array<number> => {
if (start < end) {
const length = end - start;
return Array.from(Array(length), (_, i) => start + i);
} else {
const length = start - end;
return Array.from(Array(length), (_, i) => start - i);
}
}
const findNearestBracket = (
editor: vscode.TextEditor,
current: vscode.Position,
bracket: string): vscode.Position | undefined => {
const isBackward = bracket == OPEN_CLJ_BLOCK_BRACKET;
// "open" and "close" brackets as keys related to search direction
let openBracket = OPEN_CLJ_BLOCK_BRACKET,
closeBracket = CLOSE_CLJ_BLOCK_BRACKET;
if (isBackward) {
[closeBracket, openBracket] = [openBracket, closeBracket]
};
let bracketStack: string[] = [],
// get begin of text if we are searching `(` and end of text otherwise
lastLine = isBackward ? -1 : editor.document.lineCount,
lineRange = range(current.line, lastLine);
for (var line of lineRange) {
const textLine = editor.document.lineAt(line);
if (textLine.isEmptyOrWhitespace) continue;
// get line and strip clj comments
const firstChar = textLine.firstNonWhitespaceCharacterIndex,
strippedLine = removeCljComments(textLine.text);
let startColumn = firstChar,
endColumn = strippedLine.length;
if (isBackward) {
// dec both as `range` doesn't include an end edge
[startColumn, endColumn] = [endColumn - 1, startColumn - 1];
}
// select block if cursor right after the closed bracket or right before opening
if (current.line == line) {
// get current current char index if it is first iteration of loop
let currentColumn = current.character;
// set current position as start
if (isBackward) {
if (currentColumn <= endColumn) continue;
startColumn = currentColumn;
if (strippedLine[startColumn - 1] == CLOSE_CLJ_BLOCK_BRACKET) {
startColumn = startColumn - 2;
} else if (strippedLine[startColumn] == CLOSE_CLJ_BLOCK_BRACKET) {
startColumn--;
};
} else if (currentColumn <= endColumn) {
// forward direction
startColumn = currentColumn;
if (strippedLine[startColumn - 1] == CLOSE_CLJ_BLOCK_BRACKET) {
return new vscode.Position(line, startColumn);
} else if (strippedLine[startColumn] == OPEN_CLJ_BLOCK_BRACKET) {
startColumn++;
};
}
}
// search nearest bracket
for (var column of range(startColumn, endColumn)) {
const char = strippedLine[column];
if (!bracketStack.length && char == bracket) {
// inc column if `char` is a `)` to get correct selection
if (!isBackward) column++;
return new vscode.Position(line, column);
} else if (char == openBracket) {
bracketStack.push(char);
// check if inner block is closing
} else if (char == closeBracket && bracketStack.length > 0) {
bracketStack.pop();
};
};
}
};
const getCurrentBlock = (
editor: vscode.TextEditor,
left?: vscode.Position,
right?: vscode.Position): vscode.Selection | undefined => {
if (!left || !right) {
left = right = editor.selection.active;
};
const prevBracket = findNearestBracket(editor, left, OPEN_CLJ_BLOCK_BRACKET);
if (!prevBracket) return;
const nextBracket = findNearestBracket(editor, right, CLOSE_CLJ_BLOCK_BRACKET);
if (nextBracket) {
return new vscode.Selection(prevBracket, nextBracket);
}
};
const getOuterBlock = (
editor: vscode.TextEditor,
left?: vscode.Position,
right?: vscode.Position,
prevBlock?: vscode.Selection): vscode.Selection | undefined => {
if (!left || !right) {
left = right = editor.selection.active;
};
const nextBlock = getCurrentBlock(editor, left, right);
if (nextBlock) {
// calculate left position one step before
if (nextBlock.anchor.character > 0) {
left = new vscode.Position(nextBlock.anchor.line,
nextBlock.anchor.character - 1);
} else if (nextBlock.anchor.line > 0) {
const line = nextBlock.anchor.line - 1;
left = editor.document.lineAt(line).range.end;
} else {
return new vscode.Selection(nextBlock.anchor, nextBlock.active);
}
// calculate right position one step after
const lineLength = editor.document.lineAt(nextBlock.active.line).text.length;
if (nextBlock.active.character < lineLength) {
right = new vscode.Position(nextBlock.active.line,
nextBlock.active.character + 1);
} else if (nextBlock.active.line < editor.document.lineCount - 1) {
right = new vscode.Position(nextBlock.active.line + 1, 0);
} else {
return new vscode.Selection(nextBlock.anchor, nextBlock.active);
};
// try to find next outer block
return getOuterBlock(editor, left, right, nextBlock);
} else if (right != left) {
return prevBlock;
};
}
export const cljParser = {
R_CLJ_WHITE_SPACE,
getExpressionInfo,
getNamespace,
getCurrentBlock,
getOuterBlock,
};