【问题标题】:Using vs code, how to get scala format to work and format my code?使用 vs 代码,如何让 scala 格式正常工作并格式化我的代码?
【发布时间】:2020-07-18 11:18:42
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

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

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

    只需在VSC中从Command Palette执行Format Document命令,它应该根据.scalafmt.conf.格式化

    ~scalafmt;compile
    

    但是编译时的格式是 discouraged 根据文档

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

    【讨论】:

      【解决方案2】:

      在 VS Code 中,如果你进入 File -> Preferences - > Settings 并输入 editor.format,你会看到一堆选项。

      启用所有这些,金属应该会应用所有更改。

      【讨论】:

        猜你喜欢
        • 2011-03-29
        • 1970-01-01
        • 1970-01-01
        • 2021-08-23
        • 1970-01-01
        • 2018-09-28
        • 2020-02-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多