Reputation: 71
Not long ago, I started commenting a lot of code in python, the problem is that vs code does not offer me to reduce the comment.
I have tried single line comments and group comments but same issue..
python single line comment python multiline program
In other languages for example C.. it offers me in the bar a button to collapse
How can comment collapsing be enabled in VS?
Upvotes: 2
Views: 1852
Reputation: 1
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.
Here's what that command looks like.
Upvotes: 0
Reputation: 46
I had the same problem.
In most cases, the solutions MingJie-MSFT told you will work well. However, if it still does not work, then another solution which I found might work. Try this.
This solution is to indent all lines once again, which are between the multi-line-comments-operator.
The following example will make you understand easier.
def abc():
'''
indented comments line 1
indented comments line 2
indented comments line 3
'''
actual code starts from here.
Upvotes: 1
Reputation: 9219
I tried it. I think vscode itself provides this function, but I don't know why it doesn't work for you.
As can be seen from the figure, multiline annotations can be folded.
Of course, there is no solution for your situation. We can add #region
before the content, add #endregion
after it.
Upvotes: 5