Soft question: in IntelliJ, is there a way to have fold open up just the top most level (of everything in the file)?

Recursively close all folds:

pub struct Foo { ... }

impl Foo { ... }

Recursively open everything:

pub struct Foo {
  // shows all fields
  // good, we like this
 }

impl Foo { 
  fn func_1() {
  // all lines f body
  // we want this hidden
  }

  fn func_2() {
  // all lines of body
   // we want this hidden
  }

 }

What I want is to have only the top most level shown, i.e.

impl Foo {
  fn func_1() { ... }
  fn func_2() { ... }
}
``

A file may have morethan one impl blocks (for multiple traits). Is there a way to have it open just the top level for all the impl blocks ?

Thanks!

There's a "Code" -> "Folding" -> "Expand all to level" -> "1" action. By default, its keybinding isn't very convenient (ctrl+shift+numpad* -> 1) but you can re-bind it to something easier.

1 Like
  1. Thanks @ozkriff , this does what I want.

  2. Short of writing a custom Java / Kotlin / Scala plugin, is there a way to bind this (expand to all level 1) event to get triggered every time a file is opened ?

BTW, for anyone using IdeaVim, the action name is ExpandAllToLevel1

Quick and dirty way to do this from the keyboard:

  1. Shift-Shift (hit it twice)
  2. Tab 3 times (to select "Action")
  3. Start typing "expand"
  4. Use arrow keys to select "Expand all to level"
  5. Select 1

I realize that seems like a lot, but it's a fast way to find a command if you don't use it all the time. FYI I'm on a mac and when I opened that "expand all to" menu, it had hotkeys listed for the individual levels.

As for running the command on file open, I don't know tha tthere's a way to do this, short of using a plugin.

Ah, the reason I mentioned "ExpandAllToLevel1" is that to bind it to a vim normal key sequence in ~/.ideavimrc, we need the exact name of the action. This way, inside a buffer, I can hit something like <esc> (goes to normal mode) then the desired key binding, and it executes the cmd.

This is not what you are asking for, but it might be what you want. Thereā€™s ā€œfold method bodies by defaultā€ setting:

3 Likes