-
-
Notifications
You must be signed in to change notification settings - Fork 226
/
Copy pathresults-doc.ts
443 lines (389 loc) · 14.3 KB
/
results-doc.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import * as path from 'path';
import * as vscode from 'vscode';
import * as state from '../state';
import { highlight } from '../highlight/src/extension';
import { NReplSession } from '../nrepl';
import * as util from '../utilities';
import * as select from '../select';
import { formatCode } from '../calva-fmt/src/format';
import * as namespace from '../namespace';
import * as config from '../config';
import type { ReplSessionType } from '../config';
import * as replHistory from './repl-history';
import * as docMirror from '../doc-mirror/index';
import { PrintStackTraceCodelensProvider } from '../providers/codelense';
import * as replSession from '../nrepl/repl-session';
import { formatAsLineComments, splitEditQueueForTextBatching } from './util';
const RESULTS_DOC_NAME = `output.${config.REPL_FILE_EXT}`;
const PROMPT_HINT = 'Use `alt+enter` to evaluate';
const START_GREETINGS = [
'This is the Calva evaluation results output window.',
'TIPS: The keyboard shortcut `ctrl+alt+o o` shows and focuses this window',
' when connected to a REPL session.',
'Please see https://calva.io/output/ for more info.',
'Happy coding! ♥️',
].join(`\n`);
export const CLJ_CONNECT_GREETINGS = [
'TIPS:',
' - You can edit the contents here. Use it as a REPL if you like.',
' - `alt+enter` evaluates the current top level form.',
' - `ctrl+enter` evaluates the current form.',
' - `alt+up` and `alt+down` traverse up and down the REPL command history',
' when the cursor is after the last contents at the prompt',
' - Clojure lines in stack traces are peekable and clickable.',
].join(`\n`);
export const CLJS_CONNECT_GREETINGS = [
'TIPS: You can choose which REPL to use (clj or cljs):',
' *Calva: Toggle REPL connection*',
' (There is a button in the status bar for this)',
].join(`\n`);
function outputFileDir() {
const projectRoot = state.getProjectRootUri();
util.assertIsDefined(projectRoot, 'Expected there to be a project root!');
try {
return vscode.Uri.joinPath(projectRoot, '.calva', 'output-window');
} catch {
return vscode.Uri.file(path.join(projectRoot.fsPath, '.calva', 'output-window'));
}
}
let isInitialized = false;
const DOC_URI = () => {
return vscode.Uri.joinPath(outputFileDir(), RESULTS_DOC_NAME);
};
let _sessionType: ReplSessionType = 'clj';
const _sessionInfo: { [id: string]: { ns?: string; session?: NReplSession } } = {
clj: {},
cljs: {},
};
const showPrompt: { [id: string]: boolean } = {
clj: true,
cljs: true,
};
export function getPrompt(): string {
// eslint-disable-next-line no-irregular-whitespace
let prompt = `${_sessionType}꞉${getNs()}꞉> `;
if (showPrompt[_sessionType]) {
showPrompt[_sessionType] = false;
prompt = `${prompt} ${formatAsLineComments(PROMPT_HINT)}`;
}
return prompt;
}
export function getNs(): string | undefined {
return _sessionInfo[_sessionType].ns;
}
export function getSessionType(): ReplSessionType {
return _sessionType;
}
export function getSession(): NReplSession | undefined {
return _sessionInfo[_sessionType].session;
}
export function setSession(session: NReplSession, newNs?: string): void {
if (session) {
if (session.replType) {
_sessionType = session.replType;
}
_sessionInfo[_sessionType].session = session;
}
if (newNs) {
_sessionInfo[_sessionType].ns = newNs;
}
}
export function isResultsDoc(doc?: vscode.TextDocument): boolean {
return !!doc && path.basename(doc.fileName) === RESULTS_DOC_NAME;
}
function getViewColumn(): vscode.ViewColumn {
const column: vscode.ViewColumn | undefined =
state.extensionContext.workspaceState.get(`outputWindowViewColumn`);
return column ? column : vscode.ViewColumn.Two;
}
function setViewColumn(column: vscode.ViewColumn | undefined) {
return state.extensionContext.workspaceState.update(`outputWindowViewColumn`, column);
}
export function setContextForOutputWindowActive(isActive: boolean): void {
void state.extensionContext.workspaceState.update(`outputWindowActive`, isActive);
void vscode.commands.executeCommand('setContext', 'calva:outputWindowActive', isActive);
}
export async function initResultsDoc(): Promise<vscode.TextDocument> {
const docUri = DOC_URI();
await vscode.workspace.fs.createDirectory(outputFileDir());
let resultsDoc: vscode.TextDocument;
try {
resultsDoc = await vscode.workspace.openTextDocument(docUri);
} catch (e) {
await util.writeTextToFile(docUri, '');
resultsDoc = await vscode.workspace.openTextDocument(docUri);
}
if (config.getConfig().autoOpenREPLWindow) {
const resultsEditor = await vscode.window.showTextDocument(resultsDoc, getViewColumn(), true);
const firstPos = resultsEditor.document.positionAt(0);
const lastPos = resultsDoc.positionAt(Infinity);
resultsEditor.selection = new vscode.Selection(lastPos, lastPos);
resultsEditor.revealRange(new vscode.Range(firstPos, firstPos));
}
if (isInitialized) {
return resultsDoc;
}
const greetings = `${formatAsLineComments(START_GREETINGS)}\n\n`;
const edit = new vscode.WorkspaceEdit();
const fullRange = new vscode.Range(resultsDoc.positionAt(0), resultsDoc.positionAt(Infinity));
edit.replace(docUri, fullRange, greetings);
await vscode.workspace.applyEdit(edit);
void resultsDoc.save();
registerResultDocSubscriptions();
// For some reason onDidChangeTextEditorViewColumn won't fire
state.extensionContext.subscriptions.push(
vscode.window.onDidChangeActiveTextEditor((event) => {
if (event) {
const isOutputWindow = isResultsDoc(event.document);
setContextForOutputWindowActive(isOutputWindow);
if (isOutputWindow) {
void setViewColumn(event.viewColumn);
}
}
})
);
state.extensionContext.subscriptions.push(
vscode.window.onDidChangeTextEditorSelection((event) => {
let submitOnEnter = false;
if (event.textEditor) {
const document = event.textEditor.document;
if (isResultsDoc(document)) {
const idx = document.offsetAt(event.selections[0].active);
const mirrorDoc = docMirror.getDocument(document);
const selectionCursor = mirrorDoc.getTokenCursor(idx);
selectionCursor.forwardWhitespace();
if (selectionCursor.atEnd()) {
const promptCursor = mirrorDoc.getTokenCursor(idx);
do {
promptCursor.previous();
} while (promptCursor.getPrevToken().type !== 'prompt' && !promptCursor.atStart());
const submitRange = selectionCursor.rangeForCurrentForm(idx);
submitOnEnter = submitRange && submitRange[1] > promptCursor.offsetStart;
}
}
}
void vscode.commands.executeCommand(
'setContext',
'calva:outputWindowSubmitOnEnter',
submitOnEnter
);
})
);
vscode.languages.registerCodeLensProvider(
config.documentSelector,
new PrintStackTraceCodelensProvider()
);
// If the output window is active when initResultsDoc is run, these contexts won't be set properly without the below
// until the next time it's focused
const activeTextEditor = util.tryToGetActiveTextEditor();
if (activeTextEditor && isResultsDoc(activeTextEditor.document)) {
setContextForOutputWindowActive(true);
replHistory.setReplHistoryCommandsActiveContext(activeTextEditor);
}
replHistory.resetState();
isInitialized = true;
return resultsDoc;
}
export async function openResultsDoc(): Promise<vscode.TextDocument> {
const resultsDoc = await vscode.workspace.openTextDocument(DOC_URI());
return resultsDoc;
}
export function revealResultsDoc(preserveFocus: boolean = true) {
void openResultsDoc().then((doc) => {
void vscode.window.showTextDocument(doc, getViewColumn(), preserveFocus);
});
}
export async function revealDocForCurrentNS(preserveFocus: boolean = true) {
const uri = await getUriForCurrentNamespace();
void vscode.workspace.openTextDocument(uri).then((doc) =>
vscode.window.showTextDocument(doc, {
preserveFocus,
})
);
}
export async function setNamespaceFromCurrentFile() {
const session = replSession.getSession();
const ns = namespace.getNamespace(util.tryToGetDocument({}));
if (getNs() !== ns && util.isDefined(ns)) {
await session.switchNS(ns);
}
setSession(session, ns);
replSession.updateReplSessionType();
appendPrompt();
}
async function appendFormGrabbingSessionAndNS(topLevel: boolean) {
const session = replSession.getSession();
const ns = namespace.getNamespace(util.tryToGetDocument({}));
const editor = util.getActiveTextEditor();
const doc = editor.document;
const selection = editor.selection;
let code = '';
if (selection.isEmpty) {
const formSelection = select.getFormSelection(doc, selection.active, topLevel);
code = await formatCode(doc.getText(formSelection), doc.eol);
} else {
code = await formatCode(doc.getText(selection), doc.eol);
}
if (code != '') {
if (getNs() !== ns) {
await session.switchNS(ns);
}
setSession(session, ns);
appendLine(code, (_) => revealResultsDoc(false));
}
}
export function appendCurrentForm() {
void appendFormGrabbingSessionAndNS(false);
}
export function appendCurrentTopLevelForm() {
void appendFormGrabbingSessionAndNS(true);
}
export async function lastLineIsEmpty(): Promise<boolean> {
try {
const doc = await vscode.workspace.openTextDocument(DOC_URI());
return util.lastLineIsEmpty(doc);
} catch (error) {
console.error('Failed opening results doc', error);
}
}
function visibleResultsEditors(): vscode.TextEditor[] {
return vscode.window.visibleTextEditors.filter((editor) => isResultsDoc(editor.document));
}
function handleResultDocEditorDidOpen(editor: vscode.TextEditor) {
util.scrollToBottom(editor);
}
function registerResultDocSubscriptions() {
let currentResultDocs = visibleResultsEditors();
const subOpen = vscode.window.onDidChangeVisibleTextEditors((editors) => {
const current = editors.filter((editor) => isResultsDoc(editor.document));
const opened = current.filter((editor) => currentResultDocs.includes(editor));
currentResultDocs = current;
opened.forEach(handleResultDocEditorDidOpen);
});
state.extensionContext.subscriptions.push(subOpen);
}
async function writeToResultsDoc({ text, onAppended }: ResultsBufferEntry): Promise<void> {
const docUri = DOC_URI();
const doc = await vscode.workspace.openTextDocument(docUri);
const insertPosition = doc.positionAt(Infinity);
const edit = new vscode.WorkspaceEdit();
const editText = util.stripAnsi(text);
edit.insert(docUri, insertPosition, editText);
if (!((await vscode.workspace.applyEdit(edit)) && (await doc.save()))) {
return;
}
onAppended?.(
new vscode.Location(docUri, insertPosition),
new vscode.Location(docUri, doc.positionAt(Infinity))
);
const editors = visibleResultsEditors();
editors.forEach((editor) => {
util.scrollToBottom(editor);
highlight(editor);
});
}
export type ResultsBuffer = ResultsBufferEntry[];
export type ResultsBufferEntry = {
text: string;
onAppended?: OnAppendedCallback;
};
export interface OnAppendedCallback {
(insertLocation: vscode.Location, newPosition?: vscode.Location): any;
}
let resultsBuffer: ResultsBuffer = [];
async function writeNextOutputBatch() {
if (!resultsBuffer[0]) {
return;
}
// Any entries that contain onAppended are not batched with other pending
// entries to simplify providing the correct insert position to the callback.
if (resultsBuffer[0].onAppended) {
return await writeToResultsDoc(resultsBuffer.shift());
}
// Batch all remaining entries up until another onAppended callback.
const [nextText, remaining] = splitEditQueueForTextBatching(resultsBuffer);
resultsBuffer = remaining;
await writeToResultsDoc({ text: nextText.join('') });
}
// Ensures that writeNextOutputBatch is called on buffer sequentially.
let outputPending = false;
async function flushOutput() {
if (outputPending) {
return;
}
outputPending = true;
try {
while (resultsBuffer.length > 0) {
await writeNextOutputBatch();
}
} catch (err) {
console.error('Error writing to results doc:', err);
} finally {
outputPending = false;
}
}
/* If something must be done after a particular edit, use the onAppended callback. */
export function append(text: string, onAppended?: OnAppendedCallback): void {
resultsBuffer.push({ text, onAppended });
void flushOutput();
}
export function appendLine(text = '', onAppended?: OnAppendedCallback): void {
append(`${text}\n`, onAppended);
}
export function discardPendingPrints(): void {
resultsBuffer = [];
appendPrompt();
}
export type OutputStacktraceEntry = { uri: vscode.Uri; line: number };
let _lastStacktrace: any[] = [];
let _lastStackTraceRange: vscode.Range | undefined;
const _stacktraceEntries = {} as OutputStacktraceEntry;
export function getStacktraceEntryForKey(key: string): OutputStacktraceEntry {
return _stacktraceEntries[key];
}
export function stackEntryString(entry: any): string {
const name = entry.var || entry.name;
return `${name} (${entry.file}:${entry.line})`;
}
export function saveStacktrace(stacktrace: any[]): void {
if (stacktrace === undefined || stacktrace.length === 0) {
return;
}
_lastStacktrace = [];
stacktrace
.filter((entry) => {
return (
!entry.flags.includes('dup') &&
!['clojure.lang.RestFn', 'clojure.lang.AFn'].includes(entry.class)
);
})
.forEach((entry) => {
entry.string = stackEntryString(entry);
_lastStacktrace.push(entry);
const fileUrl = entry['file-url'];
if (typeof fileUrl === 'string') {
_stacktraceEntries[entry.string] = {
uri: vscode.Uri.parse(fileUrl),
line: entry.line,
};
}
});
}
export function markLastStacktraceRange(location: vscode.Location): void {
_lastStackTraceRange = location.range; //new vscode.Range(newPosition, newPosition);
}
export function getLastStackTraceRange(): vscode.Range | undefined {
return _lastStackTraceRange;
}
export function printLastStacktrace(): void {
const text = _lastStacktrace.map((entry) => entry.string).join('\n');
appendLine(text, (_location) => {
_lastStackTraceRange = undefined;
});
}
export function appendPrompt(onAppended?: OnAppendedCallback) {
appendLine(getPrompt(), onAppended);
}
function getUriForCurrentNamespace(): Promise<vscode.Uri> {
return namespace.getUriForNamespace(getSession(), getNs());
}