Skip to content

Commit 1c0d6bb

Browse files
committed
support goto definition for inlay hints
1 parent fa80d44 commit 1c0d6bb

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

Extension/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* Add support for passing an additional parameter to `C_Cpp.ConfigurationSelect` command. [PR #12993](https://github.com/microsoft/vscode-cpptools/pull/12993)
1010
* Thank you for the contribution. [@adrianstephens](https://github.com/adrianstephens)
1111
* Update clang-format and clang-tidy from 19.1.2 to 19.1.5.
12+
* Add support for providing the definition with inlay hint for the specific symbol. [#13010](https://github.com/microsoft/vscode-cpptools/issues/13010)
1213

1314
### Bug Fixes
1415
* Increase clang-format timeout from 10 seconds to 30 seconds. [#10213](https://github.com/microsoft/vscode-cpptools/issues/10213)

Extension/src/LanguageServer/Providers/inlayHintProvider.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ export interface CppInlayHint {
3232
leftPadding: boolean;
3333
rightPadding: boolean;
3434
identifierLength: number;
35+
definitionUri?: vscode.Uri;
36+
definitionLine?: number;
37+
definitionCharacter?: number;
3538
}
3639

3740
enum InlayHintKind {
@@ -204,10 +207,11 @@ export class InlayHintsProvider implements vscode.InlayHintsProvider {
204207
const resolvedHints: vscode.InlayHint[] = [];
205208
for (const hint of hints) {
206209
const showOnLeft: boolean = settings.inlayHintsAutoDeclarationTypesShowOnLeft && hint.identifierLength > 0;
210+
let labelPart: vscode.InlayHintLabelPart = this.createInlayHintLabelPart(hint, showOnLeft ? hint.label : ": " + hint.label);
207211
const inlayHint: vscode.InlayHint = new vscode.InlayHint(
208212
new vscode.Position(hint.line, hint.character +
209213
(showOnLeft ? 0 : hint.identifierLength)),
210-
showOnLeft ? hint.label : ": " + hint.label,
214+
[labelPart],
211215
vscode.InlayHintKind.Type);
212216
inlayHint.paddingRight = showOnLeft || hint.rightPadding;
213217
inlayHint.paddingLeft = showOnLeft && hint.leftPadding;
@@ -244,14 +248,31 @@ export class InlayHintsProvider implements vscode.InlayHintsProvider {
244248
if (paramHintLabel === "" && refOperatorString === "") {
245249
continue;
246250
}
247-
251+
let labelPart: vscode.InlayHintLabelPart = this.createInlayHintLabelPart(hint, refOperatorString + paramHintLabel + ":");
248252
const inlayHint: vscode.InlayHint = new vscode.InlayHint(
249253
new vscode.Position(hint.line, hint.character),
250-
refOperatorString + paramHintLabel + ":",
254+
[labelPart],
251255
vscode.InlayHintKind.Parameter);
252256
inlayHint.paddingRight = true;
253257
resolvedHints.push(inlayHint);
254258
}
255259
return resolvedHints;
256260
}
261+
262+
private createInlayHintLabelPart(hint: CppInlayHint, hintLabel: string): vscode.InlayHintLabelPart {
263+
let labelPart: vscode.InlayHintLabelPart = new vscode.InlayHintLabelPart(hintLabel);
264+
if (hint.definitionUri !== undefined) {
265+
let definitionPos = new vscode.Position(
266+
hint.definitionLine as number,
267+
hint.character as number);
268+
const option: vscode.TextDocumentShowOptions = { selection: new vscode.Range(definitionPos, definitionPos) };
269+
const commandOpen: vscode.Command = {
270+
title: "Open Inlay Hint Definition File",
271+
command: "vscode.openWith",
272+
arguments: [hint.definitionUri, null, option]
273+
};
274+
labelPart.command = commandOpen;
275+
}
276+
return labelPart;
277+
}
257278
}

0 commit comments

Comments
 (0)