Skip to content

Commit 6178b70

Browse files
committed
Add UnfoldAllInactiveRegions
1 parent 83dcb22 commit 6178b70

6 files changed

Lines changed: 82 additions & 30 deletions

File tree

‎Extension/package.json‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4006,8 +4006,13 @@
40064006
"category": "C/C++"
40074007
},
40084008
{
4009-
"command": "C_Cpp.FoldInactiveRegions",
4010-
"title": "%c_cpp.command.foldInactiveRegions.title%",
4009+
"command": "C_Cpp.FoldAllInactiveRegions",
4010+
"title": "%c_cpp.command.foldAllInactiveRegions.title%",
4011+
"category": "C/C++"
4012+
},
4013+
{
4014+
"command": "C_Cpp.UnfoldAllInactiveRegions",
4015+
"title": "%c_cpp.command.unfoldAllInactiveRegions.title%",
40114016
"category": "C/C++"
40124017
},
40134018
{
@@ -6564,7 +6569,11 @@
65646569
"when": "config.C_Cpp.intelliSenseEngine =~ /^[dD]efault$/"
65656570
},
65666571
{
6567-
"command": "C_Cpp.FoldInactiveRegions",
6572+
"command": "C_Cpp.FoldAllInactiveRegions",
6573+
"when": "editorLangId =~ /^(c|(cuda-)?cpp)$/ && config.C_Cpp.intelliSenseEngine =~ /^[dD]efault$/ && config.C_Cpp.codeFolding =~ /^[eE]nabled$/"
6574+
},
6575+
{
6576+
"command": "C_Cpp.UnfoldAllInactiveRegions",
65686577
"when": "editorLangId =~ /^(c|(cuda-)?cpp)$/ && config.C_Cpp.intelliSenseEngine =~ /^[dD]efault$/ && config.C_Cpp.codeFolding =~ /^[eE]nabled$/"
65696578
},
65706579
{

‎Extension/package.nls.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
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.foldInactiveRegions.title": "Fold Inactive Regions",
22+
"c_cpp.command.foldAllInactiveRegions.title": "Fold All Inactive Regions",
23+
"c_cpp.command.unfoldAllInactiveRegions.title": "Unfold All Inactive Regions",
2324
"c_cpp.command.resetDatabase.title": "Reset IntelliSense Database",
2425
"c_cpp.command.takeSurvey.title": "Take Survey",
2526
"c_cpp.command.restartIntelliSenseForFile.title": "Restart IntelliSense for Active File",

‎Extension/src/LanguageServer/client.ts‎

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ interface DecorationRangesPair {
233233
ranges: vscode.Range[];
234234
}
235235

236+
type InactiveRegionFoldingOperation = 'fold' | 'unfold';
237+
236238
interface InternalSourceFileConfiguration extends SourceFileConfiguration {
237239
compilerArgsLegacy?: string[];
238240
}
@@ -800,7 +802,8 @@ export interface Client {
800802
selectTranslationUnit(uri: vscode.Uri, translationUnit: string): Promise<void>;
801803
updateActiveDocumentTextOptions(): void;
802804
didChangeActiveEditor(editor?: vscode.TextEditor, selection?: Range): Promise<void>;
803-
foldInactiveRegions(): Promise<void>;
805+
foldAllInactiveRegions(): Promise<void>;
806+
unfoldAllInactiveRegions(): Promise<void>;
804807
restartIntelliSenseForFile(document: vscode.TextDocument): Promise<void>;
805808
activate(): void;
806809
selectionChanged(selection: Range): Promise<void>;
@@ -882,7 +885,7 @@ export class DefaultClient implements Client {
882885
private trackedDocuments = new Map<string, vscode.TextDocument>();
883886
private inactiveRegionsDecorations = new Map<string, DecorationRangesPair>();
884887
private inactiveRegions = new InactiveRegionStore();
885-
private pendingInactiveRegionFolds = new Set<string>();
888+
private pendingInactiveRegionFoldingOperations = new Map<string, InactiveRegionFoldingOperation>();
886889
private autoFoldedEditors = new WeakSet<vscode.TextEditor>();
887890
private settingsTracker: SettingsTracker;
888891
private loggingLevel: number = 1;
@@ -1904,7 +1907,7 @@ export class DefaultClient implements Client {
19041907
}
19051908
if (changedSettings.autoFoldInactiveRegions !== undefined && vscode.window.activeTextEditor
19061909
&& clients.getClientFor(vscode.window.activeTextEditor.document.uri) === this) {
1907-
void this.tryFoldInactiveRegions(vscode.window.activeTextEditor).catch(logAndReturn.undefined);
1910+
void this.tryApplyInactiveRegionFolding(vscode.window.activeTextEditor).catch(logAndReturn.undefined);
19081911
}
19091912
if (changedSettings.legacyCompilerArgsBehavior !== undefined) {
19101913
this.configuration.handleConfigurationChange();
@@ -2012,7 +2015,7 @@ export class DefaultClient implements Client {
20122015
const uri: string = document.uri.toString();
20132016
openFileVersions.set(uri, document.version);
20142017
this.inactiveRegions.delete(uri);
2015-
this.pendingInactiveRegionFolds.delete(uri);
2018+
this.pendingInactiveRegionFoldingOperations.delete(uri);
20162019
}
20172020
}
20182021

@@ -2025,7 +2028,7 @@ export class DefaultClient implements Client {
20252028
this.inlayHintsProvider.removeFile(uri);
20262029
}
20272030
this.inactiveRegions.delete(uri);
2028-
this.pendingInactiveRegionFolds.delete(uri);
2031+
this.pendingInactiveRegionFoldingOperations.delete(uri);
20292032
this.inactiveRegionsDecorations.delete(uri);
20302033
if (diagnosticsCollectionIntelliSense) {
20312034
diagnosticsCollectionIntelliSense.delete(document.uri);
@@ -2981,21 +2984,29 @@ export class DefaultClient implements Client {
29812984

29822985
const activeEditor: vscode.TextEditor | undefined = vscode.window.activeTextEditor;
29832986
if (isCompletePass && activeEditor?.document.uri.toString() === uriString) {
2984-
void client.tryFoldInactiveRegions(activeEditor).catch(logAndReturn.undefined);
2987+
void client.tryApplyInactiveRegionFolding(activeEditor).catch(logAndReturn.undefined);
29852988
}
29862989
}
29872990

2988-
public async foldInactiveRegions(): Promise<void> {
2991+
public foldAllInactiveRegions(): Promise<void> {
2992+
return this.applyInactiveRegionFolding('fold');
2993+
}
2994+
2995+
public unfoldAllInactiveRegions(): Promise<void> {
2996+
return this.applyInactiveRegionFolding('unfold');
2997+
}
2998+
2999+
private async applyInactiveRegionFolding(operation: InactiveRegionFoldingOperation): Promise<void> {
29893000
const editor: vscode.TextEditor | undefined = vscode.window.activeTextEditor;
29903001
if (!editor || !util.isCpp(editor.document)) {
29913002
return;
29923003
}
29933004

2994-
this.pendingInactiveRegionFolds.add(editor.document.uri.toString());
2995-
await this.tryFoldInactiveRegions(editor);
3005+
this.pendingInactiveRegionFoldingOperations.set(editor.document.uri.toString(), operation);
3006+
await this.tryApplyInactiveRegionFolding(editor);
29963007
}
29973008

2998-
private async tryFoldInactiveRegions(editor: vscode.TextEditor): Promise<void> {
3009+
private async tryApplyInactiveRegionFolding(editor: vscode.TextEditor): Promise<void> {
29993010
if (vscode.window.activeTextEditor !== editor || !util.isCpp(editor.document)) {
30003011
return;
30013012
}
@@ -3009,12 +3020,14 @@ export class DefaultClient implements Client {
30093020
const settings: CppSettings = new CppSettings(editor.document.uri);
30103021
const autoFold: boolean = settings.autoFoldInactiveRegions && settings.codeFolding
30113022
&& !this.autoFoldedEditors.has(editor);
3012-
const manualFold: boolean = this.pendingInactiveRegionFolds.has(uri);
3013-
if (!autoFold && !manualFold) {
3023+
const pendingOperation: InactiveRegionFoldingOperation | undefined =
3024+
this.pendingInactiveRegionFoldingOperations.get(uri);
3025+
const operation: InactiveRegionFoldingOperation | undefined = pendingOperation ?? (autoFold ? 'fold' : undefined);
3026+
if (operation === undefined) {
30143027
return;
30153028
}
30163029

3017-
this.pendingInactiveRegionFolds.delete(uri);
3030+
this.pendingInactiveRegionFoldingOperations.delete(uri);
30183031

30193032
const selectionLines: number[] = getInactiveRegionStartLines(inactiveRegions);
30203033
if (selectionLines.length === 0) {
@@ -3025,16 +3038,16 @@ export class DefaultClient implements Client {
30253038
this.autoFoldedEditors.add(editor);
30263039
}
30273040
try {
3028-
await vscode.commands.executeCommand('editor.fold', {
3041+
await vscode.commands.executeCommand(`editor.${operation}`, {
30293042
selectionLines,
30303043
direction: 'down'
30313044
});
30323045
} catch (error) {
30333046
if (autoFold) {
30343047
this.autoFoldedEditors.delete(editor);
30353048
}
3036-
if (manualFold) {
3037-
this.pendingInactiveRegionFolds.add(uri);
3049+
if (pendingOperation) {
3050+
this.pendingInactiveRegionFoldingOperations.set(uri, pendingOperation);
30383051
}
30393052
throw error;
30403053
}
@@ -3207,7 +3220,7 @@ export class DefaultClient implements Client {
32073220
return;
32083221
}
32093222

3210-
void this.tryFoldInactiveRegions(editor).catch(logAndReturn.undefined);
3223+
void this.tryApplyInactiveRegionFolding(editor).catch(logAndReturn.undefined);
32113224
this.updateActiveDocumentTextOptions();
32123225

32133226
const params: DidChangeActiveEditorParams = {
@@ -4519,7 +4532,8 @@ class NullClient implements Client {
45194532
selectTranslationUnit(uri: vscode.Uri, translationUnit: string): Promise<void> { return Promise.resolve(); }
45204533
updateActiveDocumentTextOptions(): void { }
45214534
didChangeActiveEditor(editor?: vscode.TextEditor): Promise<void> { return Promise.resolve(); }
4522-
foldInactiveRegions(): Promise<void> { return Promise.resolve(); }
4535+
foldAllInactiveRegions(): Promise<void> { return Promise.resolve(); }
4536+
unfoldAllInactiveRegions(): Promise<void> { return Promise.resolve(); }
45234537
restartIntelliSenseForFile(document: vscode.TextDocument): Promise<void> { return Promise.resolve(); }
45244538
activate(): void { }
45254539
selectionChanged(selection: Range): Promise<void> { return Promise.resolve(); }

‎Extension/src/LanguageServer/extension.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,8 @@ export async function registerCommands(enabled: boolean): Promise<void> {
396396
commandDisposables.push(vscode.commands.registerCommand('C_Cpp.EnableErrorSquiggles', enabled ? onEnableSquiggles : onDisabledCommand));
397397
commandDisposables.push(vscode.commands.registerCommand('C_Cpp.DisableErrorSquiggles', enabled ? onDisableSquiggles : onDisabledCommand));
398398
commandDisposables.push(vscode.commands.registerCommand('C_Cpp.ToggleDimInactiveRegions', enabled ? onToggleDimInactiveRegions : onDisabledCommand));
399-
commandDisposables.push(vscode.commands.registerCommand('C_Cpp.FoldInactiveRegions', enabled ? onFoldInactiveRegions : onDisabledCommand));
399+
commandDisposables.push(vscode.commands.registerCommand('C_Cpp.FoldAllInactiveRegions', enabled ? onFoldAllInactiveRegions : onDisabledCommand));
400+
commandDisposables.push(vscode.commands.registerCommand('C_Cpp.UnfoldAllInactiveRegions', enabled ? onUnfoldAllInactiveRegions : onDisabledCommand));
400401
commandDisposables.push(vscode.commands.registerCommand('C_Cpp.PauseParsing', enabled ? onPauseParsing : onDisabledCommand));
401402
commandDisposables.push(vscode.commands.registerCommand('C_Cpp.ResumeParsing', enabled ? onResumeParsing : onDisabledCommand));
402403
commandDisposables.push(vscode.commands.registerCommand('C_Cpp.PauseCodeAnalysis', enabled ? onPauseCodeAnalysis : onDisabledCommand));
@@ -926,9 +927,14 @@ function onToggleDimInactiveRegions(): void {
926927
settings.update<boolean>("dimInactiveRegions", !settings.dimInactiveRegions);
927928
}
928929

929-
function onFoldInactiveRegions(): Promise<void> {
930+
function onFoldAllInactiveRegions(): Promise<void> {
930931
const editor: vscode.TextEditor | undefined = vscode.window.activeTextEditor;
931-
return editor ? clients.getClientFor(editor.document.uri).foldInactiveRegions() : Promise.resolve();
932+
return editor ? clients.getClientFor(editor.document.uri).foldAllInactiveRegions() : Promise.resolve();
933+
}
934+
935+
function onUnfoldAllInactiveRegions(): Promise<void> {
936+
const editor: vscode.TextEditor | undefined = vscode.window.activeTextEditor;
937+
return editor ? clients.getClientFor(editor.document.uri).unfoldAllInactiveRegions() : Promise.resolve();
932938
}
933939

934940
function onPauseParsing(): void {

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ suite("Inactive region folding in a multi-root workspace", function (): void {
3636
testHook.dispose();
3737
});
3838

39-
test("routes manual folding by URI during a workspace client switch", async () => {
39+
test("routes manual folding operations by URI during a workspace client switch", async () => {
4040
const configuration: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("C_Cpp", workspaceFolder.uri);
4141
const previousCodeFoldingValue: string | undefined = configuration.inspect<string>("codeFolding")?.globalValue;
4242
await configuration.update("codeFolding", "enabled", vscode.ConfigurationTarget.Global);
@@ -61,9 +61,11 @@ suite("Inactive region folding in a multi-root workspace", function (): void {
6161
editorChangePromise = extension.clients.didChangeActiveEditor(activeEditor);
6262
assert.strictEqual(extension.clients.ActiveClient, firstClient);
6363

64-
await vscode.commands.executeCommand("C_Cpp.FoldInactiveRegions");
65-
64+
await vscode.commands.executeCommand("C_Cpp.FoldAllInactiveRegions");
6665
await assertInactiveBranchIsFolded(editor);
66+
67+
await vscode.commands.executeCommand("C_Cpp.UnfoldAllInactiveRegions");
68+
await assertInactiveBranchIsUnfolded(editor);
6769
} finally {
6870
releaseEditorChange?.();
6971
await editorChangePromise;
@@ -89,6 +91,13 @@ suite("Inactive region folding in a multi-root workspace", function (): void {
8991
assert.strictEqual(line, 28);
9092
}
9193

94+
async function assertInactiveBranchIsUnfolded(editor: vscode.TextEditor): Promise<void> {
95+
const activeEditor: vscode.TextEditor = await vscode.window.showTextDocument(editor.document, editor.viewColumn, false);
96+
activeEditor.selection = new vscode.Selection(24, 0, 24, 0);
97+
await vscode.commands.executeCommand("cursorMove", { to: "down", by: "wrappedLine", value: 1 });
98+
assert.strictEqual(activeEditor.selection.active.line, 25);
99+
}
100+
92101
function waitForIntelliSenseReady(fileName: string): Promise<void> {
93102
return new Promise<void>((resolve, reject) => {
94103
const timeout: NodeJS.Timeout = setTimeout(() => {

‎Extension/test/scenarios/SingleRootProject/tests/inactiveFolding.test.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ suite("Inactive region folding", function (): void {
3838
try {
3939
const editor: vscode.TextEditor = await openFileAndWaitForIntelliSense();
4040
await vscode.commands.executeCommand("editor.unfoldAll");
41-
await vscode.commands.executeCommand("C_Cpp.FoldInactiveRegions");
41+
await vscode.commands.executeCommand("C_Cpp.FoldAllInactiveRegions");
4242

4343
await assertInactiveBranchIsFolded(editor);
4444
} finally {
@@ -71,7 +71,7 @@ suite("Inactive region folding", function (): void {
7171
try {
7272
const editor: vscode.TextEditor = await openFileAndWaitForIntelliSense();
7373
await vscode.commands.executeCommand("editor.unfoldAll");
74-
await vscode.commands.executeCommand("C_Cpp.FoldInactiveRegions");
74+
await vscode.commands.executeCommand("C_Cpp.FoldAllInactiveRegions");
7575
await assertInactiveBranchIsFolded(editor);
7676

7777
await vscode.commands.executeCommand("editor.unfoldAll");
@@ -86,6 +86,19 @@ suite("Inactive region folding", function (): void {
8686
}
8787
});
8888

89+
test("unfolds all inactive regions", async () => {
90+
const editor: vscode.TextEditor = await openFileAndWaitForIntelliSense();
91+
try {
92+
await vscode.commands.executeCommand("C_Cpp.FoldAllInactiveRegions");
93+
await assertInactiveBranchIsFolded(editor);
94+
95+
await vscode.commands.executeCommand("C_Cpp.UnfoldAllInactiveRegions");
96+
await assertInactiveBranchIsUnfolded(editor);
97+
} finally {
98+
await vscode.commands.executeCommand("workbench.action.closeActiveEditor");
99+
}
100+
});
101+
89102
test("does not automatically fold inactive regions when code folding is disabled", async () => {
90103
const configuration: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration("C_Cpp", workspaceFolder.uri);
91104
const previousAutoFoldValue: boolean | undefined = configuration.inspect<boolean>("autoFoldInactiveRegions")?.globalValue;

0 commit comments

Comments
 (0)