-
Notifications
You must be signed in to change notification settings - Fork 40.3k
Expand file tree
/
Copy pathtree.ts
More file actions
352 lines (282 loc) · 10.7 KB
/
tree.ts
File metadata and controls
352 lines (282 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import { EditorHighlights } from './highlights';
import { Navigation } from './navigation';
import { SymbolItemDragAndDrop, SymbolTreeInput } from './references-view';
import { ContextKey, isValidRequestPosition, WordAnchor } from './utils';
export class SymbolsTree {
readonly viewId = 'references-view.tree';
private readonly _ctxIsActive = new ContextKey<boolean>('reference-list.isActive');
private readonly _ctxHasResult = new ContextKey<boolean>('reference-list.hasResult');
private readonly _ctxInputSource = new ContextKey<string>('reference-list.source');
private readonly _history = new TreeInputHistory(this);
private readonly _provider = new TreeDataProviderDelegate();
private readonly _dnd = new TreeDndDelegate();
private readonly _tree: vscode.TreeView<unknown>;
private readonly _navigation: Navigation;
private _input?: SymbolTreeInput<unknown>;
private _sessionDisposable?: vscode.Disposable;
constructor() {
this._tree = vscode.window.createTreeView<unknown>(this.viewId, {
treeDataProvider: this._provider,
showCollapseAll: true,
dragAndDropController: this._dnd
});
this._navigation = new Navigation(this._tree);
}
dispose(): void {
this._history.dispose();
this._tree.dispose();
this._sessionDisposable?.dispose();
}
getInput(): SymbolTreeInput<unknown> | undefined {
return this._input;
}
async setInput(input: SymbolTreeInput<unknown>) {
if (!await isValidRequestPosition(input.location.uri, input.location.range.start)) {
this.clearInput();
return;
}
this._ctxInputSource.set(input.contextValue);
this._ctxIsActive.set(true);
this._ctxHasResult.set(true);
vscode.commands.executeCommand(`${this.viewId}.focus`);
const newInputKind = !this._input || Object.getPrototypeOf(this._input) !== Object.getPrototypeOf(input);
this._input = input;
this._sessionDisposable?.dispose();
this._tree.title = input.title;
this._tree.message = newInputKind ? undefined : this._tree.message;
const modelPromise = Promise.resolve(input.resolve());
// set promise to tree data provider to trigger tree loading UI
this._provider.update(modelPromise.then(model => model?.provider ?? this._history));
this._dnd.update(modelPromise.then(model => model?.dnd));
const model = await modelPromise;
if (this._input !== input) {
return;
}
if (!model) {
this.clearInput();
return;
}
this._history.add(input);
this._tree.message = model.message;
// navigation
this._navigation.update(model.navigation);
// reveal & select
const selection = model.navigation?.nearest(input.location.uri, input.location.range.start);
if (selection && this._tree.visible) {
await this._tree.reveal(selection, { select: true, focus: true, expand: true });
}
const disposables: vscode.Disposable[] = [];
// editor highlights
let highlights: EditorHighlights<unknown> | undefined;
if (model.highlights) {
highlights = new EditorHighlights(this._tree, model.highlights);
disposables.push(highlights);
}
// listener
if (model.provider.onDidChangeTreeData) {
disposables.push(model.provider.onDidChangeTreeData(() => {
this._tree.title = input.title;
this._tree.message = model.message;
highlights?.update();
}));
}
if (typeof model.dispose === 'function') {
disposables.push(new vscode.Disposable(() => model.dispose!()));
}
this._sessionDisposable = vscode.Disposable.from(...disposables);
}
clearInput(): void {
this._sessionDisposable?.dispose();
this._input = undefined;
this._ctxHasResult.set(false);
this._ctxInputSource.reset();
this._tree.title = vscode.l10n.t('References');
this._tree.message = this._history.size === 0
? vscode.l10n.t('No results.')
: vscode.l10n.t('No results. Try running a previous search again:');
this._provider.update(Promise.resolve(this._history));
}
}
// --- tree data
interface ActiveTreeDataProviderWrapper {
provider: Promise<vscode.TreeDataProvider<any>>;
}
class TreeDataProviderDelegate implements vscode.TreeDataProvider<undefined> {
provider?: Promise<vscode.TreeDataProvider<any>>;
private _sessionDispoables?: vscode.Disposable;
private _onDidChange = new vscode.EventEmitter<any>();
readonly onDidChangeTreeData = this._onDidChange.event;
update(provider: Promise<vscode.TreeDataProvider<any>>) {
this._sessionDispoables?.dispose();
this._sessionDispoables = undefined;
this._onDidChange.fire(undefined);
this.provider = provider;
provider.then(value => {
if (this.provider === provider && value.onDidChangeTreeData) {
this._sessionDispoables = value.onDidChangeTreeData(this._onDidChange.fire, this._onDidChange);
}
}).catch(err => {
this.provider = undefined;
console.error(err);
});
}
async getTreeItem(element: unknown) {
this._assertProvider();
return (await this.provider).getTreeItem(element);
}
async getChildren(parent?: unknown | undefined) {
this._assertProvider();
return (await this.provider).getChildren(parent);
}
async getParent(element: unknown) {
this._assertProvider();
const provider = await this.provider;
return provider.getParent ? provider.getParent(element) : undefined;
}
private _assertProvider(): asserts this is ActiveTreeDataProviderWrapper {
if (!this.provider) {
throw new Error('MISSING provider');
}
}
}
// --- tree dnd
class TreeDndDelegate implements vscode.TreeDragAndDropController<undefined> {
private _delegate: SymbolItemDragAndDrop<undefined> | undefined;
readonly dropMimeTypes: string[] = [];
readonly dragMimeTypes: string[] = ['text/uri-list'];
update(delegate: Promise<SymbolItemDragAndDrop<unknown> | undefined>) {
this._delegate = undefined;
delegate.then(value => this._delegate = value);
}
handleDrag(source: undefined[], data: vscode.DataTransfer) {
if (this._delegate) {
const urls: string[] = [];
for (const item of source) {
const uri = this._delegate.getDragUri(item);
if (uri) {
urls.push(uri.toString());
}
}
if (urls.length > 0) {
data.set('text/uri-list', new vscode.DataTransferItem(urls.join('\r\n')));
}
}
}
handleDrop(): void | Thenable<void> {
throw new Error('Method not implemented.');
}
}
// --- history
class HistoryItem {
readonly description: string;
constructor(
readonly key: string,
readonly word: string,
readonly anchor: WordAnchor,
readonly input: SymbolTreeInput<unknown>,
) {
this.description = `${vscode.workspace.asRelativePath(input.location.uri)} • ${input.title.toLocaleLowerCase()}`;
}
}
class TreeInputHistory implements vscode.TreeDataProvider<HistoryItem>{
private readonly _onDidChangeTreeData = new vscode.EventEmitter<HistoryItem | undefined>();
readonly onDidChangeTreeData = this._onDidChangeTreeData.event;
private readonly _disposables: vscode.Disposable[] = [];
private readonly _ctxHasHistory = new ContextKey<boolean>('reference-list.hasHistory');
private readonly _inputs = new Map<string, HistoryItem>();
constructor(private readonly _tree: SymbolsTree) {
this._disposables.push(
vscode.commands.registerCommand('references-view.clear', () => _tree.clearInput()),
vscode.commands.registerCommand('references-view.clearHistory', () => {
this.clear();
_tree.clearInput();
}),
vscode.commands.registerCommand('references-view.refind', (item) => {
if (item instanceof HistoryItem) {
this._reRunHistoryItem(item);
}
}),
vscode.commands.registerCommand('references-view.refresh', () => {
const item = Array.from(this._inputs.values()).pop();
if (item) {
this._reRunHistoryItem(item);
}
}),
vscode.commands.registerCommand('_references-view.showHistoryItem', async (item) => {
if (item instanceof HistoryItem) {
const position = item.anchor.guessedTrackedPosition() ?? item.input.location.range.start;
return vscode.commands.executeCommand('vscode.open', item.input.location.uri, { selection: new vscode.Range(position, position) });
}
}),
vscode.commands.registerCommand('references-view.pickFromHistory', async () => {
interface HistoryPick extends vscode.QuickPickItem {
item: HistoryItem;
}
const entries = await this.getChildren();
const picks = entries.map(item => <HistoryPick>{
label: item.word,
description: item.description,
item
});
const pick = await vscode.window.showQuickPick(picks, { placeHolder: vscode.l10n.t('Select previous reference search') });
if (pick) {
this._reRunHistoryItem(pick.item);
}
}),
);
}
dispose(): void {
vscode.Disposable.from(...this._disposables).dispose();
this._onDidChangeTreeData.dispose();
}
private _reRunHistoryItem(item: HistoryItem): void {
this._inputs.delete(item.key);
const newPosition = item.anchor.guessedTrackedPosition();
let newInput = item.input;
// create a new input when having a tracked position which is
// different than the original position.
if (newPosition && !item.input.location.range.start.isEqual(newPosition)) {
newInput = item.input.with(new vscode.Location(item.input.location.uri, newPosition));
}
this._tree.setInput(newInput);
}
async add(input: SymbolTreeInput<unknown>) {
const doc = await vscode.workspace.openTextDocument(input.location.uri);
const anchor = new WordAnchor(doc, input.location.range.start);
const range = doc.getWordRangeAtPosition(input.location.range.start) ?? doc.getWordRangeAtPosition(input.location.range.start, /[^\s]+/);
const word = range ? doc.getText(range) : '???';
const item = new HistoryItem(JSON.stringify([range?.start ?? input.location.range.start, input.location.uri, input.title]), word, anchor, input);
// use filo-ordering of native maps
this._inputs.delete(item.key);
this._inputs.set(item.key, item);
this._ctxHasHistory.set(true);
}
clear(): void {
this._inputs.clear();
this._ctxHasHistory.set(false);
this._onDidChangeTreeData.fire(undefined);
}
get size() {
return this._inputs.size;
}
// --- tree data provider
getTreeItem(item: HistoryItem): vscode.TreeItem {
const result = new vscode.TreeItem(item.word);
result.description = item.description;
result.command = { command: '_references-view.showHistoryItem', arguments: [item], title: vscode.l10n.t('Rerun') };
result.collapsibleState = vscode.TreeItemCollapsibleState.None;
result.contextValue = 'history-item';
return result;
}
getChildren() {
return Promise.all([...this._inputs.values()].reverse());
}
getParent() {
return undefined;
}
}