Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'd love to have a way of IntelliJ letting me fold for loops and if/else statements. But, I can't seem to find a way to do it automatically. I know you can use the //region and //endregion comments, 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?

like image 430
PhotonicPizza Avatar asked Apr 14 '16 22:04

PhotonicPizza


People also ask

How do you fold all methods in IntelliJ?

To collapse or expand all code fragments, press Ctrl+Shift+NumPad - / Ctrl+Shift+NumPad + . IntelliJ IDEA collapses or expands all fragments within the selection, or, if nothing is selected, all fragments in the current file, for example, all methods in a file.

How do I surround the code with if for Try Catch IntelliJ?

Simply select the code block to surround (don't forget to use Ctrl + W to increase the current selection) and then press Ctrl + Alt + T (or right-click the selection and select Surround with… from the menu). IntelliJ IDEA will show a list of options to choose from.


2 Answers

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 Block or press ++. (mac) or ctrl++. (Linux / Windows).

Note Code folding works for the keywords if/ while/ else/ for/ try/ except/ finally/ with in case of at least two statements.

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

like image 188
zhuguowei Avatar answered Oct 22 '22 12:10

zhuguowei


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

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.

like image 9
Makoto Avatar answered Oct 22 '22 13:10

Makoto