-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathclojureEval.ts
326 lines (278 loc) · 11.8 KB
/
clojureEval.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
import * as vscode from 'vscode';
import { cljConnection } from './cljConnection';
import { LANGUAGE } from './clojureMode';
import { cljParser } from './cljParser';
import { nreplClient } from './nreplClient';
import { TestListener } from './testRunner';
const HIGHLIGHTING_TIMEOUT = 350;
const BLOCK_DECORATION_TYPE = vscode.window.createTextEditorDecorationType({
backgroundColor: { id: 'editor.findMatchHighlightBackground' }
});
const INLINE_RESULT_LENGTH = 150;
const INLINE_RESULT_DECORATION_TYPE = vscode.window.createTextEditorDecorationType({
before: {
margin: '0 0 0 2em',
textDecoration: 'none',
fontWeight: 'normal',
fontStyle: 'normal',
},
rangeBehavior: vscode.DecorationRangeBehavior.ClosedOpen
});
export function clojureEval(outputChannel: vscode.OutputChannel): void {
evaluate(outputChannel, false);
}
export function clojureEvalAndShowResult(outputChannel: vscode.OutputChannel): void {
evaluate(outputChannel, true);
}
type TestResults = {
summary: {
error: number
fail: number
ns: number
pass: number
test: number
var: number
}
'testing-ns': string
'gen-input': any[]
status?: string[]
results: {
[key: string]: { // Namespace
[key: string]: {
context: any
file?: string
index: number
line?: number
message?: string
ns: string
type: string
var: string
actual?: string
expected?: string
}[];
}
}
session: string
}
function runTests(outputChannel: vscode.OutputChannel, listener: TestListener, namespace?: string): void {
if (!cljConnection.isConnected()) {
vscode.window.showWarningMessage('You must be connected to an nREPL session to test a namespace.');
return;
}
const promise: Promise<TestResults[]> = nreplClient.runTests(namespace);
promise.then((responses) => {
console.log("Test result promise delivery");
responses.forEach(response => {
console.log(response);
console.log(response.results);
if (response.status && response.status.indexOf("unknown-op") != -1) {
outputChannel.appendLine("Failed to run tests: the cider.nrepl.middleware.test middleware in not loaded.");
return;
}
for (const ns in response.results) {
const namespace = response.results[ns];
outputChannel.appendLine("Results for " + ns)
for (const varName in namespace) {
// Each var being tested reports a list of statuses, one for each
// `is` assertion in the test. Here we just want to reduce this
// down to a single pass/fail.
const statuses = new Set(namespace[varName].map(r => r.type));
const passed = (statuses.size == 0) ||
((statuses.size == 1) && statuses.has('pass'));
listener.onTestResult(ns, varName, passed);
namespace[varName].forEach(r => {
if (r.type != 'pass') {
outputChannel.appendLine(r.type + " in (" + r.var + ") (" + r.file + ":" + r.line + ")");
if (typeof r.message === 'string') {
outputChannel.appendLine(r.message);
}
if (r.expected) {
outputChannel.append("expected: " + r.expected)
}
if (r.actual) {
outputChannel.append(" actual: " + r.actual)
}
}
});
}
}
if ('summary' in response) {
const failed = response.summary.fail + response.summary.error;
if (failed > 0) {
vscode.window.showErrorMessage(failed + " tests failed.")
} else {
vscode.window.showInformationMessage(response.summary.var + " tests passed")
}
}
});
}).catch((reason): void => {
const message: string = "" + reason;
outputChannel.append("Tests failed: ");
outputChannel.appendLine(message);
});
}
export function testNamespace(outputChannel: vscode.OutputChannel, listener: TestListener): void {
const editor = vscode.window.activeTextEditor;
if (editor) {
const text = editor.document.getText();
const ns = cljParser.getNamespace(text); // log ns and 'starting'
outputChannel.appendLine("Testing " + ns)
runTests(outputChannel, listener, ns);
} else {
// if having troubles with finding the namespace (though I'm not sure
// if it can actually happen), run all tests
runAllTests(outputChannel, listener);
}
}
export function runAllTests(outputChannel: vscode.OutputChannel, listener: TestListener): void {
outputChannel.appendLine("Testing all namespaces");
runTests(outputChannel, listener);
}
const highlightSelection = (editor: vscode.TextEditor, selection: vscode.Selection) => {
let selectionRange = new vscode.Range(selection.start, selection.end);
// setup highlighting of evaluated block
editor.setDecorations(BLOCK_DECORATION_TYPE, [selectionRange])
// stop highlighting of block after timeout
setTimeout(() => {
editor.setDecorations(BLOCK_DECORATION_TYPE, [])
},
HIGHLIGHTING_TIMEOUT);
};
function evaluate(outputChannel: vscode.OutputChannel, showResults: boolean): void {
if (!cljConnection.isConnected()) {
vscode.window.showWarningMessage('You should connect to nREPL first to evaluate code.');
return;
}
const editor = vscode.window.activeTextEditor;
if (!editor) return;
// select and highlight appropriate block if selection is empty
let blockSelection: vscode.Selection | undefined;
if (editor.selection.isEmpty) {
blockSelection = showResults ? cljParser.getCurrentBlock(editor) : cljParser.getOuterBlock(editor);
if (blockSelection) {
highlightSelection(editor, blockSelection);
console.log("eval:\n", editor.document.getText(blockSelection));
} else {
console.log("eval:", "Whole file");
}
}
const selection = blockSelection || editor.selection;
let text = editor.document.getText(),
selectionEndLine: number;
if (!selection.isEmpty) {
selectionEndLine = selection.end.line;
const ns: string = cljParser.getNamespace(text);
text = `(ns ${ns})\n${editor.document.getText(selection)}`;
} else {
selectionEndLine = editor.document.lineCount - 1;
}
cljConnection.sessionForFilename(editor.document.fileName).then(session => {
let response;
if (!selection.isEmpty && session.type == 'ClojureScript') {
// Piggieback's evalFile() ignores the text sent as part of the request
// and just loads the whole file content from disk. So we use eval()
// here, which as a drawback will give us a random temporary filename in
// the stacktrace should an exception occur.
response = nreplClient.evaluate(text, session.id);
} else {
response = nreplClient.evaluateFile(text, editor.document.fileName, session.id);
}
response.then(respObjs => {
if (!!respObjs[0].ex)
return handleError(outputChannel, selection, showResults, respObjs[0].session);
return handleSuccess(outputChannel, showResults, respObjs, selectionEndLine);
})
});
}
function handleError(outputChannel: vscode.OutputChannel, selection: vscode.Selection, showResults: boolean, session: string): Promise<void> {
if (!showResults)
vscode.window.showErrorMessage('Compilation error');
return nreplClient.stacktrace(session)
.then(stacktraceObjs => {
stacktraceObjs.forEach((stacktraceObj: any) => {
if (stacktraceObj.status && stacktraceObj.status.indexOf("done") >= 0) {
return;
}
let errLine = stacktraceObj.line !== undefined ? stacktraceObj.line - 1 : 0;
let errChar = stacktraceObj.column !== undefined ? stacktraceObj.column - 1 : 0;
if (!selection.isEmpty) {
errLine += selection.start.line;
errChar += selection.start.character;
}
outputChannel.appendLine(`${stacktraceObj.class} ${stacktraceObj.message}`);
if (stacktraceObj.file) {
outputChannel.appendLine(` at ${stacktraceObj.file}:${errLine}:${errChar}`);
}
stacktraceObj.stacktrace.forEach((trace: any) => {
if (trace.flags.indexOf('tooling') > -1)
outputChannel.appendLine(` ${trace.class}.${trace.method} (${trace.file}:${trace.line})`);
});
outputChannel.show(true);
nreplClient.close(session);
});
});
}
function truncateLine(value: string): string {
if (value.length > INLINE_RESULT_LENGTH) {
return value.substring(0, INLINE_RESULT_LENGTH) + '...'
}
return value;
}
function showInlineResult(respObj: any, line: number): void {
const isError = Boolean(respObj.err),
editor = vscode.window.activeTextEditor;
let result: string,
foregroundColor: vscode.ThemeColor;
if (isError) {
// show more error description at once
result = respObj.err.replace(/\n/g, ' ');
foregroundColor = new vscode.ThemeColor('editorError.foreground');
} else {
result = respObj.value;
foregroundColor = new vscode.ThemeColor('clojureVSCode.inlineResultForeground');
}
if (result && editor) {
const decoration: vscode.DecorationOptions = {
renderOptions: {
before: {
backgroundColor: new vscode.ThemeColor('clojureVSCode.inlineResultBackground'),
color: foregroundColor,
contentText: truncateLine(result),
},
},
range: editor.document.validateRange(
new vscode.Range(line, Number.MAX_SAFE_INTEGER, line, Number.MAX_SAFE_INTEGER)
)
};
editor.setDecorations(INLINE_RESULT_DECORATION_TYPE, [decoration]);
}
}
function handleSuccess(outputChannel: vscode.OutputChannel, showResults: boolean, respObjs: any[], selectionEndLine: number): void {
if (!showResults) {
vscode.window.showInformationMessage('Successfully compiled');
} else {
const config = vscode.workspace.getConfiguration('clojureVSCode');
respObjs.forEach(respObj => {
if (respObj.out)
outputChannel.append(respObj.out);
if (respObj.err)
outputChannel.append(respObj.err);
if (respObj.value)
outputChannel.appendLine(`=> ${respObj.value}`);
if (config.showResultInline) {
showInlineResult(respObj, selectionEndLine);
} else {
outputChannel.show(true);
};
});
}
nreplClient.close(respObjs[0].session);
};
export function clearInlineResultDecorationOnMove(event: vscode.TextEditorSelectionChangeEvent) {
const config = vscode.workspace.getConfiguration('clojureVSCode');
if (config.showResultInline
&& event.textEditor.document.languageId === LANGUAGE
&& event.textEditor === vscode.window.activeTextEditor) {
event.textEditor.setDecorations(INLINE_RESULT_DECORATION_TYPE, []);
}
}