简体   繁体   English

Emacs中的Scala模式缩进

[英]Scala mode indentation in Emacs

When writing Scala code in Emacs, I notice the following indentation issue: 在Emacs中编写Scala代码时,我注意到以下缩进问题:

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

Then press enter. 然后按Enter。

Then close the bracket, and this is what ends up happening: 然后合上支架,结果就是这样:

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

Although this is one particular example, this issue appears in a variety of ways when auto-indenting in Emacs. 尽管这是一个特定的示例,但是在Emacs中自动缩进时,会以多种方式出现此问题。

An answer to either of these two questions would be appreciated: 对于以下两个问题之一的回答将不胜感激:

  1. How can this issue be fixed so that the brace gets put in the proper place and anything within the braces is indented one level to the right? 如何解决此问题,以使支架放置在正确的位置,并且支架内的所有东西都向右缩进一层?

  2. Is it possible to disable this type of auto-indentation (ie like 'set noautoindent' in vi). 是否可以禁用这种类型的自动缩进(如vi中的'set noautoindent')。 I tried solutions like the ones suggested here: Disable auto indent globally in Emacs without success. 我尝试了以下建议的解决方案: 在Emacs中全局禁用自动缩进,但没有成功。

Thanks in advance! 提前致谢!

I've coded up a simple piece of code - it makes emacs keep indent level for most time, and indents two spaces to the right when the previous non-empty line ends with "{", "(", ">", "=". 我已经编写了一段简单的代码-它使emacs在大多数时间都保持缩进级别,并在前一个非空行以“ {”,“(”,“>”,“ =“。

Add file besi.el to your load path. 将文件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))

Edit line in scala-mode.el 在scala-mode.el中编辑行

indent-line-function          'besi-indent-line

and line in scala-mode-ui.el 并在scala-mode-ui.el中行

("\r"                       'besi-newline)

Also add a line (require 'besi) to the start of scala-mode.el. 还要在scala-mode.el的开头添加一行(require 'besi)

Uploaded it to github for easy reference - besi 将其上传到github以便于参考-besi

I tried to solve this by editing scala-mode-indent.el file. 试图通过编辑scala-mode-indent.el文件来解决此问题。 It breaks indent in some of the other situations, but at least you will not have all those indents half-screen forward. 在某些其他情况下,它会打破缩进,但至少您不会让所有缩进都进入半屏显示状态。

Comment out this line: 注释掉这一行:

;; (scala-indentation-from-following)

And modify scala-indentation-from-preceding: 并从前面修改scala缩进:

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

As I said, it still remains broken after that. 正如我说的那样,此后仍然无法解决。 I plan to write a better support shortly, maybe in a week or two. 我计划在短短的一两个星期内提供更好的支持。

EDIT: 编辑:

If you want to disable scala indentation completely, comment out the line in scala-mode.el 如果要完全禁用scala缩进,请在scala-mode.el中注释掉该行

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM