使用 vs 代码,如何让 Scala 格式工作并格式化我的代码? [英] Using vs code, how to get scala format to work and format my code?
问题描述
我的多项目 sbt 存储库中有 Scala 格式插件.
I have the scala format plugin in my multi project sbt repository.
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.3.2")
所以在 sbt 控制台中,如果我运行 scalafmt 它工作正常
So in the sbt console if I run scalafmt it works fine
我的 build.sbt 有:
My build.sbt has:
scalafmtOnCompile := true
如果我在 sbt 中执行 ~compile 或只是手动编译,它不会在编译期间格式化我的代码.
If I do a ~compile in sbt or just compile manually, it doesn't format my code during compilation.
我的设置有什么问题?
此外,运行 scalafmt 可以工作,但它不会像我的 Dependencies.scala 文件那样在/project 中格式化我的 .scala 文件.为什么会忽略这些文件?
Also, running scalafmt works but it doesn't format my .scala files in /project like my Dependencies.scala file. Why is it ignoring these files?
我将 VS Code 与金属一起用作我的 IDE.
I am using VS Code with metals also as my IDE.
推荐答案
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.
这篇关于使用 vs 代码,如何让 Scala 格式工作并格式化我的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!