24

We have a common structure in our code where we have several related classes in the same file with several methods in each. What I would like to achieve is to fold all the methods in the class I'm in so that I could get a good overview of the "moving parts" in this class.

I know there is the Collapse Recursively method, but the problem with that is that it fold all the inside parts of the methods, so that when I want to dig in, I can't.

So my question is, is there a way (complex as it may be) to collapse all the elements at my current level (with a keyboard shortcut)?

LazyOne
  • 158,824
  • 45
  • 388
  • 391
Alex Zak
  • 1,924
  • 2
  • 18
  • 26
  • 1
    It will help a lot if you could illustrate your exact ideas with some screenshots. In any case: `Alt+Click` (Default keymap) on collapsed `[+]`/expanded `[-]` node will expand/collapse all children as well. There is also `Code | Folding | Expand to level` which allows expand top X levels. To overview your class structure you may also use Structure tool window. – LazyOne Nov 23 '16 at 14:00
  • Default foldings can be set at `Settings/Preferences | Editor | General | Code Folding` -- e.g. "Method bodies" – LazyOne Nov 23 '16 at 14:04

5 Answers5

54

So my question is, is there a way (complex as it may be) to collapse all the elements at my current level (with a keyboard shortcut)?

Key board short cut : Hold down ctrl shift - to collapse them all.

Ram Ghadiyaram
  • 28,239
  • 13
  • 95
  • 121
  • 3
    The question specifically states "I know there is the Collapse Recursively method, but the problem with that is that it fold all the inside parts of the methods, so that when I want to dig in, I can't." This answer will collapse **everything**, including if- and while-statements. OP is asking for a way to just fold all of the method bodies and leave everything else unfolded. – Travesty3 May 31 '19 at 14:42
19

If anyone uses Mac OS:

command + shift + -

Ivan Sanchez
  • 546
  • 5
  • 17
9

I found this question while attempting to find an answer myself. The two existing answers ignored parts of the original question. This question is very similar to this other question, but with the explicitly stated requirement that the code within the method blocks should not be folded.

The other question has a better answer:

Code -> Folding -> Expand all to level -> 1

The naming on this is a little weird because it says "Expand" when you're really trying to collapse all method bodies. But if the method bodies are already expanded, this will collapse them. The shortcut for this is ctrlshiftNumPad *, 1. If you find that shortcut combination cumbersome or don't have a num pad, you can create your own shortcut by going to Preferences -> Keymap.

This seems to me like the perfect solution. It won't expand or collapse any blocks at other levels. So if you manually collapse a block within a method body, this will leave it collapsed.

If that's not your preference, and you want it to expand all blocks within the method bodies while collapsing the method bodies themselves, you could create a macro that runs Code -> Folding -> Expand All followed by Code -> Folding -> Expand all to level -> 1, and then map your keyboard shortcut to that macro.

Travesty3
  • 14,351
  • 6
  • 61
  • 98
  • For me, "expand all to 1" collapses one level deeper than I need. If the file contains a list of functions, I would like all the functions to get collapsed, but nothing within those functions. Apparently, I would need something like "expand all to level 0"... – Jānis Elmeris Nov 24 '20 at 16:47
  • 1
    @JānisElmeris I agree, but I don't think you can do that right now. There are some feature requests open for that, but they need votes. https://youtrack.jetbrains.com/issue/IDEA-167045 – Travesty3 Nov 24 '20 at 17:42
6

Collapse all:

  • Windows and Linux : CtrlShift-
  • Mac OS : CommandShift-

Collapse current scope:
  • Windows and Linux : Ctrl-
  • Mac OS : Command-

Collapse Reccursively:
  • Windows and Linux : CtrlAlt-
  • Mac OS : CommandAlt-

Note: You can change this hotkeys in
Fille -> Setting -> Keymap
Akram Rabie
  • 473
  • 5
  • 11
3

try hold down ctrl and press the minus key

example :

ctrl -

to collapse the current block

My default keymap is Xwin. but hopefully it should work out the same.

Ram Ghadiyaram
  • 28,239
  • 13
  • 95
  • 121
Barnabas Nomo
  • 372
  • 3
  • 9
  • This will only fold the block that the cursor is currently on. The question is asking how to collapse **all** blocks at the same level as the current cursor. – Travesty3 May 31 '19 at 14:45