Skip to content

Commit e38cfef

Browse files
authored
Enable hide/show of type size/alignment (#14822)
1 parent d4f6223 commit e38cfef

9 files changed

Lines changed: 118 additions & 13 deletions

File tree

‎Extension/package.json‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,12 @@
10611061
"markdownDescription": "%c_cpp.configuration.hoverShowIntegralValuesInHexadecimal.markdownDescription%",
10621062
"scope": "resource"
10631063
},
1064+
"C_Cpp.hoverShowTypeSizeAndAlignment": {
1065+
"type": "boolean",
1066+
"default": true,
1067+
"markdownDescription": "%c_cpp.configuration.hoverShowTypeSizeAndAlignment.markdownDescription%",
1068+
"scope": "resource"
1069+
},
10641070
"C_Cpp.errorSquiggles": {
10651071
"type": "string",
10661072
"enum": [
@@ -4025,6 +4031,11 @@
40254031
"title": "%c_cpp.command.toggleDimInactiveRegions.title%",
40264032
"category": "C/C++"
40274033
},
4034+
{
4035+
"command": "C_Cpp.ToggleTypeSizeAndAlignment",
4036+
"title": "%c_cpp.command.toggleTypeSizeAndAlignment.title%",
4037+
"category": "C/C++"
4038+
},
40284039
{
40294040
"command": "C_Cpp.FoldAllInactiveRegions",
40304041
"title": "%c_cpp.command.foldAllInactiveRegions.title%",
@@ -6588,6 +6599,10 @@
65886599
"command": "C_Cpp.ToggleDimInactiveRegions",
65896600
"when": "config.C_Cpp.intelliSenseEngine =~ /^[dD]efault$/"
65906601
},
6602+
{
6603+
"command": "C_Cpp.ToggleTypeSizeAndAlignment",
6604+
"when": "config.C_Cpp.intelliSenseEngine =~ /^[dD]efault$/"
6605+
},
65916606
{
65926607
"command": "C_Cpp.FoldAllInactiveRegions",
65936608
"when": "editorLangId =~ /^(c|(cuda-)?cpp)$/ && config.C_Cpp.intelliSenseEngine =~ /^[dD]efault$/ && config.C_Cpp.codeFolding =~ /^[eE]nabled$/"

‎Extension/package.nls.json‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"c_cpp.command.enableErrorSquiggles.title": "Enable Error Squiggles",
2020
"c_cpp.command.disableErrorSquiggles.title": "Disable Error Squiggles",
2121
"c_cpp.command.toggleDimInactiveRegions.title": "Toggle Inactive Region Colorization",
22+
"c_cpp.command.toggleTypeSizeAndAlignment.title": "Toggle Type Size and Alignment in Hovers",
2223
"c_cpp.command.foldAllInactiveRegions.title": "Fold All Inactive Regions",
2324
"c_cpp.command.unfoldAllInactiveRegions.title": "Unfold All Inactive Regions",
2425
"c_cpp.command.resetDatabase.title": "Reset IntelliSense Database",
@@ -785,6 +786,12 @@
785786
"Markdown text between `` should not be translated or localized (it represents literal text) and the capitalization, spacing, and punctuation (including the ``) should not be altered."
786787
]
787788
},
789+
"c_cpp.configuration.hoverShowTypeSizeAndAlignment.markdownDescription": {
790+
"message": "Displays the size and alignment of types in IntelliSense hovers. This setting only applies if `#C_Cpp.intelliSenseEngine#` is set to `default`.",
791+
"comment": [
792+
"Markdown text between `` should not be translated or localized (it represents literal text) and the capitalization, spacing, and punctuation (including the ``) should not be altered."
793+
]
794+
},
788795
"c_cpp.configuration.vcpkg.enabled.markdownDescription": {
789796
"message": "Enable integration services for the [vcpkg dependency manager](https://aka.ms/vcpkg/).",
790797
"comment": [

‎Extension/src/LanguageServer/client.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,6 +1506,7 @@ export class DefaultClient implements Client {
15061506
markdownInComments: settings.markdownInComments,
15071507
hover: settings.hover,
15081508
hoverShowIntegralValuesInHexadecimal: settings.hoverShowIntegralValuesInHexadecimal,
1509+
hoverShowTypeSizeAndAlignment: settings.hoverShowTypeSizeAndAlignment,
15091510
vcFormatIndentBraces: settings.vcFormatIndentBraces,
15101511
vcFormatIndentMultiLineRelativeTo: settings.vcFormatIndentMultiLineRelativeTo,
15111512
vcFormatIndentWithinParentheses: settings.vcFormatIndentWithinParentheses,
@@ -1848,10 +1849,6 @@ export class DefaultClient implements Client {
18481849

18491850
public async onDidChangeSettings(_event: vscode.ConfigurationChangeEvent): Promise<Record<string, string>> {
18501851
const defaultClient: Client = clients.getDefaultClient();
1851-
if (this === defaultClient) {
1852-
// Only send the updated settings information once, as it includes values for all folders.
1853-
void this.sendDidChangeSettings().catch(logAndReturn.undefined);
1854-
}
18551852
const changedSettings: Record<string, string> = this.settingsTracker.getChangedSettings();
18561853

18571854
await this.ready;

‎Extension/src/LanguageServer/configurations.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -687,9 +687,9 @@ export class CppProperties implements vscode.Disposable {
687687
if (!this.propertiesFile) {
688688
const settings: CppSettings = new CppSettings(this.rootUri);
689689
if (providerId) {
690-
settings.update("default.configurationProvider", providerId);
690+
void settings.update("default.configurationProvider", providerId);
691691
} else {
692-
settings.update("default.configurationProvider", undefined); // delete the setting
692+
void settings.update("default.configurationProvider", undefined); // delete the setting
693693
}
694694
const config: Configuration | undefined = this.CurrentConfiguration;
695695
if (config) {

‎Extension/src/LanguageServer/extension.ts‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,13 +291,23 @@ export function updateLanguageConfigurations(): void {
291291
* workspace events
292292
*/
293293
async function onDidChangeSettings(event: vscode.ConfigurationChangeEvent): Promise<void> {
294+
let settingsChanged: boolean = false;
294295
clients.forEach(client => {
295296
if (client instanceof DefaultClient) {
296297
if (trackedSections.some(section => event.affectsConfiguration(section, client.RootUri))) {
298+
settingsChanged = true;
297299
void client.onDidChangeSettings(event).catch(logAndReturn.undefined);
298300
}
299301
}
300302
});
303+
if (settingsChanged) {
304+
// Send the updated settings once through the default client because the payload includes all workspace folders.
305+
const defaultClient: Client = clients.getDefaultClient();
306+
void defaultClient.sendDidChangeSettings().catch(logAndReturn.undefined);
307+
if (event.affectsConfiguration("C_Cpp.hoverShowTypeSizeAndAlignment")) {
308+
defaultClient.getCopilotHoverProvider()?.reset();
309+
}
310+
}
301311
}
302312

303313
function onDidChangeTextDocument(event: vscode.TextDocumentChangeEvent): void {
@@ -396,6 +406,7 @@ export async function registerCommands(enabled: boolean): Promise<void> {
396406
commandDisposables.push(vscode.commands.registerCommand('C_Cpp.EnableErrorSquiggles', enabled ? onEnableSquiggles : onDisabledCommand));
397407
commandDisposables.push(vscode.commands.registerCommand('C_Cpp.DisableErrorSquiggles', enabled ? onDisableSquiggles : onDisabledCommand));
398408
commandDisposables.push(vscode.commands.registerCommand('C_Cpp.ToggleDimInactiveRegions', enabled ? onToggleDimInactiveRegions : onDisabledCommand));
409+
commandDisposables.push(vscode.commands.registerCommand('C_Cpp.ToggleTypeSizeAndAlignment', enabled ? onToggleTypeSizeAndAlignment : onDisabledCommand));
399410
commandDisposables.push(vscode.commands.registerCommand('C_Cpp.FoldAllInactiveRegions', enabled ? onFoldAllInactiveRegions : onDisabledCommand));
400411
commandDisposables.push(vscode.commands.registerCommand('C_Cpp.UnfoldAllInactiveRegions', enabled ? onUnfoldAllInactiveRegions : onDisabledCommand));
401412
commandDisposables.push(vscode.commands.registerCommand('C_Cpp.PauseParsing', enabled ? onPauseParsing : onDisabledCommand));
@@ -912,19 +923,25 @@ async function onAddToIncludePath(path: string): Promise<void> {
912923
function onEnableSquiggles(): void {
913924
// This only applies to the active client.
914925
const settings: CppSettings = new CppSettings(clients.ActiveClient.RootUri);
915-
settings.update<string>("errorSquiggles", "enabled");
926+
void settings.update<string>("errorSquiggles", "enabled");
916927
}
917928

918929
function onDisableSquiggles(): void {
919930
// This only applies to the active client.
920931
const settings: CppSettings = new CppSettings(clients.ActiveClient.RootUri);
921-
settings.update<string>("errorSquiggles", "disabled");
932+
void settings.update<string>("errorSquiggles", "disabled");
922933
}
923934

924935
function onToggleDimInactiveRegions(): void {
925936
// This only applies to the active client.
926937
const settings: CppSettings = new CppSettings(clients.ActiveClient.RootUri);
927-
settings.update<boolean>("dimInactiveRegions", !settings.dimInactiveRegions);
938+
void settings.update<boolean>("dimInactiveRegions", !settings.dimInactiveRegions);
939+
}
940+
941+
function onToggleTypeSizeAndAlignment(): Thenable<void> {
942+
// This only applies to the active client.
943+
const settings: CppSettings = new CppSettings(clients.ActiveClient.RootUri);
944+
return settings.update<boolean>("hoverShowTypeSizeAndAlignment", !settings.hoverShowTypeSizeAndAlignment);
928945
}
929946

930947
function onFoldAllInactiveRegions(): Promise<void> {

‎Extension/src/LanguageServer/settings.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export interface WorkspaceFolderSettingsParams {
7272
clangTidyChecksDisabled: string[] | undefined;
7373
hover: string;
7474
hoverShowIntegralValuesInHexadecimal: boolean;
75+
hoverShowTypeSizeAndAlignment: boolean;
7576
markdownInComments: string;
7677
vcFormatIndentBraces: boolean;
7778
vcFormatIndentMultiLineRelativeTo: string;
@@ -490,6 +491,7 @@ export class CppSettings extends Settings {
490491
public get doxygenSectionTags(): string[] { return this.getAsArrayOfStrings("doxygen.sectionTags"); }
491492
public get hover(): string { return this.getAsString("hover"); }
492493
public get hoverShowIntegralValuesInHexadecimal(): boolean { return this.getAsBoolean("hoverShowIntegralValuesInHexadecimal"); }
494+
public get hoverShowTypeSizeAndAlignment(): boolean { return this.getAsBoolean("hoverShowTypeSizeAndAlignment"); }
493495
public get markdownInComments(): string { return this.getAsString("markdownInComments"); }
494496
public get legacyCompilerArgsBehavior(): boolean { return this.getAsBoolean("legacyCompilerArgsBehavior"); }
495497
public get inlayHintsAutoDeclarationTypes(): boolean { return this.getAsBoolean("inlayHints.autoDeclarationTypes.enabled"); }
@@ -777,8 +779,8 @@ export class CppSettings extends Settings {
777779
void super.Section.update(name, value?.toLowerCase() === value1.toLowerCase() ? value2 : value1, getTarget());
778780
}
779781

780-
public update<T>(name: string, value: T): void {
781-
void super.Section.update(name, value);
782+
public update<T>(name: string, value: T): Thenable<void> {
783+
return super.Section.update(name, value);
782784
}
783785

784786
public populateEditorConfig(document: vscode.TextDocument): void {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"minBinaryVersion": "1.35.3"
2+
"minBinaryVersion": "1.35.4"
33
}

‎Extension/test/scenarios/MultirootDeadlockTest/assets/SingleRootProject/quickInfo.cpp‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,9 @@ int main()
3535
myfunction(false);
3636
bool result = isEven(1);
3737
testDoxygen(2, 3);
38-
}
38+
}
39+
40+
struct HoverType
41+
{
42+
int value;
43+
};

‎Extension/test/scenarios/MultirootDeadlockTest/tests/quickInfo.test.ts‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,34 @@ suite("[Quick info test]", function(): void {
5858
disposables.forEach(d => d.dispose());
5959
});
6060

61+
async function getTypeHoverText(): Promise<string> {
62+
const result: vscode.Hover[] = <vscode.Hover[]>(await vscode.commands.executeCommand(
63+
'vscode.executeHoverProvider',
64+
fileUri,
65+
new vscode.Position(39, 8)));
66+
assert.ok(result.length > 0, "Expected a hover for HoverType.");
67+
return result.flatMap(hover => hover.contents)
68+
.map(content => typeof content === "string" ? content : content.value)
69+
.join("\n");
70+
}
71+
72+
async function waitForTypeHover(showSizeAndAlignment: boolean): Promise<string> {
73+
const deadline: number = Date.now() + 5000;
74+
let hoverText: string = "";
75+
while (Date.now() < deadline) {
76+
hoverText = await getTypeHoverText();
77+
const showsSize: boolean = hoverText.includes("**Size:**");
78+
const showsAlignment: boolean = hoverText.includes("**Alignment:**");
79+
if (hoverText.includes("struct HoverType")
80+
&& showsSize === showSizeAndAlignment
81+
&& showsAlignment === showSizeAndAlignment) {
82+
return hoverText;
83+
}
84+
await testHelpers.delay(50);
85+
}
86+
assert.fail(`Timed out waiting for type size and alignment to be ${showSizeAndAlignment ? "shown" : "hidden"}. Last hover:\n${hoverText}`);
87+
}
88+
6189
test("[Hover over function call - normal comment]", async () => {
6290
const result: vscode.Hover[] = <vscode.Hover[]>(await vscode.commands.executeCommand('vscode.executeHoverProvider', fileUri, new vscode.Position(35, 23)));
6391
const expected_full_comment: string = `\`\`\`cpp\nbool isEven(int value)\n\`\`\` \nVerifies if input is even number or not`;
@@ -111,15 +139,23 @@ suite("[Quick info test]", function(): void {
111139
const originalHover: string | undefined = configuration.inspect<string>("hover")?.workspaceFolderValue;
112140
const originalShowIntegralValuesInHexadecimal: boolean | undefined =
113141
configuration.inspect<boolean>("hoverShowIntegralValuesInHexadecimal")?.workspaceFolderValue;
142+
const originalShowTypeSizeAndAlignment: boolean | undefined =
143+
configuration.inspect<boolean>("hoverShowTypeSizeAndAlignment")?.workspaceFolderValue;
114144

115145
try {
116146
await configuration.update("hover", "disabled", vscode.ConfigurationTarget.WorkspaceFolder);
117147
await configuration.update("hoverShowIntegralValuesInHexadecimal", true, vscode.ConfigurationTarget.WorkspaceFolder);
148+
await configuration.update("hoverShowTypeSizeAndAlignment", false, vscode.ConfigurationTarget.WorkspaceFolder);
118149

119150
const settings: CppSettings = new CppSettings(fileUri);
120151
assert.strictEqual(settings.hover, "disabled");
121152
assert.strictEqual(settings.hoverShowIntegralValuesInHexadecimal, true);
153+
assert.strictEqual(settings.hoverShowTypeSizeAndAlignment, false);
122154
} finally {
155+
await configuration.update(
156+
"hoverShowTypeSizeAndAlignment",
157+
originalShowTypeSizeAndAlignment,
158+
vscode.ConfigurationTarget.WorkspaceFolder);
123159
await configuration.update(
124160
"hoverShowIntegralValuesInHexadecimal",
125161
originalShowIntegralValuesInHexadecimal,
@@ -128,6 +164,32 @@ suite("[Quick info test]", function(): void {
128164
}
129165
});
130166

167+
test("[Toggle type size and alignment command updates hover]", async () => {
168+
const configuration: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("C_Cpp", fileUri);
169+
const originalShowTypeSizeAndAlignment: boolean | undefined =
170+
configuration.inspect<boolean>("hoverShowTypeSizeAndAlignment")?.workspaceFolderValue;
171+
172+
try {
173+
await configuration.update("hoverShowTypeSizeAndAlignment", true, vscode.ConfigurationTarget.WorkspaceFolder);
174+
await waitForTypeHover(true);
175+
176+
await vscode.commands.executeCommand("C_Cpp.ToggleTypeSizeAndAlignment");
177+
assert.strictEqual(configuration.inspect<boolean>("hoverShowTypeSizeAndAlignment")?.workspaceFolderValue, false);
178+
const hiddenHover: string = await waitForTypeHover(false);
179+
assert.ok(hiddenHover.includes("struct HoverType"));
180+
181+
await vscode.commands.executeCommand("C_Cpp.ToggleTypeSizeAndAlignment");
182+
assert.strictEqual(configuration.inspect<boolean>("hoverShowTypeSizeAndAlignment")?.workspaceFolderValue, true);
183+
const shownHover: string = await waitForTypeHover(true);
184+
assert.ok(shownHover.includes("struct HoverType"));
185+
} finally {
186+
await configuration.update(
187+
"hoverShowTypeSizeAndAlignment",
188+
originalShowTypeSizeAndAlignment,
189+
vscode.ConfigurationTarget.WorkspaceFolder);
190+
}
191+
});
192+
131193
test("[Folder hexadecimal setting preserves global disabled hover]", async () => {
132194
const configuration: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("C_Cpp", fileUri);
133195
const originalGlobalHover: string | undefined = configuration.inspect<string>("hover")?.globalValue;

0 commit comments

Comments
 (0)