我的多项目 sbt 存储库中有 scala 格式插件。
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.3.2")
所以在 sbt 控制台中,如果我运行 scalafmt 它工作正常
我的 build.sbt 有:
scalafmtOnCompile := true
如果我在 sbt 中执行 ~compile 或只是手动编译,它不会在编译期间格式化我的代码。
我的设置有什么问题?
此外,运行 scalafmt 可以工作,但它不会像我的 Dependencies.scala 文件那样在/project 中格式化我的 .scala 文件。为什么忽略这些文件?
我也将 VS Code 与金属一起用作我的 IDE。
最佳答案
Scalafmt 似乎可以在 VSC 中使用我的机器上开箱即用的 Metals
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 中从
Format Document
执行 Command Palette
命令,它应该根据 .scalafmt.conf.
进行格式化~scalafmt;compile
然而,根据文档,编译格式为 discouraged
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.
关于scala - 使用 vs 代码,如何让 Scala 格式工作并格式化我的代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61060140/