Skip to content

Commit 9b07d49

Browse files
Add settings for inlay hints feature (#9460)
1 parent 145a1aa commit 9b07d49

File tree

5 files changed

+72
-32
lines changed

5 files changed

+72
-32
lines changed

Extension/package.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2211,6 +2211,30 @@
22112211
"description": "%c_cpp.configuration.inactiveRegionBackgroundColor.description%",
22122212
"scope": "resource"
22132213
},
2214+
"C_Cpp.inlayHints.autoDeclarationTypes.enabled": {
2215+
"type": "boolean",
2216+
"default": true,
2217+
"markdownDescription": "%c_cpp.configuration.inlayHints.autoDeclarationTypes.enabled.markdownDescription%",
2218+
"scope": "application"
2219+
},
2220+
"C_Cpp.inlayHints.parameterNames.enabled": {
2221+
"type": "boolean",
2222+
"default": true,
2223+
"markdownDescription": "%c_cpp.configuration.inlayHints.parameterNames.enabled.markdownDescription%",
2224+
"scope": "application"
2225+
},
2226+
"C_Cpp.inlayHints.parameterNames.suppressWhenArgumentContainsName": {
2227+
"type": "boolean",
2228+
"default": true,
2229+
"markdownDescription": "%c_cpp.configuration.inlayHints.parameterNames.suppressWhenArgumentContainsName.markdownDescription%",
2230+
"scope": "application"
2231+
},
2232+
"C_Cpp.inlayHints.referenceOperator.enabled": {
2233+
"type": "boolean",
2234+
"default": true,
2235+
"markdownDescription": "%c_cpp.configuration.inlayHints.referenceOperator.enabled.markdownDescription%",
2236+
"scope": "application"
2237+
},
22142238
"C_Cpp.loggingLevel": {
22152239
"type": "string",
22162240
"enum": [

Extension/package.nls.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@
166166
"c_cpp.configuration.inactiveRegionOpacity.markdownDescription": { "message": "Controls the opacity of inactive preprocessor blocks. Scales between `0.1` and `1.0`. This setting only applies when inactive region dimming is enabled.", "comment": [ "Markdown text between `` should not be translated or localized (they represent literal text) and the capitalization, spacing, and punctuation (including the ``) should not be altered." ] },
167167
"c_cpp.configuration.inactiveRegionForegroundColor.description": "Controls the font coloring of inactive preprocessor blocks. Input is in the form a hexadecimal color code or a valid Theme Color. If not set, this defaults to the syntax coloring scheme of the editor. This setting only applies when inactive region dimming is enabled.",
168168
"c_cpp.configuration.inactiveRegionBackgroundColor.description": "Controls the background coloring of inactive preprocessor blocks. Input is in the form a hexadecimal color code or a valid Theme Color. If not set, this defaults to transparent. This setting only applies when inactive region dimming is enabled.",
169+
"c_cpp.configuration.inlayHints.autoDeclarationTypes.enabled.markdownDescription": { "message": "Display inlay hints for deduced type when `auto` is used in a declaration:\n```cpp \n\n auto /* int */ index = 0;\n```", "comment": [ "Markdown text between `` and the text inside ``` block is code and should not be localized." ] },
170+
"c_cpp.configuration.inlayHints.parameterNames.enabled.markdownDescription": { "message": "Display inlay hints for parameter names:\n```cpp \n\n int a = getArea(/* width: */ x, /* height: */ y);\n```", "comment": [ "Markdown text between `` and the text inside ``` block is code and should not be localized." ] },
171+
"c_cpp.configuration.inlayHints.parameterNames.suppressWhenArgumentContainsName.markdownDescription": { "message": "Suppress parameter name hints when the argument text or inline comment contains the parameter name:\n```cpp \n\n int a = getArea(width, /* height: */ y);\n```", "comment": [ "Markdown text between `` and the text inside ``` block is code and should not be localized." ] },
172+
"c_cpp.configuration.inlayHints.referenceOperator.enabled.markdownDescription": { "message": "Display the inlay hint reference operator `&` for parameters passed by non-const reference:\n```cpp \n\n swap(/* & first: */ str1, /* & last: */ str2);\n```", "comment": [ "Markdown text between `` and the text inside ``` block is code and should not be localized." ] },
169173
"c_cpp.configuration.loggingLevel.markdownDescription": { "message": "The verbosity of logging in the Output Panel. The order of levels from least verbose to most verbose is: `None` < `Error` < `Warning` < `Information` < `Debug`.", "comment": [ "Markdown text between `` should not be translated or localized (they represent literal text) and the capitalization, spacing, and punctuation (including the ``) should not be altered." ] },
170174
"c_cpp.configuration.autoAddFileAssociations.markdownDescription": { "message": "Controls whether files are automatically added to `#files.associations#` when they are the target of a navigation operation from a C/C++ file.", "comment": [ "Markdown text between `` should not be translated or localized (they represent literal text) and the capitalization, spacing, and punctuation (including the ``) should not be altered." ] },
171175
"c_cpp.configuration.workspaceParsingPriority.markdownDescription": { "message": "Controls whether parsing of the non-active workspace files uses sleeps to avoid using 100% CPU. The values `highest`/`high`/`medium`/`low` correspond to approximately 100/75/50/25% CPU usage.", "comment": [ "Markdown text between `` should not be translated or localized (they represent literal text) and the capitalization, spacing, and punctuation (including the ``) should not be altered." ] },

Extension/src/LanguageServer/Providers/inlayHintProvider.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import * as vscode from 'vscode';
66
import { DefaultClient, openFileVersions } from '../client';
77
import { Position, RequestType } from 'vscode-languageclient';
8+
import { CppSettings } from '../settings';
89

910
interface GetInlayHintsParams {
1011
uri: string;
@@ -20,7 +21,7 @@ interface CppInlayHint {
2021
label: string;
2122
inlayHintKind: InlayHintKind;
2223
isValueRef: boolean;
23-
// hasParamName: boolean;
24+
hasParamName: boolean;
2425
}
2526

2627
interface GetInlayHintsResult {
@@ -83,35 +84,35 @@ export class InlayHintsProvider implements vscode.InlayHintsProvider {
8384

8485
private buildVSCodeHints(cacheEntry: InlayHintsCacheEntry): vscode.InlayHint[] {
8586
let result: vscode.InlayHint[] = [];
86-
const showTypeHintsEnabled: boolean = true; // TODO: get value from settings.
87-
const showParamHintsEnabled: boolean = true; // TODO: get value from settings.
88-
if (showTypeHintsEnabled) {
87+
const settings: CppSettings = new CppSettings();
88+
if (settings.inlayHintsAutoDeclarationTypes) {
8989
result = result.concat(cacheEntry?.TypeHints);
9090
}
91-
if (showParamHintsEnabled) {
92-
const resolvedParameterHints: vscode.InlayHint[] = this.resolveParameterHints(cacheEntry.ParameterHints);
93-
result = result.concat(resolvedParameterHints);
94-
}
91+
const resolvedParameterHints: vscode.InlayHint[] = this.resolveParameterHints(cacheEntry.ParameterHints);
92+
result = result.concat(resolvedParameterHints);
9593
return result;
9694
}
9795

9896
private resolveParameterHints(hints: CppInlayHint[]): vscode.InlayHint[] {
9997
const resolvedHints: vscode.InlayHint[] = [];
100-
const showRefEnabled: boolean = true; // TODO: get from settings
101-
const hideParamNameEnabled: boolean = false; // TODO: get from settings
102-
for (const h of hints) {
98+
const settings: CppSettings = new CppSettings();
99+
for (const hint of hints) {
103100
// Build parameter label based on settings.
104-
// TODO: remove label if param includes parameter name or in comments.
105-
const paramName: string = (hideParamNameEnabled /* && h.hasParamName*/) ? "" : h.label;
106-
let refString: string = "";
107-
if (showRefEnabled && h.isValueRef) {
108-
refString = (paramName.length > 0) ? "& " : "&";
101+
let paramHintLabel: string = "";
102+
if (settings.inlayHintsParameterNames) {
103+
paramHintLabel = (settings.inlayHintsParameterNamesSuppressName && hint.hasParamName) ? "" : hint.label;
104+
}
105+
let refOperatorString: string = "";
106+
if (settings.inlayHintsReferenceOperator && hint.isValueRef) {
107+
refOperatorString = (paramHintLabel.length > 0) ? "& " : "&";
108+
}
109+
let label: string = "";
110+
if (paramHintLabel.length > 0 || refOperatorString.length > 0) {
111+
label = refOperatorString + paramHintLabel + ":";
109112
}
110-
const colonString: string = (paramName.length > 0 || refString.length > 0) ? ":" : "";
111-
const label: string = refString + paramName + colonString;
112113

113114
const inlayHint: vscode.InlayHint = new vscode.InlayHint(
114-
new vscode.Position(h.position.line, h.position.character),
115+
new vscode.Position(hint.position.line, hint.position.character),
115116
label,
116117
vscode.InlayHintKind.Parameter);
117118
inlayHint.paddingRight = true;

Extension/src/LanguageServer/client.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,6 @@ export class DefaultClient implements Client {
719719
private codeFoldingProvider: FoldingRangeProvider | undefined;
720720
private codeFoldingProviderDisposable: vscode.Disposable | undefined;
721721
private inlayHintsProvider: InlayHintsProvider | undefined;
722-
private inlayHintsProviderDisposable: vscode.Disposable | undefined;
723722
private semanticTokensProvider: SemanticTokensProvider | undefined;
724723
private semanticTokensProviderDisposable: vscode.Disposable | undefined;
725724
private innerConfiguration?: configs.CppProperties;
@@ -908,6 +907,9 @@ export class DefaultClient implements Client {
908907
// e.g. prevents empty c_cpp_properties.json from generation.
909908
this.registerFileWatcher();
910909

910+
this.inlayHintsProvider = new InlayHintsProvider(this);
911+
912+
this.disposables.push(vscode.languages.registerInlayHintsProvider(this.documentSelector, this.inlayHintsProvider));
911913
this.disposables.push(vscode.languages.registerRenameProvider(this.documentSelector, new RenameProvider(this)));
912914
this.disposables.push(vscode.languages.registerReferenceProvider(this.documentSelector, new FindAllReferencesProvider(this)));
913915
this.disposables.push(vscode.languages.registerWorkspaceSymbolProvider(new WorkspaceSymbolProvider(this)));
@@ -927,12 +929,6 @@ export class DefaultClient implements Client {
927929
this.semanticTokensProvider = new SemanticTokensProvider(this);
928930
this.semanticTokensProviderDisposable = vscode.languages.registerDocumentSemanticTokensProvider(this.documentSelector, this.semanticTokensProvider, this.semanticTokensLegend);
929931
}
930-
// TODO: enable based on settings.
931-
const isInlayHintsEnabled: boolean = true;
932-
if (isInlayHintsEnabled) {
933-
this.inlayHintsProvider = new InlayHintsProvider(this);
934-
this.inlayHintsProviderDisposable = vscode.languages.registerInlayHintsProvider(this.documentSelector, this.inlayHintsProvider);
935-
}
936932
// Listen for messages from the language server.
937933
this.registerNotifications();
938934
} else {
@@ -1349,7 +1345,8 @@ export class DefaultClient implements Client {
13491345
autoSaveAfterDelay: settings_filesAutoSaveAfterDelay
13501346
},
13511347
editor: {
1352-
autoClosingBrackets: settings_editorAutoClosingBrackets
1348+
autoClosingBrackets: settings_editorAutoClosingBrackets,
1349+
inlayHintsEnabled: workspaceOtherSettings.InlayHintsEnabled
13531350
},
13541351
workspace_fallback_encoding: workspaceOtherSettings.filesEncoding,
13551352
cpp_exclude_files: settings_cppFilesExclude,
@@ -1481,7 +1478,8 @@ export class DefaultClient implements Client {
14811478
}
14821479
},
14831480
editor: {
1484-
autoClosingBrackets: otherSettingsFolder.editorAutoClosingBrackets
1481+
autoClosingBrackets: otherSettingsFolder.editorAutoClosingBrackets,
1482+
inlayHintsEnabled: otherSettingsWorkspace.InlayHintsEnabled
14851483
},
14861484
files: {
14871485
encoding: otherSettingsFolder.filesEncoding,
@@ -3140,10 +3138,6 @@ export class DefaultClient implements Client {
31403138
this.semanticTokensProviderDisposable.dispose();
31413139
this.semanticTokensProviderDisposable = undefined;
31423140
}
3143-
if (this.inlayHintsProviderDisposable) {
3144-
this.inlayHintsProviderDisposable.dispose();
3145-
this.inlayHintsProviderDisposable = undefined;
3146-
}
31473141
this.model.dispose();
31483142
}
31493143

Extension/src/LanguageServer/settings.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,22 @@ export class CppSettings extends Settings {
252252
public get codeFolding(): boolean { return super.Section.get<string>("codeFolding") === "Enabled"; }
253253
public get legacyCompilerArgsBehavior(): boolean | undefined { return super.Section.get<boolean>("legacyCompilerArgsBehavior"); }
254254

255+
public get inlayHintsAutoDeclarationTypes(): boolean {
256+
return super.Section.get<boolean>("inlayHints.autoDeclarationTypes.enabled") === true;
257+
}
258+
259+
public get inlayHintsParameterNames(): boolean {
260+
return super.Section.get<boolean>("inlayHints.parameterNames.enabled") === true;
261+
}
262+
263+
public get inlayHintsParameterNamesSuppressName(): boolean {
264+
return super.Section.get<boolean>("inlayHints.parameterNames.suppressWhenArgumentContainsName") === true;
265+
}
266+
267+
public get inlayHintsReferenceOperator(): boolean {
268+
return super.Section.get<boolean>("inlayHints.referenceOperator.enabled") === true;
269+
}
270+
255271
public get enhancedColorization(): boolean {
256272
return super.Section.get<string>("enhancedColorization") === "Enabled"
257273
&& super.Section.get<string>("intelliSenseEngine") === "Default"
@@ -788,6 +804,7 @@ export class OtherSettings {
788804
}
789805
public get filesExclude(): vscode.WorkspaceConfiguration | undefined { return vscode.workspace.getConfiguration("files", this.resource).get("exclude"); }
790806
public get filesAutoSaveAfterDelay(): boolean { return vscode.workspace.getConfiguration("files").get("autoSave") === "afterDelay"; }
807+
public get InlayHintsEnabled(): boolean { return vscode.workspace.getConfiguration("editor.inlayHints").get<string>("enabled") !== "off"; }
791808
public get searchExclude(): vscode.WorkspaceConfiguration | undefined { return vscode.workspace.getConfiguration("search", this.resource).get("exclude"); }
792809
public get settingsEditor(): string | undefined { return vscode.workspace.getConfiguration("workbench.settings").get<string>("editor"); }
793810

0 commit comments

Comments
 (0)