Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs ruby-mode indentation behavior

class Foo
  attr_accessor :a,
                :time, # ms since epoch
                :b,
                :c
end

In text mode, the variables listed after 'a' would indent as written above, but in ruby mode they would instead be flush with 'attr_accessor'. How can I get ruby mode to indent like text mode in this situation? Note that I'd like to be able to select the whole file and hit c-m-\ to get the above indentation in addition to all the other ruby-mode.el indentation rules.

like image 924
John Avatar asked Dec 10 '10 19:12

John


1 Answers

This hack should work in the majority of cases.

(defadvice ruby-indent-line (after line-up-args activate)
  (let (indent prev-indent arg-indent)
    (save-excursion
      (back-to-indentation)
      (when (zerop (car (syntax-ppss)))
        (setq indent (current-column))
        (skip-chars-backward " \t\n")
        (when (eq ?, (char-before))
          (ruby-backward-sexp)
          (back-to-indentation)
          (setq prev-indent (current-column))
          (skip-syntax-forward "w_.")
          (skip-chars-forward " ")
          (setq arg-indent (current-column)))))
    (when prev-indent
      (let ((offset (- (current-column) indent)))
        (cond ((< indent prev-indent)
               (indent-line-to prev-indent))
              ((= indent prev-indent)
               (indent-line-to arg-indent)))
        (when (> offset 0) (forward-char offset))))))

Example:

class Comment < ActiveRecord::Base
  after_create :send_email_to_author,
               :if => :author_wants_emails?,
               :unless => Proc.new { |comment| comment.post.ignore_comments? }
end
like image 100
Dmitry Avatar answered Sep 21 '22 07:09

Dmitry