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

J
Johannes Rieken 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
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;
	}
}

73
class RequestOracle {
J
Johannes Rieken 已提交
74

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

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

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

	private _update(): void {
95

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

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

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

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

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

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

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

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

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

167 168

class OutlineViewState {
169 170

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

174
	private readonly _onDidChange = new Emitter<{ followCursor?: boolean, sortBy?: boolean, filterOnType?: boolean }>();
175 176 177 178 179 180 181 182 183 184 185 186 187
	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;
	}

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

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

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

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

210 211
	persist(storageService: IStorageService): void {
		storageService.store('outline/state', JSON.stringify({
J
Johannes Rieken 已提交
212 213 214 215
			followCursor: this.followCursor,
			sortBy: this.sortBy,
			filterOnType: this.filterOnType,
		}), StorageScope.WORKSPACE);
216 217
	}

218 219
	restore(storageService: IStorageService): void {
		let raw = storageService.get('outline/state', StorageScope.WORKSPACE);
220 221 222 223 224 225 226 227 228 229 230
		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 已提交
231 232 233
		if (typeof data.filterOnType === 'boolean') {
			this.filterOnType = data.filterOnType;
		}
234 235 236
	}
}

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

J
Johannes Rieken 已提交
239
	private _disposables = new DisposableStore();
J
Johannes Rieken 已提交
240

J
Johannes Rieken 已提交
241
	private _editorDisposables = new DisposableStore();
242
	private _outlineViewState = new OutlineViewState();
M
Matt Bierner 已提交
243
	private _requestOracle?: RequestOracle;
J
Johannes Rieken 已提交
244 245 246 247 248 249 250 251
	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;
252
	private _treeFilter!: OutlineFilter;
253 254
	private _treeStates = new LRUCache<string, IDataTreeViewState>(10);

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

J
Johannes Rieken 已提交
258
	constructor(
259
		options: IViewletViewOptions,
J
Johannes Rieken 已提交
260
		@IInstantiationService private readonly _instantiationService: IInstantiationService,
261
		@IViewDescriptorService viewDescriptorService: IViewDescriptorService,
J
Johannes Rieken 已提交
262
		@IThemeService private readonly _themeService: IThemeService,
263
		@IStorageService private readonly _storageService: IStorageService,
264
		@ICodeEditorService private readonly _editorService: ICodeEditorService,
J
Johannes Rieken 已提交
265
		@IMarkerDecorationsService private readonly _markerDecorationService: IMarkerDecorationsService,
266
		@IConfigurationService private readonly _configurationService: IConfigurationService,
267
		@IKeybindingService keybindingService: IKeybindingService,
268
		@IContextKeyService contextKeyService: IContextKeyService,
J
Johannes Rieken 已提交
269
		@IContextMenuService contextMenuService: IContextMenuService,
270 271
		@IOpenerService openerService: IOpenerService,
		@IThemeService themeService: IThemeService,
272
		@ITelemetryService telemetryService: ITelemetryService,
J
Johannes Rieken 已提交
273
	) {
274
		super(options, keybindingService, contextMenuService, _configurationService, contextKeyService, viewDescriptorService, _instantiationService, openerService, themeService, telemetryService);
275
		this._outlineViewState.restore(this._storageService);
276 277
		this._contextKeyFocused = OutlineViewFocused.bindTo(contextKeyService);
		this._contextKeyFiltered = OutlineViewFiltered.bindTo(contextKeyService);
J
Johannes Rieken 已提交
278 279
		this._disposables.add(this.onDidFocus(_ => this._contextKeyFocused.set(true)));
		this._disposables.add(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
		container.classList.add('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(),
341
				accessibilityProvider: new OutlineAccessibilityProvider(localize('outline', "Outline")),
J
Johannes Rieken 已提交
342 343
				hideTwistiesOfChildlessElements: true,
				overrideStyles: {
S
SteVen Batten 已提交
344
					listBackground: this.getBackgroundColor()
J
João Moreno 已提交
345 346
				},
				openOnSingleClick: true
J
Johannes Rieken 已提交
347
			}
348
		);
349

S
SteVen Batten 已提交
350

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

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

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

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

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

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

J
Johannes Rieken 已提交
405
	protected layoutBody(height: number, width: number): void {
J
João Moreno 已提交
406
		super.layoutBody(height, width);
J
Johannes Rieken 已提交
407
		this._tree.layout(height, width);
J
Johannes Rieken 已提交
408 409
	}

J
Johannes Rieken 已提交
410 411
	getActions(): IAction[] {
		return [
412
			new CollapseAction(() => this._tree, true, 'explorer-action codicon-collapse-all')
J
Johannes Rieken 已提交
413 414 415
		];
	}

416
	getSecondaryActions(): IAction[] {
M
Matt Bierner 已提交
417
		const group = this._register(new RadioGroup([
J
Johannes Rieken 已提交
418 419
			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),
420
			new SimpleToggleAction(this._outlineViewState, localize('sortByKind', "Sort By: Category"), () => this._outlineViewState.sortBy === OutlineSortOrder.ByKind, _ => this._outlineViewState.sortBy = OutlineSortOrder.ByKind),
M
Matt Bierner 已提交
421 422
		]));
		const result = [
J
Johannes Rieken 已提交
423 424
			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 已提交
425 426 427
			new Separator(),
			...group.actions,
		];
M
Matt Bierner 已提交
428 429 430
		for (const r of result) {
			this._register(r);
		}
J
Johannes Rieken 已提交
431 432

		return result;
433 434
	}

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

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

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

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

J
Johannes Rieken 已提交
472

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

