-
Notifications
You must be signed in to change notification settings - Fork 113
/
type-hierarchy.ts
339 lines (297 loc) · 11.9 KB
/
type-hierarchy.ts
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
// This file implements the client side of the proposed type hierarchy
// extension to LSP. The proposal can be found at
// https://github.com/microsoft/vscode-languageserver-node/pull/426.
// Clangd supports the server side of this protocol.
// The feature allows querying the base and derived classes of the
// symbol under the cursor, which are visualized in a tree view.
import * as vscode from 'vscode';
import * as vscodelc from 'vscode-languageclient/node';
import {ClangdContext} from './clangd-context';
export function activate(context: ClangdContext) {
const feature = new TypeHierarchyFeature(context);
context.client.registerFeature(feature);
}
export namespace TypeHierarchyDirection {
export const Children = 0;
export const Parents = 1;
export const Both = 2;
}
type TypeHierarchyDirection = 0|1|2;
interface TypeHierarchyParams extends vscodelc.TextDocumentPositionParams {
resolve?: number;
direction: TypeHierarchyDirection;
}
interface TypeHierarchyItem {
name: string;
detail?: string;
kind: vscodelc.SymbolKind;
deprecated?: boolean;
uri: string;
range: vscodelc.Range;
selectionRange: vscodelc.Range;
parents?: TypeHierarchyItem[];
children?: TypeHierarchyItem[];
data?: any;
}
namespace TypeHierarchyRequest {
export const type =
new vscodelc
.RequestType<TypeHierarchyParams, TypeHierarchyItem|null, void, void>(
'textDocument/typeHierarchy');
}
interface ResolveTypeHierarchyItemParams {
item: TypeHierarchyItem;
resolve: number;
direction: TypeHierarchyDirection;
}
export namespace ResolveTypeHierarchyRequest {
export const type =
new vscodelc.RequestType<ResolveTypeHierarchyItemParams,
TypeHierarchyItem|null, void, void>(
'typeHierarchy/resolve');
}
// A dummy node used to indicate that a node has multiple parents
// when we are in Children mode.
const dummyNode: TypeHierarchyItem = {
name: '[multiple parents]',
kind: vscodelc.SymbolKind.Null,
uri: '',
range: {start: {line: 0, character: 0}, end: {line: 0, character: 0}},
selectionRange:
{start: {line: 0, character: 0}, end: {line: 0, character: 0}},
};
class TypeHierarchyTreeItem extends vscode.TreeItem {
constructor(item: TypeHierarchyItem) {
super(item.name);
if (item.children) {
if (item.children.length == 0) {
this.collapsibleState = vscode.TreeItemCollapsibleState.None;
} else {
this.collapsibleState = vscode.TreeItemCollapsibleState.Expanded;
}
} else {
this.collapsibleState = vscode.TreeItemCollapsibleState.Collapsed;
}
// Do not register actions for the dummy node.
if (item == dummyNode) {
return;
}
// Make the item respond to a single-click by navigating to the
// definition of the class.
this.command = {
arguments: [item],
command: 'clangd.typeHierarchy.gotoItem',
title: 'Go to'
};
}
}
class TypeHierarchyFeature implements vscodelc.StaticFeature {
private serverSupportsTypeHierarchy = false;
private state: vscodelc.State;
constructor(context: ClangdContext) {
new TypeHierarchyProvider(context);
context.subscriptions.push(context.client.onDidChangeState(stateChange => {
this.state = stateChange.newState;
this.recomputeEnableTypeHierarchy();
}));
}
fillClientCapabilities(capabilities: vscodelc.ClientCapabilities) {}
initialize(capabilities: vscodelc.ServerCapabilities,
documentSelector: vscodelc.DocumentSelector|undefined) {
const serverCapabilities: vscodelc.ServerCapabilities&
{typeHierarchyProvider?: boolean} = capabilities;
if (serverCapabilities.typeHierarchyProvider) {
this.serverSupportsTypeHierarchy = true;
this.recomputeEnableTypeHierarchy();
}
}
private recomputeEnableTypeHierarchy() {
if (this.state == vscodelc.State.Running) {
vscode.commands.executeCommand('setContext', 'clangd.enableTypeHierarchy',
this.serverSupportsTypeHierarchy);
} else if (this.state == vscodelc.State.Stopped) {
vscode.commands.executeCommand('setContext', 'clangd.enableTypeHierarchy',
false);
}
}
}
class TypeHierarchyProvider implements
vscode.TreeDataProvider<TypeHierarchyItem> {
private client: vscodelc.LanguageClient;
private _onDidChangeTreeData =
new vscode.EventEmitter<TypeHierarchyItem|null>();
readonly onDidChangeTreeData = this._onDidChangeTreeData.event;
private root?: TypeHierarchyItem;
private direction: TypeHierarchyDirection;
private treeView: vscode.TreeView<TypeHierarchyItem>;
// The item on which the type hierarchy operation was invoked.
// May be different from the root.
private startingItem: TypeHierarchyItem;
constructor(context: ClangdContext) {
this.client = context.client;
context.subscriptions.push(vscode.window.registerTreeDataProvider(
'clangd.typeHierarchyView', this));
context.subscriptions.push(vscode.commands.registerTextEditorCommand(
'clangd.typeHierarchy', this.reveal, this));
context.subscriptions.push(vscode.commands.registerCommand(
'clangd.typeHierarchy.close', this.close, this));
context.subscriptions.push(vscode.commands.registerCommand(
'clangd.typeHierarchy.gotoItem', this.gotoItem, this));
context.subscriptions.push(vscode.commands.registerCommand(
'clangd.typeHierarchy.viewParents',
() => this.setDirection(TypeHierarchyDirection.Parents)));
context.subscriptions.push(vscode.commands.registerCommand(
'clangd.typeHierarchy.viewChildren',
() => this.setDirection(TypeHierarchyDirection.Children)));
this.treeView = vscode.window.createTreeView('clangd.typeHierarchyView',
{treeDataProvider: this});
context.subscriptions.push(this.treeView);
// Show children by default.
this.direction = TypeHierarchyDirection.Children;
}
public async gotoItem(item: TypeHierarchyItem) {
const uri = vscode.Uri.parse(item.uri);
const range =
this.client.protocol2CodeConverter.asRange(item.selectionRange);
const doc = await vscode.workspace.openTextDocument(uri);
let editor: vscode.TextEditor|undefined;
if (doc) {
editor = await vscode.window.showTextDocument(doc, undefined);
} else {
editor = vscode.window.activeTextEditor;
}
if (!editor) {
return;
}
editor.revealRange(range, vscode.TextEditorRevealType.InCenter);
editor.selection = new vscode.Selection(range.start, range.end);
}
public async setDirection(direction: TypeHierarchyDirection) {
this.direction = direction;
// Recompute the root based on the starting item.
this.root = this.computeRoot();
this._onDidChangeTreeData.fire(null);
// Re-focus the starting item, which may not be the root.
this.treeView.reveal(this.startingItem, {focus: true})
.then(() => {}, (reason) => {
// Sometimes TreeView.reveal() fails. It's unclear why, and it does
// not appear to have any visible effects, but vscode complains if you
// don't handle the rejection promise, so we do so and log a warning.
console.log('Warning: TreeView.reveal() failed for reason: ' +
reason);
});
}
public getTreeItem(element: TypeHierarchyItem): vscode.TreeItem {
return new TypeHierarchyTreeItem(element);
}
public getParent(element: TypeHierarchyItem): TypeHierarchyItem|null {
// This function is implemented so that VSCode lets us call
// this.treeView.reveal().
if (element.parents) {
if (element.parents.length == 1) {
return element.parents[0];
} else if (element.parents.length > 1) {
return dummyNode;
}
}
return null;
}
public async getChildren(element
?: TypeHierarchyItem): Promise<TypeHierarchyItem[]> {
if (!this.root)
return [];
if (!element)
return [this.root];
if (this.direction == TypeHierarchyDirection.Parents) {
// Clangd always resolves parents eagerly, so just return them.
return element.parents;
}
// Otherwise, this.direction == Children.
if (!element.children) {
// Children are not resolved yet, resolve them now.
const resolved =
await this.client.sendRequest(ResolveTypeHierarchyRequest.type, {
item: element,
direction: TypeHierarchyDirection.Children,
resolve: 1
});
element.children = resolved.children;
}
return element.children;
}
private computeRoot(): TypeHierarchyItem {
// In Parents mode, the root is always the starting item.
if (this.direction == TypeHierarchyDirection.Parents) {
return this.startingItem;
}
// In Children mode, we also include base classes of
// the starting item as parents. If we encounter a class
// with multiple bases, we show a dummy node with the label
// "[multiple parents]" instead.
let root = this.startingItem;
while (root.parents && root.parents.length == 1) {
root = root.parents[0];
}
if (root.parents && root.parents.length > 1) {
dummyNode.children = [root];
// Do not set "root.parents = [dummyNode]".
// This would discard the real parents and we'd have to re-query
// them if entering Parents mode.
// Instead, we teach getParent() to return the dummy node if
// there are multiple parents.
root = dummyNode;
}
return root;
}
private async reveal(editor: vscode.TextEditor) {
// This makes the type hierarchy view visible by causing the condition
// "when": "extension.vscode-clangd.typeHierarchyVisible" from
// package.json to evaluate to true.
vscode.commands.executeCommand('setContext', 'clangd.typeHierarchyVisible',
true);
const item = await this.client.sendRequest(TypeHierarchyRequest.type, {
...this.client.code2ProtocolConverter.asTextDocumentPositionParams(
editor.document, editor.selection.active),
// Resolve up to 5 initial levels. Any additional levels will be resolved
// on the fly if the user expands the tree item.
resolve: 5,
// Resolve both directions initially. That way, if the user switches
// to the Parents view, we have the data already. Note that clangd
// does not support resolving parents via typeHierarchy/resolve,
// so otherwise we'd have to remember the TextDocumentPositionParams
// to make another textDocument/typeHierarchy request when switching
// to Parents view.
direction: TypeHierarchyDirection.Both
});
if (item) {
this.startingItem = item;
this.root = this.computeRoot();
this._onDidChangeTreeData.fire(null);
// This focuses the "explorer" view container which contains the
// type hierarchy view.
vscode.commands.executeCommand('workbench.view.explorer');
// This expands and focuses the type hierarchy view.
// Focus the item on which the operation was invoked, not the
// root (which could be its ancestor or the dummy node).
this.treeView.reveal(this.startingItem, {focus: true})
.then(() => {}, (reason) => {
// Sometimes TreeView.reveal() fails. It's unclear why, and it does
// not appear to have any visible effects, but vscode complains if
// you don't handle the rejection promise, so we do so and log a
// warning.
console.log('Warning: TreeView.reveal() failed for reason: ' +
reason);
});
} else {
vscode.window.showInformationMessage(
'No type hierarchy available for selection');
}
}
private close() {
// Hide the type hierarchy view.
vscode.commands.executeCommand('setContext', 'clangd.typeHierarchyVisible',
false);
this.root = undefined;
this._onDidChangeTreeData.fire(null);
}
}