如何在Visual Studio代码中折叠或折叠代码节?

支持该特性吗?


当前回答

注意:这些快捷方式只有在编辑keybindings.json时才能正常工作

我不喜欢默认的快捷方式,我想让他们工作如下:

折叠:Ctrl + Alt +] 使用Ctrl + Shift + Alt + 全部折叠:Ctrl + k然后Ctrl +] 展开:Ctrl + Alt + [ 用Ctrl + Shift + Alt + [ 全部展开:Ctrl + k然后Ctrl + [

设置方法:

打开快捷键(JSON) (Ctrl + Shift + p) 将以下代码片段添加到该文件中 已经为折叠/展开自定义键绑定?然后你需要更换它们。

    {
        "key": "ctrl+alt+]",
        "command": "editor.fold",
        "when": "editorTextFocus && foldingEnabled"
    },
    {
        "key": "ctrl+alt+[",
        "command": "editor.unfold",
        "when": "editorTextFocus && foldingEnabled"
    },
    {
        "key": "ctrl+shift+alt+]",
        "command": "editor.foldRecursively",
        "when": "editorTextFocus && foldingEnabled"
    },
    {
        "key": "ctrl+shift+alt+[",
        "command": "editor.unfoldRecursively",
        "when": "editorTextFocus && foldingEnabled"
    },
    {
        "key": "ctrl+k ctrl+[",
        "command": "editor.unfoldAll",
        "when": "editorTextFocus && foldingEnabled"
    },
    {
        "key": "ctrl+k ctrl+]",
        "command": "editor.foldAll",
        "when": "editorTextFocus && foldingEnabled"
    },

其他回答

我希望Visual Studio Code可以处理:

#region Function Write-Log
Function Write-Log {
    ...
}
#endregion Function Write-Log

现在Visual Studio Code只是忽略它,不会折叠它。 与此同时,notepad++和PowerGUI可以很好地处理这个问题。

更新:我刚刚注意到Visual Studio Code的更新。现在支持了!

还可以从Insiders v1.70开始查看折叠任意行代码的功能。也就是说你选择的任何线都可以折叠!

命令和演示请参见https://stackoverflow.com/a/72954133/836330。

创建手动折叠范围从选择 editor.createFoldingRangeFromSelection

这是绑定到上面的创建命令:Ctrl+K Ctrl+,

移除手动折叠架 editor.removeManualFoldingRanges

这是绑定到上面的删除命令:Ctrl+K Ctrl+。


按区域进行代码折叠是在1.17版本中实现的。按区域文档折叠。以及v1.19和1.23。

[通常你可以添加一个空格,例如//region和//endregion到//region和//endregion,它也可以工作。]

TypeScript/JavaScript: //#region and //#endregion or // #region and // #endregion
C#:                    #region and #endregion
C/C++:                 #pragma region and #pragma endregion
F#:                    //#region and //#endregion
PowerShell:            #region and #endregion
Python:                #region and #endregion
VB:                    #Region and #End Region
PHP:                   #region and #endregion
Bat:                   ::#region and ::#endregion or REM #region and REM #endregion
Markdown:              <!-- #region --> and <!-- #endregion -->
Golang                 //region and //endregion or //#region and //#endregion
Java                   //#region and //#endregion
CSS/SCSS/Less:         /* #region */ and /* #endregion */ or /*#region*/ and /*#endregion*/
SCSS/Less:             // #region and // #endregion
Go:                    // region, // endregion and // #region, // #endregion
shellscript:           # region and # endregion
Perl5                  #region and #endregion or =pod and =cut  
sql                    --#region and --#endregion

重要提示:如果你没有在列表中看到你的语言::

每种语言都有标记可用的代码片段。输入'#'并调用代码补全来查看它们。若要为您的语言配置区域标记,请与语言扩展提供商联系。

所以键入#,然后Ctrl+空格来查看任何语言的区域标记。


这里没有技术技巧,只是简单地调整了VsCode的首选项。

我设法显示代码折叠控件总是在VsCode通过选择和搜索“折叠”。现在只需选择始终显示这些控件。这适用于我测试的Angular 8解决方案中的Typescript代码和模板的HTML。

这是用VsCode Insiders 1.37.0在Windows 10操作系统上测试的。

使用JavaScript:

//#region REGION_NAME
   ...code here
//#endregion

从Visual Studio Code 1.17开始,现在支持这个特性。要折叠/折叠你的代码块,只需添加区域标签,例如//#region my块名,如果用TypeScript/JavaScript编码,则添加//#endregion。

例子: