-
-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathrepl-history.ts
More file actions
170 lines (156 loc) · 5.75 KB
/
repl-history.ts
File metadata and controls
170 lines (156 loc) · 5.75 KB
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
import * as vscode from 'vscode';
import * as state from '../state';
import * as util from '../utilities';
import {
getIndexAfterLastNonWhitespace,
getTextAfterLastOccurrenceOfSubstring,
} from '../util/string';
import type { ReplSessionType } from '../config';
import { isResultsDoc, getSessionType, getPrompt, append } from './repl-doc';
import { addToHistory } from '../results-output/util';
import { isUndefined } from 'lodash';
const replHistoryCommandsActiveContext = 'calva:replHistoryCommandsActive';
let historyIndex: number | undefined = undefined;
let lastTextAtPrompt: string | undefined = undefined;
function setReplHistoryCommandsActiveContext(editor: vscode.TextEditor): void {
if (editor && util.getConnectedState() && isResultsDoc(editor.document)) {
const document = editor.document;
const selection = editor.selections[0];
const positionAtEndOfContent = document.positionAt(
getIndexAfterLastNonWhitespace(document.getText())
);
if (selection.start.isAfterOrEqual(positionAtEndOfContent)) {
void vscode.commands.executeCommand('setContext', replHistoryCommandsActiveContext, true);
return;
}
}
void vscode.commands.executeCommand('setContext', replHistoryCommandsActiveContext, false);
}
function resetState(): void {
historyIndex = undefined;
lastTextAtPrompt = undefined;
}
function getHistoryKey(replSessionType: ReplSessionType): string {
return `calva-repl-${replSessionType}-history`;
}
function getHistory(replSessionType: ReplSessionType): Array<string> {
const key = getHistoryKey(replSessionType);
const history = state.extensionContext.workspaceState.get(key, []);
return history;
}
function updateReplHistory(
replSessionType: ReplSessionType,
history: string[],
content: string,
index: number
) {
const newHistory = [...history];
newHistory[index] = content.trim();
void state.extensionContext.workspaceState.update(getHistoryKey(replSessionType), newHistory);
}
function addToReplHistory(replSessionType: ReplSessionType, content: string) {
const newHistory = addToHistory(getHistory(replSessionType), content);
void state.extensionContext.workspaceState.update(getHistoryKey(replSessionType), newHistory);
}
function clearHistory() {
const replType = getSessionType();
const key = getHistoryKey(replType);
void state.extensionContext.workspaceState.update(key, []);
resetState();
append('; REPL history cleared');
append(getPrompt());
}
function showReplHistoryEntry(
historyEntry: string | undefined,
resultsEditor: vscode.TextEditor
): void {
const prompt = getPrompt();
const resultsDoc = resultsEditor.document;
const docText = resultsDoc.getText();
const indexOfLastPrompt = docText.lastIndexOf(prompt);
if (indexOfLastPrompt === -1) {
// Prompt not found in results doc, so append a prompt and re-run this function
append(getPrompt(), (_) => {
showReplHistoryEntry(historyEntry, resultsEditor);
});
return;
}
const insertOffset = indexOfLastPrompt + prompt.length;
const startPosition = resultsDoc.positionAt(insertOffset);
const range = new vscode.Range(startPosition, resultsDoc.positionAt(Infinity));
const entry = historyEntry || '\n';
const edit = new vscode.WorkspaceEdit();
edit.replace(resultsDoc.uri, range, entry);
void vscode.workspace.applyEdit(edit).then((_) => {
void resultsDoc.save();
util.scrollToBottom(resultsEditor);
});
}
function prependNewline(text: string) {
return `\n${text}`;
}
function showPreviousReplHistoryEntry(): void {
const editor = util.getActiveTextEditor();
const doc = editor.document;
const replSessionType = getSessionType();
const history = getHistory(replSessionType);
if (!isResultsDoc(doc) || historyIndex === 0 || history.length === 0) {
return;
}
const textAtPrompt = getTextAfterLastOccurrenceOfSubstring(doc.getText(), getPrompt());
if (isUndefined(historyIndex)) {
historyIndex = history.length;
lastTextAtPrompt = textAtPrompt;
} else {
util.assertIsDefined(textAtPrompt, 'Expected to find text at the prompt!');
updateReplHistory(replSessionType, history, textAtPrompt, historyIndex);
}
historyIndex--;
showReplHistoryEntry(prependNewline(history[historyIndex]), editor);
}
function showNextReplHistoryEntry(): void {
const editor = util.getActiveTextEditor();
const doc = editor.document;
const replSessionType = getSessionType();
const history = getHistory(replSessionType);
if (!isResultsDoc(doc) || historyIndex === null) {
return;
}
if (historyIndex === history.length - 1) {
historyIndex = undefined;
showReplHistoryEntry(lastTextAtPrompt, editor);
} else {
const textAtPrompt = getTextAfterLastOccurrenceOfSubstring(doc.getText(), getPrompt());
util.assertIsDefined(textAtPrompt, 'Expected to find text at the prompt!');
util.assertIsDefined(historyIndex, 'Expected a value for historyIndex!');
updateReplHistory(replSessionType, history, textAtPrompt, historyIndex);
historyIndex++;
const nextHistoryEntry = history[historyIndex];
showReplHistoryEntry(prependNewline(nextHistoryEntry), editor);
}
}
function searchReplHistory(): void {
const editor = util.getActiveTextEditor();
const history = getHistory(getSessionType());
vscode.window.showInputBox({
prompt: 'Enter search term for REPL history',
validateInput: (text) => {
const entry = history.filter((entry) => entry.includes(text)).pop();
if (entry !== undefined) {
showReplHistoryEntry(prependNewline(entry), editor);
return undefined;
}
return 'No matching entry found in REPL history';
},
});
}
export {
addToHistory,
addToReplHistory,
searchReplHistory,
showPreviousReplHistoryEntry,
showNextReplHistoryEntry,
resetState,
clearHistory,
setReplHistoryCommandsActiveContext,
};