outlinePane.ts 23.8 KB
Newer Older
J
Johannes Rieken 已提交
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.
 *--------------------------------------------------------------------------------------------*/

J
Johannes Rieken 已提交
6
import * as dom from 'vs/base/browser/dom';
J
Johannes Rieken 已提交
7
import { Separator } from 'vs/base/browser/ui/actionbar/actionbar';
8
import { ProgressBar } from 'vs/base/browser/ui/progressbar/progressbar';
J
Johannes Rieken 已提交
9
import { Action, IAction, RadioGroup } from 'vs/base/common/actions';
10
import { createCancelablePromise, TimeoutTimer } from 'vs/base/common/async';
11
import { isPromiseCanceledError } from 'vs/base/common/errors';
J
Johannes Rieken 已提交
12
import { Emitter } from 'vs/base/common/event';
J
Johannes Rieken 已提交
13
import { defaultGenerator } from 'vs/base/common/idGenerator';
J
Johannes Rieken 已提交
14
import { dispose, IDisposable, toDisposable, DisposableStore, MutableDisposable } from 'vs/base/common/lifecycle';
J
Johannes Rieken 已提交
15 16
import { LRUCache } from 'vs/base/common/map';
import { escape } from 'vs/base/common/strings';
17
import 'vs/css!./outlinePane';
J
Johannes Rieken 已提交
18
import { ICodeEditor, isCodeEditor, isDiffEditor } from 'vs/editor/browser/editorBrowser';
J
Johannes Rieken 已提交
19
import { Range } from 'vs/editor/common/core/range';
20
import { Selection } from 'vs/editor/common/core/selection';
J
Johannes Rieken 已提交
21
import { ITextModel } from 'vs/editor/common/model';
22
import { IModelContentChangedEvent } from 'vs/editor/common/model/textModelEvents';
J
Johannes Rieken 已提交
23
import { DocumentSymbolProviderRegistry } from 'vs/editor/common/modes';
A
Alex Dima 已提交
24
import { LanguageFeatureRegistry } from 'vs/editor/common/modes/languageFeatureRegistry';
J
Johannes Rieken 已提交
25
import { OutlineElement, OutlineModel, TreeElement, IOutlineMarker } from 'vs/editor/contrib/documentSymbols/outlineModel';
J
Johannes Rieken 已提交
26
import { localize } from 'vs/nls';
J
Johannes Rieken 已提交
27
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
28
import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
J
Johannes Rieken 已提交
29
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
30
import { TextEditorSelectionRevealType } from 'vs/platform/editor/common/editor';
J
Johannes Rieken 已提交
31
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
J
Johannes Rieken 已提交
32
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
33
import { WorkbenchDataTree } from 'vs/platform/list/browser/listService';
J
Johannes Rieken 已提交
34
import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage';
35
import { attachProgressBarStyler } from 'vs/platform/theme/common/styler';
J
Johannes Rieken 已提交
36
import { IThemeService } from 'vs/platform/theme/common/themeService';
S
SteVen Batten 已提交
37
import { ViewPane } from 'vs/workbench/browser/parts/views/viewPaneContainer';
38
import { IViewletViewOptions } from 'vs/workbench/browser/parts/views/viewsViewlet';
B
Benjamin Pasero 已提交
39
import { CollapseAction } from 'vs/workbench/browser/viewlet';
40
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
B
Benjamin Pasero 已提交
41
import { OutlineConfigKeys, OutlineViewFocused, OutlineViewFiltered } from 'vs/editor/contrib/documentSymbols/outline';
42
import { FuzzyScore } from 'vs/base/common/filters';
43
import { OutlineDataSource, OutlineItemComparator, OutlineSortOrder, OutlineVirtualDelegate, OutlineGroupRenderer, OutlineElementRenderer, OutlineItem, OutlineIdentityProvider, OutlineNavigationLabelProvider, OutlineFilter } from 'vs/editor/contrib/documentSymbols/outlineTree';
44 45
import { IDataTreeViewState } from 'vs/base/browser/ui/tree/dataTree';
import { StandardMouseEvent } from 'vs/base/browser/mouseEvent';
46
import { basename } from 'vs/base/common/resources';
J
Johannes Rieken 已提交
47
import { IDataSource } from 'vs/base/browser/ui/tree/tree';
J
Johannes Rieken 已提交
48
import { IMarkerDecorationsService } from 'vs/editor/common/services/markersDecorationService';
J
Johannes Rieken 已提交
49
import { MarkerSeverity } from 'vs/platform/markers/common/markers';
50
import { IViewDescriptorService } from 'vs/workbench/common/views';
51
import { IOpenerService } from 'vs/platform/opener/common/opener';
52
import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService';
J
Johannes Rieken 已提交
53

