How to toggle current fold in editor view?


#1

I’d like to have editor:fold-current-row and editor:unfold-current-row combined. Is there any function that does exactly this?

Thanks
Kai


#2

Add this to your user init script:

atom.workspaceView.command 'editor:toggle-current-row-folding', ->
  e = atom.workspace.getActiveEditor()
  bufferRow = e.bufferPositionForScreenPosition(e.getCursorScreenPosition()).row
  if e.isFoldedAtBufferRow(0)
    e.unfoldBufferRow(bufferRow)
  else
    e.foldBufferRow(bufferRow)

It’ll add a new command that toggle the fold at the current cursor position :).


#3

Thank you, Abe. It works like a charme. Just a tiny correction was necessary:

--- if e.isFoldedAtBufferRow(0)
+++ if e.isFoldedAtBufferRow(bufferRow)

#4

Oh yes, copy/paste typo :slight_smile:


#5

API 1.0:

atom.commands.add 'atom-text-editor',
  'editor:toggle-current-row-folding': (event) ->
    editor = @getModel()
    bufferRow = editor.bufferPositionForScreenPosition(editor.getCursorScreenPosition()).row
    if editor.isFoldedAtBufferRow(bufferRow)
      editor.unfoldBufferRow(bufferRow)
    else
      editor.foldBufferRow(bufferRow)

Single hotkey for toggling code folding?
How can I use the same shortcut for Fold and Unfold?
#6

Ah, I got mine to work again with atom-workspace, but this is much better! :smiley:


#7

Brilliant, thank you.


#8

Doesn’t seem to work on my 1.2.4, what am I missing?


#9

Finally occurred to me what I was missing, forgot to keymap to this custom function. Adding here in case others blanked on it too:

  1. Add script (from @kshaper above) to your Init Script
  2. Add this to your Keymap: 'f1': 'editor:toggle-current-row-folding'

This replicates the fold/unfold behavior of Textmate.


#10

This is a wonderful solution, however it doesn’t work for selecting multiple lines. I’m coming from Komodo Edit where you can select multiple lines and toggle the fold with a single keybinding. Can this function be improved to also work for multiple line selections?


#11

Can we make a similar function for toggle-fold-all?


#12

Here’s a similar version to toggle all foldings. In init.coffee:

allFolded = false
atom.commands.add 'atom-text-editor',
  'editor:toggle-current-folding': (event) ->
    editor = @getModel()
    bufferRow = editor.bufferPositionForScreenPosition(editor.getCursorScreenPosition()).row
    if allFolded
      editor.unfoldAll()
      allFolded = false
    else
      editor.foldAll()
      allFolded = true

In keymap.cson:

'atom-workspace':
  'cmd-alt-0': 'editor:toggle-current-folding'

or whatever you want.