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

标签 scala visual-studio-code sbt scalafmt scala-metals

我的多项目 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/

相关文章:

scala - 无法从Sqoop创建的Spark中的序列文件创建数据框

javascript - 当使用函数构造函数标识符而不是函数时,Visual Studio 代码不发出警告

visual-studio-code - 如何在 VSCode 中更改编辑器缩进的颜色

scala - 是否习惯将 lib_management 检查到 sbt 的源代码管理中?

scala - Scala(Guice)中的伴生对象和单例类有什么区别

scala - 流、 View 、迭代器

java - Scala 相当于 new HashSet(Collection)

python - 如何在不丢失 Python 扩展的情况下删除 Jupyter 扩展

sbt - 如何在sbt中获取 "re-run with -deprecation for details"?

scala - 玩! - 从 SBT 控制台与 webapp 交互