簡體   English   中英

如何在 Visual Studio Code 中為 Windows 折疊代碼段?

[英]How do I collapse sections of code in Visual Studio Code for Windows?

如何在 Visual Studio Code 中折疊或折疊代碼段?

是否支持此功能?

折疊功能已經推出,現在從 Visual Studio Code版本 0.10.11 開始實施。 有以下可用的鍵盤快捷鍵:

  • Fold折疊光標處最里面的未折疊區域:

    • Ctrl + Shift + [在 Windows 和 Linux 上
    • + + [在 macOS 上
  • Unfold展開光標處的折疊區域:

    • Ctrl + Shift + ]在 Windows 和 Linux 上
    • + + ]在 macOS 上
  • Fold All折疊編輯器中的所有區域:

    • Ctrl + KCtrl + 0 (零)在 Windows 和 Linux 上
    • + K + 0 (零)在 macOS 上
  • Unfold All在編輯器中展開所有區域:

    • Ctrl + KCtrl + J在 Windows 和 Linux 上
    • + K , + J在 macOS 上

參考資料: https ://code.visualstudio.com/docs/getstarted/keybindings

從 2017 年 4 月的 Visual Studio Code 版本 1.12.0 開始,請參閱文檔中的 基本編輯 > 折疊部分。

默認鍵是:

全部折疊: CTRL + KCTRL + 0 (零)

折疊級別 [n]: CTRL + K , CTRL + [n] *

展開全部: CTRL + KCTRL + J

折疊區域: CTRL + KCTRL + [

展開區域: CTRL + KCTRL + ]

*折疊級別:折疊除最外層之外的所有類,嘗試CTRL + KCTRL + 1

Mac:使用而不是CTRL (感謝 Prajeet)

另請參閱從 Insiders v1.70 開始折疊任意代碼行的能力。 那就是您選擇的任何行都可以折疊!

有關命令和演示,請參閱https://stackoverflow.com/a/72954133/836330

Fold Selected Lineseditor.foldSelected Ctrl + K Ctrl +


v1.17 已提供按區域折疊代碼 按區域折疊文檔 還有v1.191.23

[通常您可以在 //region 和 //endregion 中添加空格,例如// region and // endregion //region and //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 + Space以查看任何語言的區域標記。


此功能現在在標准版本中可用。 要顯示折疊/展開控件,您需要將鼠標懸停在行號右側的區域上,如下圖所示:

在此處輸入圖像描述

您應該添加用戶設置:

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

ctrl + k + 0 :折疊所有級別(命名空間、類、方法、塊)

ctrl + k + 1 :命名空間

ctrl + k + 2 :類

ctrl + k + 3 :方法

ctrl + k + 4 :塊

ctrl + k + [] :當前光標塊

ctrl + k + j :展開

折疊/擴展的默認快捷方式是:

Ctrl + Shift + [ :“折疊”

Ctrl + Shift + Alt + [ :“全部折疊”

Ctrl + Shift + ] :“展開”

Ctrl + Shift + Alt + ] :“全部展開”

或者轉到 keybindings.json 並根據需要進行更改。

例如:

{
    "key": "cmd+k cmd+m",
    "command": "editor.foldAll",
    "when": "editorFocus"
},
{
    "key": "cmd+m cmd+k",
    "command": "editor.unfoldAll",
    "when": "editorFocus"
},

如果所有快捷方式都不起作用(就像我一樣),作為一種解決方法,您還可以打開命令面板( Ctrl + 3或 View -> Command Palette...)並輸入fold all

在此處輸入圖像描述

1.0 版現在支持折疊:

源代碼折疊快捷方式

有新的折疊操作可以根據折疊級別折疊源代碼區域。

有將級別 1 ( Ctrl + K Ctrl + 1 ) 折疊到級別 5 ( Ctrl + K Ctrl + 5 ) 的操作。 要展開,請使用全部展開 ( Ctrl + Shift + Alt + ] )。

級別折疊操作不適用於包含當前光標的區域。

我在鍵盤上找不到]按鈕(挪威布局),在我的情況下是Å按鈕。 (或者從退格按鈕開始,向左和向下兩個按鈕。)

使用 JavaScript:

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

Visual Studio Code 1.17起,現在支持此功能。 要折疊/折疊您的代碼塊,只需添加區域標簽,例如//#region my block name//#endregion如果使用 TypeScript/JavaScript 編碼)。

例子:

區域折疊

只需按ctrl + shift + p ,然后輸入“折疊”。 將顯示所有關於 (un)fold 的鍵綁定。 如果ctrl k不起作用,可能是因為 vim 擴展覆蓋了鍵。 在這種情況下,您應該修改 settings.json (按ctrl + shift + p ,然后輸入 'settings' )

"vim.handleKeys": {
  "<C-k>": false,
},

在此處輸入圖像描述

這是用於折疊和展開代碼的最新內置(默認)鍵盤快捷鍵

vscode 鍵盤快捷鍵

在此處輸入圖像描述

