Vscode-cpptools: Code folding will collapse entire line containing closing bracket

Created on 18 May 2020  路  10Comments  路  Source: microsoft/vscode-cpptools

Type: LanguageService

Describe the bug

  • OS and Version: Linux, 5.6.13-arch1-1
  • VS Code Version: 1.44.2
  • C/C++ Extension Version: 0.28.0
  • Other extensions you installed (and if the issue persists after disabling them): github.vscode-pull-request-github james-yu.latex-workshop liviuschera.noctis Yes, issue persists
  • Does this issue involve using SSH remote to run the extension on a remote machine?: Dunno how to check
  • A clear and concise description of what the bug is, including information about the workspace (i.e. is the workspace a single project or multiple projects, size of the project, etc): in all types of workspaces, when you use formatting style which puts "else if"'s in the same line as closing bracket to previous conditional, folding hides too many lines (look at screenshots).

Steps to reproduce

  1. Type this code
if(cond) {
//
} else {
//
}
  1. Try to fold the "if" conditional
  2. See error: "else" gets folded with the previous line

Expected behavior

fold is only applied to what's inside the if, not touching the "else" line



Logs


Nothing shows.

Screenshots

Before folding:
image
Folding with extensions disabled:
image
Folding with C++ extension:
image

Additional context

Code Folding Language Service Visual Studio Code bug

Most helpful comment

In VS Code settings.json add this

"editor.foldingStrategy": "indentation"

All 10 comments

Hi @moojek . Thanks for reporting this.

Fixing this is tricky, due to limitations of VS Code's folding API. It's line based, and doesn't take character/column positions into account. Our folding algorithm is shared with VS, and can fold at character/column resolution, if that were supported We could omit the last line from the fold, but that would instead do the wrong thing if there is content prior to the closing bracket.

Related issues that might lead to a solution from VS Code:

https://github.com/microsoft/vscode/issues/50840
https://github.com/microsoft/vscode/issues/3352

I mean I am pretty sure it worked a while ago. Did something change since?

@moojek C++ code folding was added in v0.28.0 of the extension. If you'd prefer to use VS Code's built-in indent-based folding, you can disable C++ code folding with the following in settings.json : "C_Cpp.codeFolding": "Disabled"

Thanks! I will use it as workaround now. What am I possibly losing (what are the advantages of C++ folding over VSC)?

The main advantage of the new code folding is when region blocks are left-indented, such as

int main()
{
  int i;
#ifdef FOO
   int j;
#endif
}

in which case the "main" doesn't fold correctly.

There are also other issues. The following class cannot be collapsed entirely, only per permission section:

class A
{
private:
    int i;

public:
    void foo();
};

The folding algorithm in the C/C++ Extension understands the syntax of the language, so is better able to determine what should be grouped.

Though, as per the symptom you encountered, VS Code's folding resolution is per-line, creating some problems when that group should be started/ended within a line.

I also see this. Especially misleading with else if folded.

Same. Similar to the else if case is a bracketed case statement (for case-based variable declaration):

switch (foo) {
    case bar: {
        int x = 0;
        ...
    } case baz: {
        int y = 0;
        ...
    }
}

collapses to

switch (foo) {
    case bar: {...
        int y = 0;
        ...
    }
}

In VS Code settings.json add this

"editor.foldingStrategy": "indentation"

Thanks Rob, your solution is really straightforward and useful.

Was this page helpful?
1 / 5 - 1 ratings