-
Notifications
You must be signed in to change notification settings - Fork 328
/
Copy pathcallHierarchy.proposed.ts
234 lines (199 loc) · 9.06 KB
/
callHierarchy.proposed.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
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
'use strict';
import * as UUID from './utils/uuid';
import * as Is from './utils/is';
import {
languages as Languages, Disposable, TextDocument, ProviderResult, Position as VPosition, CancellationToken as VCancellationToken,
CallHierarchyDirection as VCallHierarchyDirection, CallHierarchyItem as VCallHierarchyItem, CallHierarchyItemProvider, Location as VLocation
} from 'vscode';
import {
ClientCapabilities, CancellationToken, ServerCapabilities, TextDocumentRegistrationOptions, DocumentSelector, StaticRegistrationOptions, Proposed,
Range
} from 'vscode-languageserver-protocol';
import { TextDocumentFeature, BaseLanguageClient, Middleware } from './client';
import { CallHierarchyItem } from 'vscode-languageserver-protocol/lib/protocol.callHierarchy.proposed';
function ensure<T, K extends keyof T>(target: T, key: K): T[K] {
if (target[key] === void 0) {
target[key] = {} as any;
}
return target[key];
}
export interface ProvideCallHierachySignature {
(document: TextDocument, positions: VPosition, token: CancellationToken): ProviderResult<VCallHierarchyItem>;
}
export interface ResolveCallHierarchySignature {
(item: VCallHierarchyItem, direction: VCallHierarchyDirection, token: CancellationToken): ProviderResult<[VCallHierarchyItem, VLocation[]][]>;
}
export interface CallHierarchyMiddleware {
provideCallHierarchy?: (this: void, document: TextDocument, positions: VPosition, token: CancellationToken, next: ProvideCallHierachySignature) => ProviderResult<VCallHierarchyItem>;
resolveCallHierarchy?: (this: void,item: VCallHierarchyItem, direction: VCallHierarchyDirection, token: CancellationToken, next: ResolveCallHierarchySignature) => ProviderResult<[VCallHierarchyItem, VLocation[]][]>;
}
class CallHierarchyProvider implements CallHierarchyItemProvider{
private _middleware: Middleware & CallHierarchyMiddleware;
constructor(private client: BaseLanguageClient) {
this._middleware = client.clientOptions.middleware!;
}
public provideCallHierarchyItem(document: TextDocument, postion: VPosition, token: VCancellationToken): ProviderResult<VCallHierarchyItem> {
return this._middleware.provideCallHierarchy
? this._middleware.provideCallHierarchy(document, postion, token, (document, position, token) => this.doProvideCallHierarchyItem(document, position, token))
: this.doProvideCallHierarchyItem(document, postion, token);
}
public resolveCallHierarchyItem(item: VCallHierarchyItem, direction: VCallHierarchyDirection, token: VCancellationToken): ProviderResult<[VCallHierarchyItem, VLocation[]][]> {
return this._middleware.resolveCallHierarchy
? this._middleware.resolveCallHierarchy(item, direction, token, (item, direction, token) => this.doResolveCallHierarchyItem(item, direction, token))
: this.doResolveCallHierarchyItem(item, direction, token);
}
private doProvideCallHierarchyItem(document: TextDocument, position: VPosition, token: VCancellationToken): ProviderResult<VCallHierarchyItem> {
const client = this.client;
return client.sendRequest(Proposed.CallHierarchyRequest.type, this.asCallHierarchyParams(document, position), token).then((values) => {
if (!Array.isArray(values) || values.length === 0) {
return undefined;
}
return this.asCallHierarchyItem(values[0].from);
});
}
private doResolveCallHierarchyItem(item: VCallHierarchyItem, direction: VCallHierarchyDirection, token: VCancellationToken): ProviderResult<[VCallHierarchyItem, VLocation[]][]> {
const client = this.client;
const converter = client.code2ProtocolConverter;
const params: Proposed.CallHierarchyParams = {
textDocument: {
uri: converter.asUri(item.uri)
},
position: converter.asPosition(item.selectionRange.start),
direction: direction
};
const makeKey = (item: CallHierarchyItem): string => {
let key: { uri: string; range: Range } = {
uri: item.uri,
range: {
start: {
line: item.selectionRange.start.line,
character: item.selectionRange.start.character
},
end: {
line: item.selectionRange.end.line,
character: item.selectionRange.end.character
}
}
}
return JSON.stringify(key);
};
return client.sendRequest(Proposed.CallHierarchyRequest.type, params, token).then(values => {
if (!Array.isArray(values) || values.length === 0) {
return undefined;
}
const result: Map<string, [VCallHierarchyItem, VLocation[]]> = new Map();
for (let relation of values) {
let key = makeKey(relation.from);
let resultItem: [VCallHierarchyItem, VLocation[]] | undefined = result.get(key);
if (resultItem === undefined) {
const callItem = this.asCallHierarchyItem(relation.from);
resultItem = [callItem, []];
result.set(key, resultItem);
}
resultItem[1].push(this.asLocation(relation.to));
}
return Array.from(result.values());
});
}
private asCallHierarchyParams(document: TextDocument, postion: VPosition): Proposed.CallHierarchyParams {
const converter = this.client.code2ProtocolConverter;
return {
textDocument: converter.asTextDocumentIdentifier(document),
position: converter.asPosition(postion),
direction: Proposed.CallHierarchyDirection.CallsFrom
};
}
private asCallHierarchyItem(value: Proposed.CallHierarchyItem): VCallHierarchyItem {
const converter = this.client.protocol2CodeConverter;
return new VCallHierarchyItem(
converter.asSymbolKind(value.kind),
value.name,
value.detail || '',
converter.asUri(value.uri),
converter.asRange(value.range),
converter.asRange(value.selectionRange)
);
}
private asLocation(value: Proposed.CallHierarchyItem): VLocation {
const converter = this.client.protocol2CodeConverter;
return new VLocation(converter.asUri(value.uri), converter.asRange(value.selectionRange));
}
}
export class CallHierarchyFeature extends TextDocumentFeature<TextDocumentRegistrationOptions> {
constructor(client: BaseLanguageClient) {
super(client, Proposed.CallHierarchyRequest.type);
}
public fillClientCapabilities(cap: ClientCapabilities): void {
let capabilites: ClientCapabilities & Proposed.CallHierarchyClientCapabilities = cap as ClientCapabilities & Proposed.CallHierarchyClientCapabilities;
let capability = ensure(ensure(capabilites, 'textDocument')!, 'callHierarchy')!;
capability.dynamicRegistration = true;
}
public initialize(cap: ServerCapabilities, documentSelector: DocumentSelector): void {
let capabilities: ServerCapabilities & Proposed.CallHierarchyServerCapabilities = cap;
if (!capabilities.callHierarchyProvider) {
return;
}
const implCapabilities = capabilities.callHierarchyProvider as TextDocumentRegistrationOptions & StaticRegistrationOptions;
const id = Is.string(implCapabilities.id) && implCapabilities.id.length > 0 ? implCapabilities.id : UUID.generateUuid();
const selector = implCapabilities.documentSelector || documentSelector;
if (selector) {
this.register(this.messages, {
id,
registerOptions: Object.assign({}, { documentSelector: selector })
});
}
}
protected registerLanguageProvider(options: TextDocumentRegistrationOptions): Disposable {
let client = this._client;
let provider = new CallHierarchyProvider(client);
return Languages.registerCallHierarchyProvider(options.documentSelector!, provider);
}
}
declare module 'vscode' {
export enum CallHierarchyDirection {
CallsFrom = 1,
CallsTo = 2,
}
export class CallHierarchyItem {
kind: SymbolKind;
name: string;
detail?: string;
uri: Uri;
range: Range;
selectionRange: Range;
constructor(kind: SymbolKind, name: string, detail: string, uri: Uri, range: Range, selectionRange: Range);
}
export interface CallHierarchyItemProvider {
/**
* Given a document and position compute a call hierarchy item. This is justed as
* anchor for call hierarchy and then `resolveCallHierarchyItem` is being called.
*/
provideCallHierarchyItem(
document: TextDocument,
postion: Position,
token: CancellationToken
): ProviderResult<CallHierarchyItem>;
/**
* Resolve a call hierarchy item, e.g. compute all calls from or to a function.
* The result is an array of item/location-tuples. The location in the returned tuples
* is always relative to the "caller" with the caller either being the provided item or
* the returned item.
*
* @param item A call hierarchy item previously returned from `provideCallHierarchyItem` or `resolveCallHierarchyItem`
* @param direction Resolve calls from a function or calls to a function
* @param token A cancellation token
*/
resolveCallHierarchyItem(
item: CallHierarchyItem,
direction: CallHierarchyDirection,
token: CancellationToken
): ProviderResult<[CallHierarchyItem, Location[]][]>;
}
export namespace languages {
export function registerCallHierarchyProvider(selector: DocumentSelector, provider: CallHierarchyItemProvider): Disposable;
}
}