java IntelliJ IDEA - 折叠 for 循环和/或 if/else 语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36635018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:42:16  来源:igfitidea点击:

IntelliJ IDEA - Fold for loop and/or if/else statements

javaintellij-idea

提问by PhotonicPizza

I'd love to have a way of IntelliJ letting me fold forloops and if/elsestatements. But, I can't seem to find a way to do it automatically. I know you can use the //regionand //endregioncomments, or that you can do it manually with Ctrl+Shift+period, but is there a way to avoid all that and have it fold like methods or classes, without doing all that other stuff?

我很想有一种 IntelliJ 方法让我折叠for循环和if/else语句。但是,我似乎无法找到自动执行此操作的方法。我知道您可以使用//region//endregion注释,或者您可以使用 Ctrl+Shift+句点手动完成,但是有没有办法避免所有这些并使其像方法或类一样折叠,而无需执行所有其他操作?

回答by zhuguowei

You can fold and unfold:

  • Code blocks, i.e. code fragments surrounded by a matched pair of curly braces {}.

    To collapse a code block, place the cursor within that block and then select Code | Folding | Fold Code Blockor press ?+?+.(mac) or ctrl+?+.(Linux / Windows).

NoteCode folding works for the keywords if/ while/ else/ for/ try/ except/ finally/ within case of at least two statements.

您可以折叠和展开:

  • 代码块,即由一对匹配的花括号包围的代码片段{}

    要折叠代码块,请将光标置于该块内,然后选择代码 | 折叠 | 折叠代码块或按?+ ?+ .(mac) 或ctrl+ ?+ .(Linux / Windows)。

需要注意的关键字代码折叠的作品if/ while/ else/ for/ try/ except/ finally/with在至少两个语句的情况。

See: https://www.jetbrains.com/help/idea/2016.3/code-folding.html

参见:https: //www.jetbrains.com/help/idea/2016.3/code-folding.html

回答by Makoto

Under Editor > General > Code Folding, you can enable this functionality expressly.

在 Editor > General > Code Folding 下,您可以明确启用此功能。

enter image description here

在此处输入图片说明

By default, if you wish for it to collapse method bodies, you can tick that selection. There are a lot of other options available, which should cover your needs.

默认情况下,如果您希望它折叠方法主体,您可以勾选该选项。还有很多其他选项可用,它们应该可以满足您的需求。

If there's something that isn't covered, you can always enable "Show code folding outline" and use that to fold certain elements, although loops aren't foldable.

如果有未涵盖的内容,您始终可以启用“显示代码折叠轮廓”并使用它来折叠某些元素,尽管循环不可折叠。

回答by Cuneyt

Put the cursor anywhere in the line of method definition for which you want the code fragments to be folded and press ctrl+shift+- this will collapse the whole method and press ctrl++ right after. this will keep the if and for loop fragments inside the method collapsed (intellij 2020.1.1 linux)

将光标放在您希望折叠代码片段的方法定义行中的任何位置,然后按 ctrl+shift+- 这将折叠整个方法,然后立即按 ctrl++。这将使方法内的 if 和 for 循环片段保持折叠状态(intellij 2020.1.1 linux)