/
inline.ts
765 lines (693 loc) · 21.6 KB
/
inline.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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { PanelLayout, Widget } from '@lumino/widgets';
import { IDisposable } from '@lumino/disposable';
import { ISignal, Signal } from '@lumino/signaling';
import { Message } from '@lumino/messaging';
import { CodeEditor } from '@jupyterlab/codeeditor';
import { HoverBox } from '@jupyterlab/ui-components';
import { CodeMirrorEditor } from '@jupyterlab/codemirror';
import { SourceChange } from '@jupyter/ydoc';
import { kernelIcon, Toolbar } from '@jupyterlab/ui-components';
import { TranslationBundle } from '@jupyterlab/translation';
import {
IInlineCompleterFactory,
IInlineCompleterSettings,
IInlineCompletionList
} from './tokens';
import { CompletionHandler } from './handler';
import { GhostTextManager } from './ghost';
const INLINE_COMPLETER_CLASS = 'jp-InlineCompleter';
const INLINE_COMPLETER_ACTIVE_CLASS = 'jp-mod-inline-completer-active';
const HOVER_CLASS = 'jp-InlineCompleter-hover';
const PROGRESS_BAR_CLASS = 'jp-InlineCompleter-progressBar';
/**
* Widget enabling user to choose among inline completions,
* typically by pressing next/previous buttons, and showing
* additional metadata about active completion, such as
* inline completion provider name.
*/
export class InlineCompleter extends Widget {
constructor(options: InlineCompleter.IOptions) {
super({ node: document.createElement('div') });
this.model = options.model ?? null;
this.editor = options.editor ?? null;
this.addClass(INLINE_COMPLETER_CLASS);
this._ghostManager = new GhostTextManager({
onBlur: this._onEditorBlur.bind(this)
});
this._trans = options.trans;
const layout = (this.layout = new PanelLayout());
layout.addWidget(this._suggestionsCounter);
layout.addWidget(this.toolbar);
layout.addWidget(this._providerWidget);
this._progressBar = document.createElement('div');
this._progressBar.className = PROGRESS_BAR_CLASS;
this.node.appendChild(this._progressBar);
this._updateShortcutsVisibility();
this._updateDisplay();
// Allow the node to receive focus, which prevents removing the ghost text
// when user mis-clicks on the tooltip instead of the button in the tooltip.
this.node.tabIndex = 0;
}
/**
* Toolbar with buttons such as previous/next/accept.
*/
get toolbar() {
return this._toolbar;
}
/**
* The editor used by the completion widget.
*/
get editor(): CodeEditor.IEditor | null | undefined {
return this._editor;
}
set editor(newValue: CodeEditor.IEditor | null | undefined) {
this.model?.reset();
this._editor = newValue;
}
/**
* The model used by the completer widget.
*/
get model(): InlineCompleter.IModel | null {
return this._model;
}
set model(model: InlineCompleter.IModel | null) {
if ((!model && !this._model) || model === this._model) {
return;
}
if (this._model) {
this._model.suggestionsChanged.disconnect(
this._onModelSuggestionsChanged,
this
);
this._model.filterTextChanged.disconnect(
this._onModelFilterTextChanged,
this
);
this._model.provisionProgress.disconnect(this._onProvisionProgress, this);
}
this._model = model;
if (this._model) {
this._model.suggestionsChanged.connect(
this._onModelSuggestionsChanged,
this
);
this._model.filterTextChanged.connect(
this._onModelFilterTextChanged,
this
);
this._model.provisionProgress.connect(this._onProvisionProgress, this);
}
}
cycle(direction: 'next' | 'previous'): void {
const items = this.model?.completions?.items;
if (!items) {
return;
}
if (direction === 'next') {
const proposed = this._current + 1;
this._current = proposed === items.length ? 0 : proposed;
} else {
const proposed = this._current - 1;
this._current = proposed === -1 ? items.length - 1 : proposed;
}
this._updateStreamTracking();
this._render();
}
accept(): void {
const model = this.model;
const candidate = this.current;
const editor = this._editor;
if (!editor || !model || !candidate) {
return;
}
const position = model.cursor;
const value = candidate.insertText;
const cursorBeforeChange = editor.getOffsetAt(editor.getCursorPosition());
const requestPosition = editor.getOffsetAt(position);
const start = requestPosition;
const end = cursorBeforeChange;
// update the shared model in a single transaction so that the undo manager works as expected
editor.model.sharedModel.updateSource(
requestPosition,
cursorBeforeChange,
value
);
if (cursorBeforeChange <= end && cursorBeforeChange >= start) {
editor.setCursorPosition(editor.getPositionAt(start + value.length)!);
}
model.reset();
this.update();
}
get current(): CompletionHandler.IInlineItem | null {
const completions = this.model?.completions;
if (!completions) {
return null;
}
return completions.items[this._current];
}
private _updateStreamTracking() {
if (this._lastItem) {
this._lastItem.stream.disconnect(this._onStream, this);
}
const current = this.current;
if (current) {
current.stream.connect(this._onStream, this);
}
this._lastItem = current;
}
private _onStream(
_emitter: CompletionHandler.IInlineItem,
_change: CompletionHandler.StraemEvent
) {
// TODO handle stuck streams, i.e. if we connected and received 'opened'
// but then did not receive 'closed' for a long time we should disconnect
// and update widget with an 'timed out' status.
const completions = this.model?.completions;
if (!completions || !completions.items || completions.items.length === 0) {
return;
}
if (this.isHidden) {
return;
}
const candidate = completions.items[this._current];
this._setText(candidate);
}
/**
* Change user-configurable settings.
*/
configure(settings: IInlineCompleterSettings) {
this._showWidget = settings.showWidget;
this._updateDisplay();
if (settings.showShortcuts !== this._showShortcuts) {
this._showShortcuts = settings.showShortcuts;
this._updateShortcutsVisibility();
}
GhostTextManager.streamingAnimation = settings.streamingAnimation;
}
/**
* Handle the DOM events for the widget.
*
* @param event - The DOM event sent to the widget.
*
* #### Notes
* This method implements the DOM `EventListener` interface and is
* called in response to events on the dock panel's node. It should
* not be called directly by user code.
*/
handleEvent(event: Event): void {
if (this.isHidden || !this._editor) {
return;
}
switch (event.type) {
case 'pointerdown':
this._evtPointerdown(event as PointerEvent);
break;
case 'scroll':
this._evtScroll(event as MouseEvent);
break;
default:
break;
}
}
/**
* Handle `update-request` messages.
*/
protected onUpdateRequest(msg: Message): void {
super.onUpdateRequest(msg);
const model = this._model;
if (!model) {
return;
}
let reply = model.completions;
// If there are no items, hide.
if (!reply || !reply.items || reply.items.length === 0) {
if (!this.isHidden) {
this.hide();
}
return;
}
if (this.isHidden) {
this.show();
this._setGeometry();
}
}
/**
* Handle `after-attach` messages for the widget.
*/
protected onAfterAttach(msg: Message): void {
document.addEventListener('scroll', this, true);
document.addEventListener('pointerdown', this, true);
}
/**
* Handle `before-detach` messages for the widget.
*/
protected onBeforeDetach(msg: Message): void {
document.removeEventListener('scroll', this, true);
document.removeEventListener('pointerdown', this, true);
}
/**
* Handle pointerdown events for the widget.
*/
private _evtPointerdown(event: PointerEvent) {
if (this.isHidden || !this._editor) {
return;
}
const target = event.target as HTMLElement;
if (this.node.contains(target)) {
return true;
}
this.hide();
this.model?.reset();
}
/**
* Handle scroll events for the widget
*/
private _evtScroll(event: MouseEvent) {
if (this.isHidden || !this._editor) {
return;
}
const { node } = this;
// All scrolls except scrolls in the actual hover box node may cause the
// referent editor that anchors the node to move, so the only scroll events
// that can safely be ignored are ones that happen inside the hovering node.
if (node.contains(event.target as HTMLElement)) {
return;
}
// Set the geometry of the node asynchronously.
requestAnimationFrame(() => {
this._setGeometry();
});
}
private _onEditorBlur(event: FocusEvent) {
if (this.node.contains(event.relatedTarget as HTMLElement)) {
// Cancel removing ghost text if our node is receiving focus
return false;
}
// The ghost text will be removed, so nothing to accept
this._editor?.host.classList.remove(INLINE_COMPLETER_ACTIVE_CLASS);
// Hide the widget if editor was blurred.
this.hide();
}
private _onModelSuggestionsChanged(
_emitter: InlineCompleter.IModel,
args: ISuggestionsChangedArgs
): void {
if (!this.isAttached) {
this.update();
return;
}
if (args.event === 'set') {
this._current = args.indexMap!.get(this._current) ?? 0;
} else if (args.event === 'clear') {
const editor = this.editor;
if (editor) {
this._ghostManager.clearGhosts((editor as CodeMirrorEditor).editor);
editor.host.classList.remove(INLINE_COMPLETER_ACTIVE_CLASS);
}
}
this._updateStreamTracking();
this.update();
this._render();
}
private _onModelFilterTextChanged(
_emitter: InlineCompleter.IModel,
mapping: Map<number, number>
): void {
const completions = this.model?.completions;
if (!completions || !completions.items || completions.items.length === 0) {
return;
}
this._current = mapping.get(this._current) ?? 0;
this._updateStreamTracking();
// Because the signal will be emitted during `EditorView.update` we want to
// wait for the update to complete before calling `this._render()`. As there
// is no API to check if update is done, we instead defer to next engine tick.
setTimeout(() => {
this._render();
// (reading layout to get coordinate to position hoverbox is not allowed either)
this._setGeometry();
}, 0);
}
private _onProvisionProgress(
_emitter: InlineCompleter.IModel,
progress: InlineCompleter.IProvisionProgress
): void {
requestAnimationFrame(() => {
if (progress.pendingProviders === 0) {
this._progressBar.style.display = 'none';
} else {
this._progressBar.style.display = '';
this._progressBar.style.width =
(100 * progress.pendingProviders) / progress.totalProviders + '%';
}
});
}
private _render(): void {
const completions = this.model?.completions;
if (!completions || !completions.items || completions.items.length === 0) {
return;
}
const candidate = completions.items[this._current];
this._setText(candidate);
if (this._showWidget === 'never') {
return;
}
this._suggestionsCounter.node.innerText = this._trans.__(
'%1/%2',
this._current + 1,
completions.items.length
);
this._providerWidget.node.title = this._trans.__(
'Provider: %1',
candidate.provider.name
);
const icon = candidate.provider.icon ?? kernelIcon;
icon.render(this._providerWidget.node);
}
private _setText(item: CompletionHandler.IInlineItem) {
const text = item.insertText;
const editor = this._editor;
const model = this._model;
if (!model || !editor) {
return;
}
const view = (editor as CodeMirrorEditor).editor;
this._ghostManager.placeGhost(view, {
from: editor.getOffsetAt(model.cursor),
content: text,
providerId: item.provider.identifier,
addedPart: item.lastStreamed,
streaming: item.streaming,
onPointerOver: this._onPointerOverGhost.bind(this),
onPointerLeave: this._onPointerLeaveGhost.bind(this)
});
editor.host.classList.add(INLINE_COMPLETER_ACTIVE_CLASS);
}
private _onPointerOverGhost() {
if (this._clearHoverTimeout !== null) {
window.clearTimeout(this._clearHoverTimeout);
this._clearHoverTimeout = null;
}
this.node.classList.add(HOVER_CLASS);
}
private _onPointerLeaveGhost() {
// Remove after a small delay to avoid flicker when moving cursor
// between the lines or around the edges of the ghost text.
this._clearHoverTimeout = window.setTimeout(
() => this.node.classList.remove(HOVER_CLASS),
500
);
}
private _setGeometry() {
const { node } = this;
const model = this._model;
const editor = this._editor;
if (!editor || !model || !model.cursor) {
return;
}
const host =
(editor.host.closest('.jp-MainAreaWidget > .lm-Widget') as HTMLElement) ||
editor.host;
let anchor: DOMRect;
try {
anchor = editor.getCoordinateForPosition(model.cursor) as DOMRect;
} catch {
// if coordinate is no longer in editor (e.g. after deleting a line), hide widget
this.hide();
return;
}
HoverBox.setGeometry({
anchor,
host: host,
maxHeight: 40,
minHeight: 20,
node: node,
privilege: 'forceAbove',
outOfViewDisplay: {
top: 'stick-outside',
bottom: 'stick-inside',
left: 'stick-inside',
right: 'stick-outside'
}
});
}
private _updateShortcutsVisibility() {
this.node.dataset.showShortcuts = this._showShortcuts + '';
}
private _updateDisplay() {
this.node.dataset.display = this._showWidget;
}
private _clearHoverTimeout: number | null = null;
private _current: number = 0;
private _editor: CodeEditor.IEditor | null | undefined = null;
private _ghostManager: GhostTextManager;
private _lastItem: CompletionHandler.IInlineItem | null = null;
private _model: InlineCompleter.IModel | null = null;
private _providerWidget = new Widget();
private _showShortcuts = InlineCompleter.defaultSettings.showShortcuts;
private _showWidget = InlineCompleter.defaultSettings.showWidget;
private _suggestionsCounter = new Widget();
private _trans: TranslationBundle;
private _toolbar = new Toolbar<Widget>();
private _progressBar: HTMLElement;
}
/**
* Map between old and new inline completion position in the list.
*/
export type IndexMap = Map<number, number>;
export interface ISuggestionsChangedArgs {
/**
* Whether completions were set (new query) or appended (for existing query)
*/
event: 'set' | 'append' | 'clear';
/**
* Map between old and new inline indices, only present for `set` event.
*/
indexMap?: IndexMap;
}
/**
* A namespace for inline completer statics.
*/
export namespace InlineCompleter {
/**
* The initialization options for inline completer widget.
*/
export interface IOptions extends IInlineCompleterFactory.IOptions {
/**
* JupyterLab translation bundle.
*/
trans: TranslationBundle;
}
/**
* Defaults for runtime user-configurable settings.
*/
export const defaultSettings: IInlineCompleterSettings = {
showWidget: 'onHover',
showShortcuts: true,
streamingAnimation: 'uncover',
providers: {}
};
/**
* Progress in generation of completion candidates by providers.
*/
export interface IProvisionProgress {
/**
* The number of providers to yet provide a reply.
* Excludes providers which resolved/rejected their promise, or timed out.
*/
pendingProviders: number;
/**
* The number of providers from which inline completions were requested.
*/
totalProviders: number;
}
/**
* Model for inline completions.
*/
export interface IModel extends IDisposable {
/**
* A signal emitted when new suggestions are set on the model.
*/
readonly suggestionsChanged: ISignal<IModel, ISuggestionsChangedArgs>;
/**
* A signal emitted when filter text is updated.
* Emits a mapping from old to new index for items after filtering.
*/
readonly filterTextChanged: ISignal<IModel, IndexMap>;
/**
* A signal emitted when new information about progress is available.
*/
readonly provisionProgress: ISignal<IModel, IProvisionProgress>;
/**
* Original placement of cursor.
*/
cursor: CodeEditor.IPosition;
/**
* Reset completer model.
*/
reset(): void;
/**
* Set completions clearing existing ones.
*/
setCompletions(
reply: IInlineCompletionList<CompletionHandler.IInlineItem>
): void;
/**
* Append completions while preserving new ones.
*/
appendCompletions(
reply: IInlineCompletionList<CompletionHandler.IInlineItem>
): void;
/**
* Notify model about progress in generation of completion candidates by providers.
*/
notifyProgress(providerProgress: IProvisionProgress): void;
/**
* Current inline completions.
*/
readonly completions: IInlineCompletionList<CompletionHandler.IInlineItem> | null;
/**
* Handle a source change.
*/
handleTextChange(change: SourceChange): void;
/**
* Handle cursor selection change.
*/
handleSelectionChange(range: CodeEditor.IRange): void;
}
/**
* Model for inline completions.
*/
export class Model implements InlineCompleter.IModel {
setCompletions(
reply: IInlineCompletionList<CompletionHandler.IInlineItem>
) {
const previousPositions: Map<string, number> = new Map(
this._completions?.items?.map((item, index) => [item.insertText, index])
);
this._completions = reply;
const indexMap = new Map(
reply.items.map((item, newIndex) => [
previousPositions.get(item.insertText)!,
newIndex
])
);
this.suggestionsChanged.emit({
event: 'set',
indexMap
});
}
appendCompletions(
reply: IInlineCompletionList<CompletionHandler.IInlineItem>
) {
if (!this._completions || !this._completions.items) {
console.warn('No completions to append to');
return;
}
this._completions.items.push(...reply.items);
this.suggestionsChanged.emit({ event: 'append' });
}
notifyProgress(progress: IProvisionProgress) {
this.provisionProgress.emit(progress);
}
get cursor(): CodeEditor.IPosition {
return this._cursor;
}
set cursor(value: CodeEditor.IPosition) {
this._cursor = value;
}
get completions() {
return this._completions;
}
reset() {
this._completions = null;
this.suggestionsChanged.emit({ event: 'clear' });
}
/**
* Get whether the model is disposed.
*/
get isDisposed(): boolean {
return this._isDisposed;
}
handleTextChange(sourceChange: SourceChange) {
const completions = this._completions;
if (
!completions ||
!completions.items ||
completions.items.length === 0
) {
return;
}
const originalPositions: Map<CompletionHandler.IInlineItem, number> =
new Map(completions.items.map((item, index) => [item, index]));
for (let change of sourceChange.sourceChange ?? []) {
const insert = change.insert;
if (insert) {
const items = completions.items.filter(item => {
const filterText = item.filterText ?? item.insertText;
if (!filterText.startsWith(insert)) {
return false;
}
item.filterText = filterText.substring(insert.length);
item.insertText = item.insertText.substring(insert.length);
return true;
});
if (items.length === 0) {
// all items from this provider were filtered out
this._completions = null;
}
completions.items = items;
} else {
if (!change.retain) {
this._completions = null;
}
}
}
const indexMap = new Map(
completions.items.map((item, newIndex) => [
originalPositions.get(item)!,
newIndex
])
);
this.filterTextChanged.emit(indexMap);
}
handleSelectionChange(range: CodeEditor.IRange) {
const initialCursor = this.cursor;
if (!initialCursor) {
return;
}
const { start, end } = range;
if (start.column !== end.column || start.line !== end.line) {
// Cancel if user started selecting text.
this.reset();
}
if (
start.line !== initialCursor.line ||
start.column < initialCursor.column
) {
// Cancel if user moved cursor to next line or receded to before the origin
this.reset();
}
}
/**
* Dispose of the resources held by the model.
*/
dispose(): void {
// Do nothing if already disposed.
if (this._isDisposed) {
return;
}
this._isDisposed = true;
Signal.clearData(this);
}
suggestionsChanged = new Signal<this, ISuggestionsChangedArgs>(this);
filterTextChanged = new Signal<this, IndexMap>(this);
provisionProgress = new Signal<this, IProvisionProgress>(this);
private _isDisposed = false;
private _completions: IInlineCompletionList<CompletionHandler.IInlineItem> | null =
null;
private _cursor: CodeEditor.IPosition;
}
}