【问题标题】:Scala mode indentation in EmacsEmacs 中的 Scala 模式缩进
【发布时间】:2012-01-20 17:13:55
【问题描述】:

在 Emacs 中编写 Scala 代码时,我注意到以下缩进问题:

List(1,2,3).foreach{ x =>

然后按回车键。

然后关闭括号,这就是最终发生的事情:

List(1,2,3).foreach{ x =>
                  }

虽然这是一个特殊示例,但在 Emacs 中自动缩进时,此问题会以多种方式出现。

如果能回答这两个问题中的任何一个,我们将不胜感激:

  1. 如何解决此问题,以便将大括号放置在正确的位置,并且大括号内的任何内容都向右缩进一级?

  2. 是否可以禁用这种类型的自动缩进(如 vi 中的“set noautoindent”)。我尝试了类似这里建议的解决方案:Disable auto indent globally in Emacs 没有成功。

提前致谢!

【问题讨论】:

    标签: scala emacs indentation auto-indent


    【解决方案1】:

    我编写了一段简单的代码 - 它使 emacs 大部分时间保持缩进水平,并在前一个非空行以“{”、“(”、“>”结尾时向右缩进两个空格", "="。

    将文件 besi.el 添加到您的加载路径。

    (provide 'besi)
    
    (defun besi-indent-line ()
      "Indent current line"
      (interactive)
      (scala-indent-line-to (besi-indent)))
    
    (defun besi-indent ()
      (save-excursion
        (forward-comment -100000)
        (backward-char 1)
        (if (or (looking-at "{") (looking-at "=") (looking-at ">") (looking-at "("))
          (+ (current-indentation) 2)
          (current-indentation))))
    
    (defun besi-newline ()
      (interactive)
      (newline-and-indent))
    

    在 scala-mode.el 中编辑行

    indent-line-function          'besi-indent-line
    

    并在 scala-mode-ui.el 中行

    ("\r"                       'besi-newline)
    

    还要在 scala-mode.el 的开头添加一行 (require 'besi)

    上传到 github 方便参考 - besi

    【讨论】:

    • 如果您有任何建议/cmets,请告诉我!
    【解决方案2】:

    尝试通过编辑 scala-mode-indent.el 文件来解决这个问题。它在其他一些情况下会破坏缩进,但至少你不会让所有这些缩进都向前半屏。

    注释掉这一行:

    ;; (scala-indentation-from-following)
    

    并修改 scala-indentation-from-preceding:

    (defun scala-indentation-from-preceding ()
       ;; Return suggested indentation based on the preceding part of the
       ;; current expression. Return nil if indentation cannot be guessed.
       (save-excursion
       (scala-backward-spaces)
       (and 
         (not (bobp))
       (if (eq (char-syntax (char-before)) ?\()
          (scala-block-indentation)
          (progn
            (when (eq (char-before) ?\))
            (backward-sexp)
            (scala-backward-spaces))
            t
           ;;(scala-looking-at-backward scala-expr-start-re)
    
          ))
        (if (scala-looking-at-backward scala-expr-start-re)
          (+ (current-indentation) scala-mode-indent:step)
          (current-indentation)
        ))))
    

    正如我所说,在那之后它仍然坏掉了。我计划很快写一个更好的支持,也许在一两周内。

    编辑:

    如果要完全禁用 scala 缩进,请在 scala-mode.el 中注释掉该行

    ;; indent-line-function          'scala-indent-line
    

    【讨论】:

    • 非常感谢罗加奇!如果您添加更多支持,请告诉我。
    • @nequitans - 不客气!完成后,我会为这个问题添加另一个答案。
    猜你喜欢
    • 1970-01-01
    • 2011-10-20
    • 1970-01-01
    • 2011-05-23
    • 1970-01-01
    • 1970-01-01
    • 2010-10-14
    • 2014-05-24
    • 1970-01-01
    相关资源
    最近更新 更多