本文介绍了Emacs ruby-mode 缩进行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Foo类attr_accessor :a,:time, # 自纪元以来的毫秒数:b,:C结尾在文本模式下,'a' 之后列出的变量将按上述方式缩进,但在 ruby 模式下,它们将与 'attr_accessor' 齐平.在这种情况下,如何让 ruby 模式像文本模式一样缩进?请注意,除了所有其他 ruby-mode.el 缩进规则之外,我希望能够选择整个文件并点击 c-m- 以获取上述缩进.
解决方案
这个 hack 应该适用于大多数情况.
(defadvice ruby-indent-line (在 line-up-args 激活后)(let (indent prev-indent arg-indent)(节省游览(回缩进)(当(zerop(汽车(语法-ppss)))(setq indent (当前列))(skip-chars-backward "
")(当 (eq ?, (char-before))(ruby-backward-sexp)(回缩进)(setq prev-indent (current-column))(skip-syntax-forward "w_.")(skip-chars-forward " ")(setq arg-indent (current-column)))))(当预先缩进时(让((偏移量(-(当前列)缩进)))(cond ((< indent prev-indent)(缩进到上一个缩进))((= indent prev-indent)(缩进到 arg-indent)))(when (> offset 0) (forward-char offset))))))
示例:
class 注释 <ActiveRecord::Baseafter_create :send_email_to_author,:如果=>:author_wants_emails?,:除非=>Proc.new { |评论|comment.post.ignore_comments?}结尾
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.
解决方案
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 "
")
(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
这篇关于Emacs ruby-mode 缩进行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!