5

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

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

然后按回车。

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

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

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

将不胜感激这两个问题中的任何一个的答案:

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

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

提前致谢!

4

2 回答 2

4

我编写了一段简单的代码 - 它使 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)

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

上传到github方便参考-besi

于 2012-01-31T17:14:46.223 回答
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
于 2012-01-20T21:13:50.470 回答