Fold code except around cursor


#1

Hi guys,

I’m coming from Sublime and so far my experience with Atom is pretty good :slight_smile:
There is one small thing about code folding which is bothering me:

In Sublime, when you press ctrl-k ctrl-1, it will fold all the functions of your file except the one where your cursor is (which is usaully the one which you are working on).

In Atom, it just folds everything so you then have to unfold the current function.

Is there a package which allows to do this?
Thanks a lot!


Code Folding (recursive)
#2

It seems you can achieve this with a few more presses, if you hit Ctrl+Alt+Shift+[ to Fold All, and then press Ctrl+Alt+] to Unfold Current Row. You have to Unfold Current Row several times, depending on how deeply nested it is.

I presume you can fairly easily make a package to run these commands on one keystroke.


#3

No need to even make a package. The following code in your init.coffee will fold all and then unfold the current row until it is unfolded. Then you just have to assign a keybinding to it.

atom.commands.add 'atom-text-editor', 'custom:fold-all-except-current-row', ->
  atom.commands.dispatch document.querySelector('atom-text-editor'), 'editor:fold-all'
  while atom.workspace.getActiveTextEditor().isFoldedAtCursorRow() == true
    atom.commands.dispatch document.querySelector('atom-text-editor'), 'editor:unfold-current-row'

#4

Hey guys,

Thanks for the tip.
I’ve copied your code and it works almost fine.
I just have to know how to scroll to where the cursor is but it’s a good start.
Cheers!


#5

Oh, that’s easy to handle.

atom.commands.add 'atom-text-editor', 'custom:fold-all-except-current-row', ->
  atom.commands.dispatch document.querySelector('atom-text-editor'), 'editor:fold-all'

  while atom.workspace.getActiveTextEditor().isFoldedAtCursorRow() == true
    atom.commands.dispatch document.querySelector('atom-text-editor'), 'editor:unfold-current-row'

  atom.workspace.getActiveTextEditor().scrollToCursorPosition({center: true})

#6

Cool, I just had to replace document.querySelector('atom-text-editor') with document.activeElement and now it works fine.
Otherwise if I had more than one tab open, the command will be executed on the first tab, even if it’s not the one focused.
Thanks a lot for your help!


#7

Damn. I didn’t test that eventuality. Good to know.


#8

For future readers, I also changed the fold-all to fold-at-indent-level-1 so that if the function has nested blocks, it does not fold the sub blocks. Here’s the final script:

atom.commands.add 'atom-text-editor', 'custom:fold-all-except-current-row', ->
    atom.commands.dispatch document.activeElement, 'editor:fold-at-indent-level-1'
    while atom.workspace.getActiveTextEditor().isFoldedAtCursorRow() == true
        atom.commands.dispatch document.activeElement, 'editor:unfold-current-row'
    atom.workspace.getActiveTextEditor().scrollToCursorPosition({center: true})

And here is the keybinding

'.editor':
    'alt-k': 'custom:fold-all-except-current-row'