ChatGPT解决这个技术问题 Extra ChatGPT

JetBrains / IntelliJ keyboard shortcut to collapse all methods

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){...}
You may take a look at intellij code folding shortcuts. I guess that Ctrl+Shift+Minus is what you need.
YEAHHHH. I didn't know it was called code folding so i couldn't find it. Thanks!
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.
ha. i would be too but it ain't my fault. I inherited this
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.

J
Jesse Hufstetler

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.


A command that does not recursively collapse everything inside the methods too would be nice.
Ctrl + - : collapse current method
@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_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
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.
B
Basil Bourque

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


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!
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.
IntelliJ has a default keymap for this: Ctrl + NumPad-* followed by the level of folding you want, entered on the NumPad.
It collapses the blocks inside of methods! Isn't there any level 0 fold?
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)
R
Rahul Tiwari

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


This is espacially useful if you use another keymap (such as the eclipse keymap).
Unfortunately, "second stroke" shortcuts don't display the shortcut in the menu properly. See overthink's answer.
o
overthink

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


For all non mac people Cmd is Ctrl and Option is Shift
Good to know about second stroke shortcuts.
M
Meysam Jafari

You Can Go To setting > editor > general > code folding and check "show code folding outline" .


R
Rosdi Kasim

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.


The number on the numpad is working, but you need to release Ctrl+Shift+* first before pressing the numpad 2.