简体   繁体   English

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

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

By "VS Code" I mean the lightweight text editor not the monolithic IDE, unfortunately searching this on google will bring up many pieces of irrelevant information about how to do this in Visual Studio. “VS Code”是指轻量级文本编辑器,而不是单一的 IDE,不幸的是,在谷歌上搜索它会显示许多关于如何在 Visual Studio 中执行此操作的不相关信息。

To the question itself, Anybody knows how to hide arbitrary selected lines of code in "Visual Studio Code" , preferably into a plus sign like collapsing does?对于问题本身,任何人都知道如何在“Visual Studio Code”中隐藏任意选定的代码行,最好像折叠一样隐藏到加号中?

Note: this is different than collapsing nested code which probably could be achieved by Ctrl+K,Ctrl+<num> , what I need here is to hide specific block of code of choice, no matter nested or not.注意:这不同于可能可以通过Ctrl+K,Ctrl+<num>实现的折叠嵌套代码,我在这里需要的是隐藏特定的选择代码块,无论嵌套与否。

EDIT: I see there are people who don't understand my requirements.编辑:我看到有些人不理解我的要求。

for example, you may think what I want is this:例如,你可能认为我想要的是这样的:

before hiding:隐藏前:

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

after hiding:隐藏后:

[+] for i in j: ...

What I actually want is this:我真正想要的是:

before hiding:隐藏前:

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

after hiding:隐藏后:

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

2017.10.17 EDIT: turns out VS Code implemented a very similar feature called "Folding Regions" in VS Code 1.17.1 Update. 2017.10.17 编辑:原来 VS Code 在 VS Code 1.17.1 更新中实现了一个非常相似的功能,称为“折叠区域”。link关联

You can use the following delimiters for code folding:您可以使用以下分隔符进行代码折叠:

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

See https://github.com/Microsoft/vscode/issues/12146 ([folding] fold regions)请参阅https://github.com/Microsoft/vscode/issues/12146 ([折叠] 折叠区域)

Unfortunately, it doesn't appear that VSCode currently allows you to hide an arbitrary selection of code like Visual Studio does via Ctrl + M , Ctrl + H .不幸的是,目前看来,VSCode 不允许您像 Visual Studio 那样通过Ctrl + MCtrl + H隐藏任意选择的代码。 For now you could use the code folding feature, which depends on indentation.现在您可以使用代码折叠功能,这取决于缩进。 That is, if you indent the code you want to hide, you could then collapse it via Ctrl + Shift + [ , like so:也就是说,如果您缩进要隐藏的代码,则可以通过Ctrl + Shift + [将其折叠,如下所示:

vscode中的代码折叠

Obviously, this is kind of an ugly solution to your problem;显然,这对您的问题来说是一种丑陋的解决方案; it requires multiple steps and makes actual changes to the file.它需要多个步骤并对文件进行实际更改。 Also, it's clearly useless if you're writing in a whitespace-dependent language like Python, but I don't think you are going to find a better solution short of finding an extension (or writing one yourself).此外,如果您使用 Python 等依赖于空格的语言编写,这显然是没有用的,但我认为您不会找到更好的解决方案,而不是找到一个扩展(或自己编写一个)。 It also might be worth posting an issue on the official VSCode GitHub repo if this feature is important to you.如果此功能对您很重要,也可能值得在官方 VSCode GitHub 存储库上发布问题。

Here is the vs code documentation for folding a selection .这是折叠选择的 vs 代码文档。

To hide : Highlight the lines you want to fold then press Ctrl+K then Ctrl+,隐藏:突出显示要折叠的行,然后按Ctrl+K ,然后Ctrl+,

To unhide : Just click the ">" icon to the left of the row of the folded code or press Ctrl+K then Ctrl+.要取消隐藏:只需单击折叠代码行左侧的“>”图标或按Ctrl+K ,然后Ctrl+.

In the Insiders Build v1.70 now is the functionality and command to truly hide arbitrary lines of code. Insiders Build v1.70 现在是真正隐藏任意代码行的功能和命令。 The command is命令是

Fold Selected Lines
editor.foldSelected

It is bound to Ctrl + K Ctrl + .它绑定到Ctrl + K Ctrl + by default.默认。 Select any lines you want to fold.选择要折叠的任何线条。

折叠选定的行演示

Comment with a delimiter with decreased indentation.使用缩进减少的分隔符进行注释。 Hiding then works as in nested, with the little arrow on the left.隐藏然后像嵌套一样工作,左边的小箭头。

see here看这里

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM