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

支持该特性吗?


当前回答

使用JavaScript:

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

其他回答

更多信息在这里:区域扩展https://marketplace.visualstudio.com/items?itemName=maptz.regionfolder

在安装扩展并使用python之后,它是这样工作的:

# region ARBITRARY_REGION_NAME

   code goes here...

# endregion

同样,选择想要的区域,使用Ctrl+M+Ctrl+R。(即:先按住Ctrl键,再按m键,放开m键,再按r键,全部放开)

注意:这些快捷方式只有在编辑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"
    },

你应该添加用户设置:

{
    "editor.showFoldingControls": "always",
    "editor.folding": true,
    "editor.foldingStrategy": "indentation", 
}

ctrl + k + 0:折叠所有级别(命名空间,类,方法,块)

CTRL + k + 1: namspace

CTRL + k + 2: class

CTRL + k + 3:方法

CTRL + k + 4:方块

CTRL + k + [or]:当前光标块

ctrl + k + j:展开

我希望Visual Studio Code可以处理:

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

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

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