suggestWidget.ts 35.5 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

6
import 'vs/css!./media/suggest';
J
Johannes Rieken 已提交
7
import * as nls from 'vs/nls';
J
Johannes Rieken 已提交
8
import { createMatches } from 'vs/base/common/filters';
9
import * as strings from 'vs/base/common/strings';
M
Matt Bierner 已提交
10
import { Event, Emitter, chain } from 'vs/base/common/event';
11
import { onUnexpectedError } from 'vs/base/common/errors';
J
Joao Moreno 已提交
12
import { IDisposable, dispose, toDisposable } from 'vs/base/common/lifecycle';
13
import { addClass, append, $, hide, removeClass, show, toggleClass, getDomNodePagePosition, hasClass } from 'vs/base/browser/dom';
J
Joao Moreno 已提交
14
import { IListVirtualDelegate, IListEvent, IListRenderer } from 'vs/base/browser/ui/list/list';
J
Joao Moreno 已提交
15 16
import { List } from 'vs/base/browser/ui/list/listWidget';
import { DomScrollableElement } from 'vs/base/browser/ui/scrollbar/scrollableElement';
J
Johannes Rieken 已提交
17 18
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
19
import { IConfigurationChangedEvent } from 'vs/editor/common/config/editorOptions';
J
Joao Moreno 已提交
20
import { ContentWidgetPositionPreference, ICodeEditor, IContentWidget, IContentWidgetPosition } from 'vs/editor/browser/editorBrowser';
21 22
import { Context as SuggestContext } from './suggest';
import { ICompletionItem, CompletionModel } from './completionModel';
J
Johannes Rieken 已提交
23
import { alert } from 'vs/base/browser/ui/aria/aria';
J
Joao Moreno 已提交
24
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
25 26
import { attachListStyler } from 'vs/platform/theme/common/styler';
import { IThemeService, ITheme, registerThemingParticipant } from 'vs/platform/theme/common/themeService';
27
import { registerColor, editorWidgetBackground, listFocusBackground, activeContrastBorder, listHighlightForeground, editorForeground, editorWidgetBorder, focusBorder, textLinkForeground, textCodeBlockBackground } from 'vs/platform/theme/common/colorRegistry';
B
Benjamin Pasero 已提交
28
import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage';
29
import { MarkdownRenderer } from 'vs/editor/contrib/markdown/markdownRenderer';
30 31
import { IModeService } from 'vs/editor/common/services/modeService';
import { IOpenerService } from 'vs/platform/opener/common/opener';
32 33
import { TimeoutTimer, CancelablePromise, createCancelablePromise } from 'vs/base/common/async';
import { CancellationToken } from 'vs/base/common/cancellation';
34
import { CompletionItemKind, completionKindToCssClass } from 'vs/editor/common/modes';
35
import { IconLabel, IIconLabelValueOptions } from 'vs/base/browser/ui/iconLabel/iconLabel';
36 37 38 39 40
import { getIconClasses } from 'vs/editor/common/services/getIconClasses';
import { IModelService } from 'vs/editor/common/services/modelService';
import { URI } from 'vs/base/common/uri';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { FileKind } from 'vs/platform/files/common/files';
E
Erich Gamma 已提交
41

42
const expandSuggestionDocsByDefault = false;
43
const maxSuggestionsToShow = 12;
J
Joao Moreno 已提交
44

E
Erich Gamma 已提交
45 46 47 48
interface ISuggestionTemplateData {
	root: HTMLElement;
	icon: HTMLElement;
	colorspan: HTMLElement;
49
	iconLabel: IconLabel;
50
	typeLabel: HTMLElement;
51
	readMore: HTMLElement;
52
	disposables: IDisposable[];
E
Erich Gamma 已提交
53 54
}

M
Martin Aeschlimann 已提交
55 56 57
/**
 * Suggest widget colors
 */
58
export const editorSuggestWidgetBackground = registerColor('editorSuggestWidget.background', { dark: editorWidgetBackground, light: editorWidgetBackground, hc: editorWidgetBackground }, nls.localize('editorSuggestWidgetBackground', 'Background color of the suggest widget.'));
59
export const editorSuggestWidgetBorder = registerColor('editorSuggestWidget.border', { dark: editorWidgetBorder, light: editorWidgetBorder, hc: editorWidgetBorder }, nls.localize('editorSuggestWidgetBorder', 'Border color of the suggest widget.'));
60 61 62
export const editorSuggestWidgetForeground = registerColor('editorSuggestWidget.foreground', { dark: editorForeground, light: editorForeground, hc: editorForeground }, nls.localize('editorSuggestWidgetForeground', 'Foreground color of the suggest widget.'));
export const editorSuggestWidgetSelectedBackground = registerColor('editorSuggestWidget.selectedBackground', { dark: listFocusBackground, light: listFocusBackground, hc: listFocusBackground }, nls.localize('editorSuggestWidgetSelectedBackground', 'Background color of the selected entry in the suggest widget.'));
export const editorSuggestWidgetHighlightForeground = registerColor('editorSuggestWidget.highlightForeground', { dark: listHighlightForeground, light: listHighlightForeground, hc: listHighlightForeground }, nls.localize('editorSuggestWidgetHighlightForeground', 'Color of the match highlights in the suggest widget.'));
63

M
Martin Aeschlimann 已提交
64

