Emacs红宝石模式缩进行为 [英] Emacs ruby-mode indentation behavior

查看:122
本文介绍了Emacs红宝石模式缩进行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


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

在文本模式下,'a'之后列出的变量将如上所述缩进,但是在ruby模式下,它们将与'attr_accessor 。在这种情况下,如何让文本模式缩进红宝石模式?请注意,除了所有其他ruby-mode.el缩进规则之外,我希望能够选择整个文件并按下cm-\来获得上述缩进。

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.

推荐答案

这个黑客应该在大多数情况下工作。

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))))))

示例:

class Comment < ActiveRecord::Base
  after_create :send_email_to_author,
               :if => :author_wants_emails?,
               :unless => Proc.new { |comment| comment.post.ignore_comments? }
end

这篇关于Emacs红宝石模式缩进行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