J
Johannes Rieken 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
class RequestState {

	constructor(
		private _editorId: string,
		private _modelId: string,
		private _modelVersion: number,
		private _providerCount: number
	) {
		//
	}

	equals(other: RequestState): boolean {
		return other
			&& this._editorId === other._editorId
			&& this._modelId === other._modelId
			&& this._modelVersion === other._modelVersion
			&& this._providerCount === other._providerCount;
	}
}

74
class RequestOracle {
J
Johannes Rieken 已提交
75

M
Matt Bierner 已提交
76
	private readonly _disposables = new DisposableStore();
J
Johannes Rieken 已提交
77
	private _sessionDisposable = new MutableDisposable();
M
Matt Bierner 已提交
78
	private _lastState?: RequestState;
J
Johannes Rieken 已提交
79 80

	constructor(
M
Matt Bierner 已提交
81
		private readonly _callback: (editor: ICodeEditor | undefined, change: IModelContentChangedEvent | undefined) => any,
J
Johannes Rieken 已提交
82
		private readonly _featureRegistry: LanguageFeatureRegistry<any>,
83
		@IEditorService private readonly _editorService: IEditorService,
J
Johannes Rieken 已提交
84
	) {
B
Benjamin Pasero 已提交
85
		_editorService.onDidActiveEditorChange(this._update, this, this._disposables);
J
Johannes Rieken 已提交
86
		_featureRegistry.onDidChange(this._update, this, this._disposables);
87
		this._update();
J
Johannes Rieken 已提交
88 89 90
	}

	dispose(): void {
J
Johannes Rieken 已提交
91 92
		this._disposables.dispose();
		this._sessionDisposable.dispose();
J
Johannes Rieken 已提交
93 94 95
	}

	private _update(): void {
96

97
		let widget = this._editorService.activeTextEditorWidget;
98
		let codeEditor: ICodeEditor | undefined = undefined;
99 100 101 102
		if (isCodeEditor(widget)) {
			codeEditor = widget;
		} else if (isDiffEditor(widget)) {
			codeEditor = widget.getModifiedEditor();
J
Johannes Rieken 已提交
103
		}
104

M
Matt Bierner 已提交
105
		if (!codeEditor || !codeEditor.hasModel()) {
J
Johannes Rieken 已提交
106
			this._lastState = undefined;
107
			this._callback(undefined, undefined);
J
Johannes Rieken 已提交
108 109
			return;
		}
110

J
Johannes Rieken 已提交
111 112 113 114 115 116 117
		let thisState = new RequestState(
			codeEditor.getId(),
			codeEditor.getModel().id,
			codeEditor.getModel().getVersionId(),
			this._featureRegistry.all(codeEditor.getModel()).length
		);

M
Matt Bierner 已提交
118
		if (this._lastState && thisState.equals(this._lastState)) {
H
Howard Hung 已提交
119
			// prevent unnecessary changes...
J
Johannes Rieken 已提交
120
			return;
121
		}
J
Johannes Rieken 已提交
122
		this._lastState = thisState;
123
		this._callback(codeEditor, undefined);
J
Johannes Rieken 已提交
124

125
		let handle: any;
J
Johannes Rieken 已提交
126
		let contentListener = codeEditor.onDidChangeModelContent(event => {
J
Johannes Rieken 已提交
127
			clearTimeout(handle);
128 129
			const timeout = OutlineModel.getRequestDelay(codeEditor!.getModel());
			handle = setTimeout(() => this._callback(codeEditor!, event), timeout);
J
Johannes Rieken 已提交
130
		});
J
Johannes Rieken 已提交
131
		let modeListener = codeEditor.onDidChangeModelLanguage(_ => {
M
Matt Bierner 已提交
132
			this._callback(codeEditor!, undefined);
J
Johannes Rieken 已提交
133
		});
J
Johannes Rieken 已提交
134 135 136
		let disposeListener = codeEditor.onDidDispose(() => {
			this._callback(undefined, undefined);
		});
J
Johannes Rieken 已提交
137
		this._sessionDisposable.value = {
J
Johannes Rieken 已提交
138
			dispose() {
J
Johannes Rieken 已提交
139
				contentListener.dispose();
J
Johannes Rieken 已提交
140
				clearTimeout(handle);
J
Johannes Rieken 已提交
141
				modeListener.dispose();
J
Johannes Rieken 已提交
142
				disposeListener.dispose();
J
Johannes Rieken 已提交
143 144
			}
		};
J
Johannes Rieken 已提交
145 146 147
	}
}

