Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/editor/contrib/suggest/browser/suggestWidget.ts
5334 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import * as dom from '../../../../base/browser/dom.js';
7
import { IKeyboardEvent } from '../../../../base/browser/keyboardEvent.js';
8
import '../../../../base/browser/ui/codicons/codiconStyles.js'; // The codicon symbol styles are defined here and must be loaded
9
import { IListEvent, IListGestureEvent, IListMouseEvent } from '../../../../base/browser/ui/list/list.js';
10
import { List } from '../../../../base/browser/ui/list/listWidget.js';
11
import { CancelablePromise, createCancelablePromise, disposableTimeout, TimeoutTimer } from '../../../../base/common/async.js';
12
import { onUnexpectedError } from '../../../../base/common/errors.js';
13
import { Emitter, Event, PauseableEmitter } from '../../../../base/common/event.js';
14
import { DisposableStore, IDisposable, MutableDisposable } from '../../../../base/common/lifecycle.js';
15
import { clamp } from '../../../../base/common/numbers.js';
16
import * as strings from '../../../../base/common/strings.js';
17
import './media/suggest.css';
18
import { ContentWidgetPositionPreference, ICodeEditor, IContentWidget, IContentWidgetPosition, IEditorMouseEvent } from '../../../browser/editorBrowser.js';
19
import { EmbeddedCodeEditorWidget } from '../../../browser/widget/codeEditor/embeddedCodeEditorWidget.js';
20
import { EditorOption } from '../../../common/config/editorOptions.js';
21
import { IPosition } from '../../../common/core/position.js';
22
import { SuggestWidgetStatus } from './suggestWidgetStatus.js';
23
import '../../symbolIcons/browser/symbolIcons.js'; // The codicon symbol colors are defined here and must be loaded to get colors
24
import * as nls from '../../../../nls.js';
25
import { IContextKey, IContextKeyService } from '../../../../platform/contextkey/common/contextkey.js';
26
import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';
27
import { IStorageService, StorageScope, StorageTarget } from '../../../../platform/storage/common/storage.js';
28
import { activeContrastBorder, editorForeground, editorWidgetBackground, editorWidgetBorder, listFocusHighlightForeground, listHighlightForeground, quickInputListFocusBackground, quickInputListFocusForeground, quickInputListFocusIconForeground, registerColor, transparent } from '../../../../platform/theme/common/colorRegistry.js';
29
import { IThemeService } from '../../../../platform/theme/common/themeService.js';
30
import { CompletionModel } from './completionModel.js';
31
import { ResizableHTMLElement } from '../../../../base/browser/ui/resizable/resizable.js';
32
import { CompletionItem, Context as SuggestContext, suggestWidgetStatusbarMenu } from './suggest.js';
33
import { canExpandCompletionItem, SuggestDetailsOverlay, SuggestDetailsWidget } from './suggestWidgetDetails.js';
34
import { ItemRenderer } from './suggestWidgetRenderer.js';
35
import { getListStyles } from '../../../../platform/theme/browser/defaultStyles.js';
36
import { status } from '../../../../base/browser/ui/aria/aria.js';
37
import { CompletionItemKinds } from '../../../common/languages.js';
38
import { isWindows } from '../../../../base/common/platform.js';
39
40
/**
41
* Suggest widget colors
42
*/
43
registerColor('editorSuggestWidget.background', editorWidgetBackground, nls.localize('editorSuggestWidgetBackground', 'Background color of the suggest widget.'));
44
registerColor('editorSuggestWidget.border', editorWidgetBorder, nls.localize('editorSuggestWidgetBorder', 'Border color of the suggest widget.'));
45
export const editorSuggestWidgetForeground = registerColor('editorSuggestWidget.foreground', editorForeground, nls.localize('editorSuggestWidgetForeground', 'Foreground color of the suggest widget.'));
46
registerColor('editorSuggestWidget.selectedForeground', quickInputListFocusForeground, nls.localize('editorSuggestWidgetSelectedForeground', 'Foreground color of the selected entry in the suggest widget.'));
47
registerColor('editorSuggestWidget.selectedIconForeground', quickInputListFocusIconForeground, nls.localize('editorSuggestWidgetSelectedIconForeground', 'Icon foreground color of the selected entry in the suggest widget.'));
48
export const editorSuggestWidgetSelectedBackground = registerColor('editorSuggestWidget.selectedBackground', quickInputListFocusBackground, nls.localize('editorSuggestWidgetSelectedBackground', 'Background color of the selected entry in the suggest widget.'));
49
registerColor('editorSuggestWidget.highlightForeground', listHighlightForeground, nls.localize('editorSuggestWidgetHighlightForeground', 'Color of the match highlights in the suggest widget.'));
50
registerColor('editorSuggestWidget.focusHighlightForeground', listFocusHighlightForeground, nls.localize('editorSuggestWidgetFocusHighlightForeground', 'Color of the match highlights in the suggest widget when an item is focused.'));
51
registerColor('editorSuggestWidgetStatus.foreground', transparent(editorSuggestWidgetForeground, .5), nls.localize('editorSuggestWidgetStatusForeground', 'Foreground color of the suggest widget status.'));
52
53
const enum State {
54
Hidden,
55
Loading,
56
Empty,
57
Open,
58
Frozen,
59
Details,
60
onDetailsKeyDown
61
}
62
63
export interface ISelectedSuggestion {
64
item: CompletionItem;
65
index: number;
66
model: CompletionModel;
67
}
68
69
class PersistedWidgetSize {
70
71
private readonly _key: string;
72
73
constructor(
74
private readonly _service: IStorageService,
75
editor: ICodeEditor
76
) {
77
this._key = `suggestWidget.size/${editor.getEditorType()}/${editor instanceof EmbeddedCodeEditorWidget}`;
78
}
79
80
restore(): dom.Dimension | undefined {
81
const raw = this._service.get(this._key, StorageScope.PROFILE) ?? '';
82
try {
83
const obj = JSON.parse(raw);
84
if (dom.Dimension.is(obj)) {
85
return dom.Dimension.lift(obj);
86
}
87
} catch {
88
// ignore
89
}
90
return undefined;
91
}
92
93
store(size: dom.Dimension) {
94
this._service.store(this._key, JSON.stringify(size), StorageScope.PROFILE, StorageTarget.MACHINE);
95
}
96
97
reset(): void {
98
this._service.remove(this._key, StorageScope.PROFILE);
99
}
100
}
101
102
export class SuggestWidget implements IDisposable {
103
104
private static LOADING_MESSAGE: string = nls.localize('suggestWidget.loading', "Loading...");
105
private static NO_SUGGESTIONS_MESSAGE: string = nls.localize('suggestWidget.noSuggestions', "No suggestions.");
106
107
private _state: State = State.Hidden;
108
private _isAuto: boolean = false;
109
private _loadingTimeout?: IDisposable;
110
private readonly _pendingLayout = new MutableDisposable();
111
private readonly _pendingShowDetails = new MutableDisposable();
112
private _currentSuggestionDetails?: CancelablePromise<void>;
113
private _focusedItem?: CompletionItem;
114
private _ignoreFocusEvents: boolean = false;
115
private _completionModel?: CompletionModel;
116
private _cappedHeight?: { wanted: number; capped: number };
117
private _forceRenderingAbove: boolean = false;
118
private _explainMode: boolean = false;
119
120
readonly element: ResizableHTMLElement;
121
private readonly _messageElement: HTMLElement;
122
private readonly _listElement: HTMLElement;
123
private readonly _list: List<CompletionItem>;
124
private readonly _status: SuggestWidgetStatus;
125
private readonly _details: SuggestDetailsOverlay;
126
private readonly _contentWidget: SuggestContentWidget;
127
private readonly _persistedSize: PersistedWidgetSize;
128
129
private readonly _ctxSuggestWidgetVisible: IContextKey<boolean>;
130
private readonly _ctxSuggestWidgetDetailsVisible: IContextKey<boolean>;
131
private readonly _ctxSuggestWidgetMultipleSuggestions: IContextKey<boolean>;
132
private readonly _ctxSuggestWidgetHasFocusedSuggestion: IContextKey<boolean>;
133
private readonly _ctxSuggestWidgetDetailsFocused: IContextKey<boolean>;
134
135
private readonly _showTimeout = new TimeoutTimer();
136
private readonly _disposables = new DisposableStore();
137
138
139
private readonly _onDidSelect = new PauseableEmitter<ISelectedSuggestion>();
140
private readonly _onDidFocus = new PauseableEmitter<ISelectedSuggestion>();
141
private readonly _onDidHide = new Emitter<this>();
142
private readonly _onDidShow = new Emitter<this>();
143
144
readonly onDidSelect: Event<ISelectedSuggestion> = this._onDidSelect.event;
145
readonly onDidFocus: Event<ISelectedSuggestion> = this._onDidFocus.event;
146
readonly onDidHide: Event<this> = this._onDidHide.event;
147
readonly onDidShow: Event<this> = this._onDidShow.event;
148
149
private readonly _onDetailsKeydown = new Emitter<IKeyboardEvent>();
150
readonly onDetailsKeyDown: Event<IKeyboardEvent> = this._onDetailsKeydown.event;
151
152
constructor(
153
private readonly editor: ICodeEditor,
154
@IStorageService private readonly _storageService: IStorageService,
155
@IContextKeyService _contextKeyService: IContextKeyService,
156
@IThemeService _themeService: IThemeService,
157
@IInstantiationService instantiationService: IInstantiationService,
158
) {
159
this.element = new ResizableHTMLElement();
160
this.element.domNode.classList.add('editor-widget', 'suggest-widget');
161
162
this._contentWidget = new SuggestContentWidget(this, editor);
163
this._persistedSize = new PersistedWidgetSize(_storageService, editor);
164
165
class ResizeState {
166
constructor(
167
readonly persistedSize: dom.Dimension | undefined,
168
readonly currentSize: dom.Dimension,
169
public persistHeight = false,
170
public persistWidth = false,
171
) { }
172
}
173
174
let state: ResizeState | undefined;
175
this._disposables.add(this.element.onDidWillResize(() => {
176
this._contentWidget.lockPreference();
177
state = new ResizeState(this._persistedSize.restore(), this.element.size);
178
}));
179
this._disposables.add(this.element.onDidResize(e => {
180
181
this._resize(e.dimension.width, e.dimension.height);
182
183
if (state) {
184
state.persistHeight = state.persistHeight || !!e.north || !!e.south;
185
state.persistWidth = state.persistWidth || !!e.east || !!e.west;
186
}
187
188
if (!e.done) {
189
return;
190
}
191
192
if (state) {
193
// only store width or height value that have changed and also
194
// only store changes that are above a certain threshold
195
const { itemHeight, defaultSize } = this.getLayoutInfo();
196
const threshold = Math.round(itemHeight / 2);
197
let { width, height } = this.element.size;
198
if (!state.persistHeight || Math.abs(state.currentSize.height - height) <= threshold) {
199
height = state.persistedSize?.height ?? defaultSize.height;
200
}
201
if (!state.persistWidth || Math.abs(state.currentSize.width - width) <= threshold) {
202
width = state.persistedSize?.width ?? defaultSize.width;
203
}
204
this._persistedSize.store(new dom.Dimension(width, height));
205
}
206
207
// reset working state
208
this._contentWidget.unlockPreference();
209
state = undefined;
210
}));
211
212
this._messageElement = dom.append(this.element.domNode, dom.$('.message'));
213
this._listElement = dom.append(this.element.domNode, dom.$('.tree'));
214
215
const details = this._disposables.add(instantiationService.createInstance(SuggestDetailsWidget, this.editor));
216
details.onDidClose(() => this.toggleDetails(), this, this._disposables);
217
this._details = new SuggestDetailsOverlay(details, this.editor);
218
219
const applyIconStyle = () => this.element.domNode.classList.toggle('no-icons', !this.editor.getOption(EditorOption.suggest).showIcons);
220
applyIconStyle();
221
222
const renderer = instantiationService.createInstance(ItemRenderer, this.editor);
223
this._disposables.add(renderer);
224
this._disposables.add(renderer.onDidToggleDetails(() => this.toggleDetails()));
225
226
this._list = new List('SuggestWidget', this._listElement, {
227
getHeight: (_element: CompletionItem): number => this.getLayoutInfo().itemHeight,
228
getTemplateId: (_element: CompletionItem): string => 'suggestion'
229
}, [renderer], {
230
alwaysConsumeMouseWheel: true,
231
useShadows: false,
232
mouseSupport: false,
233
multipleSelectionSupport: false,
234
accessibilityProvider: {
235
getRole: () => isWindows ? 'listitem' : 'option',
236
getWidgetAriaLabel: () => nls.localize('suggest', "Suggest"),
237
getWidgetRole: () => 'listbox',
238
getAriaLabel: (item: CompletionItem) => {
239
240
let label = item.textLabel;
241
const kindLabel = CompletionItemKinds.toLabel(item.completion.kind);
242
if (typeof item.completion.label !== 'string') {
243
const { detail, description } = item.completion.label;
244
if (detail && description) {
245
label = nls.localize('label.full', '{0} {1}, {2}, {3}', label, detail, description, kindLabel);
246
} else if (detail) {
247
label = nls.localize('label.detail', '{0} {1}, {2}', label, detail, kindLabel);
248
} else if (description) {
249
label = nls.localize('label.desc', '{0}, {1}, {2}', label, description, kindLabel);
250
}
251
} else {
252
label = nls.localize('label', '{0}, {1}', label, kindLabel);
253
}
254
if (!item.isResolved || !this._isDetailsVisible()) {
255
return label;
256
}
257
258
const { documentation, detail } = item.completion;
259
const docs = strings.format(
260
'{0}{1}',
261
detail || '',
262
documentation ? (typeof documentation === 'string' ? documentation : documentation.value) : '');
263
264
return nls.localize('ariaCurrenttSuggestionReadDetails', "{0}, docs: {1}", label, docs);
265
},
266
}
267
});
268
this._list.style(getListStyles({
269
listInactiveFocusBackground: editorSuggestWidgetSelectedBackground,
270
listInactiveFocusOutline: activeContrastBorder
271
}));
272
273
this._status = instantiationService.createInstance(SuggestWidgetStatus, this.element.domNode, suggestWidgetStatusbarMenu, undefined);
274
const applyStatusBarStyle = () => this.element.domNode.classList.toggle('with-status-bar', this.editor.getOption(EditorOption.suggest).showStatusBar);
275
applyStatusBarStyle();
276
277
this._disposables.add(this._list.onMouseDown(e => this._onListMouseDownOrTap(e)));
278
this._disposables.add(this._list.onTap(e => this._onListMouseDownOrTap(e)));
279
this._disposables.add(this._list.onDidChangeSelection(e => this._onListSelection(e)));
280
this._disposables.add(this._list.onDidChangeFocus(e => this._onListFocus(e)));
281
this._disposables.add(this.editor.onDidChangeCursorSelection(() => this._onCursorSelectionChanged()));
282
this._disposables.add(this.editor.onDidChangeConfiguration(e => {
283
if (e.hasChanged(EditorOption.suggest)) {
284
applyStatusBarStyle();
285
applyIconStyle();
286
}
287
if (this._completionModel && (e.hasChanged(EditorOption.fontInfo) || e.hasChanged(EditorOption.suggestFontSize) || e.hasChanged(EditorOption.suggestLineHeight))) {
288
this._list.splice(0, this._list.length, this._completionModel.items);
289
}
290
}));
291
292
this._ctxSuggestWidgetVisible = SuggestContext.Visible.bindTo(_contextKeyService);
293
this._ctxSuggestWidgetDetailsVisible = SuggestContext.DetailsVisible.bindTo(_contextKeyService);
294
this._ctxSuggestWidgetMultipleSuggestions = SuggestContext.MultipleSuggestions.bindTo(_contextKeyService);
295
this._ctxSuggestWidgetHasFocusedSuggestion = SuggestContext.HasFocusedSuggestion.bindTo(_contextKeyService);
296
this._ctxSuggestWidgetDetailsFocused = SuggestContext.DetailsFocused.bindTo(_contextKeyService);
297
298
const detailsFocusTracker = dom.trackFocus(this._details.widget.domNode);
299
this._disposables.add(detailsFocusTracker);
300
this._disposables.add(detailsFocusTracker.onDidFocus(() => this._ctxSuggestWidgetDetailsFocused.set(true)));
301
this._disposables.add(detailsFocusTracker.onDidBlur(() => this._ctxSuggestWidgetDetailsFocused.set(false)));
302
303
this._disposables.add(dom.addStandardDisposableListener(this._details.widget.domNode, 'keydown', e => {
304
this._onDetailsKeydown.fire(e);
305
}));
306
307
this._disposables.add(this.editor.onMouseDown((e: IEditorMouseEvent) => this._onEditorMouseDown(e)));
308
}
309
310
dispose(): void {
311
this._details.widget.dispose();
312
this._details.dispose();
313
this._list.dispose();
314
this._status.dispose();
315
this._disposables.dispose();
316
this._loadingTimeout?.dispose();
317
this._pendingLayout.dispose();
318
this._pendingShowDetails.dispose();
319
this._showTimeout.dispose();
320
this._contentWidget.dispose();
321
this.element.dispose();
322
this._onDidSelect.dispose();
323
this._onDidFocus.dispose();
324
this._onDidHide.dispose();
325
this._onDidShow.dispose();
326
this._onDetailsKeydown.dispose();
327
}
328
329
private _onEditorMouseDown(mouseEvent: IEditorMouseEvent): void {
330
if (this._details.widget.domNode.contains(mouseEvent.target.element)) {
331
// Clicking inside details
332
this._details.widget.domNode.focus();
333
} else {
334
// Clicking outside details and inside suggest
335
if (this.element.domNode.contains(mouseEvent.target.element)) {
336
this.editor.focus();
337
}
338
}
339
}
340
341
private _onCursorSelectionChanged(): void {
342
if (this._state !== State.Hidden) {
343
this._contentWidget.layout();
344
}
345
}
346
347
private _onListMouseDownOrTap(e: IListMouseEvent<CompletionItem> | IListGestureEvent<CompletionItem>): void {
348
if (typeof e.element === 'undefined' || typeof e.index === 'undefined') {
349
return;
350
}
351
352
// prevent stealing browser focus from the editor
353
e.browserEvent.preventDefault();
354
e.browserEvent.stopPropagation();
355
356
this._select(e.element, e.index);
357
}
358
359
private _onListSelection(e: IListEvent<CompletionItem>): void {
360
if (e.elements.length) {
361
this._select(e.elements[0], e.indexes[0]);
362
}
363
}
364
365
private _select(item: CompletionItem, index: number): void {
366
const completionModel = this._completionModel;
367
if (completionModel) {
368
this._onDidSelect.fire({ item, index, model: completionModel });
369
this.editor.focus();
370
}
371
}
372
373
private _onListFocus(e: IListEvent<CompletionItem>): void {
374
if (this._ignoreFocusEvents) {
375
return;
376
}
377
378
if (this._state === State.Details) {
379
// This can happen when focus is in the details-panel and when
380
// arrow keys are pressed to select next/prev items
381
this._setState(State.Open);
382
}
383
384
if (!e.elements.length) {
385
if (this._currentSuggestionDetails) {
386
this._currentSuggestionDetails.cancel();
387
this._currentSuggestionDetails = undefined;
388
this._focusedItem = undefined;
389
}
390
391
this.editor.setAriaOptions({ activeDescendant: undefined });
392
this._ctxSuggestWidgetHasFocusedSuggestion.set(false);
393
return;
394
}
395
396
if (!this._completionModel) {
397
return;
398
}
399
400
this._ctxSuggestWidgetHasFocusedSuggestion.set(true);
401
const item = e.elements[0];
402
const index = e.indexes[0];
403
404
if (item !== this._focusedItem) {
405
406
this._currentSuggestionDetails?.cancel();
407
this._currentSuggestionDetails = undefined;
408
409
this._focusedItem = item;
410
411
this._list.reveal(index);
412
413
this._currentSuggestionDetails = createCancelablePromise(async token => {
414
const loading = disposableTimeout(() => {
415
if (this._isDetailsVisible()) {
416
this._showDetails(true, false);
417
}
418
}, 250);
419
const sub = token.onCancellationRequested(() => loading.dispose());
420
try {
421
return await item.resolve(token);
422
} finally {
423
loading.dispose();
424
sub.dispose();
425
}
426
});
427
428
this._currentSuggestionDetails.then(() => {
429
if (index >= this._list.length || item !== this._list.element(index)) {
430
return;
431
}
432
433
// item can have extra information, so re-render
434
this._ignoreFocusEvents = true;
435
this._list.splice(index, 1, [item]);
436
this._list.setFocus([index]);
437
this._ignoreFocusEvents = false;
438
439
if (this._isDetailsVisible()) {
440
this._showDetails(false, false);
441
} else {
442
this.element.domNode.classList.remove('docs-side');
443
}
444
445
this.editor.setAriaOptions({ activeDescendant: this._list.getElementID(index) });
446
}).catch(onUnexpectedError);
447
}
448
449
// emit an event
450
this._onDidFocus.fire({ item, index, model: this._completionModel });
451
}
452
453
private _setState(state: State): void {
454
455
if (this._state === state) {
456
return;
457
}
458
this._state = state;
459
460
this.element.domNode.classList.toggle('frozen', state === State.Frozen);
461
this.element.domNode.classList.remove('message');
462
463
switch (state) {
464
case State.Hidden:
465
dom.hide(this._messageElement, this._listElement, this._status.element);
466
this._details.hide(true);
467
this._status.hide();
468
this._contentWidget.hide();
469
this._ctxSuggestWidgetVisible.reset();
470
this._ctxSuggestWidgetMultipleSuggestions.reset();
471
this._ctxSuggestWidgetHasFocusedSuggestion.reset();
472
this._showTimeout.cancel();
473
this.element.domNode.classList.remove('visible');
474
this._list.splice(0, this._list.length);
475
this._focusedItem = undefined;
476
this._cappedHeight = undefined;
477
this._explainMode = false;
478
break;
479
case State.Loading:
480
this.element.domNode.classList.add('message');
481
this._messageElement.textContent = SuggestWidget.LOADING_MESSAGE;
482
dom.hide(this._listElement, this._status.element);
483
dom.show(this._messageElement);
484
this._details.hide();
485
this._show();
486
this._focusedItem = undefined;
487
status(SuggestWidget.LOADING_MESSAGE);
488
break;
489
case State.Empty:
490
this.element.domNode.classList.add('message');
491
this._messageElement.textContent = SuggestWidget.NO_SUGGESTIONS_MESSAGE;
492
dom.hide(this._listElement, this._status.element);
493
dom.show(this._messageElement);
494
this._details.hide();
495
this._show();
496
this._focusedItem = undefined;
497
status(SuggestWidget.NO_SUGGESTIONS_MESSAGE);
498
break;
499
case State.Open:
500
dom.hide(this._messageElement);
501
dom.show(this._listElement, this._status.element);
502
this._show();
503
break;
504
case State.Frozen:
505
dom.hide(this._messageElement);
506
dom.show(this._listElement, this._status.element);
507
this._show();
508
break;
509
case State.Details:
510
dom.hide(this._messageElement);
511
dom.show(this._listElement, this._status.element);
512
this._details.show();
513
this._show();
514
this._details.widget.focus();
515
break;
516
}
517
}
518
519
private _show(): void {
520
this._status.show();
521
this._contentWidget.show();
522
this._layout(this._persistedSize.restore());
523
this._ctxSuggestWidgetVisible.set(true);
524
525
this._showTimeout.cancelAndSet(() => {
526
this.element.domNode.classList.add('visible');
527
this._onDidShow.fire(this);
528
}, 100);
529
}
530
531
showTriggered(auto: boolean, delay: number) {
532
if (this._state !== State.Hidden) {
533
return;
534
}
535
this._contentWidget.setPosition(this.editor.getPosition());
536
this._isAuto = !!auto;
537
538
if (!this._isAuto) {
539
this._loadingTimeout = disposableTimeout(() => this._setState(State.Loading), delay);
540
}
541
}
542
543
showSuggestions(completionModel: CompletionModel, selectionIndex: number, isFrozen: boolean, isAuto: boolean, noFocus: boolean): void {
544
545
this._contentWidget.setPosition(this.editor.getPosition());
546
this._loadingTimeout?.dispose();
547
548
this._currentSuggestionDetails?.cancel();
549
this._currentSuggestionDetails = undefined;
550
551
if (this._completionModel !== completionModel) {
552
this._completionModel = completionModel;
553
}
554
555
if (isFrozen && this._state !== State.Empty && this._state !== State.Hidden) {
556
this._setState(State.Frozen);
557
return;
558
}
559
560
const visibleCount = this._completionModel.items.length;
561
const isEmpty = visibleCount === 0;
562
this._ctxSuggestWidgetMultipleSuggestions.set(visibleCount > 1);
563
564
if (isEmpty) {
565
this._setState(isAuto ? State.Hidden : State.Empty);
566
this._completionModel = undefined;
567
return;
568
}
569
570
this._focusedItem = undefined;
571
572
// calling list.splice triggers focus event which this widget forwards. That can lead to
573
// suggestions being cancelled and the widget being cleared (and hidden). All this happens
574
// before revealing and focusing is done which means revealing and focusing will fail when
575
// they get run.
576
this._onDidFocus.pause();
577
this._onDidSelect.pause();
578
try {
579
this._list.splice(0, this._list.length, this._completionModel.items);
580
this._setState(isFrozen ? State.Frozen : State.Open);
581
this._list.reveal(selectionIndex, 0, selectionIndex === 0 ? 0 : this.getLayoutInfo().itemHeight * 0.33);
582
this._list.setFocus(noFocus ? [] : [selectionIndex]);
583
} finally {
584
this._onDidFocus.resume();
585
this._onDidSelect.resume();
586
}
587
588
this._pendingLayout.value = dom.runAtThisOrScheduleAtNextAnimationFrame(dom.getWindow(this.element.domNode), () => {
589
this._pendingLayout.clear();
590
this._layout(this.element.size);
591
// Reset focus border
592
this._details.widget.domNode.classList.remove('focused');
593
});
594
}
595
596
focusSelected(): void {
597
if (this._list.length > 0) {
598
this._list.setFocus([0]);
599
}
600
}
601
602
selectNextPage(): boolean {
603
switch (this._state) {
604
case State.Hidden:
605
return false;
606
case State.Details:
607
this._details.widget.pageDown();
608
return true;
609
case State.Loading:
610
return !this._isAuto;
611
default:
612
this._list.focusNextPage();
613
return true;
614
}
615
}
616
617
selectNext(): boolean {
618
switch (this._state) {
619
case State.Hidden:
620
return false;
621
case State.Loading:
622
return !this._isAuto;
623
default:
624
this._list.focusNext(1, true);
625
return true;
626
}
627
}
628
629
selectLast(): boolean {
630
switch (this._state) {
631
case State.Hidden:
632
return false;
633
case State.Details:
634
this._details.widget.scrollBottom();
635
return true;
636
case State.Loading:
637
return !this._isAuto;
638
default:
639
this._list.focusLast();
640
return true;
641
}
642
}
643
644
selectPreviousPage(): boolean {
645
switch (this._state) {
646
case State.Hidden:
647
return false;
648
case State.Details:
649
this._details.widget.pageUp();
650
return true;
651
case State.Loading:
652
return !this._isAuto;
653
default:
654
this._list.focusPreviousPage();
655
return true;
656
}
657
}
658
659
selectPrevious(): boolean {
660
switch (this._state) {
661
case State.Hidden:
662
return false;
663
case State.Loading:
664
return !this._isAuto;
665
default:
666
this._list.focusPrevious(1, true);
667
return false;
668
}
669
}
670
671
selectFirst(): boolean {
672
switch (this._state) {
673
case State.Hidden:
674
return false;
675
case State.Details:
676
this._details.widget.scrollTop();
677
return true;
678
case State.Loading:
679
return !this._isAuto;
680
default:
681
this._list.focusFirst();
682
return true;
683
}
684
}
685
686
getFocusedItem(): ISelectedSuggestion | undefined {
687
if (this._state !== State.Hidden
688
&& this._state !== State.Empty
689
&& this._state !== State.Loading
690
&& this._completionModel
691
&& this._list.getFocus().length > 0
692
) {
693
694
return {
695
item: this._list.getFocusedElements()[0],
696
index: this._list.getFocus()[0],
697
model: this._completionModel
698
};
699
}
700
return undefined;
701
}
702
703
toggleDetailsFocus(): void {
704
if (this._state === State.Details) {
705
// Should return the focus to the list item.
706
this._list.setFocus(this._list.getFocus());
707
this._setState(State.Open);
708
} else if (this._state === State.Open) {
709
this._setState(State.Details);
710
if (!this._isDetailsVisible()) {
711
this.toggleDetails(true);
712
} else {
713
this._details.widget.focus();
714
}
715
}
716
}
717
718
toggleDetails(focused: boolean = false): void {
719
if (this._isDetailsVisible()) {
720
// hide details widget
721
this._pendingShowDetails.clear();
722
this._ctxSuggestWidgetDetailsVisible.set(false);
723
this._setDetailsVisible(false);
724
this._details.hide();
725
this.element.domNode.classList.remove('shows-details');
726
727
} else if ((canExpandCompletionItem(this._list.getFocusedElements()[0]) || this._explainMode) && (this._state === State.Open || this._state === State.Details || this._state === State.Frozen)) {
728
// show details widget (iff possible)
729
this._ctxSuggestWidgetDetailsVisible.set(true);
730
this._setDetailsVisible(true);
731
this._showDetails(false, focused);
732
}
733
}
734
735
private _showDetails(loading: boolean, focused: boolean): void {
736
this._pendingShowDetails.value = dom.runAtThisOrScheduleAtNextAnimationFrame(dom.getWindow(this.element.domNode), () => {
737
this._pendingShowDetails.clear();
738
this._details.show();
739
let didFocusDetails = false;
740
if (loading) {
741
this._details.widget.renderLoading();
742
} else {
743
this._details.widget.renderItem(this._list.getFocusedElements()[0], this._explainMode);
744
}
745
if (!this._details.widget.isEmpty) {
746
this._positionDetails();
747
this.element.domNode.classList.add('shows-details');
748
if (focused) {
749
this._details.widget.focus();
750
didFocusDetails = true;
751
}
752
} else {
753
this._details.hide();
754
}
755
if (!didFocusDetails) {
756
this.editor.focus();
757
}
758
});
759
}
760
761
toggleExplainMode(): void {
762
if (this._list.getFocusedElements()[0]) {
763
this._explainMode = !this._explainMode;
764
if (!this._isDetailsVisible()) {
765
this.toggleDetails();
766
} else {
767
this._showDetails(false, false);
768
}
769
}
770
}
771
772
resetPersistedSize(): void {
773
this._persistedSize.reset();
774
}
775
776
hideWidget(): void {
777
this._pendingLayout.clear();
778
this._pendingShowDetails.clear();
779
this._loadingTimeout?.dispose();
780
781
this._setState(State.Hidden);
782
this._onDidHide.fire(this);
783
this.element.clearSashHoverState();
784
785
// ensure that a reasonable widget height is persisted so that
786
// accidential "resize-to-single-items" cases aren't happening
787
const dim = this._persistedSize.restore();
788
const minPersistedHeight = Math.ceil(this.getLayoutInfo().itemHeight * 4.3);
789
if (dim && dim.height < minPersistedHeight) {
790
this._persistedSize.store(dim.with(undefined, minPersistedHeight));
791
}
792
}
793
794
isFrozen(): boolean {
795
return this._state === State.Frozen;
796
}
797
798
_afterRender(position: ContentWidgetPositionPreference | null) {
799
if (position === null) {
800
if (this._isDetailsVisible()) {
801
this._details.hide(); //todo@jrieken soft-hide
802
}
803
return;
804
}
805
if (this._state === State.Empty || this._state === State.Loading) {
806
// no special positioning when widget isn't showing list
807
return;
808
}
809
if (this._isDetailsVisible() && !this._details.widget.isEmpty) {
810
this._details.show();
811
}
812
this._positionDetails();
813
}
814
815
private _layout(size: dom.Dimension | undefined): void {
816
if (!this.editor.hasModel()) {
817
return;
818
}
819
if (!this.editor.getDomNode()) {
820
// happens when running tests
821
return;
822
}
823
824
const bodyBox = dom.getClientArea(this.element.domNode.ownerDocument.body);
825
const info = this.getLayoutInfo();
826
827
if (!size) {
828
size = info.defaultSize;
829
}
830
831
let height = size.height;
832
let width = size.width;
833
834
// status bar
835
this._status.element.style.height = `${info.itemHeight}px`;
836
837
if (this._state === State.Empty || this._state === State.Loading) {
838
// showing a message only
839
height = info.itemHeight + info.borderHeight;
840
width = info.defaultSize.width / 2;
841
this.element.enableSashes(false, false, false, false);
842
this.element.minSize = this.element.maxSize = new dom.Dimension(width, height);
843
this._contentWidget.setPreference(ContentWidgetPositionPreference.BELOW);
844
845
} else {
846
// showing items
847
848
// width math
849
const maxWidth = bodyBox.width - info.borderHeight - 2 * info.horizontalPadding;
850
if (width > maxWidth) {
851
width = maxWidth;
852
}
853
const preferredWidth = this._completionModel ? this._completionModel.stats.pLabelLen * info.typicalHalfwidthCharacterWidth : width;
854
855
// height math
856
const fullHeight = info.statusBarHeight + this._list.contentHeight + info.borderHeight;
857
const minHeight = info.itemHeight + info.statusBarHeight;
858
const editorBox = dom.getDomNodePagePosition(this.editor.getDomNode());
859
const cursorBox = this.editor.getScrolledVisiblePosition(this.editor.getPosition());
860
const cursorBottom = editorBox.top + cursorBox.top + cursorBox.height;
861
const maxHeightBelow = Math.min(bodyBox.height - cursorBottom - info.verticalPadding, fullHeight);
862
const availableSpaceAbove = editorBox.top + cursorBox.top - info.verticalPadding;
863
const maxHeightAbove = Math.min(availableSpaceAbove, fullHeight);
864
let maxHeight = Math.min(Math.max(maxHeightAbove, maxHeightBelow) + info.borderHeight, fullHeight);
865
866
if (height === this._cappedHeight?.capped) {
867
// Restore the old (wanted) height when the current
868
// height is capped to fit
869
height = this._cappedHeight.wanted;
870
}
871
872
if (height < minHeight) {
873
height = minHeight;
874
}
875
if (height > maxHeight) {
876
height = maxHeight;
877
}
878
879
const forceRenderingAboveRequiredSpace = 150;
880
if ((height > maxHeightBelow && maxHeightAbove > maxHeightBelow) || (this._forceRenderingAbove && availableSpaceAbove > forceRenderingAboveRequiredSpace)) {
881
this._contentWidget.setPreference(ContentWidgetPositionPreference.ABOVE);
882
this.element.enableSashes(true, true, false, false);
883
maxHeight = maxHeightAbove;
884
} else {
885
this._contentWidget.setPreference(ContentWidgetPositionPreference.BELOW);
886
this.element.enableSashes(false, true, true, false);
887
maxHeight = maxHeightBelow;
888
}
889
this.element.preferredSize = new dom.Dimension(preferredWidth, info.defaultSize.height);
890
this.element.maxSize = new dom.Dimension(maxWidth, maxHeight);
891
this.element.minSize = new dom.Dimension(220, minHeight);
892
893
// Know when the height was capped to fit and remember
894
// the wanted height for later. This is required when going
895
// left to widen suggestions.
896
this._cappedHeight = height === fullHeight
897
? { wanted: this._cappedHeight?.wanted ?? size.height, capped: height }
898
: undefined;
899
}
900
this._resize(width, height);
901
}
902
903
private _resize(width: number, height: number): void {
904
905
const { width: maxWidth, height: maxHeight } = this.element.maxSize;
906
width = Math.min(maxWidth, width);
907
height = Math.min(maxHeight, height);
908
909
const { statusBarHeight } = this.getLayoutInfo();
910
this._list.layout(height - statusBarHeight, width);
911
this._listElement.style.height = `${height - statusBarHeight}px`;
912
this.element.layout(height, width);
913
this._contentWidget.layout();
914
915
this._positionDetails();
916
}
917
918
private _positionDetails(): void {
919
if (this._isDetailsVisible()) {
920
this._details.placeAtAnchor(this.element.domNode, this._contentWidget.getPosition()?.preference[0] === ContentWidgetPositionPreference.BELOW);
921
}
922
}
923
924
getLayoutInfo() {
925
const fontInfo = this.editor.getOption(EditorOption.fontInfo);
926
const itemHeight = clamp(this.editor.getOption(EditorOption.suggestLineHeight) || fontInfo.lineHeight, 8, 1000);
927
const statusBarHeight = !this.editor.getOption(EditorOption.suggest).showStatusBar || this._state === State.Empty || this._state === State.Loading ? 0 : itemHeight;
928
const borderWidth = this._details.widget.getLayoutInfo().borderWidth;
929
const borderHeight = 2 * borderWidth;
930
931
return {
932
itemHeight,
933
statusBarHeight,
934
borderWidth,
935
borderHeight,
936
typicalHalfwidthCharacterWidth: fontInfo.typicalHalfwidthCharacterWidth,
937
verticalPadding: 22,
938
horizontalPadding: 14,
939
defaultSize: new dom.Dimension(430, statusBarHeight + 12 * itemHeight)
940
};
941
}
942
943
private _isDetailsVisible(): boolean {
944
return this._storageService.getBoolean('expandSuggestionDocs', StorageScope.PROFILE, false);
945
}
946
947
private _setDetailsVisible(value: boolean) {
948
this._storageService.store('expandSuggestionDocs', value, StorageScope.PROFILE, StorageTarget.USER);
949
}
950
951
forceRenderingAbove() {
952
if (!this._forceRenderingAbove) {
953
this._forceRenderingAbove = true;
954
this._layout(this._persistedSize.restore());
955
}
956
}
957
958
stopForceRenderingAbove() {
959
this._forceRenderingAbove = false;
960
}
961
}
962
963
export class SuggestContentWidget implements IContentWidget {
964
965
readonly allowEditorOverflow = true;
966
readonly suppressMouseDown = false;
967
968
private _position?: IPosition | null;
969
private _preference?: ContentWidgetPositionPreference;
970
private _preferenceLocked = false;
971
972
private _added: boolean = false;
973
private _hidden: boolean = false;
974
975
constructor(
976
private readonly _widget: SuggestWidget,
977
private readonly _editor: ICodeEditor
978
) { }
979
980
dispose(): void {
981
if (this._added) {
982
this._added = false;
983
this._editor.removeContentWidget(this);
984
}
985
}
986
987
getId(): string {
988
return 'editor.widget.suggestWidget';
989
}
990
991
getDomNode(): HTMLElement {
992
return this._widget.element.domNode;
993
}
994
995
show(): void {
996
this._hidden = false;
997
if (!this._added) {
998
this._added = true;
999
this._editor.addContentWidget(this);
1000
}
1001
}
1002
1003
hide(): void {
1004
if (!this._hidden) {
1005
this._hidden = true;
1006
this.layout();
1007
}
1008
}
1009
1010
layout(): void {
1011
this._editor.layoutContentWidget(this);
1012
}
1013
1014
getPosition(): IContentWidgetPosition | null {
1015
if (this._hidden || !this._position || !this._preference) {
1016
return null;
1017
}
1018
return {
1019
position: this._position,
1020
preference: [this._preference]
1021
};
1022
}
1023
1024
beforeRender() {
1025
const { height, width } = this._widget.element.size;
1026
const { borderWidth, horizontalPadding } = this._widget.getLayoutInfo();
1027
return new dom.Dimension(width + 2 * borderWidth + horizontalPadding, height + 2 * borderWidth);
1028
}
1029
1030
afterRender(position: ContentWidgetPositionPreference | null) {
1031
this._widget._afterRender(position);
1032
}
1033
1034
setPreference(preference: ContentWidgetPositionPreference) {
1035
if (!this._preferenceLocked) {
1036
this._preference = preference;
1037
}
1038
}
1039
1040
lockPreference() {
1041
this._preferenceLocked = true;
1042
}
1043
1044
unlockPreference() {
1045
this._preferenceLocked = false;
1046
}
1047
1048
setPosition(position: IPosition | null): void {
1049
this._position = position;
1050
}
1051
}
1052
1053