Repository navigation
Code folding not working properly in certain circumstances #4121
Description
Describe the bug
At least in top-level functions, code folding can sometimes be unavailable. For example, I might be able to fold a function signature, but not the implementation block (and some but not all blocks within). While this is difficult to describe, it is most easily observed through reproduction or the videos below.
The issue does not appear to happen in Android Studio, so I'm guessing this is not an SDK issue (though VSCode can fold parameter lists, while Android Studio cannot--I'm not sure if this is intended).
To Reproduce
Steps to reproduce the behavior:
- Clone https://github.com/flutter/flutter (head is f477c8b184d2364fcef0a15cee6ab5c909188229 at the time of writing)
- Open run_hot.dart (packages/flutter_tools/lib/src)
- Fold all code. This can be done by opening the command palette (Cmd/Ctrl+Shift+P) and typing Fold All.
- You should see multiple unfolded blocks of code. As a specific example, the body
_defaultReassembleHelperwon't be folded, andforloops within might not be folded as well. See screenshots below.
Expected behavior
All code blocks should be folded after issuing a Fold All command. Interestingly, copying the contents of the file into a new editor and then using Fold All appears to work as expected. However, saving the file (say, to my Desktop) will unfold the code, and if I try to Fold All again, it is still slightly broken.
Screenshots
The issue happening in run_hot.dart
2022-08-25_10-39-03.mp4
The issue not happening if I copy and paste a problematic function into a new editor:
https://user-images.githubusercontent.com/9027551/186756680-939ba825-7c52-44da-9324-b183e5add931.mp4
The issue happening if I save the new file to disk:
https://user-images.githubusercontent.com/9027551/186756837-88813e9f-1048-409e-9d83-c8e4e06bf0c5.mp4
Please complete the following information:
- Operating System and version: macOS Monterey 12.5.1
- VS Code version: 1.70.2
- Dart extension version: 3.46.1
- Dart/Flutter SDK version: 2.17.6
- Target device (if the issue relates to Flutter debugging): N/A
I have reproduced this on Windows and another MacBook. Let me know if more information on those environments is needed.
(edit: typo)
Activity
Hexagonale commentedon Sep 29, 2022
So the issue is for the functions with multiline parameters. I opened an issue for the VS Code and I think they have provided valuable debug info.
microsoft/vscode#162300
DanTup commentedon Sep 29, 2022
Thanks for the link. I didn't realise this used to work - it looks like there were some changes in VS Code (microsoft/vscode#155302) that stopp it from handling ranges that start/end on the same line.
The data from the server initially contains column numbers (so they don't really intersect at all), but VS Code only supports line folding. I think we'll need to detect this and reduce the first range to end on the line before the next one begins in this case.
DanTup commentedon Sep 29, 2022
Working on a fix at https://dart-review.googlesource.com/c/sdk/+/261900/. I'll close this once it lands. It's a change in the Dart analysis server, so will ship in a future SDK release (rather than a Dart VS Code extension release).
[analysis_server] Don't overlap folding regions when lineFoldingOnly
DanTup commentedon Oct 3, 2022
Fixed by dart-lang/sdk@604bf2f.
methompson commentedon Nov 1, 2022
I'm still encountering the issues described in the OP. I've installed Flutter / Dart from the beta channel and the pre-release Dart / Flutter VS Code Plugins.
All running on MacOS 13.0 on a MacBook Pro 16", 2021.
Functions still minimize only on the function definition and NOT the entire function. Please note the screenshots before / after folding.


In the above screenshots,
func3andfunc4should be folded the same asfunc1andfunc2.I'm uncertain which combination of SDKs, plugins and versions I should be utilizing to get code folding to work correctly.
DanTup commentedon Nov 1, 2022
@methompson looking at the change dart-lang/sdk@604bf2f, it looks like the first Dart SDK it was included in is 2.19.0-257.0.dev:
The beta you're on is 2.19.0-255.2.beta so seems like it was cut just before this. Presumably the fix will be in the next beta (I actually thought there had been a more recent beta, but apparently not).
Hope this helps!
methompson commentedon Nov 1, 2022
Thanks for the insight. I've updated to the Master channel, so now I have the following installed on my system, along with the same pre-release versions of dart-code and flutter-code:
I want to note that after installing the stable channel, I completely closed VSCode to make sure any LSPs or daemons would spawn correctly.
Attached is the current state of folding:

It seems any code block can be folded, but you cannot fold anything if a foldable code block exists within that code block. As such, there's a definite regression. Note that the class cannot be folded.
func1 can be folded, but as soon as you put a foldable block within a function, you cannot fold the function completely (evidenced by func2). The provided class cannot be folded.
Finally, here's how the Code folding looked on dart.dev:
Code used for this example as follows:
DanTup commentedon Nov 3, 2022
@methompson thanks for the example! There's definitely something wrong there. I've filed #4242 with a cut-down example and will follow up there.
TimothyFran commentedon Mar 3, 2025
Any news on this?
DanTup commentedon Mar 3, 2025
@TimothyFran it's not clear what you're asking about - both this issue and the issue linked in my last comment above were closed several years ago. If something isn't working, please open a new issue with a clear description/screenshot of the problem. Thanks!