J
Johannes Rieken 已提交
148
class SimpleToggleAction extends Action {
149

J
Johannes Rieken 已提交
150 151 152
	private readonly _listener: IDisposable;

	constructor(state: OutlineViewState, label: string, isChecked: () => boolean, callback: (action: SimpleToggleAction) => any, className?: string) {
M
Matt Bierner 已提交
153
		super(`simple` + defaultGenerator.nextId(), label, className, true, () => {
J
Johannes Rieken 已提交
154 155
			this.checked = !this.checked;
			callback(this);
M
Matt Bierner 已提交
156
			return Promise.resolve();
157
		});
J
Johannes Rieken 已提交
158 159 160 161 162 163 164
		this.checked = isChecked();
		this._listener = state.onDidChange(() => this.checked = isChecked());
	}

	dispose(): void {
		this._listener.dispose();
		super.dispose();
165 166 167
	}
}

168 169

class OutlineViewState {
170 171

	private _followCursor = false;
172
	private _filterOnType = true;
J
Johannes Rieken 已提交
173
	private _sortBy = OutlineSortOrder.ByKind;
174

175
	private readonly _onDidChange = new Emitter<{ followCursor?: boolean, sortBy?: boolean, filterOnType?: boolean }>();
176 177 178 179 180 181 182 183 184 185 186 187 188
	readonly onDidChange = this._onDidChange.event;

	set followCursor(value: boolean) {
		if (value !== this._followCursor) {
			this._followCursor = value;
			this._onDidChange.fire({ followCursor: true });
		}
	}

	get followCursor(): boolean {
		return this._followCursor;
	}

189 190 191 192 193 194 195 196 197 198 199
	get filterOnType() {
		return this._filterOnType;
	}

	set filterOnType(value) {
		if (value !== this._filterOnType) {
			this._filterOnType = value;
			this._onDidChange.fire({ filterOnType: true });
		}
	}

J
Johannes Rieken 已提交
200
	set sortBy(value: OutlineSortOrder) {
201 202 203 204 205 206
		if (value !== this._sortBy) {
			this._sortBy = value;
			this._onDidChange.fire({ sortBy: true });
		}
	}

J
Johannes Rieken 已提交
207
	get sortBy(): OutlineSortOrder {
208 209 210 211
		return this._sortBy;
	}

	persist(storageService: IStorageService): void {
J
Johannes Rieken 已提交
212 213 214 215 216
		storageService.store('outline/state', JSON.stringify({
			followCursor: this.followCursor,
			sortBy: this.sortBy,
			filterOnType: this.filterOnType,
		}), StorageScope.WORKSPACE);
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
	}

	restore(storageService: IStorageService): void {
		let raw = storageService.get('outline/state', StorageScope.WORKSPACE);
		if (!raw) {
			return;
		}
		let data: any;
		try {
			data = JSON.parse(raw);
		} catch (e) {
			return;
		}
		this.followCursor = data.followCursor;
		this.sortBy = data.sortBy;
J
Johannes Rieken 已提交
232 233 234
		if (typeof data.filterOnType === 'boolean') {
			this.filterOnType = data.filterOnType;
		}
235 236 237
	}
}

