I actually just made a VS Code extension to solve this exact problem: Fold Single-Line Comments
It enables folding for consecutive single-line comments in Python (and other languages like Ruby, Shell, and YAML). To do this, it implements a FoldingRangeProvider that detects sequences of lines starting with '#':
if (trimmedText.startsWith('#')) {
if (startLine === null) {
startLine = i;
}
lastLine = i;
} else if (startLine !== null && lastLine !== null) {
if (lastLine > startLine) {
ranges.push(new vscode.FoldingRange(
startLine,
lastLine,
vscode.FoldingRangeKind.Comment
));
}
}
Currently, it not will count empty lines as part of a foldable area, which is different from how VS Code officially supports it for JavaScript/TypeScript's single-line comments, but I personally thought it would be better that way. I'm not sure if there's enough demand to toggle it otherwise/set it as the default.
The extension automatically enables folding for consecutive single-line comments, and you can click the folding icon in the gutter (like in your C example) to collapse/expand them.
Here's a picture showing the gutter with Python code.
Here's how they look when folded.
You can also use the command "Fold Single-Line Comments" from the command palette to specifically fold only those single-line comments. There is currently no "Unfold Single-Line Comments," but it should not be too hard to add. The official Unfold All command will work with this, because we've simply consecutive single-line comments as viable folding areas.