Skip to content

Commit f57b552

Browse files
angelozerrdatho7561
authored andcommitted
Clickable InlayHint for Java type
Fixes #702 Signed-off-by: azerr <[email protected]>
1 parent b32a24b commit f57b552

File tree

1 file changed

+104
-57
lines changed

1 file changed

+104
-57
lines changed
Lines changed: 104 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,50 @@
1-
import { CancellationToken, EventEmitter, InlayHint, InlayHintKind, InlayHintsProvider, Range, TextDocument } from "vscode";
1+
import { CancellationToken, Command, EventEmitter, InlayHint, InlayHintKind, InlayHintLabelPart, InlayHintsProvider, MarkdownString, Range, TextDocument } from "vscode";
22
import * as ls from 'vscode-languageserver-protocol';
33
import { LanguageClient, RequestType } from "vscode-languageclient/node";
44

55
export class QuteInlayHintsProvider implements InlayHintsProvider {
66

7-
private onDidChange = new EventEmitter<void>();
8-
public onDidChangeInlayHints = this.onDidChange.event;
7+
private onDidChange = new EventEmitter<void>();
8+
public onDidChangeInlayHints = this.onDidChange.event;
99

10-
constructor(private client: LanguageClient) {
11-
this.client.onRequest(InlayHintRefreshRequest.type, async () => {
12-
this.onDidChange.fire();
13-
});
14-
}
10+
constructor(private client: LanguageClient) {
11+
this.client.onRequest(InlayHintRefreshRequest.type, async () => {
12+
this.onDidChange.fire();
13+
});
14+
}
1515

16-
public async provideInlayHints(document: TextDocument, range: Range, token: CancellationToken): Promise<InlayHint[]> {
17-
const requestParams: InlayHintParams = {
18-
textDocument: this.client.code2ProtocolConverter.asTextDocumentIdentifier(document),
19-
range: this.client.code2ProtocolConverter.asRange(range)
20-
};
21-
try {
22-
const values = await this.client.sendRequest(InlayHintRequest.type, requestParams, token);
23-
if (token.isCancellationRequested) {
24-
return [];
25-
}
26-
return asInlayHints(values, this.client);
27-
} catch (error) {
28-
return this.client.handleFailedRequest(InlayHintRequest.type, token, error);
29-
}
16+
public async provideInlayHints(document: TextDocument, range: Range, token: CancellationToken): Promise<InlayHint[]> {
17+
const requestParams: InlayHintParams = {
18+
textDocument: this.client.code2ProtocolConverter.asTextDocumentIdentifier(document),
19+
range: this.client.code2ProtocolConverter.asRange(range)
20+
};
21+
try {
22+
const values = await this.client.sendRequest(InlayHintRequest.type, requestParams, token);
23+
if (token.isCancellationRequested) {
24+
return [];
25+
}
26+
return asInlayHints(values, this.client);
27+
} catch (error) {
28+
return this.client.handleFailedRequest(InlayHintRequest.type, token, error);
3029
}
30+
}
3131
}
3232

3333
/**
3434
* A parameter literal used in inlay hints requests.
3535
*
3636
* @since 3.17.0 - proposed state
3737
*/
38-
export type InlayHintParams = /*WorkDoneProgressParams &*/ {
39-
/**
40-
* The text document.
41-
*/
42-
textDocument: ls.TextDocumentIdentifier;
43-
44-
/**
45-
* The document range for which inlay hints should be computed.
46-
*/
47-
range: ls.Range;
38+
export type InlayHintParams = /*WorkDoneProgressParams &*/ {
39+
/**
40+
* The text document.
41+
*/
42+
textDocument: ls.TextDocumentIdentifier;
43+
44+
/**
45+
* The document range for which inlay hints should be computed.
46+
*/
47+
range: ls.Range;
4848
};
4949

5050
/**
@@ -54,42 +54,89 @@ export class QuteInlayHintsProvider implements InlayHintsProvider {
5454
*/
5555
export type LSInlayHint = {
5656

57-
/**
58-
* The position of this hint.
59-
*/
60-
position: ls.Position;
61-
62-
/**
63-
* The label of this hint. A human readable string or an array of
64-
* InlayHintLabelPart label parts.
65-
*
66-
* *Note* that neither the string nor the label part can be empty.
67-
*/
68-
label: string; // label: string | InlayHintLabelPart[];
57+
/**
58+
* The position of this hint.
59+
*/
60+
position: ls.Position;
61+
62+
/**
63+
* The label of this hint. A human readable string or an array of
64+
* InlayHintLabelPart label parts.
65+
*
66+
* *Note* that neither the string nor the label part can be empty.
67+
*/
68+
label: string | LSInlayHintLabelPart[];
69+
};
70+
71+
export type LSInlayHintLabelPart = {
72+
73+
/**
74+
* The value of this label part.
75+
*/
76+
value: string;
77+
78+
/**
79+
* An optional command for this label part.
80+
*
81+
* Depending on the client capability `inlayHint.resolveSupport` clients
82+
* might resolve this property late using the resolve request.
83+
*/
84+
command?: ls.Command;
85+
86+
/**
87+
* The tooltip text when you hover over this label part. Depending on
88+
* the client capability `inlayHint.resolveSupport` clients might resolve
89+
* this property late using the resolve request.
90+
*/
91+
tooltip?: string | ls.MarkupContent;
92+
6993
};
7094

7195
namespace InlayHintRequest {
72-
export const type: RequestType<InlayHintParams, LSInlayHint[], any> = new RequestType('textDocument/inlayHint');
96+
export const type: RequestType<InlayHintParams, LSInlayHint[], any> = new RequestType('textDocument/inlayHint');
7397
}
7498

7599
/**
76100
* @since 3.17.0 - proposed state
77101
*/
78102
namespace InlayHintRefreshRequest {
79-
export const type: RequestType<void, void, void> = new RequestType('workspace/inlayHint/refresh');
103+
export const type: RequestType<void, void, void> = new RequestType('workspace/inlayHint/refresh');
80104
}
81105

82-
async function asInlayHints(values: LSInlayHint[] | undefined | null, client: LanguageClient, ): Promise<InlayHint[] | undefined> {
83-
if (!Array.isArray(values)) {
84-
return undefined;
85-
}
86-
return values.map(lsHint => asInlayHint(lsHint, client));
106+
async function asInlayHints(values: LSInlayHint[] | undefined | null, client: LanguageClient,): Promise<InlayHint[] | undefined> {
107+
if (!Array.isArray(values)) {
108+
return undefined;
109+
}
110+
return values.map(lsHint => asInlayHint(lsHint, client));
111+
}
112+
113+
function asInlayHint(item: LSInlayHint, client: LanguageClient): InlayHint {
114+
const label = typeof item.label === 'string'
115+
? item.label
116+
: item.label.map(asInlayHintLabelPart);
117+
const result = new InlayHint(client.protocol2CodeConverter.asPosition(item.position), label);
118+
result.paddingRight = true;
119+
result.kind = InlayHintKind.Parameter;
120+
return result;
121+
}
122+
123+
function asInlayHintLabelPart(item: LSInlayHintLabelPart): InlayHintLabelPart {
124+
const result = new InlayHintLabelPart(item.value);
125+
if (item.command !== undefined) { result.command = asCommand(item.command); }
126+
if (item.tooltip !== undefined) { result.tooltip = asTooltip(item.tooltip); }
127+
return result;
128+
}
129+
130+
function asCommand(item: ls.Command): Command {
131+
const result = { title: item.title, command: item.command } as Command;
132+
if (item.arguments) { result.arguments = item.arguments; }
133+
return result;
87134
}
88135

89-
function asInlayHint(value: LSInlayHint, client: LanguageClient): InlayHint {
90-
const label = value.label;
91-
const result = new InlayHint(client.protocol2CodeConverter.asPosition(value.position), label);
92-
result.paddingRight = true;
93-
result.kind = InlayHintKind.Parameter;
94-
return result;
136+
function asTooltip(value: string | ls.MarkupContent): string | MarkdownString {
137+
if (typeof value === 'string') {
138+
return value;
139+
}
140+
const result = new MarkdownString(value.value);
141+
return result;
95142
}

0 commit comments

Comments
 (0)