outlinePanel.ts 10.1 KB
Newer Older
J
Johannes Rieken 已提交
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

J
Johannes Rieken 已提交
7
import * as dom from 'vs/base/browser/dom';
J
Johannes Rieken 已提交
8
import { Separator } from 'vs/base/browser/ui/actionbar/actionbar';
J
Johannes Rieken 已提交
9
import { InputBox } from 'vs/base/browser/ui/inputbox/inputBox';
J
Johannes Rieken 已提交
10
import { Action, IAction, RadioGroup } from 'vs/base/common/actions';
J
Johannes Rieken 已提交
11
import { Emitter, Event } from 'vs/base/common/event';
J
Johannes Rieken 已提交
12
import { defaultGenerator } from 'vs/base/common/idGenerator';
J
Johannes Rieken 已提交
13 14
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
import { TPromise } from 'vs/base/common/winjs.base';
J
Johannes Rieken 已提交
15
import { Tree } from 'vs/base/parts/tree/browser/treeImpl';
J
Johannes Rieken 已提交
16
import 'vs/css!./outlinePanel';
J
Johannes Rieken 已提交
17
import { ICodeEditor, isCodeEditor, isDiffEditor } from 'vs/editor/browser/editorBrowser';
J
Johannes Rieken 已提交
18
import { CursorChangeReason } from 'vs/editor/common/controller/cursorEvents';
J
Johannes Rieken 已提交
19 20
import { Range } from 'vs/editor/common/core/range';
import { ScrollType } from 'vs/editor/common/editorCommon';
J
Johannes Rieken 已提交
21
import { DocumentSymbolProviderRegistry } from 'vs/editor/common/modes';
J
Johannes Rieken 已提交
22
import { localize } from 'vs/nls';
J
Johannes Rieken 已提交
23 24
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
J
Johannes Rieken 已提交
25
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
J
Johannes Rieken 已提交
26 27
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { WorkbenchTree } from 'vs/platform/list/browser/listService';
J
Johannes Rieken 已提交
28 29
import { attachInputBoxStyler } from 'vs/platform/theme/common/styler';
import { IThemeService } from 'vs/platform/theme/common/themeService';
J
Johannes Rieken 已提交
30 31
import { IViewOptions, ViewsViewletPanel } from 'vs/workbench/browser/parts/views/viewsViewlet';
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
J
Johannes Rieken 已提交
32
import { IEditorGroupService } from 'vs/workbench/services/group/common/groupService';
33
import { OutlineItem, getOutline, OutlineModel } from './outlineModel';
34
import { OutlineDataSource, OutlineItemComparator, OutlineItemCompareType, OutlineItemFilter, OutlineRenderer, OutlineTreeState } from './outlineTree';
35
import { KeyCode } from '../../../../base/common/keyCodes';
36
import { LRUCache } from '../../../../base/common/map';
J
Johannes Rieken 已提交
37

J
Johannes Rieken 已提交
38
class ActiveEditorOracle {
J
Johannes Rieken 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

	private readonly _disposables = new Array<IDisposable>();
	private readonly _onDidChangeActiveEditor = new Emitter<ICodeEditor>();

	private _editorListener: IDisposable;

	readonly onDidChangeActiveEditor: Event<ICodeEditor> = this._onDidChangeActiveEditor.event;

	constructor(
		@IEditorGroupService editorGroupService: IEditorGroupService,
		@IWorkbenchEditorService private readonly _workbenchEditorService: IWorkbenchEditorService,
	) {
		editorGroupService.onEditorsChanged(this._update, this, this._disposables);
		DocumentSymbolProviderRegistry.onDidChange(this._update, this, this._disposables);
	}

	dispose(): void {
		dispose(this._disposables);
	}

	private _update(): void {
		dispose(this._editorListener);
		let editor = this._workbenchEditorService.getActiveEditor();
		let control = editor && editor.getControl();
		let codeEditor: ICodeEditor = undefined;
		if (isCodeEditor(control)) {
			codeEditor = control;
		} else if (isDiffEditor(control)) {
			codeEditor = control.getModifiedEditor();
		}
		this._editorListener = codeEditor && codeEditor.onDidChangeModelContent(e => this._onDidChangeActiveEditor.fire(codeEditor));
		this._onDidChangeActiveEditor.fire(codeEditor);
	}
}

