modesContentHover.ts 24.2 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.
 *--------------------------------------------------------------------------------------------*/

A
Alex Dima 已提交
6
import * as nls from 'vs/nls';
M
Michel Kaporin 已提交
7
import * as dom from 'vs/base/browser/dom';
A
Alex Dima 已提交
8 9
import { CancellationToken } from 'vs/base/common/cancellation';
import { Color, RGBA } from 'vs/base/common/color';
10
import { IMarkdownString, MarkdownString, isEmptyMarkdownString, markedStringsEquals } from 'vs/base/common/htmlContent';
11
import { IDisposable, toDisposable, DisposableStore, combinedDisposable, MutableDisposable } from 'vs/base/common/lifecycle';
A
Alex Dima 已提交
12 13 14
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { Position } from 'vs/editor/common/core/position';
import { IRange, Range } from 'vs/editor/common/core/range';
A
Alex Dima 已提交
15
import { ModelDecorationOptions } from 'vs/editor/common/model/textModel';
16
import { DocumentColorProvider, Hover as MarkdownHover, HoverProviderRegistry, IColor, TokenizationRegistry, CodeActionTriggerType } from 'vs/editor/common/modes';
A
Alex Dima 已提交
17 18
import { getColorPresentations } from 'vs/editor/contrib/colorPicker/color';
import { ColorDetector } from 'vs/editor/contrib/colorPicker/colorDetector';
19 20
import { ColorPickerModel } from 'vs/editor/contrib/colorPicker/colorPickerModel';
import { ColorPickerWidget } from 'vs/editor/contrib/colorPicker/colorPickerWidget';
A
Alex Dima 已提交
21 22 23 24
import { getHover } from 'vs/editor/contrib/hover/getHover';
import { HoverOperation, HoverStartMode, IHoverComputer } from 'vs/editor/contrib/hover/hoverOperation';
import { ContentHoverWidget } from 'vs/editor/contrib/hover/hoverWidgets';
import { MarkdownRenderer } from 'vs/editor/contrib/markdown/markdownRenderer';
25
import { IThemeService, registerThemingParticipant } from 'vs/platform/theme/common/themeService';
M
Matt Bierner 已提交
26
import { coalesce, isNonEmptyArray, asArray } from 'vs/base/common/arrays';
S
Sandeep Somavarapu 已提交
27
import { IMarker, IMarkerData, MarkerSeverity } from 'vs/platform/markers/common/markers';
B
Benjamin Pasero 已提交
28
import { basename } from 'vs/base/common/resources';
29
import { IMarkerDecorationsService } from 'vs/editor/common/services/markersDecorationService';
S
Sandeep Somavarapu 已提交
30 31
import { onUnexpectedError } from 'vs/base/common/errors';
import { IOpenerService, NullOpenerService } from 'vs/platform/opener/common/opener';
S
Sandeep Somavarapu 已提交
32 33
import { MarkerController, NextMarkerAction } from 'vs/editor/contrib/gotoError/gotoError';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
34
import { CancelablePromise, createCancelablePromise } from 'vs/base/common/async';
35 36
import { getCodeActions, CodeActionSet } from 'vs/editor/contrib/codeAction/codeAction';
import { QuickFixAction, QuickFixController } from 'vs/editor/contrib/codeAction/codeActionCommands';
37
import { CodeActionKind, CodeActionTrigger } from 'vs/editor/contrib/codeAction/types';
38
import { IModeService } from 'vs/editor/common/services/modeService';
M
Matt Bierner 已提交
39
import { IIdentifiedSingleEditOperation } from 'vs/editor/common/model';
A
Alex Dima 已提交
40
import { EditorOption } from 'vs/editor/common/config/editorOptions';
41
import { Constants } from 'vs/base/common/uint';
42
import { textLinkForeground } from 'vs/platform/theme/common/colorRegistry';
43
import { Progress } from 'vs/platform/progress/common/progress';
R
Rob Lourens 已提交
44
import { IContextKey } from 'vs/platform/contextkey/common/contextkey';
45

M
Michel Kaporin 已提交
46
const $ = dom.$;
E
Erich Gamma 已提交
47

48 49 50 51
class ColorHover {

	constructor(
		public readonly range: IRange,
J
Joao Moreno 已提交
52
		public readonly color: IColor,
53
		public readonly provider: DocumentColorProvider
54 55 56
	) { }
}

