Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code folding will collapse entire line containing closing bracket #5521

Closed
moojek opened this issue May 18, 2020 · 17 comments · Fixed by #7716
Closed

Code folding will collapse entire line containing closing bracket #5521

moojek opened this issue May 18, 2020 · 17 comments · Fixed by #7716
Labels
bug Feature: Code Folding fixed Check the Milestone for the release in which the fix is or will be available. Language Service Visual Studio Code Caused by (or depends on changes from) VS Code

Comments

@moojek
Copy link

moojek commented May 18, 2020

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

@Colengms
Copy link
Collaborator

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:

microsoft/vscode#50840
microsoft/vscode#3352

@Colengms Colengms added bug Feature: Code Folding Language Service Visual Studio Code Caused by (or depends on changes from) VS Code labels May 18, 2020
@Colengms Colengms added this to the Tracking milestone May 18, 2020
@Colengms Colengms changed the title Code folding error on certain code style Code folding will collapse entire line containing closing bracket May 18, 2020
@moojek
Copy link
Author

moojek commented May 20, 2020

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

@Colengms
Copy link
Collaborator

@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"

@moojek
Copy link
Author

moojek commented May 20, 2020

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

@sean-mcmanus
Copy link
Collaborator

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.

@Colengms
Copy link
Collaborator

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.

@xgdgsc
Copy link

xgdgsc commented Jul 29, 2020

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

@ClydeHobart
Copy link

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;
        ...
    }
}

@informedecommerce
Copy link

In VS Code settings.json add this

"editor.foldingStrategy": "indentation"

@beachstrider
Copy link

Thanks Rob, your solution is really straightforward and useful.

@sean-mcmanus sean-mcmanus added the fixed Check the Milestone for the release in which the fix is or will be available. label Jun 18, 2021
@sean-mcmanus
Copy link
Collaborator

@vacing
Copy link

vacing commented Jul 7, 2021

Fixed with https://github.com/microsoft/vscode-cpptools/releases/tag/1.5.0-insiders2 .

@sean-mcmanus thanks for your great work : )
it doesn't work for me when I remote connect to linux folder from windows, both are 1.5.0-insiders3.

code block likes below, the first if folding will hide else

    if (true) {
      if (false) {
        ;
      }
      ;
    } else {
      ;
    }

@Colengms
Copy link
Collaborator

Colengms commented Jul 7, 2021

Thanks @vacing . It looks like we have more work to do here. I opened #7792 to track scenarios that were not addressed by the fix associated with this issue.

@sean-mcmanus
Copy link
Collaborator

@vacing By "not working" I assume you mean it doesn't work with the nested if case you provided. We have a fix at #7793 if you want to code review and/or test the fix yourself.

@vacing
Copy link

vacing commented Jul 8, 2021

@vacing By "not working" I assume you mean it doesn't work with the nested if case you provided. We have a fix at #7793 if you want to code review and/or test the fix yourself.

thank you for your rapid response
but sorry, I am not familiar with vscode plugin development, even I would like to test the fix very much, I don't know how...

@sean-mcmanus
Copy link
Collaborator

@vacing
Copy link

vacing commented Jul 9, 2021

@vacing The fix is in 1.5.0 (https://github.com/microsoft/vscode-cpptools/releases/tag/1.5.0).

thank you, it workes fine for the previous case, now I have a new bad case : (

    if (true) {
        if (true) {
            ;
        } else {
            ;
        }
        ;
    } else if(true)  // else or else if(), the most important is no brace here
        ;

@github-actions github-actions bot locked and limited conversation to collaborators Aug 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Feature: Code Folding fixed Check the Milestone for the release in which the fix is or will be available. Language Service Visual Studio Code Caused by (or depends on changes from) VS Code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants