collapse python在vs代码中的评论

发布于 2025-01-27 05:40:17 字数 453 浏览 3 评论 0原文

不久前,我开始在Python中评论很多代码,问题是VS代码并没有为我提供减少评论。

我尝试过单行评论和组评论,但同一问题。

python单行评论 python multiline程序

例如,其他语言为例如级。 collapse

c

在vs中如何启用注释崩溃?

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

example in C

How can comment collapsing be enabled in VS?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

毁梦 2025-02-03 05:40:17

我尝试了。我认为VSCODE本身提供了此功能,但是我不知道为什么它不适合您。

从图可以看出,可以折叠多元注释。

”在此处输入图像描述”

当然,您的情况没有解决方案。我们可以在内容之前添加#Region,在内容之后添加#endregion

”在此处输入图像描述”

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.

enter image description here

enter image description here

Of course, there is no solution for your situation. We can add #region before the content, add #endregion after it.

enter image description here

enter image description here

萌吟 2025-02-03 05:40:17

我遇到了同样的问题。

在大多数情况下,Mingjie-Msft的解决方案告诉您您的工作状况会很好。
但是,如果它仍然不起作用,那么我发现的另一种解决方案可能会起作用。尝试一下。

该解决方案是再次缩进所有线路,它们位于多行操作员之间。

以下示例将使您理解更容易。

 def abc():
     '''
         indented comments line 1
         indented comments line 2
         indented comments line 3
     '''
     actual code starts from here.



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.

enter image description here
enter image description here
enter image description here

明月夜 2025-02-03 05:40:17

实际上,我刚刚制作了一个VS代码扩展程序来解决此确切的问题:

它可以折叠python中连续的单行注释(以及其他语言,例如Ruby,Shell和YAML)。为此,它实现了一个折叠式的程序,该折叠式提供者检测以“#”开头的线序列:

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

当前,它不会将空行计算为可折叠区域的一部分,这与VS代码如何正式支持JavaScript/Typescript的单个单曲单词 -行评论,但我个人认为这样会更好。我不确定是否有足够的需求将其切换为否则/将其设置为默认设置。

该扩展名可以自动启用连续单行注释的折叠,您可以单击“排水沟”中的折叠图标(如在C示例中)以折叠/展开它们。

这是一张图片,显示了带有python代码的天沟。

​ - 线评论。当前没有“展开的单线评论”,但不应该太难添加。官方展开的所有命令将与此作用,因为我们只是将单线评论作为可行的折叠区域。

这就是该命令的样子。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文