【问题标题】:Indent if previous line ends with a comma如果上一行以逗号结尾,则缩进
【发布时间】:2014-08-05 13:42:15
【问题描述】:

在编写 ruby​​ 代码时,Emacs 不会缩进以逗号结尾的行:

attr_accessor :a, :b,
:c

我希望下面的行缩进一级:

attr_accessor :a, :b,
  :c

看了ruby-mode.el之后,我觉得这不是一件容易的事。有没有办法做到这一点?

【问题讨论】:

  • 如果这是您认为 ruby-mode.el 无论如何都应该做的事情,那么无论您是否在这里得到答案,请考虑为此提交 Emacs 增强请求:@ 987654325@.

标签: ruby emacs elisp


【解决方案1】:

好的,感谢this post 我找到了一种方法:

(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 (+ (ruby-current-indentation) ruby-indent-level))))) ;; (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))))))

【讨论】:

    【解决方案2】:

    我一直在开发一个库来自定义您对缩进的需求,它仍在开发中,但我自己将它用于 Erlang 模式,也许您想检查一下。我不认为这是一个合适的解决方案,但它可能对你有用。

    https://github.com/AtticHacker/indent-of-doom

    这是一个自定义缩进的 DSL,根据您的需要,这是您想要添加到您的 emacs 中的内容:

    (setq tab-width 2) ; Set default tab with to 2
    (setq doom-indent-fallback t) ; If no rules match, use default tab
    (setq doom-use-tab-cycle nil) ; Don't use tab cycling
    (setq my-doom '(
        ; Rules for ruby mode
        (ruby-mode . (
            ; When previous line ends on "," and starts with "attr_accessor"
            ; indent the current line as previous + 1 tab
            ((and (prev 'ends-on ",") (prev 'starts-with "attr_accessor")) (prev 'indent 1)))
        )
    ))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-16
      • 1970-01-01
      • 2019-02-12
      • 1970-01-01
      • 2022-08-21
      相关资源
      最近更新 更多