2010-12-10 60 views
14
 
class Foo 
    attr_accessor :a, 
       :time, # ms since epoch 
       :b, 
       :c 
end 

在文本模式下,'a'后面列出的变量将按照上面所述缩进,但在红宝石模式下,它们将替换为'attr_accessor'。在这种情况下,我怎样才能让ruby模式缩进文本模式?请注意,我希望能够选择整个文件,并且除了所有其他ruby-mode.el缩进规则之外,还可以通过c-m- \获得上述缩进。Emacs红宝石模式压痕行为

+2

注意,Emacs会正确地将缩进 类Foo attr_accessor(:一, :时间,因为时代 #MS:B, :C) 结束 – 2010-12-11 08:50:53

+0

“口齿不清” 不是这个问题 – drdo 2010-12-11 12:01:49

+0

加入了正确的标签parens作品,谢谢! – John 2010-12-12 05:22:09

回答

4

从雷米(在评论): 注意,Emacs会正确地缩进类Foo attr_accessor(:一,:时间,因为时代#MS:B,:C)结束 - 雷米12月11日'10在8:50

你可以添加parens并让它正确缩进 - 我在这里添加这个是因为我正在寻找没有答案的问题,并且这个问题出现了(错误地,因为它已经在评论中得到了回答)。

+0

哇,这很糟糕。在这种情况下,我不想添加括号来让Ruby正确缩进。你有没有找到任何其他方式来完成这项工作? – slacy 2011-09-28 23:17:19

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

实施例:

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

美丽!感谢名单! – aL3xa 2012-02-06 01:37:26

+0

不客气!我已经使用这个调整了几个月,现在没有任何重大问题。 – Dmitry 2012-02-07 05:47:40

+0

这看起来很酷。当你说“没有任何重大问题”时,你是否谦虚?我应该注意哪些小问题? – 2012-02-23 02:14:05

2

当使用Emacs 24.4或更高,您的示例将被默认缩进这样。