57 58 59 60 61 62 63 64 65
class MarkerHover {

	constructor(
		public readonly range: IRange,
		public readonly marker: IMarker,
	) { }
}

type HoverPart = MarkdownHover | ColorHover | MarkerHover;
E
Erich Gamma 已提交
66

67
class ModesContentComputer implements IHoverComputer<HoverPart[]> {
E
Erich Gamma 已提交
68

69
	private readonly _editor: ICodeEditor;
70
	private _result: HoverPart[];
M
Matt Bierner 已提交
71
	private _range?: Range;
E
Erich Gamma 已提交
72

73 74
	constructor(
		editor: ICodeEditor,
75
		private readonly _markerDecorationsService: IMarkerDecorationsService
76
	) {
E
Erich Gamma 已提交
77
		this._editor = editor;
A
Alex Dima 已提交
78
		this._result = [];
E
Erich Gamma 已提交
79 80
	}

J
Joao Moreno 已提交
81
	setRange(range: Range): void {
E
Erich Gamma 已提交
82 83 84 85
		this._range = range;
		this._result = [];
	}

J
Joao Moreno 已提交
86
	clearResult(): void {
E
Erich Gamma 已提交
87 88 89
		this._result = [];
	}

90
	computeAsync(token: CancellationToken): Promise<HoverPart[]> {
A
Alex Dima 已提交
91 92 93 94
		if (!this._editor.hasModel() || !this._range) {
			return Promise.resolve([]);
		}

J
Joao Moreno 已提交
95
		const model = this._editor.getModel();
E
Erich Gamma 已提交
96

A
Alex Dima 已提交
97
		if (!HoverProviderRegistry.has(model)) {
A
Alex Dima 已提交
98
			return Promise.resolve([]);
E
Erich Gamma 已提交
99 100
		}

101
		return getHover(model, new Position(
102 103
			this._range.startLineNumber,
			this._range.startColumn
104
		), token);
E
Erich Gamma 已提交
105 106
	}

107
	computeSync(): HoverPart[] {
A
Alex Dima 已提交
108 109 110 111
		if (!this._editor.hasModel() || !this._range) {
			return [];
		}

112
		const model = this._editor.getModel();
J
Joao Moreno 已提交
113
		const lineNumber = this._range.startLineNumber;
E
Erich Gamma 已提交
114 115 116

		if (lineNumber > this._editor.getModel().getLineCount()) {
			// Illegal line number => no results
J
Joao Moreno 已提交
117
			return [];
E
Erich Gamma 已提交
118 119
		}

120
		const colorDetector = ColorDetector.get(this._editor);
121
		const maxColumn = model.getLineMaxColumn(lineNumber);
J
Joao Moreno 已提交
122
		const lineDecorations = this._editor.getLineDecorations(lineNumber);
123
		let didFindColor = false;
J
Joao Moreno 已提交
124

125 126
		const hoverRange = this._range;
		const result = lineDecorations.map((d): HoverPart | null => {
J
Joao Moreno 已提交
127 128 129
			const startColumn = (d.range.startLineNumber === lineNumber) ? d.range.startColumn : 1;
			const endColumn = (d.range.endLineNumber === lineNumber) ? d.range.endColumn : maxColumn;

130
			if (startColumn > hoverRange.startColumn || hoverRange.endColumn > endColumn) {
J
Joao Moreno 已提交
131 132 133
				return null;
			}

134
			const range = new Range(hoverRange.startLineNumber, startColumn, hoverRange.startLineNumber, endColumn);
135 136 137 138 139
			const marker = this._markerDecorationsService.getMarker(model, d);
			if (marker) {
				return new MarkerHover(range, marker);
			}

140
			const colorData = colorDetector.getColorData(d.range.getStartPosition());
J
Joao Moreno 已提交
141

142
			if (!didFindColor && colorData) {
143
				didFindColor = true;
144

145 146
				const { color, range } = colorData.colorInfo;
				return new ColorHover(range, color, colorData.provider);
147
			} else {
148
				if (isEmptyMarkdownString(d.options.hoverMessage)) {
149
					return null;
E
Erich Gamma 已提交
150
				}
J
Joao Moreno 已提交
151

M
Matt Bierner 已提交
152
				const contents: IMarkdownString[] = d.options.hoverMessage ? asArray(d.options.hoverMessage) : [];
153 154
				return { contents, range };
			}
E
Erich Gamma 已提交
155
		});
J
Joao Moreno 已提交
156

157
		return coalesce(result);
E
Erich Gamma 已提交
158 159
	}

160
	onResult(result: HoverPart[], isFromSynchronousComputation: boolean): void {
E
Erich Gamma 已提交
161 162
		// Always put synchronous messages before asynchronous ones
		if (isFromSynchronousComputation) {
163
			this._result = result.concat(this._result.sort((a, b) => {
164
				if (a instanceof ColorHover) { // sort picker messages at to the top
165
					return -1;
166
				} else if (b instanceof ColorHover) {
167 168 169 170
					return 1;
				}
				return 0;
			}));
E
Erich Gamma 已提交
171 172 173 174 175
		} else {
			this._result = this._result.concat(result);
		}
	}

176
	getResult(): HoverPart[] {
E
Erich Gamma 已提交
177 178 179
		return this._result.slice(0);
	}

180
	getResultWithLoadingMessage(): HoverPart[] {
E
Erich Gamma 已提交
181 182 183
		return this._result.slice(0).concat([this._getLoadingMessage()]);
	}

184
	private _getLoadingMessage(): HoverPart {
E
Erich Gamma 已提交
185
		return {
M
Matt Bierner 已提交
186
			range: this._range,
187
			contents: [new MarkdownString().appendText(nls.localize('modesContentHover.loading', "Loading..."))]
E
Erich Gamma 已提交
188 189 190 191
		};
	}
}

