297

I'm working on some legacy code that has a class that is 10,000+ lines of code and has 100s of methods. Is there a shortcut for any JetBrains IDE (since the shortcut would likely be shared across all of them) to collapse all the methods / functions so that only the method signatures are shown?

Something like this:

public String myMethod(String arg1, int arg2){...}

public String mySecondMethod(String arg1, int arg2){...}
informatik01
  • 16,038
  • 10
  • 74
  • 104
Brad
  • 6,106
  • 4
  • 31
  • 43
  • 19
    You may take a look at [intellij code folding shortcuts](http://www.jetbrains.com/idea/webhelp/code-folding-2.html). I guess that Ctrl+Shift+Minus is what you need. – lifus May 13 '13 at 16:33
  • YEAHHHH. I didn't know it was called code folding so i couldn't find it. Thanks! – Brad May 13 '13 at 16:34
  • 4
    I would be tempted to downvote except I almost cried when you said "a class that is 10,000+ lines of code and has around 100s of methods" so +1 for trying. – Peter Lawrey May 13 '13 at 16:37
  • ha. i would be too but it ain't my fault. I inherited this – Brad May 13 '13 at 16:39
  • 6
    I realize this doesn't answer the question as asked, but `Ctrl-F12` will open a navigator to hop between methods, etc. (You can narrow down the list of methods by typing in this *Structure View* as well.) The *Structure Pane* (`Alt-7`) adds more options. This might be a more effective way of exploring files with large numbers of methods, so I leave the suggestion here for future googlers. – kghastie Aug 30 '17 at 14:27

7 Answers7

576

You may take a look at intellij code folding shortcuts.

For Windows/Linux do: Ctrl+Shift+-

For mac use Command+Shift+-

To unfold again do Ctrl+Shift++ or Command+Shift++ respectivley.

To unfold only a single method on Windows, use Ctrl+Alt+Plus.

Josiah Yoder
  • 3,321
  • 4
  • 40
  • 58
lifus
  • 8,074
  • 1
  • 19
  • 24
  • 35
    A command that does not recursively collapse everything inside the methods too would be nice. – trinity420 Jul 21 '18 at 11:33
  • 4
    Ctrl + - : collapse current method – Ali_Ai_Dev Aug 29 '18 at 04:10
  • The answer below is much better, cause it has all the granularity I was looking for! – Angelos Pikoulas Feb 28 '20 at 17:03
  • @Ali_dev is there a way to do the same to all methods? – shashwat Mar 29 '20 at 04:50
  • 3
    @shashwat not exactly. you can use Ctrl+Shift+Minus to collapse all of them and then use Ctrl+Alt+Plus to open one of that recursively. – Ali_Ai_Dev Mar 31 '20 at 07:04
  • 2
    @Ali_dev thanks, the old shortcuts don't work the same anymore. But with the ctrl alt plus gets the desired effect since ctrl shift minus now does a method-level recursive collapse – Josh C Nov 03 '20 at 02:13
  • WRONG ANSWER - see the one below, that's the correct one. This will fold up other code segments inside the methods, making reviewing code a pita. – Steve Horvath Feb 23 '21 at 23:41
  • 1
    Maybe it's the fact that I'm using Goland and this is still the most appropriate response I could find, but the below answer doesn't work. This answer works better for Goland, IMO. – swade May 14 '21 at 17:30
  • 1
    Note that this also works on a code selection. Select a block of code that contains methods, press Ctrl+Shift+Minus, and all the methods within the selection will be individually folded to one line each. – JoeMjr2 Apr 12 '22 at 20:02
  • `Ctrl+Alt+H` on the main method shows a call hierarchy that can help with reordering the methods in a file. I had to create a custom scope including only my code to make this hierarchy useful. – Josiah Yoder Aug 15 '22 at 15:34
146

The above suggestion of Ctrl+Shift+- code folds all code blocks recursively. I only wanted to fold the methods for my classes.

Code > Folding > Expand all to level > 1

I managed to achieve this by using the menu option Code > Folding > Expand all to level > 1.

I re-assigned it to Ctrl+NumPad-1 which gives me a quick way to collapse my classes down to their methods.

This works at the 'block level' of the file and assumes that you have classes defined at the top level of your file, which works for code such as PHP but not for JavaScript (nested closures etc.)

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Precastic
  • 3,742
  • 1
  • 24
  • 29
  • 7
    This should be the accepted answer IMO. It solves the problem in 1 step and solves it exactly as asked. If instead you the do the accepted answer you will be folding everything that can be folded and then unfolding to see your methods. I've been doing the latter and it's not *that* time consuming, but it's nice to know I can skip some steps with the former from this post. Thanks! – John Pancoast Oct 08 '17 at 14:31
  • 1
    Nice, this does it. The only problem im facing now is I cant do it by pressing keyboard buttons as I have a MacbookPro with no `numpad`. Will have to Google on how to assign custom keys i suppose. Sigh. – CodeGodie Jan 12 '18 at 23:16
  • 1
    IntelliJ has a default keymap for this: `Ctrl` + `NumPad-*` followed by the level of folding you want, entered on the NumPad. – Matt C Jan 25 '19 at 19:13
  • 1
    It collapses the blocks inside of methods! Isn't there any **level 0 fold**? – Amir Fo Feb 10 '20 at 10:53
  • Anyone knows how to set shortcuts to ctlr/shift/num1 ? I'd like to do that, but phpstorm interprets it as ctrl/end for some reason (ctrl/num1 works, interestingly, adding the shift is the problem) – Steve Horvath Feb 23 '21 at 23:47
  • How did you manage to reassign keyboard shortcut for this? I'm using intellij 2021.1.3 on macOs Catalina. – Sumit Kumar Sep 22 '21 at 09:27
  • @SumitKumar and Steve Horvath I'm on a mac using PhpStorm 2021.3.3. You can edit, remove, and update the key bindings by opening Preferences > Keymap. Once you've got that open you can search for "folding" and see what the default mappings are. You can right click on them to add additional key combos or change/remove the defaults. – Hunter Apr 08 '22 at 19:27
  • If you don't want to use keyboard shortcuts or the menu every time you can also update the preferences to do this by default when viewing files. Open `Preferences` > `Editor` > `General` > `Code Folding` from there under general you can check `Method Bodies` or if you only want this to apply to PHP scroll down to PHP and check `Method body` and `Function body` then click `Apply`. For me this ran on all the open tabs I had as well as files opened afterwards. macOS 12.3.1 | PhpStorm 2021.3.3 – Hunter Apr 08 '22 at 19:31
29

go to menu option Code > Folding to access all code folding related options and their shortcuts.

Rahul Tiwari
  • 6,851
  • 3
  • 49
  • 78
10

@precastic's answer above is, imo, the right idea.

Worth noting that in IDEA 2018.2 (and surely other nearby versions) there are default keyboard shortcuts for this: (showing Mac, see Code > Folding > Expand All to Level for your system):

Cmd+Option+Keypad *, 1 - expand all to level 1
Cmd+Option+Keypad *, 2 - expand all to level 2
...
Cmd+Option+Keypad *, 5 - expand all to level 5

Note: these are "second stroke" shortcuts. First press Cmd+Option+*, then release, then hit the number you want.

overthink
  • 23,985
  • 4
  • 69
  • 69
3

You can go to setting > editor > general > code folding and check "Show code folding outline" .

Pang
  • 9,564
  • 146
  • 81
  • 122
1

In Rider, this would be Ctrl +Shift+Keypad *, 2

But!, you cannot use the number 2 on keypad, only number 2 on the top row of the keyboard would work.

Rosdi Kasim
  • 24,267
  • 23
  • 130
  • 154
  • 3
    The number on the numpad is working, but you need to release Ctrl+Shift+* first before pressing the numpad 2. – Northnroro Mar 05 '20 at 09:47
0

I realize this doesn't answer the question as asked, but ticking the Method bodies option in Code Folding settings may solve OP's problem. If enabled, all methods are folded by default.

rvrsd
  • 111
  • 1
  • 5