Ctrl+Shift+[ Fold (collapse) region
Ctrl+Shift+] Unfold (uncollapse) region
Ctrl+K Ctrl+[ Fold (collapse) all subregions
Ctrl+K Ctrl+] Unfold (uncollapse) all subregions
Ctrl+K Ctrl+0 Fold (collapse) all regions
Ctrl+K Ctrl+J Unfold (uncollapse) all

注意:但在某些情況下,您的 vs 代碼擴展或用戶會更改鍵盤綁定(快捷方式)。 所以像這樣檢查的最佳選擇

  1. 視圖->命令面板或cntrl + shift + p
  2. 鍵入“折疊”。它會建議折疊和展開以及快捷方式。 您可以鍵入該快捷方式而不是命令面板

在此處輸入圖像描述

例如:

全部折疊

在此處輸入圖像描述

展開全部

在此處輸入圖像描述

v1.42 對折疊的外觀和功能進行了一些很好的改進。 請參閱https://github.com/microsoft/vscode-docs/blob/vnext/release-notes/v1_42.md#folded-range-highlighting

折疊范圍突出顯示

由於所有棄牌范圍的背景顏色,現在更容易發現棄牌范圍。

折疊高光

折疊高亮顏色主題:Dark+

該功能由設置 editor.foldingHighlight 控制,並且可以使用顏色 editor.foldBackground 自定義顏色。

"workbench.colorCustomizations": { "editor.foldBackground": "#355000" }

折疊改進

Shift + Click折疊指示器首先只折疊內部范圍。 Shift + Click再次(當所有內部范圍都已折疊時)也將折疊父級。 Shift + Click再次展開全部。

折疊移位點擊

在已經折疊的范圍上使用折疊命令 (kb( editor.fold ))] 時,下一個展開的父范圍將被折疊。

編輯器內的代碼折疊控件,用於在 VsCode 中展開 XML 結構文檔和源代碼的節點

這里沒有技術提示,只是對 VsCode 的偏好進行簡單的調整。

通過轉到首選項並搜索“折疊”,我設法始終在 VsCode 中顯示代碼折疊控件。 現在只需選擇始終顯示這些控件。 這適用於我測試過的 Angular 8 解決方案中的 Typescript 代碼和模板的 HTML。

這是在 Windows 10 操作系統上運行的 VsCode Insiders 1.37.0 測試的。

始終在 VsCode 中顯示代碼折疊控件

從版本 1.3.1 (2016-07-17) 開始,Block Collapse 更加方便。

任何后跟縮進行的行都將帶有一個“-”字符以允許折疊。 如果該塊被折疊,它將被替換為“+”字符,該字符將打開折疊的塊。

Ctrl + Shift + Alt + ] )仍然會影響所有塊,關閉一個級別。 每次重復使用都會關閉一個級別。 ( Ctrl + Shift + Alt + [ ) 以相反的方式工作。

萬歲,塊崩潰終於有用了。

這是 VS 代碼最常用的默認鍵盤映射。 並且您可以輕松地通過自己的鍵盤映射進行自定義。

Fold All: CTRL + 0

Unfold All: CTRL + J

Fold Region: CTRL + [

Unfold Region: CTRL + ]

Fold Level 1: CTRL+ 1

Fold Level 2: CTRL+ 2

Fold Level 3: CTRL+ 3

Fold Level 1: CTRL+ 4

VSCode 擴展: Fold Level ,一鍵折疊到你想要的關卡。

在此處輸入圖像描述

注意:這些快捷方式只有在您編輯 keybindings.json 時才能按預期工作

我對默認快捷方式不滿意,我希望它們按以下方式工作:

  • 折疊: Ctrl + Alt + ]
  • 遞歸折疊: Ctrl + ⇧ Shift + Alt + ]
  • 全部折疊: Ctrl + k然后Ctrl + ]
  • 展開: Ctrl + Alt + [
  • 遞歸展開: Ctrl + ⇧ Shift + Alt + [
  • 展開全部: Ctrl + k然后Ctrl + [

要設置它:

  • 打開Preferences: Open Keyboard Shortcuts (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"
    },

折疊/展開當前塊使用 ( ctrl + k )+( ctrl + l )

在 Mac 上,它是 RHS Command 鍵 K ,而不是代碼折疊命令的左側。

否則,左手 Command 鍵將刪除當前行 K

我希望 Visual Studio Code 可以處理:

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

現在 Visual Studio Code 只是忽略它並且不會折疊它。 同時 Notepad++ 和 PowerGUI 處理得很好。

更新:我剛剛注意到 Visual Studio Code 的更新。 現在支持這個!

或者,如果您想移除折疊按鈕,以獲得額外空間:

"editor.folding": false

(添加到您的settings.json文件)

在Windows構建中,快捷方式設置為Alt + 0

命令 K + 命令 0 不起作用。

此處提供更多信息: 區域擴展https://marketplace.visualstudio.com/items?itemName=maptz.regionfolder

安裝擴展並使用 python 后,它的工作原理如下:

# region ARBITRARY_REGION_NAME

   code goes here...

# endregion

此外,選擇所需區域,使用 Ctrl+M+Ctrl+R。

(即:先按住Ctrl,再按m,讓go m,再按r,讓go全部)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM