JetBrains / IntelliJ keyboard shortcut to collapse all methods
Asked Answered
P

7

307

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){...}
Punishable answered 13/5, 2013 at 16:28 Comment(5)
You may take a look at intellij code folding shortcuts. I guess that Ctrl+Shift+Minus is what you need.Boyle
YEAHHHH. I didn't know it was called code folding so i couldn't find it. Thanks!Punishable
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.Overexpose
ha. i would be too but it ain't my fault. I inherited thisPunishable
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.Jerrybuilt
B
589

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.

Boyle answered 13/5, 2013 at 16:37 Comment(10)
A command that does not recursively collapse everything inside the methods too would be nice.Niel
Ctrl + - : collapse current methodSwansea
The answer below is much better, cause it has all the granularity I was looking for!Birdhouse
@Ali_dev is there a way to do the same to all methods?Kosiur
@Kosiur 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.Swansea
@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 collapseLetta
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.Coniology
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.Meridethmeridian
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.Dynameter
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.Valetudinary
D
155

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.)

Delitescence answered 7/3, 2016 at 10:27 Comment(8)
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!An
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.Isadoraisadore
IntelliJ has a default keymap for this: Ctrl + NumPad-* followed by the level of folding you want, entered on the NumPad.Odawa
It collapses the blocks inside of methods! Isn't there any level 0 fold?Benitabenites
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)Coniology
How did you manage to reassign keyboard shortcut for this? I'm using intellij 2021.1.3 on macOs Catalina.Weakkneed
@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.Kilmarnock
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.3Kilmarnock
G
29

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

Grume answered 18/5, 2016 at 11:9 Comment(2)
This is espacially useful if you use another keymap (such as the eclipse keymap).Tamarah
Unfortunately, "second stroke" shortcuts don't display the shortcut in the menu properly. See overthink's answer.Roborant
B
11

@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.

Balls answered 16/11, 2018 at 15:22 Comment(2)
For all non mac people Cmd is Ctrl and Option is ShiftLomax
Good to know about second stroke shortcuts.Moiramoirai
G
3

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

Ginsburg answered 15/9, 2018 at 9:7 Comment(0)
F
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.

Fumatorium answered 4/1, 2020 at 13:30 Comment(1)
The number on the numpad is working, but you need to release Ctrl+Shift+* first before pressing the numpad 2.Ne
A
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.

Aseptic answered 18/2, 2023 at 9:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.