且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 vs 代码,如何让 Scala 格式工作并格式化我的代码?

更新时间:2022-05-20 02:01:53

Scalafmt 似乎可以在 VSC 中使用我的机器上开箱即用的 Metals

Scalafmt seeems to work in VSC with Metals out-of-the-box on my machine

Metals 自动使用 Scalafmt 来响应格式化请求从编辑器中,根据中定义的配置.scalafmt.conf... 如果没有.scalafmt.conf,在收到第一个格式请求 Metals 将创建 .scalafmt.conf 文件你.

Metals automatically uses Scalafmt to respond to formatting requests from the editor, according to the configuration defined in .scalafmt.conf... if there is no .scalafmt.conf, upon receiving the first format request Metals will create the .scalafmt.conf file for you.

只需在 VSC 中从 Command Palette 执行 Format Document 命令,它应该根据 .scalafmt.conf 进行格式化. 如果你想在每次源代码更改时连续格式化和编译,并且 scalafmtOnCompile 不起作用,然后尝试在 sbt

Simply execute Format Document command from Command Palette in VSC and it should format according to .scalafmt.conf. If you would like to continuously format and compile on every source change and scalafmtOnCompile is not working, then try executing in sbt

~scalafmt;compile

然而,根据文档,编译时的格式是不鼓励

however format on compile is discouraged as per documentation

不鼓励使用此选项,因为它会弄乱编辑器,它会减慢编译速度.建议使用格式在编辑器中保存".

This option is discouraged since it messes up undo buffers in the editor and it slows down compilation. It is recommended to use "format on save" in the editor instead.