192 193 194 195 196
const markerCodeActionTrigger: CodeActionTrigger = {
	type: CodeActionTriggerType.Manual,
	filter: { include: CodeActionKind.QuickFix }
};

A
Alex Dima 已提交
197
export class ModesContentHoverWidget extends ContentHoverWidget {
E
Erich Gamma 已提交
198

199
	static readonly ID = 'editor.contrib.modesContentHoverWidget';
J
Joao Moreno 已提交
200

201
	private _messages: HoverPart[];
A
Alex Dima 已提交
202
	private _lastRange: Range | null;
203 204
	private readonly _computer: ModesContentComputer;
	private readonly _hoverOperation: HoverOperation<HoverPart[]>;
205
	private _highlightDecorations: string[];
E
Erich Gamma 已提交
206
	private _isChangingDecorations: boolean;
I
isidor 已提交
207
	private _shouldFocus: boolean;
A
Alex Dima 已提交
208
	private _colorPicker: ColorPickerWidget | null;
209

210 211
	private _codeLink?: HTMLElement;

212
	private readonly renderDisposable = this._register(new MutableDisposable<IDisposable>());
E
Erich Gamma 已提交
213

A
Alex Dima 已提交
214 215
	constructor(
		editor: ICodeEditor,
R
Rob Lourens 已提交
216
		_hoverVisibleKey: IContextKey<boolean>,
217
		markerDecorationsService: IMarkerDecorationsService,
218
		keybindingService: IKeybindingService,
S
Sandeep Somavarapu 已提交
219
		private readonly _themeService: IThemeService,
220
		private readonly _modeService: IModeService,
221
		private readonly _openerService: IOpenerService = NullOpenerService,
A
Alex Dima 已提交
222
	) {
R
Rob Lourens 已提交
223
		super(ModesContentHoverWidget.ID, editor, _hoverVisibleKey, keybindingService);
E
Erich Gamma 已提交
224

A
Alex Dima 已提交
225 226
		this._messages = [];
		this._lastRange = null;
227
		this._computer = new ModesContentComputer(this._editor, markerDecorationsService);
E
Erich Gamma 已提交
228 229
		this._highlightDecorations = [];
		this._isChangingDecorations = false;
P
Peng Lyu 已提交
230 231
		this._shouldFocus = false;
		this._colorPicker = null;
J
Joao Moreno 已提交
232

A
Alex Dima 已提交
233
		this._hoverOperation = new HoverOperation(
E
Erich Gamma 已提交
234
			this._computer,
235
			result => this._withResult(result, true),
E
Erich Gamma 已提交
236
			null,
A
Alex Dima 已提交
237
			result => this._withResult(result, false),
A
Alex Dima 已提交
238
			this._editor.getOption(EditorOption.hover).delay
E
Erich Gamma 已提交
239
		);
M
Michel Kaporin 已提交
240

B
Benjamin Pasero 已提交
241
		this._register(dom.addStandardDisposableListener(this.getDomNode(), dom.EventType.FOCUS, () => {
M
Michel Kaporin 已提交
242
			if (this._colorPicker) {
243
				this.getDomNode().classList.add('colorpicker-hover');
M
Michel Kaporin 已提交
244 245
			}
		}));
B
Benjamin Pasero 已提交
246
		this._register(dom.addStandardDisposableListener(this.getDomNode(), dom.EventType.BLUR, () => {
247
			this.getDomNode().classList.remove('colorpicker-hover');
M
Michel Kaporin 已提交
248
		}));
B
Benjamin Pasero 已提交
249
		this._register(editor.onDidChangeConfiguration((e) => {
A
Alex Dima 已提交
250
			this._hoverOperation.setHoverTime(this._editor.getOption(EditorOption.hover).delay);
251
		}));
A
Alexandru Dima 已提交
252 253
		this._register(TokenizationRegistry.onDidChange((e) => {
			if (this.isVisible && this._lastRange && this._messages.length > 0) {
254
				this._hover.contentsDomNode.textContent = '';
A
Alexandru Dima 已提交
255 256 257
				this._renderMessages(this._lastRange, this._messages);
			}
		}));
E
Erich Gamma 已提交
258 259
	}

J
Joao Moreno 已提交
260
	dispose(): void {
261 262 263 264
		this._hoverOperation.cancel();
		super.dispose();
	}

J
Joao Moreno 已提交
265
	onModelDecorationsChanged(): void {
E
Erich Gamma 已提交
266 267 268
		if (this._isChangingDecorations) {
			return;
		}
J
Joao Moreno 已提交
269
		if (this.isVisible) {
E
Erich Gamma 已提交
270 271 272 273
			// The decorations have changed and the hover is visible,
			// we need to recompute the displayed text
			this._hoverOperation.cancel();
			this._computer.clearResult();
274 275

			if (!this._colorPicker) { // TODO@Michel ensure that displayed text for other decorations is computed even if color picker is in place
276
				this._hoverOperation.start(HoverStartMode.Delayed);
277
			}
E
Erich Gamma 已提交
278 279 280
		}
	}

281
	startShowingAt(range: Range, mode: HoverStartMode, focus: boolean): void {
J
Joao Moreno 已提交
282 283 284
		if (this._lastRange && this._lastRange.equalsRange(range)) {
			// We have to show the widget at the exact same range as before, so no work is needed
			return;
E
Erich Gamma 已提交
285 286 287 288
		}

		this._hoverOperation.cancel();

J
Joao Moreno 已提交
289
		if (this.isVisible) {
E
Erich Gamma 已提交
290 291 292
			// The range might have changed, but the hover is visible
			// Instead of hiding it completely, filter out messages that are still in the new range and
			// kick off a new computation
293
			if (!this._showAtPosition || this._showAtPosition.lineNumber !== range.startLineNumber) {
E
Erich Gamma 已提交
294 295
				this.hide();
			} else {
A
Alex Dima 已提交
296 297 298 299
				let filteredMessages: HoverPart[] = [];
				for (let i = 0, len = this._messages.length; i < len; i++) {
					const msg = this._messages[i];
					const rng = msg.range;
300
					if (rng && rng.startColumn <= range.startColumn && rng.endColumn >= range.endColumn) {
E
Erich Gamma 已提交
301 302 303 304
						filteredMessages.push(msg);
					}
				}
				if (filteredMessages.length > 0) {
305 306 307
					if (hoverContentsEquals(filteredMessages, this._messages)) {
						return;
					}
E
Erich Gamma 已提交
308 309 310 311 312 313 314 315 316
					this._renderMessages(range, filteredMessages);
				} else {
					this.hide();
				}
			}
		}

		this._lastRange = range;
		this._computer.setRange(range);
I
isidor 已提交
317
		this._shouldFocus = focus;
318
		this._hoverOperation.start(mode);
E
Erich Gamma 已提交
319 320
	}

J
Joao Moreno 已提交
321
	hide(): void {
E
Erich Gamma 已提交
322 323 324 325 326 327
		this._lastRange = null;
		this._hoverOperation.cancel();
		super.hide();
		this._isChangingDecorations = true;
		this._highlightDecorations = this._editor.deltaDecorations(this._highlightDecorations, []);
		this._isChangingDecorations = false;
328
		this.renderDisposable.clear();
329
		this._colorPicker = null;
E
Erich Gamma 已提交
330 331
	}

332 333 334 335 336 337 338
	isColorPickerVisible(): boolean {
		if (this._colorPicker) {
			return true;
		}
		return false;
	}

339
	private _withResult(result: HoverPart[], complete: boolean): void {
E
Erich Gamma 已提交
340 341
		this._messages = result;

A
Alex Dima 已提交
342
		if (this._lastRange && this._messages.length > 0) {
E
Erich Gamma 已提交
343
			this._renderMessages(this._lastRange, this._messages);
344
		} else if (complete) {
E
Erich Gamma 已提交
345 346 347 348
			this.hide();
		}
	}

349
	private _renderMessages(renderRange: Range, messages: HoverPart[]): void {
350 351
		this.renderDisposable.dispose();
		this._colorPicker = null;
E
Erich Gamma 已提交
352 353

		// update column from which to show
354
		let renderColumn = Constants.MAX_SAFE_SMALL_INTEGER;
A
Alex Dima 已提交
355
		let highlightRange: Range | null = messages[0].range ? Range.lift(messages[0].range) : null;
A
Alex Dima 已提交
356 357
		let fragment = document.createDocumentFragment();
		let isEmptyHoverContent = true;
E
Erich Gamma 已提交
358

359
		let containColorPicker = false;
360
		const markdownDisposeables = new DisposableStore();
361
		const markerMessages: MarkerHover[] = [];
E
Erich Gamma 已提交
362 363 364 365 366 367
		messages.forEach((msg) => {
			if (!msg.range) {
				return;
			}

			renderColumn = Math.min(renderColumn, msg.range.startColumn);
A
Alex Dima 已提交
368
			highlightRange = highlightRange ? Range.plusRange(highlightRange, msg.range) : Range.lift(msg.range);
E
Erich Gamma 已提交
369

370
			if (msg instanceof ColorHover) {
371 372
				containColorPicker = true;

J
Joao Moreno 已提交
373
				const { red, green, blue, alpha } = msg.color;
N
NeeEoo 已提交
374
				const rgba = new RGBA(Math.round(red * 255), Math.round(green * 255), Math.round(blue * 255), alpha);
J
Joao Moreno 已提交
375
				const color = new Color(rgba);
376

377 378 379 380
				if (!this._editor.hasModel()) {
					return;
				}

381 382
				const editorModel = this._editor.getModel();
				let range = new Range(msg.range.startLineNumber, msg.range.startColumn, msg.range.endLineNumber, msg.range.endColumn);
383
				let colorInfo = { range: msg.range, color: msg.color };
384

385 386
				// create blank olor picker model and widget first to ensure it's positioned correctly.
				const model = new ColorPickerModel(color, [], 0);
A
Alex Dima 已提交
387
				const widget = new ColorPickerWidget(fragment, model, this._editor.getOption(EditorOption.pixelRatio), this._themeService);
388

389
				getColorPresentations(editorModel, colorInfo, msg.provider, CancellationToken.None).then(colorPresentations => {
A
Alex Dima 已提交
390 391 392 393 394
					model.colorPresentations = colorPresentations || [];
					if (!this._editor.hasModel()) {
						// gone...
						return;
					}
395 396 397 398
					const originalText = this._editor.getModel().getValueInRange(msg.range);
					model.guessColorPresentation(color, originalText);

					const updateEditorModel = () => {
M
Matt Bierner 已提交
399 400
						let textEdits: IIdentifiedSingleEditOperation[];
						let newRange: Range;
401
						if (model.presentation.textEdit) {
M
Matt Bierner 已提交
402
							textEdits = [model.presentation.textEdit as IIdentifiedSingleEditOperation];
403 404 405 406 407 408 409 410 411 412 413 414
							newRange = new Range(
								model.presentation.textEdit.range.startLineNumber,
								model.presentation.textEdit.range.startColumn,
								model.presentation.textEdit.range.endLineNumber,
								model.presentation.textEdit.range.endColumn
							);
							newRange = newRange.setEndPosition(newRange.endLineNumber, newRange.startColumn + model.presentation.textEdit.text.length);
						} else {
							textEdits = [{ identifier: null, range, text: model.presentation.label, forceMoveMarkers: false }];
							newRange = range.setEndPosition(range.endLineNumber, range.startColumn + model.presentation.label.length);
						}

415
						this._editor.pushUndoStop();
A
Alex Dima 已提交
416
						this._editor.executeEdits('colorpicker', textEdits);
417 418

						if (model.presentation.additionalTextEdits) {
M
Matt Bierner 已提交
419
							textEdits = [...model.presentation.additionalTextEdits as IIdentifiedSingleEditOperation[]];
A
Alex Dima 已提交
420
							this._editor.executeEdits('colorpicker', textEdits);
421
							this.hide();
E
Erich Gamma 已提交
422
						}
423
						this._editor.pushUndoStop();
424 425 426 427
						range = newRange;
					};

					const updateColorPresentations = (color: Color) => {
428
						return getColorPresentations(editorModel, {
429 430 431 432 433 434 435
							range: range,
							color: {
								red: color.rgba.r / 255,
								green: color.rgba.g / 255,
								blue: color.rgba.b / 255,
								alpha: color.rgba.a
							}
436
						}, msg.provider, CancellationToken.None).then((colorPresentations) => {
A
Alex Dima 已提交
437
							model.colorPresentations = colorPresentations || [];
438 439 440 441 442
						});
					};

					const colorListener = model.onColorFlushed((color: Color) => {
						updateColorPresentations(color).then(updateEditorModel);
J
Joao Moreno 已提交
443
					});
444
					const colorChangeListener = model.onDidChangeColor(updateColorPresentations);
445

446
					this._colorPicker = widget;
A
Alex Dima 已提交
447
					this.showAt(range.getStartPosition(), range, this._shouldFocus);
448 449
					this.updateContents(fragment);
					this._colorPicker.layout();
450

451
					this.renderDisposable.value = combinedDisposable(colorListener, colorChangeListener, widget, markdownDisposeables);
J
Joao Moreno 已提交
452
				});
453 454
			} else {
				if (msg instanceof MarkerHover) {
455
					markerMessages.push(msg);
456 457 458 459 460
					isEmptyHoverContent = false;
				} else {
					msg.contents
						.filter(contents => !isEmptyMarkdownString(contents))
						.forEach(contents => {
461 462
							const markdownHoverElement = $('div.hover-row.markdown-hover');
							const hoverContentsElement = dom.append(markdownHoverElement, $('div.hover-contents'));
463 464
							const renderer = markdownDisposeables.add(new MarkdownRenderer(this._editor, this._modeService, this._openerService));
							markdownDisposeables.add(renderer.onDidRenderCodeBlock(() => {
S
Sandeep Somavarapu 已提交
465
								hoverContentsElement.className = 'hover-contents code-hover-contents';
466
								this._hover.onContentsChanged();
S
Sandeep Somavarapu 已提交
467
							}));
468
							const renderedContents = markdownDisposeables.add(renderer.render(contents));
469 470
							hoverContentsElement.appendChild(renderedContents.element);
							fragment.appendChild(markdownHoverElement);
471 472 473
							isEmptyHoverContent = false;
						});
				}
J
Joao Moreno 已提交
474
			}
E
Erich Gamma 已提交
475 476
		});

S
Sandeep Somavarapu 已提交
477 478
		if (markerMessages.length) {
			markerMessages.forEach(msg => fragment.appendChild(this.renderMarkerHover(msg)));
S
Sandeep Somavarapu 已提交
479 480
			const markerHoverForStatusbar = markerMessages.length === 1 ? markerMessages[0] : markerMessages.sort((a, b) => MarkerSeverity.compare(a.marker.severity, b.marker.severity))[0];
			fragment.appendChild(this.renderMarkerStatusbar(markerHoverForStatusbar));
S
Sandeep Somavarapu 已提交
481
		}
482

E
Erich Gamma 已提交
483 484
		// show

485
		if (!containColorPicker && !isEmptyHoverContent) {
A
Alex Dima 已提交
486
			this.showAt(new Position(renderRange.startLineNumber, renderColumn), highlightRange, this._shouldFocus);
487
			this.updateContents(fragment);
M
Michel Kaporin 已提交
488
		}
J
Joao Moreno 已提交
489

E
Erich Gamma 已提交
490
		this._isChangingDecorations = true;
A
Alex Dima 已提交
491
		this._highlightDecorations = this._editor.deltaDecorations(this._highlightDecorations, highlightRange ? [{
E
Erich Gamma 已提交
492
			range: highlightRange,
493
			options: ModesContentHoverWidget._DECORATION_OPTIONS
A
Alex Dima 已提交
494
		}] : []);
E
Erich Gamma 已提交
495 496
		this._isChangingDecorations = false;
	}
497

498
	private renderMarkerHover(markerHover: MarkerHover): HTMLElement {
499 500
		const hoverElement = $('div.hover-row');
		const markerElement = dom.append(hoverElement, $('div.marker.hover-contents'));
501 502
		const { source, message, code, relatedInformation } = markerHover.marker;

S
Sandeep Somavarapu 已提交
503
		this._editor.applyFontInfo(markerElement);
504
		const messageElement = dom.append(markerElement, $('span'));
505
		messageElement.style.whiteSpace = 'pre-wrap';
S
Sandeep Somavarapu 已提交
506
		messageElement.innerText = message;
507 508

		if (source || code) {
S
Sandeep Somavarapu 已提交
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
			// Code has link
			if (code && typeof code !== 'string') {
				const sourceAndCodeElement = $('span');
				if (source) {
					const sourceElement = dom.append(sourceAndCodeElement, $('span'));
					sourceElement.innerText = source;
				}
				this._codeLink = dom.append(sourceAndCodeElement, $('a.code-link'));
				this._codeLink.setAttribute('href', code.target.toString());

				this._codeLink.onclick = (e) => {
					this._openerService.open(code.target);
					e.preventDefault();
					e.stopPropagation();
				};

				const codeElement = dom.append(this._codeLink, $('span'));
				codeElement.innerText = code.value;

				const detailsElement = dom.append(markerElement, sourceAndCodeElement);
				detailsElement.style.opacity = '0.6';
				detailsElement.style.paddingLeft = '6px';
			} else {
532 533 534 535 536
				const detailsElement = dom.append(markerElement, $('span'));
				detailsElement.style.opacity = '0.6';
				detailsElement.style.paddingLeft = '6px';
				detailsElement.innerText = source && code ? `${source}(${code})` : source ? source : `(${code})`;
			}
537 538 539 540
		}

		if (isNonEmptyArray(relatedInformation)) {
			for (const { message, resource, startLineNumber, startColumn } of relatedInformation) {
541 542 543
				const relatedInfoContainer = dom.append(markerElement, $('div'));
				relatedInfoContainer.style.marginTop = '8px';
				const a = dom.append(relatedInfoContainer, $('a'));
S
Sandeep Somavarapu 已提交
544
				a.innerText = `${basename(resource)}(${startLineNumber}, ${startColumn}): `;
S
Sandeep Somavarapu 已提交
545 546 547 548
				a.style.cursor = 'pointer';
				a.onclick = e => {
					e.stopPropagation();
					e.preventDefault();
S
Sandeep Somavarapu 已提交
549
					if (this._openerService) {
550
						this._openerService.open(resource.with({ fragment: `${startLineNumber},${startColumn}` }), { fromUserGesture: true }).catch(onUnexpectedError);
S
Sandeep Somavarapu 已提交
551
					}
S
Sandeep Somavarapu 已提交
552
				};
553
				const messageElement = dom.append<HTMLAnchorElement>(relatedInfoContainer, $('span'));
S
Sandeep Somavarapu 已提交
554 555
				messageElement.innerText = message;
				this._editor.applyFontInfo(messageElement);
556 557
			}
		}
558

S
Sandeep Somavarapu 已提交
559 560 561 562 563
		return hoverElement;
	}

