You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Code folding didn't work as expected in directives and block comment #407
EDIT (Bob Brown (@bobbrow)): (inserting request for folding on multiple consecutive single-line comments from #1443)
// Comment
// Comment
// Comment
// Comment
The code folding only work correctly between #else and #endif.
From #if to next directive (#else in 1st case or #endif in 2nd case), if all line is started with ';//', the code folding didn't work.
From #endif to next directive, it automatically group the code outside the directive for all time.
The coding folder didn't work for 3rd, too.
System spec:
Windows 7 SP1 x86
VSCode 1.7.2
Plugin 0.9.3
Please correct this.
Reacted by hoffmael, Sean McManus, intijk, Inês Almeida, Adam Byrd, William, Jiading Guo, Joel Santos, Evgeny Smirnov, Jani and 44 more
changed the title [-]No code folding after #if directive and incorrect folding after #endif[/-][+]Code folding didn't work as expected in directives and block comment[/+]on Dec 13, 2016
Yes, the code folding is buggy, but it's not a bug with the C/C++ extension. We don't implement any code folding. Can you move this to the vscode team?
Actually, it sounds like the folding behavior you are seeing is "by design". The fixing of this is being tracked at microsoft/vscode#3422 . VS Code implements the folding behavior and there isn't an API yet to allow language-aware folding.
This would be a great feature and bug fix to have and essential for the code base I work on which relies on many preprocessor switches to support multiple builds. Diming the inactive code with "C_Cpp.dimInactiveRegions": true is a good first step, but having the ability to collapse and hide the inactive code region altogether is even better.
Eclipse even has the option to collapse inactive regions by default when opening a file by default.
Here's a screenshot from 0.17.4 showing the various issues with folding currently.
Reacted by Alexander Grund, Joel Santos, Jani, 飞, Ben S. and zpyatt
hoffmael Great example why this is required. I ran into pretty much the same thing. What is even worse: It breaks folding of whole namespaces making that completely useless.
The folding strategy that you are seeing is based purely on the indentation of lines. A folding region starts when a line has a smaller indent than one or more following lines and ends when there is a line with the same or smaller indent.
Ok. I understand that this issue is actually an umbrella for a lot of small issues related to how C/C++ code should be folded. I also believe the following problem, which are different issues about the same problem, indentation folding, is the most prominent of the bunch: #1584 #2116 #3589 microsoft/vscode#57696
I just checked, it's still not fixed, and indentation-based folding is still at play here, so I'm not sure what you mean by "Accurate"?
Pedro (@KaeLL) Are you still reproing those issues with 0.28.0? I'm not able to repro those issues. Could you file a new issue with more repro details?
mc (@ret394)Pedro (@KaeLL) Yeah, our extension doesn't process unsaved files, so it'll fall back to use the previous code folding behavior provided by VS. You can upvote the unsaved file issue at #3343 .
The file was saved, for heaven's sake.
Open VS, open a new file, write that code, save it as trash.c, and there'll be no collapse arrow for the curly brackets (the main function block of code), only the individual preprocessor directives.
Indent the directives by 1 tab, and the collapse arrow shows up for the curly brackets and disappear for the directives, proving it's still indentation based folding. I'm using the 0.28.0 version of the extension, with the 1.45.1 version of VSCode on Clear Linux.
Here's my settings.json
I have nothing more to add. The issue exists, but if it's happening only to me, then it's not happening and it's fixed for all intents and purposes, and I no longer care enough to pursue to the end. Might as well close the thread. Good day.
I tried using the .28 version cpp extension, vscode version 1.45.1 and found an interesting thing.
the case block cannot be folded in [switch] statement in an exist .cpp file, but when I copied the same content to a new created cpp file, case block folding works!
VS Code seems to cache the folding regions of files. When you open a folder or workspace it saves your open files and some other state. I think folding is part of that state. I haven't played around with it enough to know how to reliably reset that state. Maybe editing the file? Closing all files and clearing editor history? Just a thought.
yuchongbo The case folding is being tracked by #5523 . The fact that it starts working when it's copied to unsaved/new file is due to the fact that our extension only works on "file"-based URIs (and not the "untitled" URI scheme), which is tracked by #3343 .
Activity
[-]No code folding after #if directive and incorrect folding after #endif[/-][+]Code folding didn't work as expected in directives and block comment[/+]sean-mcmanus commentedon Dec 14, 2016
Yes, the code folding is buggy, but it's not a bug with the C/C++ extension. We don't implement any code folding. Can you move this to the vscode team?
sean-mcmanus commentedon Dec 15, 2016
Actually, it sounds like the folding behavior you are seeing is "by design". The fixing of this is being tracked at microsoft/vscode#3422 . VS Code implements the folding behavior and there isn't an API yet to allow language-aware folding.
LeThiHyVong commentedon Apr 19, 2018
According to microsoft/vscode#3422 (comment), please re-open this issue.
Thanks.
hoffmael commentedon Jun 1, 2018
This would be a great feature and bug fix to have and essential for the code base I work on which relies on many preprocessor switches to support multiple builds. Diming the inactive code with "C_Cpp.dimInactiveRegions": true is a good first step, but having the ability to collapse and hide the inactive code region altogether is even better.
Eclipse even has the option to collapse inactive regions by default when opening a file by default.
Here's a screenshot from 0.17.4 showing the various issues with folding currently.

sean-mcmanus commentedon Jun 11, 2018
+1 I hit this issue sometimes with our cross platform code.
intijk commentedon Aug 30, 2018
C++ folding without supporting this feature basically is useless for cross platform project.
Flamefire commentedon Sep 3, 2018
hoffmael Great example why this is required. I ran into pretty much the same thing. What is even worse: It breaks folding of whole namespaces making that completely useless.
Note why this breaks (microsoft/vscode#57696 (comment)):
47 remaining items
sean-mcmanus commentedon May 8, 2020
A C_Cpp.codeFolding ("Enabled"/"Disabled") setting has been added for 0.28.0 (not released yet): #5435 .
There is no ETA on when the blocking issues are going to be fixed.
KaeLL commentedon May 13, 2020
Ok. I understand that this issue is actually an umbrella for a lot of small issues related to how C/C++ code should be folded. I also believe the following problem, which are different issues about the same problem, indentation folding, is the most prominent of the bunch:
#1584
#2116
#3589
microsoft/vscode#57696
I just checked, it's still not fixed, and indentation-based folding is still at play here, so I'm not sure what you mean by "Accurate"?
akbyrd commentedon May 13, 2020
It's only in the insiders build of the plugin. If you follow the link it has instructions for enabling the insiders branch of the cpp plugin.
KaeLL commentedon May 13, 2020
Here's the link to my settings.json file backup I update every time I change something in case you think I just added this setting. It's been enabled for quite a while.
https://pastebin.com/txMGa5Et
elahehrashedi commentedon May 13, 2020
This issue is fixed in 0.28.0.
0.28.0 release: https://github.com/microsoft/vscode-cpptools/releases/tag/0.28.0
sean-mcmanus commentedon May 15, 2020
Pedro (@KaeLL) Are you still reproing those issues with 0.28.0? I'm not able to repro those issues. Could you file a new issue with more repro details?
sean-mcmanus commentedon May 15, 2020
mc (@ret394) Pedro (@KaeLL) Yeah, our extension doesn't process unsaved files, so it'll fall back to use the previous code folding behavior provided by VS. You can upvote the unsaved file issue at #3343 .
KaeLL commentedon May 15, 2020
The file was saved, for heaven's sake.
Open VS, open a new file, write that code, save it as trash.c, and there'll be no collapse arrow for the curly brackets (the main function block of code), only the individual preprocessor directives.
Indent the directives by 1 tab, and the collapse arrow shows up for the curly brackets and disappear for the directives, proving it's still indentation based folding. I'm using the 0.28.0 version of the extension, with the 1.45.1 version of VSCode on Clear Linux.
Here's my settings.json
I have nothing more to add. The issue exists, but if it's happening only to me, then it's not happening and it's fixed for all intents and purposes, and I no longer care enough to pursue to the end. Might as well close the thread. Good day.
sean-mcmanus commentedon May 16, 2020
Pedro (@KaeLL) It's caused by your setting
"editor.foldingStrategy": "indentation". The issue should be fixed if you remove that.yuchongbo commentedon May 16, 2020
I tried using the .28 version cpp extension, vscode version 1.45.1 and found an interesting thing.

the case block cannot be folded in [switch] statement in an exist .cpp file, but when I copied the same content to a new created cpp file, case block folding works!
akbyrd commentedon May 16, 2020
VS Code seems to cache the folding regions of files. When you open a folder or workspace it saves your open files and some other state. I think folding is part of that state. I haven't played around with it enough to know how to reliably reset that state. Maybe editing the file? Closing all files and clearing editor history? Just a thought.
sean-mcmanus commentedon May 18, 2020
yuchongbo The case folding is being tracked by #5523 . The fact that it starts working when it's copied to unsaved/new file is due to the fact that our extension only works on "file"-based URIs (and not the "untitled" URI scheme), which is tracked by #3343 .
sean-mcmanus commentedon Jan 21, 2021
Adam Byrd (@akbyrd) Your issue 4 is fixed with https://github.com/microsoft/vscode-cpptools/releases/tag/1.2.0-insiders2 (i.e. #5429 ).