J
Johannes Rieken 已提交
74
class SimpleToggleAction extends Action {
75

J
Johannes Rieken 已提交
76 77 78 79
	constructor(label: string, checked: boolean, callback: (action: SimpleToggleAction) => any) {
		super(`simple` + defaultGenerator.nextId(), label, undefined, true, _ => {
			this.checked = !this.checked;
			callback(this);
80 81
			return undefined;
		});
J
Johannes Rieken 已提交
82
		this.checked = checked;
83 84 85
	}
}

J
Johannes Rieken 已提交
86 87 88
export class OutlinePanel extends ViewsViewletPanel {

	private readonly _disposables = new Array<IDisposable>();
J
Johannes Rieken 已提交
89 90 91
	private readonly _activeEditorOracle: ActiveEditorOracle;

	private _editorDisposables = new Array<IDisposable>();
J
Johannes Rieken 已提交
92

J
Johannes Rieken 已提交
93
	private _input: InputBox;
J
Johannes Rieken 已提交
94
	private _tree: Tree;
J
Johannes Rieken 已提交
95 96
	private _treeFilter: OutlineItemFilter;
	private _treeComparator: OutlineItemComparator;
97 98
	private _treeStates = new LRUCache<string, OutlineTreeState>(10);
	private _followCursor = true;
J
Johannes Rieken 已提交
99 100 101 102

	constructor(
		options: IViewOptions,
		@IInstantiationService private readonly _instantiationService: IInstantiationService,
J
Johannes Rieken 已提交
103
		@IThemeService private readonly _themeService: IThemeService,
J
Johannes Rieken 已提交
104 105 106 107 108 109
		@IKeybindingService keybindingService: IKeybindingService,
		@IContextMenuService contextMenuService: IContextMenuService,
		@IConfigurationService configurationService: IConfigurationService
	) {
		super(options, keybindingService, contextMenuService, configurationService);

J
Johannes Rieken 已提交
110 111
		this._activeEditorOracle = _instantiationService.createInstance(ActiveEditorOracle);
		this._disposables.push(this._activeEditorOracle.onDidChangeActiveEditor(this._onEditor, this));
J
Johannes Rieken 已提交
112 113 114 115 116 117 118 119 120
		this._disposables.push(this._activeEditorOracle);
	}

	dispose(): void {
		dispose(this._disposables);
		super.dispose();
	}

	protected renderBody(container: HTMLElement): void {
J
Johannes Rieken 已提交
121 122 123 124 125 126 127

		dom.addClass(container, 'outline-panel');

		let inputContainer = dom.$('.outline-input');
		let treeContainer = dom.$('.outline-tree');
		dom.append(container, inputContainer, treeContainer);

J
Johannes Rieken 已提交
128 129
		this._input = new InputBox(inputContainer, null, { placeholder: localize('filter', "Filter") });
		this._input.disable();
J
Johannes Rieken 已提交
130
		this.disposables.push(attachInputBoxStyler(this._input, this._themeService));
131 132 133 134 135
		this.disposables.push(dom.addStandardDisposableListener(this._input.inputElement, 'keyup', event => {
			if (event.keyCode === KeyCode.DownArrow) {
				this._tree.domFocus();
			}
		}));
J
Johannes Rieken 已提交
136

J
Johannes Rieken 已提交
137 138
		const dataSource = new OutlineDataSource();
		const renderer = new OutlineRenderer();
J
Johannes Rieken 已提交
139 140 141 142
		this._treeComparator = new OutlineItemComparator();
		this._treeFilter = new OutlineItemFilter();
		this._tree = this._instantiationService.createInstance(WorkbenchTree, treeContainer, { dataSource, renderer, sorter: this._treeComparator, filter: this._treeFilter }, {});

J
Johannes Rieken 已提交
143
		this._disposables.push(this._tree, this._input);
J
Johannes Rieken 已提交
144 145 146
	}

	protected layoutBody(height: number): void {
J
Johannes Rieken 已提交
147
		this._tree.layout(height - 36);
J
Johannes Rieken 已提交
148 149 150 151 152
	}

	setVisible(visible: boolean): TPromise<void> {
		return super.setVisible(visible);
	}
J
Johannes Rieken 已提交
153

154 155
	getSecondaryActions(): IAction[] {
		let group = new RadioGroup([
J
Johannes Rieken 已提交
156 157 158
			new SimpleToggleAction(localize('sortByPosition', "Sort By: Position"), true, _ => this._onSortTypeChanged(OutlineItemCompareType.ByPosition)),
			new SimpleToggleAction(localize('sortByName', "Sort By: Name"), false, _ => this._onSortTypeChanged(OutlineItemCompareType.ByName)),
			new SimpleToggleAction(localize('sortByKind', "Sort By: Type"), false, _ => this._onSortTypeChanged(OutlineItemCompareType.ByKind)),
159
		]);
J
Johannes Rieken 已提交
160 161 162 163 164 165 166 167 168
		let result = [
			new SimpleToggleAction(localize('live', "Follow Cursor"), true, action => this._followCursor = action.checked),
			new Separator(),
			...group.actions,
		];

		this.disposables.push(...result);
		this.disposables.push(group);
		return result;
169 170 171 172 173 174 175 176 177
	}

	private _onSortTypeChanged(type: OutlineItemCompareType) {
		if (this._treeComparator.type !== type) {
			this._treeComparator.type = type;
			this._tree.refresh(undefined, true);
		}
	}

178
	private async _onEditor(editor: ICodeEditor): TPromise<void> {
J
Johannes Rieken 已提交
179
		dispose(this._editorDisposables);
180

J
Johannes Rieken 已提交
181
		this._editorDisposables = new Array();
182
		this._input.disable();
J
Johannes Rieken 已提交
183 184

		if (!editor) {
185 186
			// todo@joh show empty message
			return undefined;
J
Johannes Rieken 已提交
187 188
		}

189 190 191 192 193 194 195 196 197 198 199 200 201
		let textModel = editor.getModel();
		let oldModel = <OutlineModel>this._tree.getInput();
		let model = new OutlineModel(textModel.uri, getOutline(textModel));

		if (oldModel && oldModel.merge(model)) {
			model = oldModel;
			this._tree.refresh(undefined, true);

		} else {
			// persist state
			if (oldModel) {
				let state = OutlineTreeState.capture(this._tree);
				this._treeStates.set(oldModel.uri.toString(), state);
J
Johannes Rieken 已提交
202
			}
203 204 205 206
			await this._tree.setInput(model);
			let state = this._treeStates.get(model.uri.toString());
			OutlineTreeState.restore(this._tree, state);
		}
J
Johannes Rieken 已提交
207

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
		// wait for the actual model to work with...
		let itemGroup = await model.selected();

		this._input.enable();

		// feature: filter on type
		// on type -> update filters
		// on first type -> capture tree state
		// on erase -> restore captured tree state
		let beforePatternState: OutlineTreeState;
		this._editorDisposables.push(this._input.onDidChange(async pattern => {
			if (!beforePatternState) {
				beforePatternState = OutlineTreeState.capture(this._tree);
			}
			let item = itemGroup.updateMatches(pattern);
			await this._tree.refresh(undefined, true);
			if (item) {
				await this._tree.reveal(item);
				this._tree.setFocus(item, this);
				this._tree.setSelection([item], this);
				this._tree.expandAll(undefined /*all*/);
J
Johannes Rieken 已提交
229 230
			}

231 232 233 234 235
			if (!pattern && beforePatternState) {
				await OutlineTreeState.restore(this._tree, beforePatternState);
				beforePatternState = undefined;
			}
		}));
236

237 238 239 240 241
		// feature: reveal outline selection in editor
		// on change -> reveal/select defining range
		this._editorDisposables.push(this._tree.onDidChangeSelection(e => {
			if (e.payload === this) {
				return;
242
			}
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
			let [first] = e.selection;
			if (first instanceof OutlineItem) {
				let { range } = first.symbol.location;
				editor.revealRangeInCenterIfOutsideViewport(range, ScrollType.Smooth);
				editor.setSelection(Range.collapseToStart(range));
				// editor.focus();
			}
		}));

		// feature: reveal editor selection in outline
		this._editorDisposables.push(editor.onDidChangeCursorSelection(async e => {
			if (!this._followCursor || e.reason !== CursorChangeReason.Explicit) {
				return;
			}
			let item = itemGroup.getItemEnclosingPosition({
				lineNumber: e.selection.selectionStartLineNumber,
				column: e.selection.selectionStartColumn
			});
			if (item) {
				await this._tree.reveal(item);
				this._tree.setFocus(item, this);
				this._tree.setSelection([item], this);
			} else {
				this._tree.setSelection([], this);
			}
		}));
J
Johannes Rieken 已提交
269
	}
J
Johannes Rieken 已提交
270
}