	private renderMarkerStatusbar(markerHover: MarkerHover): HTMLElement {
		const hoverElement = $('div.hover-row.status-bar');
564
		const disposables = new DisposableStore();
565
		const actionsElement = dom.append(hoverElement, $('div.actions'));
S
Sandeep Somavarapu 已提交
566
		if (markerHover.marker.severity === MarkerSeverity.Error || markerHover.marker.severity === MarkerSeverity.Warning || markerHover.marker.severity === MarkerSeverity.Info) {
567
			disposables.add(this._renderAction(actionsElement, {
S
Sandeep Somavarapu 已提交
568
				label: nls.localize('peek problem', "Peek Problem"),
S
Sandeep Somavarapu 已提交
569 570 571
				commandId: NextMarkerAction.ID,
				run: () => {
					this.hide();
572
					MarkerController.get(this._editor).showAtMarker(markerHover.marker);
S
Sandeep Somavarapu 已提交
573 574 575 576
					this._editor.focus();
				}
			}));
		}
577

M
Matt Bierner 已提交
578
		const quickfixPlaceholderElement = dom.append(actionsElement, $('div'));
579 580 581
		quickfixPlaceholderElement.style.opacity = '0';
		quickfixPlaceholderElement.style.transition = 'opacity 0.2s';
		setTimeout(() => quickfixPlaceholderElement.style.opacity = '1', 200);
M
Matt Bierner 已提交
582 583 584
		quickfixPlaceholderElement.textContent = nls.localize('checkingForQuickFixes', "Checking for quick fixes...");
		disposables.add(toDisposable(() => quickfixPlaceholderElement.remove()));

585 586 587
		const codeActionsPromise = this.getCodeActions(markerHover.marker);
		disposables.add(toDisposable(() => codeActionsPromise.cancel()));
		codeActionsPromise.then(actions => {
588 589 590
			quickfixPlaceholderElement.style.transition = '';
			quickfixPlaceholderElement.style.opacity = '1';

M
Matt Bierner 已提交
591
			if (!actions.validActions.length) {
592
				actions.dispose();
M
Matt Bierner 已提交
593
				quickfixPlaceholderElement.textContent = nls.localize('noQuickFixes', "No quick fixes available");
594 595
				return;
			}
M
Matt Bierner 已提交
596
			quickfixPlaceholderElement.remove();
597

M
Matt Bierner 已提交
598
			let showing = false;
599 600 601 602 603 604
			disposables.add(toDisposable(() => {
				if (!showing) {
					actions.dispose();
				}
			}));

605
			disposables.add(this._renderAction(actionsElement, {
S
Sandeep Somavarapu 已提交
606
				label: nls.localize('quick fixes', "Quick Fix..."),
607 608 609 610 611
				commandId: QuickFixAction.Id,
				run: (target) => {
					showing = true;
					const controller = QuickFixController.get(this._editor);
					const elementPosition = dom.getDomNodePagePosition(target);
612 613 614
					// Hide the hover pre-emptively, otherwise the editor can close the code actions
					// context menu as well when using keyboard navigation
					this.hide();
615
					controller.showCodeActions(markerCodeActionTrigger, actions, {
616 617 618 619 620 621 622
						x: elementPosition.left + 6,
						y: elementPosition.top + elementPosition.height + 6
					});
				}
			}));
		});

623
		this.renderDisposable.value = disposables;
624 625 626
		return hoverElement;
	}

627
	private getCodeActions(marker: IMarker): CancelablePromise<CodeActionSet> {
628 629
		return createCancelablePromise(cancellationToken => {
			return getCodeActions(
630 631
				this._editor.getModel()!,
				new Range(marker.startLineNumber, marker.startColumn, marker.endLineNumber, marker.endColumn),
632
				markerCodeActionTrigger,
633
				Progress.None,
634
				cancellationToken);
635 636 637
		});
	}

638
	private static readonly _DECORATION_OPTIONS = ModelDecorationOptions.register({
639 640 641
		className: 'hoverHighlight'
	});
}
642

643
function hoverContentsEquals(first: HoverPart[], second: HoverPart[]): boolean {
644 645 646 647
	if ((!first && second) || (first && !second) || first.length !== second.length) {
		return false;
	}
	for (let i = 0; i < first.length; i++) {
648 649
		const firstElement = first[i];
		const secondElement = second[i];
650 651 652 653
		if (firstElement instanceof MarkerHover && secondElement instanceof MarkerHover) {
			return IMarkerData.makeKey(firstElement.marker) === IMarkerData.makeKey(secondElement.marker);
		}
		if (firstElement instanceof ColorHover || secondElement instanceof ColorHover) {
654 655
			return false;
		}
656
		if (firstElement instanceof MarkerHover || secondElement instanceof MarkerHover) {
657 658 659
			return false;
		}
		if (!markedStringsEquals(firstElement.contents, secondElement.contents)) {
660 661 662 663 664
			return false;
		}
	}
	return true;
}
665 666 667 668

registerThemingParticipant((theme, collector) => {
	const linkFg = theme.getColor(textLinkForeground);
	if (linkFg) {
669
		collector.addRule(`.monaco-hover .hover-contents a.code-link span:hover { color: ${linkFg}; }`);
670 671
	}
});