Emacs ruby​​ 模式缩进行为

发布于 2024-10-07 05:37:25 字数 286 浏览 2 评论 0原文

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

似狗非友 2024-10-14 05:37:25

这个技巧应该适用于大多数情况。

(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

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
夜灵血窟げ 2024-10-14 05:37:25

来自雷米(评论中):
请注意,Emacs 将正确缩进 class Foo attr_accessor(:a, :time, # ms since epoch :b, :c) end – Rémi Dec 11 '10 at 8:50

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

From Remi (in comments):
Note that Emacs will correctly indent class Foo attr_accessor(:a, :time, # ms since epoch :b, :c) end – Rémi Dec 11 '10 at 8:50

You can add parens and have it indent properly -- I'm adding this here because I'm looking for unanswered questions, and this one comes up (incorrectly, since it has been answered in the comments).

比忠 2024-10-14 05:37:25

当使用 Emacs 24.4 或更高版本时,默认情况下您的示例将像这样缩进。

When using Emacs 24.4 or newer, your example will be indented like this by default.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文