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

'use strict';

import 'vs/css!./gotoError';
A
Alex Dima 已提交
9
import * as nls from 'vs/nls';
10 11 12
import { Emitter } from 'vs/base/common/event';
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
13
import Severity from 'vs/base/common/severity';
A
Alex Dima 已提交
14 15
import URI from 'vs/base/common/uri';
import * as dom from 'vs/base/browser/dom';
16 17 18
import { RawContextKey, IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { IMarker, IMarkerService } from 'vs/platform/markers/common/markers';
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
A
Alex Dima 已提交
19
import { Position } from 'vs/editor/common/core/position';
20
import { Range } from 'vs/editor/common/core/range';
A
Alex Dima 已提交
21
import * as editorCommon from 'vs/editor/common/editorCommon';
22 23 24 25
import { editorAction, ServicesAccessor, IActionOptions, EditorAction, EditorCommand, CommonEditorRegistry } from 'vs/editor/common/editorCommonExtensions';
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
import { editorContribution } from 'vs/editor/browser/editorBrowserExtensions';
import { ZoneWidget } from 'vs/editor/contrib/zoneWidget/browser/zoneWidget';
26
import { registerColor, oneOf } from 'vs/platform/theme/common/colorRegistry';
27 28
import { IThemeService, ITheme } from 'vs/platform/theme/common/themeService';
import { Color } from 'vs/base/common/color';
29
import { EditorContextKeys } from 'vs/editor/common/editorContextKeys';
30 31
import { getAccessibilitySupport } from 'vs/base/browser/browser';
import { AccessibilitySupport } from 'vs/base/common/platform';
B
Benjamin Pasero 已提交
32
import { editorErrorForeground, editorErrorBorder, editorWarningForeground, editorWarningBorder } from 'vs/editor/common/view/editorColorRegistry';
A
Alex Dima 已提交
33

E
Erich Gamma 已提交
34 35
class MarkerModel {

A
Alex Dima 已提交
36
	private _editor: ICodeEditor;
E
Erich Gamma 已提交
37 38
	private _markers: IMarker[];
	private _nextIdx: number;
A
Alex Dima 已提交
39
	private _toUnbind: IDisposable[];
E
Erich Gamma 已提交
40 41 42 43
	private _ignoreSelectionChange: boolean;
	private _onCurrentMarkerChanged: Emitter<IMarker>;
	private _onMarkerSetChanged: Emitter<MarkerModel>;

A
Alex Dima 已提交
44
	constructor(editor: ICodeEditor, markers: IMarker[]) {
E
Erich Gamma 已提交
45 46 47 48 49 50 51 52 53 54
		this._editor = editor;
		this._markers = null;
		this._nextIdx = -1;
		this._toUnbind = [];
		this._ignoreSelectionChange = false;
		this._onCurrentMarkerChanged = new Emitter<IMarker>();
		this._onMarkerSetChanged = new Emitter<MarkerModel>();
		this.setMarkers(markers);

		// listen on editor
A
Alex Dima 已提交
55
		this._toUnbind.push(this._editor.onDidDispose(() => this.dispose()));
A
Alex Dima 已提交
56
		this._toUnbind.push(this._editor.onDidChangeCursorPosition(() => {
E
Erich Gamma 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
			if (!this._ignoreSelectionChange) {
				this._nextIdx = -1;
			}
		}));
	}

	public get onCurrentMarkerChanged() {
		return this._onCurrentMarkerChanged.event;
	}

	public get onMarkerSetChanged() {
		return this._onMarkerSetChanged.event;
	}

	public setMarkers(markers: IMarker[]): void {
		// assign
		this._markers = markers || [];

		// sort markers
76
		this._markers.sort((left, right) => Severity.compare(left.severity, right.severity) || Range.compareRangesUsingStarts(left, right));
E
Erich Gamma 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90

		this._nextIdx = -1;
		this._onMarkerSetChanged.fire(this);
	}

	public withoutWatchingEditorPosition(callback: () => void): void {
		this._ignoreSelectionChange = true;
		try {
			callback();
		} finally {
			this._ignoreSelectionChange = false;
		}
	}

J
Johannes Rieken 已提交
91 92 93 94
	private _initIdx(fwd: boolean): void {
		let found = false;
		const position = this._editor.getPosition();
		for (let i = 0; i < this._markers.length; i++) {
J
Johannes Rieken 已提交
95 96 97 98 99 100 101 102 103 104
			let range = Range.lift(this._markers[i]);

			if (range.isEmpty()) {
				const word = this._editor.getModel().getWordAtPosition(range.getStartPosition());
				if (word) {
					range = new Range(range.startLineNumber, word.startColumn, range.startLineNumber, word.endColumn);
				}
			}

			if (range.containsPosition(position) || position.isBeforeOrEqual(range.getStartPosition())) {
E
Erich Gamma 已提交
105 106
				this._nextIdx = i + (fwd ? 0 : -1);
				found = true;
J
Johannes Rieken 已提交
107
				break;
E
Erich Gamma 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
			}
		}
		if (!found) {
			// after the last change
			this._nextIdx = fwd ? 0 : this._markers.length - 1;
		}
		if (this._nextIdx < 0) {
			this._nextIdx = this._markers.length - 1;
		}
	}

	private move(fwd: boolean): void {
		if (!this.canNavigate()) {
			this._onCurrentMarkerChanged.fire(undefined);
			return;
		}

		if (this._nextIdx === -1) {
J
Johannes Rieken 已提交
126
			this._initIdx(fwd);
E
Erich Gamma 已提交
127 128 129 130 131 132 133 134 135 136 137 138

		} else if (fwd) {
			this._nextIdx += 1;
			if (this._nextIdx >= this._markers.length) {
				this._nextIdx = 0;
			}
		} else {
			this._nextIdx -= 1;
			if (this._nextIdx < 0) {
				this._nextIdx = this._markers.length - 1;
			}
		}
J
Johannes Rieken 已提交
139
		const marker = this._markers[this._nextIdx];
E
Erich Gamma 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
		this._onCurrentMarkerChanged.fire(marker);
	}

	public canNavigate(): boolean {
		return this._markers.length > 0;
	}

	public next(): void {
		this.move(true);
	}

	public previous(): void {
		this.move(false);
	}

A
Alex Dima 已提交
155
	public findMarkerAtPosition(pos: Position): IMarker {
156 157 158
		for (const marker of this._markers) {
			if (Range.containsPosition(marker, pos)) {
				return marker;
E
Erich Gamma 已提交
159 160
			}
		}
161
		return undefined;
E
Erich Gamma 已提交
162 163
	}

J
Johannes Rieken 已提交
164 165 166 167 168 169 170 171
	public get total() {
		return this._markers.length;
	}

	public indexOf(marker: IMarker): number {
		return 1 + this._markers.indexOf(marker);
	}

E
Erich Gamma 已提交
172 173 174 175 176 177 178
	public reveal(): void {

		if (this._nextIdx === -1) {
			return;
		}

		this.withoutWatchingEditorPosition(() => {
J
Johannes Rieken 已提交
179
			const pos = new Position(this._markers[this._nextIdx].startLineNumber, this._markers[this._nextIdx].startColumn);
E
Erich Gamma 已提交
180 181 182 183 184 185
			this._editor.setPosition(pos);
			this._editor.revealPositionInCenter(pos);
		});
	}

	public dispose(): void {
A
Alex Dima 已提交
186
		this._toUnbind = dispose(this._toUnbind);
E
Erich Gamma 已提交
187 188 189
	}
}

190 191 192
class MessageWidget {

	domNode: HTMLDivElement;
193
	lines: number = 0;
194 195 196 197 198 199 200 201 202

	constructor(container: HTMLElement) {
		this.domNode = document.createElement('div');
		this.domNode.className = 'block descriptioncontainer';
		this.domNode.setAttribute('aria-live', 'assertive');
		this.domNode.setAttribute('role', 'alert');
		container.appendChild(this.domNode);
	}

203
	update({ source, message }: IMarker): void {
204
		this.lines = 1;
205 206
		if (source) {
			const indent = new Array(source.length + 3 + 1).join(' ');
207 208
			message = `[${source}] ` + message.replace(/\r\n|\r|\n/g, () => {
				this.lines += 1;
209 210 211 212 213 214 215
				return '\n' + indent;
			});
		}
		this.domNode.innerText = message;
	}
}

216
class MarkerNavigationWidget extends ZoneWidget {
E
Erich Gamma 已提交
217

218
	private _parentContainer: HTMLElement;
219
	private _container: HTMLElement;
220
	private _title: HTMLElement;
221
	private _message: MessageWidget;
A
Alex Dima 已提交
222
	private _callOnDispose: IDisposable[] = [];
223 224
	private _severity: Severity;
	private _backgroundColor: Color;
E
Erich Gamma 已提交
225

226
	constructor(editor: ICodeEditor, private _model: MarkerModel, private _themeService: IThemeService) {
227
		super(editor, { showArrow: true, showFrame: true, isAccessible: true });
228 229 230 231 232 233
		this._severity = Severity.Warning;
		this._backgroundColor = Color.white;

		this._applyTheme(_themeService.getTheme());
		this._callOnDispose.push(_themeService.onThemeChange(this._applyTheme.bind(this)));

E
Erich Gamma 已提交
234 235 236 237
		this.create();
		this._wireModelAndView();
	}

238 239 240 241 242 243 244 245 246
	private _applyTheme(theme: ITheme) {
		this._backgroundColor = theme.getColor(editorMarkerNavigationBackground);
		let frameColor = theme.getColor(this._severity === Severity.Error ? editorMarkerNavigationError : editorMarkerNavigationWarning);
		this.style({
			arrowColor: frameColor,
			frameColor: frameColor
		}); // style() will trigger _applyStyles
	}

247
	protected _applyStyles(): void {
248 249 250 251 252 253
		if (this._parentContainer) {
			this._parentContainer.style.backgroundColor = this._backgroundColor.toString();
		}
		super._applyStyles();
	}

254 255 256 257 258 259 260 261 262
	dispose(): void {
		this._callOnDispose = dispose(this._callOnDispose);
		super.dispose();
	}

	focus(): void {
		this._parentContainer.focus();
	}

263
	protected _fillContainer(container: HTMLElement): void {
264 265 266 267
		this._parentContainer = container;
		dom.addClass(container, 'marker-widget');
		this._parentContainer.tabIndex = 0;
		this._parentContainer.setAttribute('role', 'tooltip');
268

269 270
		this._container = document.createElement('div');
		container.appendChild(this._container);
271

272 273 274 275
		this._title = document.createElement('div');
		this._title.className = 'block title';
		this._container.appendChild(this._title);

276 277
		this._message = new MessageWidget(this._container);
		this.editor.applyFontInfo(this._message.domNode);
278 279
	}

A
Alex Dima 已提交
280
	public show(where: Position, heightInLines: number): void {
281
		super.show(where, heightInLines);
282 283 284
		if (getAccessibilitySupport() !== AccessibilitySupport.Disabled) {
			this.focus();
		}
E
Erich Gamma 已提交
285 286 287
	}

	private _wireModelAndView(): void {
288
		// listen to events
E
Erich Gamma 已提交
289
		this._model.onCurrentMarkerChanged(this.showAtMarker, this, this._callOnDispose);
290
		this._model.onMarkerSetChanged(this._onMarkersChanged, this, this._callOnDispose);
E
Erich Gamma 已提交
291 292 293 294 295 296 297 298
	}

	public showAtMarker(marker: IMarker): void {

		if (!marker) {
			return;
		}

299 300 301 302
		// update:
		// * title
		// * message
		this._container.classList.remove('stale');
303 304
		this._title.innerHTML = nls.localize('title.wo_source', "({0}/{1})", this._model.indexOf(marker), this._model.total);
		this._message.update(marker);
305

306 307
		this._model.withoutWatchingEditorPosition(() => {
			// update frame color (only applied on 'show')
308 309
			this._severity = marker.severity;
			this._applyTheme(this._themeService.getTheme());
310

A
Alex Dima 已提交
311
			this.show(new Position(marker.startLineNumber, marker.startColumn), this.computeRequiredHeight());
312
		});
313 314
	}

315 316 317
	private _onMarkersChanged(): void {
		const marker = this._model.findMarkerAtPosition(this.position);
		if (marker) {
318 319 320 321
			this._container.classList.remove('stale');
			this._message.update(marker);
		} else {
			this._container.classList.add('stale');
322
		}
323
		this._relayout();
324 325
	}

326 327 328 329 330
	protected _relayout(): void {
		super._relayout(this.computeRequiredHeight());
	}

	private computeRequiredHeight() {
331
		return 1 + this._message.lines;
E
Erich Gamma 已提交
332 333 334
	}
}

A
Alex Dima 已提交
335
class MarkerNavigationAction extends EditorAction {
E
Erich Gamma 已提交
336 337 338

	private _isNext: boolean;

339
	constructor(next: boolean, opts: IActionOptions) {
340
		super(opts);
E
Erich Gamma 已提交
341 342 343
		this._isNext = next;
	}

344
	public run(accessor: ServicesAccessor, editor: editorCommon.ICommonCodeEditor): void {
A
Alex Dima 已提交
345 346
		const telemetryService = accessor.get(ITelemetryService);

J
Johannes Rieken 已提交
347
		const controller = MarkerController.get(editor);
348 349 350 351 352
		if (!controller) {
			return;
		}

		let model = controller.getOrCreateModel();
353
		telemetryService.publicLog('zoneWidgetShown', { mode: 'go to error', ...editor.getTelemetryData() });
E
Erich Gamma 已提交
354 355 356 357 358 359 360 361 362 363 364
		if (model) {
			if (this._isNext) {
				model.next();
			} else {
				model.previous();
			}
			model.reveal();
		}
	}
}

365
@editorContribution
A
Alex Dima 已提交
366
class MarkerController implements editorCommon.IEditorContribution {
E
Erich Gamma 已提交
367

368
	private static ID = 'editor.contrib.markerController';
E
Erich Gamma 已提交
369

A
Alex Dima 已提交
370 371
	public static get(editor: editorCommon.ICommonCodeEditor): MarkerController {
		return editor.getContribution<MarkerController>(MarkerController.ID);
E
Erich Gamma 已提交
372 373
	}

374
	private _editor: ICodeEditor;
E
Erich Gamma 已提交
375 376
	private _model: MarkerModel;
	private _zone: MarkerNavigationWidget;
A
Alex Dima 已提交
377
	private _callOnClose: IDisposable[] = [];
A
Alex Dima 已提交
378
	private _markersNavigationVisible: IContextKey<boolean>;
E
Erich Gamma 已提交
379

380
	constructor(
381 382
		editor: ICodeEditor,
		@IMarkerService private _markerService: IMarkerService,
383
		@IContextKeyService private _contextKeyService: IContextKeyService,
384
		@IThemeService private _themeService: IThemeService
385 386
	) {
		this._editor = editor;
387
		this._markersNavigationVisible = CONTEXT_MARKERS_NAVIGATION_VISIBLE.bindTo(this._contextKeyService);
E
Erich Gamma 已提交
388 389 390 391 392 393 394 395 396 397 398 399
	}

	public getId(): string {
		return MarkerController.ID;
	}

	public dispose(): void {
		this._cleanUp();
	}

	private _cleanUp(): void {
		this._markersNavigationVisible.reset();
J
Joao Moreno 已提交
400
		this._callOnClose = dispose(this._callOnClose);
401
		this._zone = null;
E
Erich Gamma 已提交
402 403 404 405 406 407 408 409 410
		this._model = null;
	}

	public getOrCreateModel(): MarkerModel {

		if (this._model) {
			return this._model;
		}

J
Johannes Rieken 已提交
411
		const markers = this._getMarkers();
412
		this._model = new MarkerModel(this._editor, markers);
413
		this._zone = new MarkerNavigationWidget(this._editor, this._model, this._themeService);
E
Erich Gamma 已提交
414 415 416
		this._markersNavigationVisible.set(true);

		this._callOnClose.push(this._model);
417
		this._callOnClose.push(this._zone);
E
Erich Gamma 已提交
418

419
		this._callOnClose.push(this._editor.onDidChangeModel(() => this._cleanUp()));
E
Erich Gamma 已提交
420
		this._model.onCurrentMarkerChanged(marker => !marker && this._cleanUp(), undefined, this._callOnClose);
421 422 423
		this._markerService.onMarkerChanged(this._onMarkerChanged, this, this._callOnClose);
		return this._model;
	}
E
Erich Gamma 已提交
424 425

	public closeMarkersNavigation(): void {
426 427
		this._cleanUp();
		this._editor.focus();
E
Erich Gamma 已提交
428 429 430
	}

	private _onMarkerChanged(changedResources: URI[]): void {
431
		if (!changedResources.some(r => this._editor.getModel().uri.toString() === r.toString())) {
E
Erich Gamma 已提交
432 433 434 435 436 437
			return;
		}
		this._model.setMarkers(this._getMarkers());
	}

	private _getMarkers(): IMarker[] {
J
Johannes Rieken 已提交
438
		return this._markerService.read({ resource: this._editor.getModel().uri });
E
Erich Gamma 已提交
439 440 441
	}
}

A
Alex Dima 已提交
442
@editorAction
E
Erich Gamma 已提交
443
class NextMarkerAction extends MarkerNavigationAction {
A
Alex Dima 已提交
444
	constructor() {
445 446 447 448
		super(true, {
			id: 'editor.action.marker.next',
			label: nls.localize('markerAction.next.label', "Go to Next Error or Warning"),
			alias: 'Go to Next Error or Warning',
449
			precondition: EditorContextKeys.writable,
450
			kbOpts: {
451
				kbExpr: EditorContextKeys.focus,
452 453 454
				primary: KeyCode.F8
			}
		});
E
Erich Gamma 已提交
455 456 457
	}
}

A
Alex Dima 已提交
458
@editorAction
E
Erich Gamma 已提交
459
class PrevMarkerAction extends MarkerNavigationAction {
A
Alex Dima 已提交
460
	constructor() {
461 462 463 464
		super(false, {
			id: 'editor.action.marker.prev',
			label: nls.localize('markerAction.previous.label', "Go to Previous Error or Warning"),
			alias: 'Go to Previous Error or Warning',
465
			precondition: EditorContextKeys.writable,
466
			kbOpts: {
467
				kbExpr: EditorContextKeys.focus,
468 469 470
				primary: KeyMod.Shift | KeyCode.F8
			}
		});
E
Erich Gamma 已提交
471 472 473
	}
}

J
Johannes Rieken 已提交
474
const CONTEXT_MARKERS_NAVIGATION_VISIBLE = new RawContextKey<boolean>('markersNavigationVisible', false);
A
Alex Dima 已提交
475

A
Alex Dima 已提交
476
const MarkerCommand = EditorCommand.bindToContribution<MarkerController>(MarkerController.get);
E
Erich Gamma 已提交
477

478
CommonEditorRegistry.registerEditorCommand(new MarkerCommand({
479 480 481 482 483
	id: 'closeMarkersNavigation',
	precondition: CONTEXT_MARKERS_NAVIGATION_VISIBLE,
	handler: x => x.closeMarkersNavigation(),
	kbOpts: {
		weight: CommonEditorRegistry.commandWeight(50),
484
		kbExpr: EditorContextKeys.focus,
A
Alex Dima 已提交
485 486 487
		primary: KeyCode.Escape,
		secondary: [KeyMod.Shift | KeyCode.Escape]
	}
488
}));
489 490 491

// theming

492 493 494 495 496
let errorDefault = oneOf(editorErrorForeground, editorErrorBorder);
let warningDefault = oneOf(editorWarningForeground, editorWarningBorder);

export const editorMarkerNavigationError = registerColor('editorMarkerNavigationError.background', { dark: errorDefault, light: errorDefault, hc: errorDefault }, nls.localize('editorMarkerNavigationError', 'Editor marker navigation widget error color.'));
export const editorMarkerNavigationWarning = registerColor('editorMarkerNavigationWarning.background', { dark: warningDefault, light: warningDefault, hc: warningDefault }, nls.localize('editorMarkerNavigationWarning', 'Editor marker navigation widget warning color.'));
497
export const editorMarkerNavigationBackground = registerColor('editorMarkerNavigation.background', { dark: '#2D2D30', light: Color.white, hc: '#0C141F' }, nls.localize('editorMarkerNavigationBackground', 'Editor marker navigation widget background.'));