Emacs ruby​​-mode缩进行为

Joh*_*ohn 14 ruby emacs indentation

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

在文本模式中,'a'之后列出的变量将如上所述缩进,但在ruby模式下,它们将与'attr_accessor'齐平.在这种情况下,如何让ruby模式像文本模式一样缩进?请注意,除了所有其他ruby-mode.el缩进规则之外,我还希望能够选择整个文件并点击cm- \以获得上述缩进.

Dmi*_*try 12

这种黑客行为应该适用于大多数情况.

(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))))))
Run Code Online (Sandbox Code Playgroud)

例:

class Comment < ActiveRecord::Base
  after_create :send_email_to_author,
               :if => :author_wants_emails?,
               :unless => Proc.new { |comment| comment.post.ignore_comments? }
end
Run Code Online (Sandbox Code Playgroud)

  • 也许"改善"是一个强有力的词.;)但我确实涵盖了一些更多的角落案例,我已经完全删除了ruby-indent-line with around advice.https://github.com/lewang/le_emacs_libs/blob/master/ruby-mode-indent-fix.el (3认同)

Kem*_*son 4

来自 Remi(在评论中):\n请注意,Emacs 将正确缩进类 Foo attr_accessor(:a, :time, # 自 epoch :b, :c) 结束 \xe2\x80\x93 R\xc3\xa9mi Dec 11 '10 8点50分

\n\n

您可以添加括号并使其正确缩进——我在这里添加这个是因为我正在寻找未回答的问题,而这个问题出现了(错误的,因为它已在评论中得到了回答)。

\n