如何在 VS Code 中隐藏任意一段代码?

How do you hide arbitrary section of code in VS Code?

"VS Code" 我指的是轻量级文本编辑器,而不是单一的 IDE,不幸的是,在 google 上搜索此内容会在 Visual Studio。

对于问题本身,任何人都知道如何隐藏"Visual Studio Code"中任意选定的代码行,最好像折叠一样隐藏到加号中吗?

注意: 这与折叠嵌套代码不同,后者可能可以通过 Ctrl+K,Ctrl+<num> 实现,我在这里需要的是隐藏特定块选择代码的数量,无论嵌套与否。

编辑:我看到有些人不理解我的要求。

比如你可能认为我想要的是这样的:

隐藏前:

for i in j:
    for k in i:
        for l in k:
            somestuff...

隐藏后:

[+] for i in j: ...

其实我想要的是这样的:

隐藏前:

# doing stuff about a
a = ClassA()
a.bar()
a.i = 2
a.j = 3
a.k = 5

隐藏后:

[+] ...  ( doing stuff about a )

2017.10.17 编辑: 原来 VS Code 在 VS Code 1.17.1 更新中实现了一个非常相似的功能,称为 "Folding Regions"。 link

不幸的是,VSCode 目前似乎不允许您像 Visual Studio 那样通过 Ctrl+[= 隐藏任意选择的代码18=]M,Ctrl+H。现在您可以使用代码折叠功能,这取决于缩进。也就是说,如果缩进要隐藏的代码,则可以通过 Ctrl+Shift+[[ 折叠它=25=],像这样:

显然,这对您的问题来说是一种丑陋的解决方案;它需要多个步骤并对文件进行实际更改。此外,如果您使用像 Python 这样的依赖于空格的语言编写,这显然是无用的,但我认为除了找到扩展(或自己编写扩展)之外,您不会找到更好的解决方案。如果此功能对您很重要,也可能值得在 the official VSCode GitHub repo 上发布问题。

您可以使用以下分隔符进行代码折叠:

C/C++:       #pragma region and #pragma endregion
C#:          #region and #endregion
CSS:         /* #region */ and /* #endregion */
Java:        //region and //endregion
JavaScript:  //#region and //#endregion and //region and //endregion
PHP:         #region and #endregion
Powershell:  #region and #endregion
Python:      #region and #endregion
VB:          #Region and #End Region

参见https://github.com/Microsoft/vscode/issues/12146([折叠]折叠区域)

评论时使用减少缩进的分隔符。 隐藏然后像嵌套一样工作,左边的小箭头。

see here

# Below here comes the code to hide.
    a = ClassA()
    a.bar()
    a.i = 2
    a.j = 3
    a.k = 5