-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutilities.js
257 lines (233 loc) · 8.18 KB
/
utilities.js
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
const vscode = require('vscode');
const specialWords = ['-', '+', '/', '*']; //TODO: Add more here
const _ = require('lodash');
const state = require('./state');
function getNamespace(text) {
let match = text.match(/^[\s\t]*\((?:[\s\t\n]*(?:in-){0,1}ns)[\s\t\n]+'?([\w.\-\/]+)[\s\S]*\)[\s\S]*/);
return match ? match[1] : 'user';
};
function getStartExpression(text) {
let match = text.match(/^\(([^\)]+)[\)]+/g);
return match ? match[0] : "(ns user)";
}
function getActualWord(document, position, selected, word) {
if (selected === undefined) {
let selectedChar = document.lineAt(position.line).text.slice(position.character, position.character + 1),
isFn = document.lineAt(position.line).text.slice(position.character - 1, position.character) === "(";
if (selectedChar !== undefined &&
specialWords.indexOf(selectedChar) !== -1 &&
isFn) {
return selectedChar;
} else {
return "";
}
} else {
return word;
}
};
function getDocument(document) {
return document.hasOwnProperty('fileName') ? document : vscode.window.activeTextEditor.document;
};
function getFileType(document) {
let doc = getDocument(document);
filetypeIndex = (doc.fileName.lastIndexOf('.') + 1);
return doc.fileName.substr(filetypeIndex, doc.fileName.length);
};
function getFileName(document) {
let fileNameIndex = (document.fileName.lastIndexOf('\\') + 1);
return document.fileName.substr(fileNameIndex, document.fileName.length)
};
//using algorithm from: http://stackoverflow.com/questions/15717436/js-regex-to-match-everything-inside-braces-including-nested-braces-i-want/27088184#27088184
function getContentToNextBracket(block) {
var currPos = 0,
openBrackets = 0,
stillSearching = true,
waitForChar = false;
while (stillSearching && currPos <= block.length) {
var currChar = block.charAt(currPos);
if (!waitForChar) {
switch (currChar) {
case '(':
openBrackets++;
break;
case ')':
openBrackets--;
break;
case '"':
case "'":
waitForChar = currChar;
break;
case '/':
var nextChar = block.charAt(currPos + 1);
if (nextChar === '/') {
waitForChar = '\n';
} else if (nextChar === '*') {
waitForChar = '*/';
}
break;
}
} else {
if (currChar === waitForChar) {
if (waitForChar === '"' || waitForChar === "'") {
block.charAt(currPos - 1) !== '\\' && (waitForChar = false);
} else {
waitForChar = false;
}
} else if (currChar === '*') {
block.charAt(currPos + 1) === '/' && (waitForChar = false);
}
}
currPos++
if (openBrackets === 0) {
stillSearching = false;
}
}
return [currPos, block.substr(0, currPos)];
};
function getContentToPreviousBracket(block) {
var currPos = (block.length - 1),
openBrackets = 0,
stillSearching = true,
waitForChar = false;
while (stillSearching && currPos >= 0) {
var currChar = block.charAt(currPos);
if (!waitForChar) {
switch (currChar) {
case '(':
openBrackets--;
break;
case ')':
openBrackets++;
break;
case '"':
case "'":
waitForChar = currChar;
break;
case '/':
var nextChar = block.charAt(currPos + 1);
if (nextChar === '/') {
waitForChar = '\n';
} else if (nextChar === '*') {
waitForChar = '*/';
}
break;
}
} else {
if (currChar === waitForChar) {
if (waitForChar === '"' || waitForChar === "'") {
block.charAt(currPos - 1) !== '\\' && (waitForChar = false);
} else {
waitForChar = false;
}
} else if (currChar === '*') {
block.charAt(currPos + 1) === '/' && (waitForChar = false);
}
}
currPos--
if (openBrackets === 0) {
stillSearching = false;
}
}
return [currPos, block.substr(currPos + 1, block.length)];
};
// ERROR HELPERS
const ERROR_TYPE = {
WARNING: "warning",
ERROR: "error"
};
function logSuccess(results) {
let chan = state.deref().get('outputChannel');
chan.appendLine("Evaluation completed successfully");
_.each(results, (r) => {
let value = r.hasOwnProperty("value") ? r.value : null;
let out = r.hasOwnProperty("out") ? r.out : null;
if (value !== null) {
chan.appendLine("=>\n" + value);
}
if (out !== null) {
chan.appendLine("out:\n" + out);
}
});
};
function logError(error) {
let chan = state.deref().get('outputChannel');
chan.appendLine(error.reason);
if (error.line !== undefined && error.line !== null &&
error.column !== undefined && error.column !== null) {
chan.appendLine("at line: " + error.line + " and column: " + error.column)
}
};
function markError(error) {
if (error.line === null) {
error.line = 0;
}
if (error.column === null) {
error.column = 0;
}
let position = new vscode.Position(error.line, error.column),
diagnostic = state.deref().get('diagnosticCollection'),
editor = vscode.window.activeTextEditor;
//editor.selection = new vscode.Selection(position, position);
let line = error.line - 1,
column = error.column,
lineLength = editor.document.lineAt(line).text.length,
lineText = editor.document.lineAt(line).text.substring(column, lineLength),
firstWordStart = column + lineText.indexOf(" "),
existing = diagnostic.get(editor.document.uri),
err = new vscode.Diagnostic(new vscode.Range(line, column, line, firstWordStart),
error.reason,
vscode.DiagnosticSeverity.Error);
let errors = (existing !== undefined && existing.length > 0) ? [...existing, err] :
[err];
diagnostic.set(editor.document.uri, errors);
};
function logWarning(warning) {
let chan = state.deref().get('outputChannel');
chan.appendLine(warning.reason);
if (warning.line !== null) {
if (warning.column !== null) {
chan.appendLine("at line: " + warning.line + " and column: " + warning.column)
} else {
chan.appendLine("at line: " + warning.line)
}
}
};
function markWarning(warning) {
if (warning.line === null) {
warning.line = 0;
}
if (warning.column === null) {
warning.column = 0;
}
let position = new vscode.Position(warning.line, warning.column),
diagnostic = state.deref().get('diagnosticCollection'),
editor = vscode.window.activeTextEditor;
//editor.selection = new vscode.Selection(position, position);
let line = Math.max(0, (warning.line - 1)),
column = warning.column,
lineLength = editor.document.lineAt(line).text.length,
existing = diagnostic.get(editor.document.uri),
warn = new vscode.Diagnostic(new vscode.Range(line, column, line, lineLength),
warning.reason,
vscode.DiagnosticSeverity.Warning);
let warnings = (existing !== undefined && existing.length > 0) ? [...existing, warn] :
[warn];
diagnostic.set(editor.document.uri, warnings);
};
module.exports = {
getNamespace,
getStartExpression,
getActualWord,
getDocument,
getFileType,
getFileName,
getContentToNextBracket,
getContentToPreviousBracket,
specialWords,
ERROR_TYPE,
logError,
markError,
logWarning,
markWarning,
logSuccess
};