S
SteVen Batten 已提交
238
export class OutlinePane extends ViewPane {
J
Johannes Rieken 已提交
239

240
	private _disposables = new Array<IDisposable>();
J
Johannes Rieken 已提交
241

J
Johannes Rieken 已提交
242
	private _editorDisposables = new DisposableStore();
243
	private _outlineViewState = new OutlineViewState();
M
Matt Bierner 已提交
244
	private _requestOracle?: RequestOracle;
J
Johannes Rieken 已提交
245 246 247 248 249 250 251 252
	private _domNode!: HTMLElement;
	private _message!: HTMLDivElement;
	private _inputContainer!: HTMLDivElement;
	private _progressBar!: ProgressBar;
	private _tree!: WorkbenchDataTree<OutlineModel, OutlineItem, FuzzyScore>;
	private _treeDataSource!: OutlineDataSource;
	private _treeRenderer!: OutlineElementRenderer;
	private _treeComparator!: OutlineItemComparator;
253
	private _treeFilter!: OutlineFilter;
254 255
	private _treeStates = new LRUCache<string, IDataTreeViewState>(10);

256 257 258
	private readonly _contextKeyFocused: IContextKey<boolean>;
	private readonly _contextKeyFiltered: IContextKey<boolean>;

J
Johannes Rieken 已提交
259
	constructor(
260
		options: IViewletViewOptions,
J
Johannes Rieken 已提交
261
		@IInstantiationService private readonly _instantiationService: IInstantiationService,
262
		@IViewDescriptorService viewDescriptorService: IViewDescriptorService,
J
Johannes Rieken 已提交
263
		@IThemeService private readonly _themeService: IThemeService,
264
		@IStorageService private readonly _storageService: IStorageService,
265
		@ICodeEditorService private readonly _editorService: ICodeEditorService,
J
Johannes Rieken 已提交
266
		@IMarkerDecorationsService private readonly _markerDecorationService: IMarkerDecorationsService,
267
		@IConfigurationService private readonly _configurationService: IConfigurationService,
268
		@IKeybindingService keybindingService: IKeybindingService,
269
		@IContextKeyService contextKeyService: IContextKeyService,
J
Johannes Rieken 已提交
270
		@IContextMenuService contextMenuService: IContextMenuService,
271 272
		@IOpenerService openerService: IOpenerService,
		@IThemeService themeService: IThemeService,
J
Johannes Rieken 已提交
273
	) {
274
		super(options, keybindingService, contextMenuService, _configurationService, contextKeyService, viewDescriptorService, _instantiationService, openerService, themeService);
275
		this._outlineViewState.restore(this._storageService);
276 277 278 279
		this._contextKeyFocused = OutlineViewFocused.bindTo(contextKeyService);
		this._contextKeyFiltered = OutlineViewFiltered.bindTo(contextKeyService);
		this._disposables.push(this.onDidFocus(_ => this._contextKeyFocused.set(true)));
		this._disposables.push(this.onDidBlur(_ => this._contextKeyFocused.set(false)));
J
Johannes Rieken 已提交
280 281 282 283
	}

