2010-12-10 79 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或更高,您的示例將被默認縮進這樣。