4

I found a snippet that let's me fold bad(large) blocks of code

(add-hook 'ruby-mode-hook
          (lambda () (hs-minor-mode)))

(eval-after-load "hideshow"
  '(add-to-list 'hs-special-modes-alist
        `(ruby-mode
                  ,(rx (or "def" "class" "module" "do" "{" "[")) ; Block start                                   
                  ,(rx (or "}" "]" "end"))                       ; Block end                                     
                  ,(rx (or "#" "=begin"))                        ; Comment start                                 
                  ruby-forward-sexp nil)))

(global-set-key (kbd "C-c h") 'hs-hide-block)
(global-set-key (kbd "C-c s") 'hs-show-block)

Unfortunately, this code is not smart enough.

fold.gif

What are my alternatives?

Update: Noticed sublime text does it out of the box

enter image description here

american-ninja-warrior
  • 3,773
  • 2
  • 21
  • 40

1 Answers1

5

I tried yafolding, seems to work (theres room for improvement on this, though)

(require 'yafolding)
(add-hook 'ruby-mode-hook 'yafolding-mode)

(global-set-key (kbd "M-RET")   'yafolding-toggle-element)

Possible alternatives: "yafolding", "hideshow", "folding.el", "imenu.el","vimish-fold"

Here's a how yafolding folded blocks look like:

enter image description here

american-ninja-warrior
  • 3,773
  • 2
  • 21
  • 40