3

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

3

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"

  • 1
    works well for folding/unfolding elixir code blocks in elixir-mode too, thanks! – ashraf Dec 21 '18 at 15:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.