forked from jupyterlab/jupyterlab
/
history.ts
371 lines (331 loc) · 9.85 KB
/
history.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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { KernelMessage } from '@jupyterlab/services';
import { IDisposable } from '@phosphor/disposable';
import { Signal } from '@phosphor/signaling';
import { IClientSession } from '@jupyterlab/apputils';
import { CodeEditor } from '@jupyterlab/codeeditor';
/**
* The definition of a console history manager object.
*/
export interface IConsoleHistory extends IDisposable {
/**
* The client session used by the foreign handler.
*/
readonly session: IClientSession;
/**
* The current editor used by the history widget.
*/
editor: CodeEditor.IEditor | null;
/**
* The placeholder text that a history session began with.
*/
readonly placeholder: string;
/**
* Get the previous item in the console history.
*
* @param placeholder - The placeholder string that gets temporarily added
* to the history only for the duration of one history session. If multiple
* placeholders are sent within a session, only the first one is accepted.
*
* @returns A Promise for console command text or `undefined` if unavailable.
*/
back(placeholder: string): Promise<string>;
/**
* Get the next item in the console history.
*
* @param placeholder - The placeholder string that gets temporarily added
* to the history only for the duration of one history session. If multiple
* placeholders are sent within a session, only the first one is accepted.
*
* @returns A Promise for console command text or `undefined` if unavailable.
*/
forward(placeholder: string): Promise<string>;
/**
* Add a new item to the bottom of history.
*
* @param item The item being added to the bottom of history.
*
* #### Notes
* If the item being added is undefined or empty, it is ignored. If the item
* being added is the same as the last item in history, it is ignored as well
* so that the console's history will consist of no contiguous repetitions.
*/
push(item: string): void;
/**
* Reset the history navigation state, i.e., start a new history session.
*/
reset(): void;
}
/**
* A console history manager object.
*/
export class ConsoleHistory implements IConsoleHistory {
/**
* Construct a new console history object.
*/
constructor(options: ConsoleHistory.IOptions) {
this.session = options.session;
void this._handleKernel();
this.session.kernelChanged.connect(this._handleKernel, this);
}
/**
* The client session used by the foreign handler.
*/
readonly session: IClientSession;
/**
* The current editor used by the history manager.
*/
get editor(): CodeEditor.IEditor | null {
return this._editor;
}
set editor(value: CodeEditor.IEditor | null) {
if (this._editor === value) {
return;
}
let prev = this._editor;
if (prev) {
prev.edgeRequested.disconnect(this.onEdgeRequest, this);
prev.model.value.changed.disconnect(this.onTextChange, this);
}
this._editor = value;
if (value) {
value.edgeRequested.connect(this.onEdgeRequest, this);
value.model.value.changed.connect(this.onTextChange, this);
}
}
/**
* The placeholder text that a history session began with.
*/
get placeholder(): string {
return this._placeholder;
}
/**
* Get whether the console history manager is disposed.
*/
get isDisposed(): boolean {
return this._isDisposed;
}
/**
* Dispose of the resources held by the console history manager.
*/
dispose(): void {
this._isDisposed = true;
this._history.length = 0;
Signal.clearData(this);
}
/**
* Get the previous item in the console history.
*
* @param placeholder - The placeholder string that gets temporarily added
* to the history only for the duration of one history session. If multiple
* placeholders are sent within a session, only the first one is accepted.
*
* @returns A Promise for console command text or `undefined` if unavailable.
*/
back(placeholder: string): Promise<string> {
if (!this._hasSession) {
this._hasSession = true;
this._placeholder = placeholder;
// Filter the history with the placeholder string.
this.setFilter(placeholder);
this._cursor = this._filtered.length - 1;
}
--this._cursor;
this._cursor = Math.max(0, this._cursor);
let content = this._filtered[this._cursor];
return Promise.resolve(content);
}
/**
* Get the next item in the console history.
*
* @param placeholder - The placeholder string that gets temporarily added
* to the history only for the duration of one history session. If multiple
* placeholders are sent within a session, only the first one is accepted.
*
* @returns A Promise for console command text or `undefined` if unavailable.
*/
forward(placeholder: string): Promise<string> {
if (!this._hasSession) {
this._hasSession = true;
this._placeholder = placeholder;
// Filter the history with the placeholder string.
this.setFilter(placeholder);
this._cursor = this._filtered.length;
}
++this._cursor;
this._cursor = Math.min(this._filtered.length - 1, this._cursor);
let content = this._filtered[this._cursor];
return Promise.resolve(content);
}
/**
* Add a new item to the bottom of history.
*
* @param item The item being added to the bottom of history.
*
* #### Notes
* If the item being added is undefined or empty, it is ignored. If the item
* being added is the same as the last item in history, it is ignored as well
* so that the console's history will consist of no contiguous repetitions.
*/
push(item: string): void {
if (item && item !== this._history[this._history.length - 1]) {
this._history.push(item);
}
this.reset();
}
/**
* Reset the history navigation state, i.e., start a new history session.
*/
reset(): void {
this._cursor = this._history.length;
this._hasSession = false;
this._placeholder = '';
}
/**
* Populate the history collection on history reply from a kernel.
*
* @param value The kernel message history reply.
*
* #### Notes
* History entries have the shape:
* [session: number, line: number, input: string]
* Contiguous duplicates are stripped out of the API response.
*/
protected onHistory(value: KernelMessage.IHistoryReplyMsg): void {
this._history.length = 0;
let last = '';
let current = '';
if (value.content.status === 'ok') {
for (let i = 0; i < value.content.history.length; i++) {
current = (value.content.history[i] as string[])[2];
if (current !== last) {
this._history.push((last = current));
}
}
}
// Reset the history navigation cursor back to the bottom.
this._cursor = this._history.length;
}
/**
* Handle a text change signal from the editor.
*/
protected onTextChange(): void {
if (this._setByHistory) {
this._setByHistory = false;
return;
}
this.reset();
}
/**
* Handle an edge requested signal.
*/
protected onEdgeRequest(
editor: CodeEditor.IEditor,
location: CodeEditor.EdgeLocation
): void {
let model = editor.model;
let source = model.value.text;
if (location === 'top' || location === 'topLine') {
void this.back(source).then(value => {
if (this.isDisposed || !value) {
return;
}
if (model.value.text === value) {
return;
}
this._setByHistory = true;
model.value.text = value;
let columnPos = 0;
columnPos = value.indexOf('\n');
if (columnPos < 0) {
columnPos = value.length;
}
editor.setCursorPosition({ line: 0, column: columnPos });
});
} else {
void this.forward(source).then(value => {
if (this.isDisposed) {
return;
}
let text = value || this.placeholder;
if (model.value.text === text) {
return;
}
this._setByHistory = true;
model.value.text = text;
let pos = editor.getPositionAt(text.length);
if (pos) {
editor.setCursorPosition(pos);
}
});
}
}
/**
* Handle the current kernel changing.
*/
private async _handleKernel(): Promise<void> {
let kernel = this.session.kernel;
if (!kernel) {
this._history.length = 0;
return;
}
return kernel.requestHistory(Private.initialRequest).then(v => {
this.onHistory(v);
});
}
/**
* Set the filter data.
*
* @param filterStr - The string to use when filtering the data.
*/
protected setFilter(filterStr: string = ''): void {
// Apply the new filter and remove contiguous duplicates.
this._filtered.length = 0;
let last = '';
let current = '';
for (let i = 0; i < this._history.length; i++) {
current = this._history[i];
if (
current !== last &&
filterStr === current.slice(0, filterStr.length)
) {
this._filtered.push((last = current));
}
}
this._filtered.push(filterStr);
}
private _cursor = 0;
private _hasSession = false;
private _history: string[] = [];
private _placeholder: string = '';
private _setByHistory = false;
private _isDisposed = false;
private _editor: CodeEditor.IEditor | null = null;
private _filtered: string[] = [];
}
/**
* A namespace for ConsoleHistory statics.
*/
export namespace ConsoleHistory {
/**
* The initialization options for a console history object.
*/
export interface IOptions {
/**
* The client session used by the foreign handler.
*/
session: IClientSession;
}
}
/**
* A namespace for private data.
*/
namespace Private {
export const initialRequest: KernelMessage.IHistoryRequestMsg['content'] = {
output: false,
raw: true,
hist_access_type: 'tail',
n: 500
};
}