		// persist state
		if (oldModel) {
477
			this._treeStates.set(oldModel.uri.toString(), this._tree.getViewState());
478 479
		}

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

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

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

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

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

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

508
		this._domNode.classList.remove('message');
509

510
		if (event && oldModel && textModel.getLineCount() >= 25) {
511 512
			// 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 已提交
513
			let newSize = TreeElement.size(newModel);
514 515 516 517 518 519
			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 已提交
520 521

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

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

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

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

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

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

557 558
		// feature: reveal outline selection in editor
		// on change -> reveal/select defining range
J
Johannes Rieken 已提交
559
		this._editorDisposables.add(this._tree.onDidOpen(e => {
560
			if (!(e.element instanceof OutlineElement)) {
561 562 563
				return;
			}

J
João Moreno 已提交
564
			this._revealTreeSelection(newModel, e.element, !!e.editorOptions.preserveFocus || !e.editorOptions.pinned, e.sideBySide);
565 566 567
		}));

		// feature: reveal editor selection in outline
568
		this._revealEditorSelection(newModel, editor.getSelection());
569
		const versionIdThen = textModel.getVersionId();
J
Johannes Rieken 已提交
570
		this._editorDisposables.add(editor.onDidChangeCursorSelection(e => {
J
Johannes Rieken 已提交
571 572 573
			// 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
574
			if (!textModel.isDisposed() && textModel.getVersionId() === versionIdThen) {
575
				this._revealEditorSelection(newModel, e.selection);
J
Johannes Rieken 已提交
576 577
			}
		}));
578 579

		// feature: show markers in outline
J
Johannes Rieken 已提交
580
		const updateMarker = (model: ITextModel, ignoreEmpty?: boolean) => {
581 582 583
			if (!this._configurationService.getValue(OutlineConfigKeys.problemsEnabled)) {
				return;
			}
J
Johannes Rieken 已提交
584
			if (model !== textModel) {
585 586
				return;
			}
J
Johannes Rieken 已提交
587 588 589 590 591 592 593 594
			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);
595
				this._tree.updateChildren();
596 597
			}
		};
J
Johannes Rieken 已提交
598
		updateMarker(textModel, true);
599
		this._editorDisposables.add(Event.debounce(this._markerDecorationService.onDidChangeMarker, (_, e) => e, 64)(updateMarker));
600

J
Johannes Rieken 已提交
601
		this._editorDisposables.add(this.configurationService.onDidChangeConfiguration(e => {
602
			if (e.affectsConfiguration(OutlineConfigKeys.problemsBadges) || e.affectsConfiguration(OutlineConfigKeys.problemsColors)) {
603
				this._tree.updateChildren();
604 605 606 607 608 609
				return;
			}
			if (!e.affectsConfiguration(OutlineConfigKeys.problemsEnabled)) {
				return;
			}
			if (!this._configurationService.getValue(OutlineConfigKeys.problemsEnabled)) {
610
				newModel.updateMarker([]);
611
				this._tree.updateChildren();
612
			} else {
J
Johannes Rieken 已提交
613
				updateMarker(textModel, true);
614 615
			}
		}));
616 617
	}

J
João Moreno 已提交
618
	private async _revealTreeSelection(model: OutlineModel, element: OutlineElement, preserveFocus: boolean, aside: boolean): Promise<void> {
619 620
		await this._editorService.openCodeEditor(
			{
621
				resource: model.uri,
622
				options: {
J
João Moreno 已提交
623
					preserveFocus,
624
					selection: Range.collapseToStart(element.symbol.selectionRange),
625
					selectionRevealType: TextEditorSelectionRevealType.NearTopIfOutsideViewport,
626 627 628 629 630
				}
			},
			this._editorService.getActiveCodeEditor(),
			aside
		);
631 632
	}

J
Johannes Rieken 已提交
633
	private _revealEditorSelection(model: OutlineModel, selection: Selection): void {
J
Johannes Rieken 已提交
634
		if (!this._outlineViewState.followCursor || !this._tree.getInput() || !selection) {
635 636
			return;
		}
637
		let [first] = this._tree.getSelection();
J
Johannes Rieken 已提交
638
		let item = model.getItemEnclosingPosition({
639 640
			lineNumber: selection.selectionStartLineNumber,
			column: selection.selectionStartColumn
641
		}, first instanceof OutlineElement ? first : undefined);
J
Johannes Rieken 已提交
642 643 644 645 646
		if (!item) {
			// nothing to reveal
			return;
		}
		let top = this._tree.getRelativeTop(item);
J
Johannes Rieken 已提交
647 648
		if (top === null) {
			this._tree.reveal(item, 0.5);
649
		}
J
Johannes Rieken 已提交
650 651
		this._tree.setFocus([item]);
		this._tree.setSelection([item]);
652 653
	}
}