65
const colorRegExp = /^(#([\da-f]{3}){1,2}|(rgb|hsl)a\(\s*(\d{1,3}%?\s*,\s*){3}(1|0?\.\d+)\)|(rgb|hsl)\(\s*\d{1,3}%?(\s*,\s*\d{1,3}%?){2}\s*\))$/i;
66 67 68
function matchesColor(text: string) {
	return text && text.match(colorRegExp) ? text : null;
}
69

70
function canExpandCompletionItem(item: ICompletionItem) {
R
Ramya Achutha Rao 已提交
71 72 73
	if (!item) {
		return false;
	}
74 75 76 77
	const suggestion = item.suggestion;
	if (suggestion.documentation) {
		return true;
	}
78
	return (suggestion.detail && suggestion.detail !== suggestion.label);
79 80
}

J
Joao Moreno 已提交
81
class Renderer implements IListRenderer<ICompletionItem, ISuggestionTemplateData> {
E
Erich Gamma 已提交
82

J
Joao Moreno 已提交
83 84
	constructor(
		private widget: SuggestWidget,
85
		private editor: ICodeEditor,
86 87 88
		private triggerKeybindingLabel: string,
		@IModelService private readonly _modelService: IModelService,
		@IModeService private readonly _modeService: IModeService,
J
Joao Moreno 已提交
89
	) {
90

J
Joao Moreno 已提交
91
	}
J
Joao Moreno 已提交
92

J
Joao Moreno 已提交
93 94 95
	get templateId(): string {
		return 'suggestion';
	}
E
Erich Gamma 已提交
96

J
Joao Moreno 已提交
97
	renderTemplate(container: HTMLElement): ISuggestionTemplateData {
J
Johannes Rieken 已提交
98
		const data = <ISuggestionTemplateData>Object.create(null);
99
		data.disposables = [];
E
Erich Gamma 已提交
100
		data.root = container;
J
Joao Moreno 已提交
101

J
Joao Moreno 已提交
102 103
		data.icon = append(container, $('.icon'));
		data.colorspan = append(data.icon, $('span.colorspan'));
E
Erich Gamma 已提交
104

J
Joao Moreno 已提交
105
		const text = append(container, $('.contents'));
J
Joao Moreno 已提交
106
		const main = append(text, $('.main'));
107 108 109 110

		data.iconLabel = new IconLabel(main, { supportHighlights: true });
		data.disposables.push(data.iconLabel);

111 112
		data.typeLabel = append(main, $('span.type-label'));

113 114
		data.readMore = append(main, $('span.readMore'));
		data.readMore.title = nls.localize('readMore', "Read More...{0}", this.triggerKeybindingLabel);
115

116
		const configureFont = () => {
J
Joao Moreno 已提交
117 118 119 120
			const configuration = this.editor.getConfiguration();
			const fontFamily = configuration.fontInfo.fontFamily;
			const fontSize = configuration.contribInfo.suggestFontSize || configuration.fontInfo.fontSize;
			const lineHeight = configuration.contribInfo.suggestLineHeight || configuration.fontInfo.lineHeight;
121
			const fontWeight = configuration.fontInfo.fontWeight;
J
Johannes Rieken 已提交
122 123
			const fontSizePx = `${fontSize}px`;
			const lineHeightPx = `${lineHeight}px`;
J
Joao Moreno 已提交
124 125

			data.root.style.fontSize = fontSizePx;
126
			data.root.style.fontWeight = fontWeight;
J
Joao Moreno 已提交
127 128 129 130
			main.style.fontFamily = fontFamily;
			main.style.lineHeight = lineHeightPx;
			data.icon.style.height = lineHeightPx;
			data.icon.style.width = lineHeightPx;
131 132
			data.readMore.style.height = lineHeightPx;
			data.readMore.style.width = lineHeightPx;
133 134 135 136
		};

		configureFont();

J
Joao Moreno 已提交
137
		chain<IConfigurationChangedEvent>(this.editor.onDidChangeConfiguration.bind(this.editor))
J
Joao Moreno 已提交
138
			.filter(e => e.fontInfo || e.contribInfo)
J
Joao Moreno 已提交
139
			.on(configureFont, null, data.disposables);
140

E
Erich Gamma 已提交
141 142 143
		return data;
	}

144
	renderElement(element: ICompletionItem, _index: number, templateData: ISuggestionTemplateData): void {
J
Johannes Rieken 已提交
145
		const data = <ISuggestionTemplateData>templateData;
146
		const suggestion = (<ICompletionItem>element).suggestion;
E
Erich Gamma 已提交
147

148
		data.icon.className = 'icon ' + completionKindToCssClass(suggestion.kind);
149 150
		data.colorspan.style.backgroundColor = '';

151
		if (suggestion.kind === CompletionItemKind.Color) {
152
			let color = matchesColor(suggestion.label) || typeof suggestion.documentation === 'string' && matchesColor(suggestion.documentation);
153 154 155 156
			if (color) {
				data.icon.className = 'icon customcolor';
				data.colorspan.style.backgroundColor = color;
			}
E
Erich Gamma 已提交
157 158
		}

159 160 161 162 163
		const labelOptions: IIconLabelValueOptions = {
			labelEscapeNewLines: true,
			matches: createMatches(element.matches)
		};

164
		if (suggestion.kind === CompletionItemKind.File || suggestion.kind === CompletionItemKind.Folder) {
165
			addClass(data.root, 'show-file-icons');
166 167 168 169 170 171 172 173 174 175
			data.icon.className = 'icon hide';
			labelOptions.extraClasses = getIconClasses(
				this._modelService,
				this._modeService,
				URI.from({ scheme: 'fake', path: suggestion.label }),
				suggestion.kind === CompletionItemKind.Folder ? FileKind.FOLDER : FileKind.FILE
			);
			labelOptions.extraClasses.push(suggestion.kind === CompletionItemKind.File
				? 'default-file-icon'
				: 'default-folder-icon');
176 177 178 179
		}

		data.iconLabel.setValue(suggestion.label, undefined, labelOptions);

180
		data.typeLabel.textContent = (suggestion.detail || '').replace(/\n.*$/m, '');
J
Joao Moreno 已提交
181

182
		if (canExpandCompletionItem(element)) {
183 184
			show(data.readMore);
			data.readMore.onmousedown = e => {
185 186 187
				e.stopPropagation();
				e.preventDefault();
			};
188
			data.readMore.onclick = e => {
189 190 191 192 193
				e.stopPropagation();
				e.preventDefault();
				this.widget.toggleDetails();
			};
		} else {
194 195 196
			hide(data.readMore);
			data.readMore.onmousedown = null;
			data.readMore.onclick = null;
197
		}
J
Joao Moreno 已提交
198
	}
J
Joao Moreno 已提交
199

J
Joao Moreno 已提交
200 201
	disposeElement(): void {
		// noop
E
Erich Gamma 已提交
202 203
	}

J
Joao Moreno 已提交
204
	disposeTemplate(templateData: ISuggestionTemplateData): void {
205
		templateData.disposables = dispose(templateData.disposables);
J
Joao Moreno 已提交
206 207
	}
}
E
Erich Gamma 已提交
208

A
Alex Dima 已提交
209
const enum State {
J
Joao Moreno 已提交
210 211 212
	Hidden,
	Loading,
	Empty,
J
Joao Moreno 已提交
213
	Open,
J
Joao Moreno 已提交
214 215 216 217 218 219 220
	Frozen,
	Details
}

class SuggestionDetails {

	private el: HTMLElement;
221
	private close: HTMLElement;
222
	private scrollbar: DomScrollableElement;
223
	private body: HTMLElement;
224
	private header: HTMLElement;
J
Joao Moreno 已提交
225 226
	private type: HTMLElement;
	private docs: HTMLElement;
227 228
	private ariaLabel: string;
	private disposables: IDisposable[];
229
	private renderDisposeable: IDisposable;
230
	private borderWidth: number = 1;
231 232 233 234

	constructor(
		container: HTMLElement,
		private widget: SuggestWidget,
235
		private editor: ICodeEditor,
236
		private markdownRenderer: MarkdownRenderer,
237
		private triggerKeybindingLabel: string
238 239
	) {
		this.disposables = [];
J
Joao Moreno 已提交
240 241

		this.el = append(container, $('.details'));
242 243
		this.disposables.push(toDisposable(() => container.removeChild(this.el)));

244
		this.body = $('.body');
245

246
		this.scrollbar = new DomScrollableElement(this.body, {});
247
		append(this.el, this.scrollbar.getDomNode());
248 249
		this.disposables.push(this.scrollbar);

250 251
		this.header = append(this.body, $('.header'));
		this.close = append(this.header, $('span.close'));
252
		this.close.title = nls.localize('readLess', "Read less...{0}", this.triggerKeybindingLabel);
253
		this.type = append(this.header, $('p.type'));
254

255
		this.docs = append(this.body, $('p.docs'));
256
		this.ariaLabel = null;
257 258 259

		this.configureFont();

J
Joao Moreno 已提交
260 261 262
		chain<IConfigurationChangedEvent>(this.editor.onDidChangeConfiguration.bind(this.editor))
			.filter(e => e.fontInfo)
			.on(this.configureFont, this, this.disposables);
263 264

		markdownRenderer.onDidRenderCodeBlock(() => this.scrollbar.scanDomNode(), this, this.disposables);
J
Joao Moreno 已提交
265 266
	}

J
Joao Moreno 已提交
267 268 269
	get element() {
		return this.el;
	}
J
Joao Moreno 已提交
270

271
	render(item: ICompletionItem): void {
272 273
		this.renderDisposeable = dispose(this.renderDisposeable);

274
		if (!item || !canExpandCompletionItem(item)) {
J
Joao Moreno 已提交
275 276
			this.type.textContent = '';
			this.docs.textContent = '';
277
			addClass(this.el, 'no-docs');
278
			this.ariaLabel = null;
J
Joao Moreno 已提交
279 280
			return;
		}
281
		removeClass(this.el, 'no-docs');
282
		if (typeof item.suggestion.documentation === 'string') {
283
			removeClass(this.docs, 'markdown-docs');
284 285
			this.docs.textContent = item.suggestion.documentation;
		} else {
286
			addClass(this.docs, 'markdown-docs');
M
Matt Bierner 已提交
287
			this.docs.innerHTML = '';
288 289 290
			const renderedContents = this.markdownRenderer.render(item.suggestion.documentation);
			this.renderDisposeable = renderedContents;
			this.docs.appendChild(renderedContents.element);
291
		}
292

R
Ramya Achutha Rao 已提交
293 294 295 296 297 298 299 300
		if (item.suggestion.detail) {
			this.type.innerText = item.suggestion.detail;
			show(this.type);
		} else {
			this.type.innerText = '';
			hide(this.type);
		}

301
		this.el.style.height = this.header.offsetHeight + this.docs.offsetHeight + (this.borderWidth * 2) + 'px';
302

303
		this.close.onmousedown = e => {
304 305 306
			e.preventDefault();
			e.stopPropagation();
		};
307
		this.close.onclick = e => {
308 309 310 311
			e.preventDefault();
			e.stopPropagation();
			this.widget.toggleDetails();
		};
312

J
Joao Moreno 已提交
313
		this.body.scrollTop = 0;
314
		this.scrollbar.scanDomNode();
315

316 317 318 319
		this.ariaLabel = strings.format(
			'{0}{1}',
			item.suggestion.detail || '',
			item.suggestion.documentation ? (typeof item.suggestion.documentation === 'string' ? item.suggestion.documentation : item.suggestion.documentation.value) : '');
320 321 322 323
	}

	getAriaLabel(): string {
		return this.ariaLabel;
J
Joao Moreno 已提交
324 325
	}

326 327 328 329 330 331 332 333
	scrollDown(much = 8): void {
		this.body.scrollTop += much;
	}

	scrollUp(much = 8): void {
		this.body.scrollTop -= much;
	}

334 335 336 337 338 339 340 341
	scrollTop(): void {
		this.body.scrollTop = 0;
	}

	scrollBottom(): void {
		this.body.scrollTop = this.body.scrollHeight;
	}

342 343 344 345 346 347 348 349
	pageDown(): void {
		this.scrollDown(80);
	}

	pageUp(): void {
		this.scrollUp(80);
	}

350 351 352 353
	setBorderWidth(width: number): void {
		this.borderWidth = width;
	}

354
	private configureFont() {
J
Joao Moreno 已提交
355 356 357
		const configuration = this.editor.getConfiguration();
		const fontFamily = configuration.fontInfo.fontFamily;
		const fontSize = configuration.contribInfo.suggestFontSize || configuration.fontInfo.fontSize;
358
		const lineHeight = configuration.contribInfo.suggestLineHeight || configuration.fontInfo.lineHeight;
359
		const fontWeight = configuration.fontInfo.fontWeight;
J
Johannes Rieken 已提交
360
		const fontSizePx = `${fontSize}px`;
361
		const lineHeightPx = `${lineHeight}px`;
J
Joao Moreno 已提交
362

J
Joao Moreno 已提交
363
		this.el.style.fontSize = fontSizePx;
364
		this.el.style.fontWeight = fontWeight;
J
Joao Moreno 已提交
365
		this.type.style.fontFamily = fontFamily;
366 367
		this.close.style.height = lineHeightPx;
		this.close.style.width = lineHeightPx;
368
	}
369

370 371
	dispose(): void {
		this.disposables = dispose(this.disposables);
372
		this.renderDisposeable = dispose(this.renderDisposeable);
J
Joao Moreno 已提交
373
	}
J
Joao Moreno 已提交
374 375
}

376 377 378 379 380 381
export interface ISelectedSuggestion {
	item: ICompletionItem;
	index: number;
	model: CompletionModel;
}

J
Joao Moreno 已提交
382
export class SuggestWidget implements IContentWidget, IListVirtualDelegate<ICompletionItem>, IDisposable {
J
Joao Moreno 已提交
383

M
Matt Bierner 已提交
384
	private static readonly ID: string = 'editor.widget.suggestWidget';
E
Erich Gamma 已提交
385

J
Johannes Rieken 已提交
386 387
	static LOADING_MESSAGE: string = nls.localize('suggestWidget.loading', "Loading...");
	static NO_SUGGESTIONS_MESSAGE: string = nls.localize('suggestWidget.noSuggestions', "No suggestions.");
E
Erich Gamma 已提交
388

J
Joao Moreno 已提交
389
	// Editor.IContentWidget.allowEditorOverflow
390
	readonly allowEditorOverflow = true;
J
Joao Moreno 已提交
391

J
Joao Moreno 已提交
392
	private state: State;
E
Erich Gamma 已提交
393
	private isAuto: boolean;
394
	private loadingTimeout: any;
395
	private currentSuggestionDetails: CancelablePromise<void>;
396
	private focusedItem: ICompletionItem;
397
	private ignoreFocusEvents = false;
J
Joao Moreno 已提交
398
	private completionModel: CompletionModel;
J
Joao Moreno 已提交
399

E
Erich Gamma 已提交
400
	private element: HTMLElement;
J
Joao Moreno 已提交
401
	private messageElement: HTMLElement;
J
Joao Moreno 已提交
402
	private listElement: HTMLElement;
J
Joao Moreno 已提交
403
	private details: SuggestionDetails;
404
	private list: List<ICompletionItem>;
405
	private listHeight: number;
E
Erich Gamma 已提交
406

A
Alex Dima 已提交
407 408 409
	private suggestWidgetVisible: IContextKey<boolean>;
	private suggestWidgetMultipleSuggestions: IContextKey<boolean>;
	private suggestionSupportsAutoAccept: IContextKey<boolean>;
J
Joao Moreno 已提交
410

411 412
	private readonly editorBlurTimeout = new TimeoutTimer();
	private readonly showTimeout = new TimeoutTimer();
J
Joao Moreno 已提交
413
	private toDispose: IDisposable[];
J
Joao Moreno 已提交
414

415 416
	private onDidSelectEmitter = new Emitter<ISelectedSuggestion>();
	private onDidFocusEmitter = new Emitter<ISelectedSuggestion>();
417 418 419
	private onDidHideEmitter = new Emitter<this>();
	private onDidShowEmitter = new Emitter<this>();

420

421 422
	readonly onDidSelect: Event<ISelectedSuggestion> = this.onDidSelectEmitter.event;
	readonly onDidFocus: Event<ISelectedSuggestion> = this.onDidFocusEmitter.event;
423 424
	readonly onDidHide: Event<this> = this.onDidHideEmitter.event;
	readonly onDidShow: Event<this> = this.onDidShowEmitter.event;
425

426
	private readonly maxWidgetWidth = 660;
427
	private readonly listWidth = 330;
428
	private storageService: IStorageService;
429 430
	private detailsFocusBorderColor: string;
	private detailsBorderColor: string;
431

432 433
	private firstFocusInCurrentList: boolean = false;

434
	constructor(
A
Alex Dima 已提交
435
		private editor: ICodeEditor,
J
Joao Moreno 已提交
436
		@ITelemetryService private telemetryService: ITelemetryService,
437
		@IContextKeyService contextKeyService: IContextKeyService,
438
		@IThemeService themeService: IThemeService,
439
		@IStorageService storageService: IStorageService,
440 441
		@IKeybindingService keybindingService: IKeybindingService,
		@IModeService modeService: IModeService,
442 443
		@IOpenerService openerService: IOpenerService,
		@IInstantiationService instantiationService: IInstantiationService,
444
	) {
445 446
		const kb = keybindingService.lookupKeybinding('editor.action.triggerSuggest');
		const triggerKeybindingLabel = !kb ? '' : ` (${kb.getLabel()})`;
447
		const markdownRenderer = new MarkdownRenderer(editor, modeService, openerService);
448

E
Erich Gamma 已提交
449
		this.isAuto = false;
J
Joao Moreno 已提交
450
		this.focusedItem = null;
451
		this.storageService = storageService;
E
Erich Gamma 已提交
452

453
		this.element = $('.editor-widget.suggest-widget');
A
Alex Dima 已提交
454
		if (!this.editor.getConfiguration().contribInfo.iconsInSuggestions) {
J
Joao Moreno 已提交
455
			addClass(this.element, 'no-icons');
E
Erich Gamma 已提交
456 457
		}

J
Joao Moreno 已提交
458
		this.messageElement = append(this.element, $('.message'));
J
Joao Moreno 已提交
459
		this.listElement = append(this.element, $('.tree'));
460
		this.details = new SuggestionDetails(this.element, this, this.editor, markdownRenderer, triggerKeybindingLabel);
J
Joao Moreno 已提交
461

462
		let renderer = instantiationService.createInstance(Renderer, this, this.editor, triggerKeybindingLabel);
J
Joao Moreno 已提交
463

464 465
		this.list = new List(this.listElement, this, [renderer], {
			useShadows: false,
J
Joao Moreno 已提交
466
			selectOnMouseDown: true,
J
Joao Moreno 已提交
467 468
			focusOnMouseDown: false,
			openController: { shouldOpen: () => false }
469
		});
J
Joao Moreno 已提交
470 471

		this.toDispose = [
472
			attachListStyler(this.list, themeService, {
473
				listInactiveFocusBackground: editorSuggestWidgetSelectedBackground,
474 475
				listInactiveFocusOutline: activeContrastBorder
			}),
M
Martin Aeschlimann 已提交
476
			themeService.onThemeChange(t => this.onThemeChange(t)),
477
			editor.onDidLayoutChange(() => this.onEditorLayoutChange()),
J
Joao Moreno 已提交
478
			this.list.onSelectionChange(e => this.onListSelection(e)),
J
Joao Moreno 已提交
479
			this.list.onFocusChange(e => this.onListFocus(e)),
480
			this.editor.onDidChangeCursorSelection(() => this.onCursorSelectionChanged())
J
Joao Moreno 已提交
481 482
		];

483 484 485
		this.suggestWidgetVisible = SuggestContext.Visible.bindTo(contextKeyService);
		this.suggestWidgetMultipleSuggestions = SuggestContext.MultipleSuggestions.bindTo(contextKeyService);
		this.suggestionSupportsAutoAccept = SuggestContext.AcceptOnKey.bindTo(contextKeyService);
J
Joao Moreno 已提交
486

J
Joao Moreno 已提交
487 488
		this.editor.addContentWidget(this);
		this.setState(State.Hidden);
489

M
Martin Aeschlimann 已提交
490
		this.onThemeChange(themeService.getTheme());
J
Joao Moreno 已提交
491
	}
E
Erich Gamma 已提交
492

J
Joao Moreno 已提交
493 494 495 496
	private onCursorSelectionChanged(): void {
		if (this.state === State.Hidden) {
			return;
		}
E
Erich Gamma 已提交
497

J
Joao Moreno 已提交
498 499
		this.editor.layoutContentWidget(this);
	}
J
Joao Moreno 已提交
500

501 502 503 504 505 506
	private onEditorLayoutChange(): void {
		if ((this.state === State.Open || this.state === State.Details) && this.expandDocsSettingFromStorage()) {
			this.expandSideOrBelow();
		}
	}

J
Joao Moreno 已提交
507
	private onListSelection(e: IListEvent<ICompletionItem>): void {
J
Joao Moreno 已提交
508 509 510
		if (!e.elements.length) {
			return;
		}
E
Erich Gamma 已提交
511

J
Joao Moreno 已提交
512
		const item = e.elements[0];
513
		const index = e.indexes[0];
514
		item.resolve(CancellationToken.None).then(() => {
515
			this.onDidSelectEmitter.fire({ item, index, model: this.completionModel });
J
Johannes Rieken 已提交
516 517
			alert(nls.localize('suggestionAriaAccepted', "{0}, accepted", item.suggestion.label));
			this.editor.focus();
518
		});
J
Joao Moreno 已提交
519
	}
E
Erich Gamma 已提交
520

J
Johannes Rieken 已提交
521
	private _getSuggestionAriaAlertLabel(item: ICompletionItem): string {
522
		const isSnippet = item.suggestion.kind === CompletionItemKind.Snippet;
523 524 525 526 527 528 529

		if (!canExpandCompletionItem(item)) {
			return isSnippet ? nls.localize('ariaCurrentSnippetSuggestion', "{0}, snippet suggestion", item.suggestion.label)
				: nls.localize('ariaCurrentSuggestion', "{0}, suggestion", item.suggestion.label);
		} else if (this.expandDocsSettingFromStorage()) {
			return isSnippet ? nls.localize('ariaCurrentSnippeSuggestionReadDetails', "{0}, snippet suggestion. Reading details. {1}", item.suggestion.label, this.details.getAriaLabel())
				: nls.localize('ariaCurrenttSuggestionReadDetails', "{0}, suggestion. Reading details. {1}", item.suggestion.label, this.details.getAriaLabel());
530
		} else {
531 532
			return isSnippet ? nls.localize('ariaCurrentSnippetSuggestionWithDetails', "{0}, snippet suggestion, has details", item.suggestion.label)
				: nls.localize('ariaCurrentSuggestionWithDetails', "{0}, suggestion, has details", item.suggestion.label);
533 534 535 536
		}
	}

	private _lastAriaAlertLabel: string;
J
Johannes Rieken 已提交
537
	private _ariaAlert(newAriaAlertLabel: string): void {
538 539 540 541 542 543 544 545 546
		if (this._lastAriaAlertLabel === newAriaAlertLabel) {
			return;
		}
		this._lastAriaAlertLabel = newAriaAlertLabel;
		if (this._lastAriaAlertLabel) {
			alert(this._lastAriaAlertLabel);
		}
	}

M
Martin Aeschlimann 已提交
547
	private onThemeChange(theme: ITheme) {
M
Matt Bierner 已提交
548
		const backgroundColor = theme.getColor(editorSuggestWidgetBackground);
M
Martin Aeschlimann 已提交
549
		if (backgroundColor) {
550 551
			this.listElement.style.backgroundColor = backgroundColor.toString();
			this.details.element.style.backgroundColor = backgroundColor.toString();
R
Ramya Achutha Rao 已提交
552
			this.messageElement.style.backgroundColor = backgroundColor.toString();
M
Martin Aeschlimann 已提交
553
		}
M
Matt Bierner 已提交
554
		const borderColor = theme.getColor(editorSuggestWidgetBorder);
M
Martin Aeschlimann 已提交
555
		if (borderColor) {
556 557 558
			this.listElement.style.borderColor = borderColor.toString();
			this.details.element.style.borderColor = borderColor.toString();
			this.messageElement.style.borderColor = borderColor.toString();
559
			this.detailsBorderColor = borderColor.toString();
M
Martin Aeschlimann 已提交
560
		}
M
Matt Bierner 已提交
561
		const focusBorderColor = theme.getColor(focusBorder);
562 563 564
		if (focusBorderColor) {
			this.detailsFocusBorderColor = focusBorderColor.toString();
		}
565
		this.details.setBorderWidth(theme.type === 'hc' ? 2 : 1);
M
Martin Aeschlimann 已提交
566 567
	}

J
Joao Moreno 已提交
568
	private onListFocus(e: IListEvent<ICompletionItem>): void {
569 570 571 572
		if (this.ignoreFocusEvents) {
			return;
		}

J
Joao Moreno 已提交
573
		if (!e.elements.length) {
J
Joao Moreno 已提交
574 575 576 577 578
			if (this.currentSuggestionDetails) {
				this.currentSuggestionDetails.cancel();
				this.currentSuggestionDetails = null;
				this.focusedItem = null;
			}
579

J
Joao Moreno 已提交
580
			this._ariaAlert(null);
J
Joao Moreno 已提交
581 582
			return;
		}
E
Erich Gamma 已提交
583

J
Joao Moreno 已提交
584
		const item = e.elements[0];
J
Johannes Rieken 已提交
585
		const index = e.indexes[0];
586

587
		this.firstFocusInCurrentList = !this.focusedItem;
J
Johannes Rieken 已提交
588
		if (item !== this.focusedItem) {
E
Erich Gamma 已提交
589

J
Joao Moreno 已提交
590

J
Johannes Rieken 已提交
591 592 593 594
			if (this.currentSuggestionDetails) {
				this.currentSuggestionDetails.cancel();
				this.currentSuggestionDetails = null;
			}
E
Erich Gamma 已提交
595

596

J
Johannes Rieken 已提交
597
			this.suggestionSupportsAutoAccept.set(!item.suggestion.noAutoAccept);
598

J
Johannes Rieken 已提交
599
			this.focusedItem = item;
J
Joao Moreno 已提交
600

J
Johannes Rieken 已提交
601
			this.list.reveal(index);
602

J
Johannes Rieken 已提交
603
			this.currentSuggestionDetails = createCancelablePromise(token => item.resolve(token));
604

J
Johannes Rieken 已提交
605 606 607 608
			this.currentSuggestionDetails.then(() => {
				if (this.list.length < index) {
					return;
				}
609

J
Johannes Rieken 已提交
610 611 612 613 614
				// item can have extra information, so re-render
				this.ignoreFocusEvents = true;
				this.list.splice(index, 1, [item]);
				this.list.setFocus([index]);
				this.ignoreFocusEvents = false;
615

J
Johannes Rieken 已提交
616 617 618 619 620 621 622 623 624 625 626 627 628
				if (this.expandDocsSettingFromStorage()) {
					this.showDetails();
				} else {
					removeClass(this.element, 'docs-side');
				}

				this._ariaAlert(this._getSuggestionAriaAlertLabel(item));
			}).catch(onUnexpectedError).then(() => {
				if (this.focusedItem === item) {
					this.currentSuggestionDetails = null;
				}
			});
		}
629 630

		// emit an event
631
		this.onDidFocusEmitter.fire({ item, index, model: this.completionModel });
J
Joao Moreno 已提交
632
	}
J
Joao Moreno 已提交
633 634

	private setState(state: State): void {
J
Joao Moreno 已提交
635 636 637 638
		if (!this.element) {
			return;
		}

639
		const stateChanged = this.state !== state;
J
Joao Moreno 已提交
640 641
		this.state = state;

J
Joao Moreno 已提交
642 643
		toggleClass(this.element, 'frozen', state === State.Frozen);

J
Joao Moreno 已提交
644 645
		switch (state) {
			case State.Hidden:
646
				hide(this.messageElement, this.details.element, this.listElement);
J
Joao Moreno 已提交
647
				this.hide();
648
				this.listHeight = 0;
A
tslint  
Alex Dima 已提交
649
				if (stateChanged) {
650
					this.list.splice(0, this.list.length);
A
tslint  
Alex Dima 已提交
651
				}
652
				this.focusedItem = null;
653
				break;
J
Joao Moreno 已提交
654
			case State.Loading:
J
Joao Moreno 已提交
655
				this.messageElement.textContent = SuggestWidget.LOADING_MESSAGE;
J
Joao Moreno 已提交
656
				hide(this.listElement, this.details.element);
J
Joao Moreno 已提交
657
				show(this.messageElement);
R
Ramya Achutha Rao 已提交
658
				removeClass(this.element, 'docs-side');
659
				this.show();
660
				this.focusedItem = null;
J
Joao Moreno 已提交
661 662
				break;
			case State.Empty:
J
Joao Moreno 已提交
663
				this.messageElement.textContent = SuggestWidget.NO_SUGGESTIONS_MESSAGE;
J
Joao Moreno 已提交
664
				hide(this.listElement, this.details.element);
J
Joao Moreno 已提交
665
				show(this.messageElement);
R
Ramya Achutha Rao 已提交
666
				removeClass(this.element, 'docs-side');
667
				this.show();
668
				this.focusedItem = null;
J
Joao Moreno 已提交
669 670
				break;
			case State.Open:
671
				hide(this.messageElement);
672
				show(this.listElement);
673
				this.show();
J
Joao Moreno 已提交
674 675
				break;
			case State.Frozen:
676
				hide(this.messageElement);
J
Joao Moreno 已提交
677
				show(this.listElement);
678
				this.show();
J
Joao Moreno 已提交
679 680
				break;
			case State.Details:
681 682
				hide(this.messageElement);
				show(this.details.element, this.listElement);
683
				this.show();
684
				this._ariaAlert(this.details.getAriaLabel());
J
Joao Moreno 已提交
685 686
				break;
		}
687 688
	}

689
	showTriggered(auto: boolean, delay: number) {
J
Joao Moreno 已提交
690 691 692
		if (this.state !== State.Hidden) {
			return;
		}
E
Erich Gamma 已提交
693

694
		this.isAuto = !!auto;
J
Joao Moreno 已提交
695 696 697 698 699

		if (!this.isAuto) {
			this.loadingTimeout = setTimeout(() => {
				this.loadingTimeout = null;
				this.setState(State.Loading);
700
			}, delay);
J
Joao Moreno 已提交
701
		}
702
	}
E
Erich Gamma 已提交
703

704
	showSuggestions(completionModel: CompletionModel, selectionIndex: number, isFrozen: boolean, isAuto: boolean): void {
A
Alex Dima 已提交
705 706 707 708
		if (this.loadingTimeout) {
			clearTimeout(this.loadingTimeout);
			this.loadingTimeout = null;
		}
J
Joao Moreno 已提交
709

710 711 712
		if (this.completionModel !== completionModel) {
			this.completionModel = completionModel;
		}
J
Joao Moreno 已提交
713

R
Ramya Achutha Rao 已提交
714
		if (isFrozen && this.state !== State.Empty && this.state !== State.Hidden) {
715 716
			this.setState(State.Frozen);
			return;
717 718
		}

719 720
		let visibleCount = this.completionModel.items.length;

J
Joao Moreno 已提交
721
		const isEmpty = visibleCount === 0;
722
		this.suggestWidgetMultipleSuggestions.set(visibleCount > 1);
J
Joao Moreno 已提交
723 724

		if (isEmpty) {
725
			if (isAuto) {
J
Joao Moreno 已提交
726 727
				this.setState(State.Hidden);
			} else {
Y
Yuki Ueda 已提交
728
				this.setState(State.Empty);
J
Joao Moreno 已提交
729 730
			}

J
Joao Moreno 已提交
731
			this.completionModel = null;
J
Joao Moreno 已提交
732

J
Joao Moreno 已提交
733
		} else {
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748

			if (this.state !== State.Open) {
				const { stats } = this.completionModel;
				stats['wasAutomaticallyTriggered'] = !!isAuto;
				/* __GDPR__
					"suggestWidget" : {
						"wasAutomaticallyTriggered" : { "classification": "SystemMetaData", "purpose": "FeatureInsight", "isMeasurement": true },
						"${include}": [
							"${ICompletionStats}",
							"${EditorTelemetryData}"
						]
					}
				*/
				this.telemetryService.publicLog('suggestWidget', { ...stats, ...this.editor.getTelemetryData() });
			}
J
Joao Moreno 已提交
749

750 751
			this.list.splice(0, this.list.length, this.completionModel.items);

R
Ramya Achutha Rao 已提交
752 753 754 755 756
			if (isFrozen) {
				this.setState(State.Frozen);
			} else {
				this.setState(State.Open);
			}
R
Ramya Achutha Rao 已提交
757

J
Joao Moreno 已提交
758
			this.list.reveal(selectionIndex, selectionIndex);
J
Joao Moreno 已提交
759 760
			this.list.setFocus([selectionIndex]);

R
Ramya Achutha Rao 已提交
761 762 763 764
			// Reset focus border
			if (this.detailsBorderColor) {
				this.details.element.style.borderColor = this.detailsBorderColor;
			}
J
Joao Moreno 已提交
765
		}
766
	}
E
Erich Gamma 已提交
767

J
Joao Moreno 已提交
768
	selectNextPage(): boolean {
J
Joao Moreno 已提交
769 770 771 772 773 774 775 776 777 778 779 780
		switch (this.state) {
			case State.Hidden:
				return false;
			case State.Details:
				this.details.pageDown();
				return true;
			case State.Loading:
				return !this.isAuto;
			default:
				this.list.focusNextPage();
				return true;
		}
E
Erich Gamma 已提交
781 782
	}

J
Joao Moreno 已提交
783
	selectNext(): boolean {
J
Joao Moreno 已提交
784 785 786 787 788 789 790 791 792
		switch (this.state) {
			case State.Hidden:
				return false;
			case State.Loading:
				return !this.isAuto;
			default:
				this.list.focusNext(1, true);
				return true;
		}
E
Erich Gamma 已提交
793 794
	}

795 796 797 798 799
	selectLast(): boolean {
		switch (this.state) {
			case State.Hidden:
				return false;
			case State.Details:
800
				this.details.scrollBottom();
801 802 803 804 805 806 807 808 809
				return true;
			case State.Loading:
				return !this.isAuto;
			default:
				this.list.focusLast();
				return true;
		}
	}

J
Joao Moreno 已提交
810
	selectPreviousPage(): boolean {
J
Joao Moreno 已提交
811 812 813 814 815 816 817 818 819 820 821 822
		switch (this.state) {
			case State.Hidden:
				return false;
			case State.Details:
				this.details.pageUp();
				return true;
			case State.Loading:
				return !this.isAuto;
			default:
				this.list.focusPreviousPage();
				return true;
		}
E
Erich Gamma 已提交
823 824
	}

J
Joao Moreno 已提交
825
	selectPrevious(): boolean {
J
Joao Moreno 已提交
826 827 828 829 830 831 832
		switch (this.state) {
			case State.Hidden:
				return false;
			case State.Loading:
				return !this.isAuto;
			default:
				this.list.focusPrevious(1, true);
J
Joao Moreno 已提交
833
				return false;
J
Joao Moreno 已提交
834
		}
E
Erich Gamma 已提交
835 836
	}

837 838 839 840 841
	selectFirst(): boolean {
		switch (this.state) {
			case State.Hidden:
				return false;
			case State.Details:
842
				this.details.scrollTop();
843 844 845 846 847 848 849 850 851
				return true;
			case State.Loading:
				return !this.isAuto;
			default:
				this.list.focusFirst();
				return true;
		}
	}

852
	getFocusedItem(): ISelectedSuggestion {
853 854 855 856
		if (this.state !== State.Hidden
			&& this.state !== State.Empty
			&& this.state !== State.Loading) {

857 858 859 860 861
			return {
				item: this.list.getFocusedElements()[0],
				index: this.list.getFocus()[0],
				model: this.completionModel
			};
J
Joao Moreno 已提交
862
		}
863
		return undefined;
E
Erich Gamma 已提交
864 865
	}

866
	toggleDetailsFocus(): void {
J
Joao Moreno 已提交
867 868
		if (this.state === State.Details) {
			this.setState(State.Open);
869 870 871
			if (this.detailsBorderColor) {
				this.details.element.style.borderColor = this.detailsBorderColor;
			}
872
		} else if (this.state === State.Open && this.expandDocsSettingFromStorage()) {
873
			this.setState(State.Details);
874 875 876
			if (this.detailsFocusBorderColor) {
				this.details.element.style.borderColor = this.detailsFocusBorderColor;
			}
J
Joao Moreno 已提交
877
		}
K
kieferrm 已提交
878
		/* __GDPR__
K
kieferrm 已提交
879 880 881 882 883 884
			"suggestWidget:toggleDetailsFocus" : {
				"${include}": [
					"${EditorTelemetryData}"
				]
			}
		*/
885
		this.telemetryService.publicLog('suggestWidget:toggleDetailsFocus', this.editor.getTelemetryData());
886
	}
J
Joao Moreno 已提交
887

888
	toggleDetails(): void {
R
Ramya Achutha Rao 已提交
889 890 891
		if (!canExpandCompletionItem(this.list.getFocusedElements()[0])) {
			return;
		}
892

893 894
		if (this.expandDocsSettingFromStorage()) {
			this.updateExpandDocsSetting(false);
895
			hide(this.details.element);
896
			removeClass(this.element, 'docs-side');
897
			removeClass(this.element, 'docs-below');
898
			this.editor.layoutContentWidget(this);
K
kieferrm 已提交
899
			/* __GDPR__
K
kieferrm 已提交
900 901 902 903 904 905
				"suggestWidget:collapseDetails" : {
					"${include}": [
						"${EditorTelemetryData}"
					]
				}
			*/
906
			this.telemetryService.publicLog('suggestWidget:collapseDetails', this.editor.getTelemetryData());
907
		} else {
908
			if (this.state !== State.Open && this.state !== State.Details && this.state !== State.Frozen) {
909 910 911
				return;
			}

912
			this.updateExpandDocsSetting(true);
913
			this.showDetails();
914
			this._ariaAlert(this.details.getAriaLabel());
K
kieferrm 已提交
915
			/* __GDPR__
K
kieferrm 已提交
916 917 918 919 920 921
				"suggestWidget:expandDetails" : {
					"${include}": [
						"${EditorTelemetryData}"
					]
				}
			*/
922
			this.telemetryService.publicLog('suggestWidget:expandDetails', this.editor.getTelemetryData());
923
		}
924

925 926
	}

927
	showDetails(): void {
R
Ramya Achutha Rao 已提交
928
		this.expandSideOrBelow();
929 930

		show(this.details.element);
931
		this.details.render(this.list.getFocusedElements()[0]);
932
		this.details.element.style.maxHeight = this.maxWidgetHeight + 'px';
933

R
Ramya Achutha Rao 已提交
934 935
		// Reset margin-top that was set as Fix for #26416
		this.listElement.style.marginTop = '0px';
936 937 938 939 940

		// with docs showing up widget width/height may change, so reposition the widget
		this.editor.layoutContentWidget(this);

		this.adjustDocsPosition();
J
Joao Moreno 已提交
941

J
Joao Moreno 已提交
942
		this.editor.focus();
J
Joao Moreno 已提交
943 944
	}

945
	private show(): void {
946 947 948 949 950 951
		const newHeight = this.updateListHeight();
		if (newHeight !== this.listHeight) {
			this.editor.layoutContentWidget(this);
			this.listHeight = newHeight;
		}

J
Joao Moreno 已提交
952
		this.suggestWidgetVisible.set(true);
953

954
		this.showTimeout.cancelAndSet(() => {
J
Joao Moreno 已提交
955
			addClass(this.element, 'visible');
956
			this.onDidShowEmitter.fire(this);
957
		}, 100);
E
Erich Gamma 已提交
958 959
	}

960
	private hide(): void {
J
Joao Moreno 已提交
961
		this.suggestWidgetVisible.reset();
S
Sean Kelly 已提交
962
		this.suggestWidgetMultipleSuggestions.reset();
J
Joao Moreno 已提交
963
		removeClass(this.element, 'visible');
E
Erich Gamma 已提交
964 965
	}

966 967 968
	hideWidget(): void {
		clearTimeout(this.loadingTimeout);
		this.setState(State.Hidden);
969
		this.onDidHideEmitter.fire(this);
970 971
	}

J
Joao Moreno 已提交
972
	getPosition(): IContentWidgetPosition {
J
Joao Moreno 已提交
973 974
		if (this.state === State.Hidden) {
			return null;
E
Erich Gamma 已提交
975
		}
J
Joao Moreno 已提交
976 977

		return {
978
			position: this.editor.getPosition(),
A
Alex Dima 已提交
979
			preference: [ContentWidgetPositionPreference.BELOW, ContentWidgetPositionPreference.ABOVE]
J
Joao Moreno 已提交
980
		};
E
Erich Gamma 已提交
981 982
	}

J
Joao Moreno 已提交
983
	getDomNode(): HTMLElement {
E
Erich Gamma 已提交
984 985 986
		return this.element;
	}

J
Joao Moreno 已提交
987
	getId(): string {
E
Erich Gamma 已提交
988 989 990
		return SuggestWidget.ID;
	}

991
	private updateListHeight(): number {
J
Joao Moreno 已提交
992
		let height = 0;
E
Erich Gamma 已提交
993

J
Joao Moreno 已提交
994
		if (this.state === State.Empty || this.state === State.Loading) {
995
			height = this.unfocusedHeight;
J
Joao Moreno 已提交
996
		} else {
997 998
			const suggestionCount = this.list.contentHeight / this.unfocusedHeight;
			height = Math.min(suggestionCount, maxSuggestionsToShow) * this.unfocusedHeight;
J
Joao Moreno 已提交
999
		}
J
Joao Moreno 已提交
1000

1001
		this.element.style.lineHeight = `${this.unfocusedHeight}px`;
1002
		this.listElement.style.height = `${height}px`;
J
Joao Moreno 已提交
1003
		this.list.layout(height);
1004
		return height;
J
Joao Moreno 已提交
1005 1006
	}

1007
	private adjustDocsPosition() {
R
Ramya Achutha Rao 已提交
1008
		const lineHeight = this.editor.getConfiguration().fontInfo.lineHeight;
1009 1010 1011 1012
		const cursorCoords = this.editor.getScrolledVisiblePosition(this.editor.getPosition());
		const editorCoords = getDomNodePagePosition(this.editor.getDomNode());
		const cursorX = editorCoords.left + cursorCoords.left;
		const cursorY = editorCoords.top + cursorCoords.top + cursorCoords.height;
1013 1014 1015
		const widgetCoords = getDomNodePagePosition(this.element);
		const widgetX = widgetCoords.left;
		const widgetY = widgetCoords.top;
1016

1017 1018 1019
		if (widgetX < cursorX - this.listWidth) {
			// Widget is too far to the left of cursor, swap list and docs
			addClass(this.element, 'list-right');
1020 1021
		} else {
			removeClass(this.element, 'list-right');
1022 1023
		}

R
Ramya Achutha Rao 已提交
1024 1025 1026
		// Compare top of the cursor (cursorY - lineheight) with widgetTop to determine if
		// margin-top needs to be applied on list to make it appear right above the cursor
		// Cannot compare cursorY directly as it may be a few decimals off due to zoooming
R
Ramya Achutha Rao 已提交
1027
		if (hasClass(this.element, 'docs-side')
R
Ramya Achutha Rao 已提交
1028
			&& cursorY - lineHeight > widgetY
R
Ramya Achutha Rao 已提交
1029 1030 1031 1032 1033
			&& this.details.element.offsetHeight > this.listElement.offsetHeight) {

			// Fix for #26416
			// Docs is bigger than list and widget is above cursor, apply margin-top so that list appears right above cursor
			this.listElement.style.marginTop = `${this.details.element.offsetHeight - this.listElement.offsetHeight}px`;
1034
		}
1035 1036
	}

1037
	private expandSideOrBelow() {
1038 1039 1040 1041 1042 1043
		if (!canExpandCompletionItem(this.focusedItem) && this.firstFocusInCurrentList) {
			removeClass(this.element, 'docs-side');
			removeClass(this.element, 'docs-below');
			return;
		}

1044 1045 1046 1047
		let matches = this.element.style.maxWidth.match(/(\d+)px/);
		if (!matches || Number(matches[1]) < this.maxWidgetWidth) {
			addClass(this.element, 'docs-below');
			removeClass(this.element, 'docs-side');
1048
		} else if (canExpandCompletionItem(this.focusedItem)) {
1049 1050 1051 1052 1053
			addClass(this.element, 'docs-side');
			removeClass(this.element, 'docs-below');
		}
	}

1054 1055
	// Heights

1056 1057
	private get maxWidgetHeight(): number {
		return this.unfocusedHeight * maxSuggestionsToShow;
1058 1059 1060
	}

	private get unfocusedHeight(): number {
J
Joao Moreno 已提交
1061 1062
		const configuration = this.editor.getConfiguration();
		return configuration.contribInfo.suggestLineHeight || configuration.fontInfo.lineHeight;
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
	}

	// IDelegate

	getHeight(element: ICompletionItem): number {
		return this.unfocusedHeight;
	}

	getTemplateId(element: ICompletionItem): string {
		return 'suggestion';
	}

1075
	private expandDocsSettingFromStorage(): boolean {
1076
		return this.storageService.getBoolean('expandSuggestionDocs', StorageScope.GLOBAL, expandSuggestionDocsByDefault);
1077 1078 1079
	}

	private updateExpandDocsSetting(value: boolean) {
B
Benjamin Pasero 已提交
1080
		this.storageService.store('expandSuggestionDocs', value, StorageScope.GLOBAL);
1081 1082
	}

J
Joao Moreno 已提交
1083
	dispose(): void {
J
Joao Moreno 已提交
1084 1085 1086
		this.state = null;
		this.suggestionSupportsAutoAccept = null;
		this.currentSuggestionDetails = null;
J
Joao Moreno 已提交
1087
		this.focusedItem = null;
J
Joao Moreno 已提交
1088 1089
		this.element = null;
		this.messageElement = null;
J
Joao Moreno 已提交
1090
		this.listElement = null;
J
Joao Moreno 已提交
1091 1092
		this.details.dispose();
		this.details = null;
J
Joao Moreno 已提交
1093 1094
		this.list.dispose();
		this.list = null;
J
Joao Moreno 已提交
1095
		this.toDispose = dispose(this.toDispose);
A
Alex Dima 已提交
1096 1097 1098 1099
		if (this.loadingTimeout) {
			clearTimeout(this.loadingTimeout);
			this.loadingTimeout = null;
		}
J
Joao Moreno 已提交
1100

1101 1102
		this.editorBlurTimeout.dispose();
		this.showTimeout.dispose();
E
Erich Gamma 已提交
1103
	}
J
Johannes Rieken 已提交
1104
}
1105 1106

registerThemingParticipant((theme, collector) => {
M
Matt Bierner 已提交
1107
	const matchHighlight = theme.getColor(editorSuggestWidgetHighlightForeground);
1108
	if (matchHighlight) {
J
Johannes Rieken 已提交
1109
		collector.addRule(`.monaco-editor .suggest-widget .monaco-list .monaco-list-row .monaco-highlighted-label .highlight { color: ${matchHighlight}; }`);
1110
	}
M
Matt Bierner 已提交
1111
	const foreground = theme.getColor(editorSuggestWidgetForeground);
1112
	if (foreground) {
1113
		collector.addRule(`.monaco-editor .suggest-widget { color: ${foreground}; }`);
1114
	}
M
Matt Bierner 已提交
1115 1116 1117 1118 1119

	const link = theme.getColor(textLinkForeground);
	if (link) {
		collector.addRule(`.monaco-editor .suggest-widget a { color: ${link}; }`);
	}
1120

M
Matt Bierner 已提交
1121
	const codeBackground = theme.getColor(textCodeBlockBackground);
1122 1123 1124
	if (codeBackground) {
		collector.addRule(`.monaco-editor .suggest-widget code { background-color: ${codeBackground}; }`);
	}
1125
});