	dispose(): void {
		dispose(this._disposables);
J
Johannes Rieken 已提交
284
		dispose(this._requestOracle);
J
Johannes Rieken 已提交
285
		dispose(this._editorDisposables);
J
Johannes Rieken 已提交
286 287 288
		super.dispose();
	}

289 290
	focus(): void {
		if (this._tree) {
J
Johannes Rieken 已提交
291 292 293
			// focus on tree and fallback to root
			// dom node when the tree cannot take focus,
			// e.g. when hidden
294
			this._tree.domFocus();
J
Johannes Rieken 已提交
295 296 297
			if (!this._tree.isDOMFocused()) {
				this._domNode.focus();
			}
298 299 300
		}
	}

J
Johannes Rieken 已提交
301
	protected renderBody(container: HTMLElement): void {
J
Joao Moreno 已提交
302 303
		super.renderBody(container);

304
		this._domNode = container;
J
Johannes Rieken 已提交
305
		this._domNode.tabIndex = 0;
306
		dom.addClass(container, 'outline-pane');
J
Johannes Rieken 已提交
307

308
		let progressContainer = dom.$('.outline-progress');
309
		this._message = dom.$('.outline-message');
310
		this._inputContainer = dom.$('.outline-input');
J
Johannes Rieken 已提交
311 312

		this._progressBar = new ProgressBar(progressContainer);
M
Matt Bierner 已提交
313
		this._register(attachProgressBarStyler(this._progressBar, this._themeService));
J
Johannes Rieken 已提交
314

J
Johannes Rieken 已提交
315
		let treeContainer = dom.$('.outline-tree');
J
Johannes Rieken 已提交
316 317
		dom.append(
			container,
318
			progressContainer, this._message, this._inputContainer, treeContainer
J
Johannes Rieken 已提交
319
		);
J
Johannes Rieken 已提交
320

J
Johannes Rieken 已提交
321 322 323
		this._treeRenderer = this._instantiationService.createInstance(OutlineElementRenderer);
		this._treeDataSource = new OutlineDataSource();
		this._treeComparator = new OutlineItemComparator(this._outlineViewState.sortBy);
324
		this._treeFilter = this._instantiationService.createInstance(OutlineFilter, 'outline');
325
		this._tree = <WorkbenchDataTree<OutlineModel, OutlineItem, FuzzyScore>>this._instantiationService.createInstance(
326
			WorkbenchDataTree,
327
			'OutlinePane',
328
			treeContainer,
J
Johannes Rieken 已提交
329 330
			new OutlineVirtualDelegate(),
			[new OutlineGroupRenderer(), this._treeRenderer],
331
			// https://github.com/microsoft/TypeScript/issues/32526
J
Johannes Rieken 已提交
332
			this._treeDataSource as IDataSource<OutlineModel, OutlineItem>,
333
			{
334
				expandOnlyOnTwistieClick: true,
J
Johannes Rieken 已提交
335
				multipleSelectionSupport: false,
336
				filterOnType: this._outlineViewState.filterOnType,
337
				sorter: this._treeComparator,
338
				filter: this._treeFilter,
J
Johannes Rieken 已提交
339
				identityProvider: new OutlineIdentityProvider(),
J
Joao Moreno 已提交
340
				keyboardNavigationLabelProvider: new OutlineNavigationLabelProvider(),
J
Johannes Rieken 已提交
341 342
				hideTwistiesOfChildlessElements: true,
				overrideStyles: {
S
SteVen Batten 已提交
343
					listBackground: this.getBackgroundColor()
J
Johannes Rieken 已提交
344
				}
J
Johannes Rieken 已提交
345
			}
346
		);
347

S
SteVen Batten 已提交
348

349
		this._disposables.push(this._tree);
350
		this._disposables.push(this._outlineViewState.onDidChange(this._onDidChangeUserState, this));
351
		this._disposables.push(this.viewDescriptorService.onDidChangeLocation(({ views }) => {
S
SteVen Batten 已提交
352 353 354 355
			if (views.some(v => v.id === this.id)) {
				this._tree.updateOptions({ overrideStyles: { listBackground: this.getBackgroundColor() } });
			}
		}));
356

J
Johannes Rieken 已提交
357
		// override the globally defined behaviour
358 359 360 361
		this._tree.updateOptions({
			filterOnType: this._outlineViewState.filterOnType
		});

362
		// feature: filter on type - keep tree and menu in sync
M
Matt Bierner 已提交
363
		this._register(this._tree.onDidUpdateOptions(e => {
J
Johannes Rieken 已提交
364
			this._outlineViewState.filterOnType = Boolean(e.filterOnType);
365 366 367 368
		}));

		// feature: expand all nodes when filtering (not when finding)
		let viewState: IDataTreeViewState | undefined;
M
Matt Bierner 已提交
369
		this._register(this._tree.onDidChangeTypeFilterPattern(pattern => {
370 371 372 373 374 375 376
			if (!this._tree.options.filterOnType) {
				return;
			}
			if (!viewState && pattern) {
				viewState = this._tree.getViewState();
				this._tree.expandAll();
			} else if (!pattern && viewState) {
J
Johannes Rieken 已提交
377
				this._tree.setInput(this._tree.getInput()!, viewState);
378 379 380
				viewState = undefined;
			}
		}));
J
Johannes Rieken 已提交
381

382
		// feature: toggle icons
M
Matt Bierner 已提交
383
		this._register(this._configurationService.onDidChangeConfiguration(e => {
384
			if (e.affectsConfiguration(OutlineConfigKeys.icons)) {
385
				this._tree.updateChildren();
386
			}
387
			if (e.affectsConfiguration('outline')) {
388 389
				this._tree.refilter();
			}
390
		}));
B
Benjamin Pasero 已提交
391

M
Matt Bierner 已提交
392
		this._register(this.onDidChangeBodyVisibility(visible => {
B
Benjamin Pasero 已提交
393
			if (visible && !this._requestOracle) {
J
Johannes Rieken 已提交
394
				this._requestOracle = this._instantiationService.createInstance(RequestOracle, (editor, event) => this._doUpdate(editor, event), DocumentSymbolProviderRegistry);
B
Benjamin Pasero 已提交
395 396 397 398 399 400
			} else if (!visible) {
				dispose(this._requestOracle);
				this._requestOracle = undefined;
				this._doUpdate(undefined, undefined);
			}
		}));
J
Johannes Rieken 已提交
401 402
	}

J
Johannes Rieken 已提交
403 404
	protected layoutBody(height: number, width: number): void {
		this._tree.layout(height, width);
J
Johannes Rieken 已提交
405 406
	}

J
Johannes Rieken 已提交
407 408
	getActions(): IAction[] {
		return [
409
			new Action('collapse', localize('collapse', "Collapse All"), 'explorer-action codicon-collapse-all', true, () => {
B
Benjamin Pasero 已提交
410
				return new CollapseAction(this._tree, true, undefined).run();
J
Johannes Rieken 已提交
411 412 413 414
			})
		];
	}

415
	getSecondaryActions(): IAction[] {
M
Matt Bierner 已提交
416
		const group = this._register(new RadioGroup([
J
Johannes Rieken 已提交
417 418
			new SimpleToggleAction(this._outlineViewState, localize('sortByPosition', "Sort By: Position"), () => this._outlineViewState.sortBy === OutlineSortOrder.ByPosition, _ => this._outlineViewState.sortBy = OutlineSortOrder.ByPosition),
			new SimpleToggleAction(this._outlineViewState, localize('sortByName', "Sort By: Name"), () => this._outlineViewState.sortBy === OutlineSortOrder.ByName, _ => this._outlineViewState.sortBy = OutlineSortOrder.ByName),
419
			new SimpleToggleAction(this._outlineViewState, localize('sortByKind', "Sort By: Category"), () => this._outlineViewState.sortBy === OutlineSortOrder.ByKind, _ => this._outlineViewState.sortBy = OutlineSortOrder.ByKind),
M
Matt Bierner 已提交
420 421
		]));
		const result = [
J
Johannes Rieken 已提交
422 423
			new SimpleToggleAction(this._outlineViewState, localize('followCur', "Follow Cursor"), () => this._outlineViewState.followCursor, action => this._outlineViewState.followCursor = action.checked),
			new SimpleToggleAction(this._outlineViewState, localize('filterOnType', "Filter on Type"), () => this._outlineViewState.filterOnType, action => this._outlineViewState.filterOnType = action.checked),
J
Johannes Rieken 已提交
424 425 426
			new Separator(),
			...group.actions,
		];
M
Matt Bierner 已提交
427 428 429
		for (const r of result) {
			this._register(r);
		}
J
Johannes Rieken 已提交
430 431

		return result;
432 433
	}

434
	private _onDidChangeUserState(e: { followCursor?: boolean, sortBy?: boolean, filterOnType?: boolean }) {
435 436 437 438 439 440
		this._outlineViewState.persist(this._storageService);
		if (e.followCursor) {
			// todo@joh update immediately
		}
		if (e.sortBy) {
			this._treeComparator.type = this._outlineViewState.sortBy;
J
Joao Moreno 已提交
441
			this._tree.resort();
442
		}
443
		if (e.filterOnType) {
444 445 446
			this._tree.updateOptions({
				filterOnType: this._outlineViewState.filterOnType
			});
447
		}
448 449
	}

450
	private _showMessage(message: string) {
J
Johannes Rieken 已提交
451
		dom.addClass(this._domNode, 'message');
J
Johannes Rieken 已提交
452
		this._tree.setInput(undefined!);
J
Johannes Rieken 已提交
453
		this._progressBar.stop().hide();
454 455 456
		this._message.innerText = escape(message);
	}

J
Johannes Rieken 已提交
457
	private static _createOutlineModel(model: ITextModel, disposables: DisposableStore): Promise<OutlineModel | undefined> {
458
		let promise = createCancelablePromise(token => OutlineModel.create(model, token));
J
Johannes Rieken 已提交
459
		disposables.add({ dispose() { promise.cancel(); } });
460 461 462 463 464 465 466 467
		return promise.catch(err => {
			if (!isPromiseCanceledError(err)) {
				throw err;
			}
			return undefined;
		});
	}

M
Matt Bierner 已提交
468
	private async _doUpdate(editor: ICodeEditor | undefined, event: IModelContentChangedEvent | undefined): Promise<void> {
J
Johannes Rieken 已提交
469
		this._editorDisposables.clear();
470

J
Johannes Rieken 已提交
471

J
Johannes Rieken 已提交
472
		const oldModel = this._tree.getInput();
473 474 475

		// persist state
		if (oldModel) {
J
Johannes Rieken 已提交
476
			this._treeStates.set(oldModel.textModel.uri.toString(), this._tree.getViewState());
477 478
		}

M
Matt Bierner 已提交
479
		if (!editor || !editor.hasModel() || !DocumentSymbolProviderRegistry.has(editor.getModel())) {
J
Johannes Rieken 已提交
480
			return this._showMessage(localize('no-editor', "The active editor cannot provide outline information."));
J
Johannes Rieken 已提交
481 482
		}

483
		const textModel = editor.getModel();
J
Johannes Rieken 已提交
484 485 486 487 488 489 490 491

		let loadingMessage: IDisposable | undefined;
		if (!oldModel) {
			loadingMessage = new TimeoutTimer(
				() => this._showMessage(localize('loading', "Loading document symbols for '{0}'...", basename(textModel.uri))),
				100
			);
		}
492 493 494

		const requestDelay = OutlineModel.getRequestDelay(textModel);
		this._progressBar.infinite().show(requestDelay);
495

496
		const createdModel = await OutlinePane._createOutlineModel(textModel, this._editorDisposables);
497
		dispose(loadingMessage);
M
Matt Bierner 已提交
498
		if (!createdModel) {
J
Johannes Rieken 已提交
499 500 501
			return;
		}

M
Matt Bierner 已提交
502
		let newModel = createdModel;
503
		if (TreeElement.empty(newModel)) {
504
			return this._showMessage(localize('no-symbols', "No symbols found in document '{0}'", basename(textModel.uri)));
505 506
		}

507
		dom.removeClass(this._domNode, 'message');
508

509
		if (event && oldModel && textModel.getLineCount() >= 25) {
510 511
			// heuristic: when the symbols-to-lines ratio changes by 50% between edits
			// wait a little (and hope that the next change isn't as drastic).
J
Johannes Rieken 已提交
512
			let newSize = TreeElement.size(newModel);
513 514 515 516 517 518
			let newLength = textModel.getValueLength();
			let newRatio = newSize / newLength;
			let oldSize = TreeElement.size(oldModel);
			let oldLength = newLength - event.changes.reduce((prev, value) => prev + value.rangeLength, 0);
			let oldRatio = oldSize / oldLength;
			if (newRatio <= oldRatio * 0.5 || newRatio >= oldRatio * 1.5) {
J
Johannes Rieken 已提交
519 520

				let waitPromise = new Promise<boolean>(resolve => {
M
Matt Bierner 已提交
521
					let handle: any = setTimeout(() => {
J
Johannes Rieken 已提交
522 523 524 525 526 527 528 529 530 531 532 533
						handle = undefined;
						resolve(true);
					}, 2000);
					this._disposables.push({
						dispose() {
							clearTimeout(handle);
							resolve(false);
						}
					});
				});

				if (!await waitPromise) {
534 535 536 537 538
					return;
				}
			}
		}

539 540
		this._progressBar.stop().hide();

541
		if (oldModel && oldModel.merge(newModel)) {
542
			this._tree.updateChildren();
543
			newModel = oldModel;
544
		} else {
545
			let state = this._treeStates.get(newModel.textModel.uri.toString());
546
			this._tree.setInput(newModel, state);
547
		}
J
Johannes Rieken 已提交
548

J
Johannes Rieken 已提交
549 550 551 552 553
		// transfer focus from domNode to the tree
		if (this._domNode === document.activeElement) {
			this._tree.domFocus();
		}

J
Johannes Rieken 已提交
554
		this._editorDisposables.add(toDisposable(() => this._contextKeyFiltered.reset()));
555

556 557
		// feature: reveal outline selection in editor
		// on change -> reveal/select defining range
J
Johannes Rieken 已提交
558 559
		this._editorDisposables.add(this._tree.onDidOpen(e => {

560
			let [first] = e.elements;
561 562 563 564 565 566
			if (!(first instanceof OutlineElement)) {
				return;
			}

			let focus = false;
			let aside = false;
567 568 569
			// todo@Joh
			if (e.browserEvent) {
				if (e.browserEvent.type === 'keydown') {
570
					focus = true;
571 572 573 574 575
				} else if (e.browserEvent.type === 'click') {
					const event = new StandardMouseEvent(e.browserEvent as MouseEvent);
					focus = e.browserEvent.detail === 2;
					aside = (!this._tree.useAltAsMultipleSelectionModifier && event.altKey)
						|| (this._tree.useAltAsMultipleSelectionModifier && (event.ctrlKey || event.metaKey));
576
				}
577
			}
578
			this._revealTreeSelection(newModel, first, focus, aside);
579 580 581
		}));

		// feature: reveal editor selection in outline
582 583
		this._revealEditorSelection(newModel, editor.getSelection());
		const versionIdThen = newModel.textModel.getVersionId();
J
Johannes Rieken 已提交
584
		this._editorDisposables.add(editor.onDidChangeCursorSelection(e => {
J
Johannes Rieken 已提交
585 586 587
			// first check if the document has changed and stop revealing the
			// cursor position iff it has -> we will update/recompute the
			// outline view then anyways
588 589
			if (!newModel.textModel.isDisposed() && newModel.textModel.getVersionId() === versionIdThen) {
				this._revealEditorSelection(newModel, e.selection);
J
Johannes Rieken 已提交
590 591
			}
		}));
592 593

		// feature: show markers in outline
J
Johannes Rieken 已提交
594
		const updateMarker = (model: ITextModel, ignoreEmpty?: boolean) => {
595 596 597
			if (!this._configurationService.getValue(OutlineConfigKeys.problemsEnabled)) {
				return;
			}
J
Johannes Rieken 已提交
598
			if (model !== textModel) {
599 600
				return;
			}
J
Johannes Rieken 已提交
601 602 603 604 605 606 607 608
			const markers: IOutlineMarker[] = [];
			for (const [range, marker] of this._markerDecorationService.getLiveMarkers(textModel)) {
				if (marker.severity === MarkerSeverity.Error || marker.severity === MarkerSeverity.Warning) {
					markers.push({ ...range, severity: marker.severity });
				}
			}
			if (markers.length > 0 || !ignoreEmpty) {
				newModel.updateMarker(markers);
609
				this._tree.updateChildren();
610 611
			}
		};
J
Johannes Rieken 已提交
612
		updateMarker(textModel, true);
J
Johannes Rieken 已提交
613
		this._editorDisposables.add(this._markerDecorationService.onDidChangeMarker(updateMarker));
614

J
Johannes Rieken 已提交
615
		this._editorDisposables.add(this.configurationService.onDidChangeConfiguration(e => {
616
			if (e.affectsConfiguration(OutlineConfigKeys.problemsBadges) || e.affectsConfiguration(OutlineConfigKeys.problemsColors)) {
617
				this._tree.updateChildren();
618 619 620 621 622 623
				return;
			}
			if (!e.affectsConfiguration(OutlineConfigKeys.problemsEnabled)) {
				return;
			}
			if (!this._configurationService.getValue(OutlineConfigKeys.problemsEnabled)) {
624
				newModel.updateMarker([]);
625
				this._tree.updateChildren();
626
			} else {
J
Johannes Rieken 已提交
627
				updateMarker(textModel, true);
628 629
			}
		}));
630 631
	}

632
	private async _revealTreeSelection(model: OutlineModel, element: OutlineElement, focus: boolean, aside: boolean): Promise<void> {
633 634 635 636 637 638 639 640 641 642 643 644
		await this._editorService.openCodeEditor(
			{
				resource: model.textModel.uri,
				options: {
					preserveFocus: !focus,
					selection: Range.collapseToStart(element.symbol.selectionRange),
					selectionRevealType: TextEditorSelectionRevealType.NearTop,
				}
			},
			this._editorService.getActiveCodeEditor(),
			aside
		);
645 646
	}

J
Johannes Rieken 已提交
647
	private _revealEditorSelection(model: OutlineModel, selection: Selection): void {
J
Johannes Rieken 已提交
648
		if (!this._outlineViewState.followCursor || !this._tree.getInput() || !selection) {
649 650
			return;
		}
651
		let [first] = this._tree.getSelection();
J
Johannes Rieken 已提交
652
		let item = model.getItemEnclosingPosition({
653 654
			lineNumber: selection.selectionStartLineNumber,
			column: selection.selectionStartColumn
655
		}, first instanceof OutlineElement ? first : undefined);
J
Johannes Rieken 已提交
656 657 658 659 660
		if (!item) {
			// nothing to reveal
			return;
		}
		let top = this._tree.getRelativeTop(item);
J
Johannes Rieken 已提交
661 662
		if (top === null) {
			this._tree.reveal(item, 0.5);
663
		}
J
Johannes Rieken 已提交
664 665
		this._tree.setFocus([item]);
		this._tree.setSelection([item]);
666 667
	}
}