forked from redhat-developer/vscode-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinlayHintsProvider.ts
More file actions
95 lines (82 loc) · 3 KB
/
inlayHintsProvider.ts
File metadata and controls
95 lines (82 loc) · 3 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
import { CancellationToken, EventEmitter, InlayHint, InlayHintKind, InlayHintsProvider, Range, TextDocument } from "vscode";
import * as ls from 'vscode-languageserver-protocol';
import { LanguageClient, RequestType } from "vscode-languageclient/node";
export class JavaInlayHintsProvider implements InlayHintsProvider {
private onDidChange = new EventEmitter<void>();
public onDidChangeInlayHints = this.onDidChange.event;
constructor(private client: LanguageClient) {
this.client.onRequest(InlayHintRefreshRequest.type, async () => {
this.onDidChange.fire();
});
}
public async provideInlayHints(document: TextDocument, range: Range, token: CancellationToken): Promise<InlayHint[]> {
const requestParams: InlayHintParams = {
textDocument: this.client.code2ProtocolConverter.asTextDocumentIdentifier(document),
range: this.client.code2ProtocolConverter.asRange(range)
};
try {
const values = await this.client.sendRequest(InlayHintRequest.type, requestParams, token);
if (token.isCancellationRequested) {
return [];
}
return asInlayHints(values, this.client);
} catch (error) {
return this.client.handleFailedRequest(InlayHintRequest.type, token, error, []);
}
}
}
/**
* A parameter literal used in inlay hints requests.
*
* @since 3.17.0 - proposed state
*/
export type InlayHintParams = /* WorkDoneProgressParams &*/ {
/**
* The text document.
*/
textDocument: ls.TextDocumentIdentifier;
/**
* The document range for which inlay hints should be computed.
*/
range: ls.Range;
};
/**
* Inlay hint information.
*
* @since 3.17.0 - proposed state
*/
export type LSInlayHint = {
/**
* The position of this hint.
*/
position: ls.Position;
/**
* The label of this hint. A human readable string or an array of
* InlayHintLabelPart label parts.
*
* *Note* that neither the string nor the label part can be empty.
*/
label: string; // label: string | InlayHintLabelPart[];
};
namespace InlayHintRequest {
export const type: RequestType<InlayHintParams, LSInlayHint[], any> = new RequestType('textDocument/inlayHint');
}
/**
* @since 3.17.0 - proposed state
*/
namespace InlayHintRefreshRequest {
export const type: RequestType<void, void, void> = new RequestType('workspace/inlayHint/refresh');
}
async function asInlayHints(values: LSInlayHint[] | undefined | null, client: LanguageClient, ): Promise<InlayHint[] | undefined> {
if (!Array.isArray(values)) {
return undefined;
}
return values.map(lsHint => asInlayHint(lsHint, client));
}
function asInlayHint(value: LSInlayHint, client: LanguageClient): InlayHint {
const label = value.label;
const result = new InlayHint(client.protocol2CodeConverter.asPosition(value.position), label);
result.paddingRight = true;
result.kind = InlayHintKind.Parameter;
return result;
}