理解 Scala 开发工具

Making sense of Scala development tools

生态系统中有无数的开发工具和术语,例如语言服务器、构建服务器、Metals、BSP、LSP、Bloop、Zinc、Coursier、增量编译器、演示编译器等

我想知道是否有人可以证明它们是如何结合在一起的,并简要解释一下关系和差异。具体来说,我希望得到一个图表,并按照 的方式进行回答。例如,这是我的尝试

(Concept)                       (Example implementation)
--------------------------------------------------------------------
IDE                             Visual Studio Code
 |                               |
Scala IDE plugin                Metals Visual Studio Extension
 |                               |
Language Server Protocol        Microsoft LSP
 |                               |
Scala language server           Metals
 |                               |
Build Server Protocol           BSP from JetBrains and Scala Center
 |                               |
Scala build server              Bloop
 |                               |
Build tool                      sbt
 |                               |
Dependency resolution           Coursier
 |                               |
Incremental compiler            Zinc
 |                               |
Presentation compiler           parser and typer phases of scalac
 |                               |
Bytecode generation             remaining compiler phases

IDE 像 Intellij 或(曾经)Scala IDE 用于 "smart" 开发,编辑器会告诉您代码是否正确,建议修复,自动生成一些代码,提供围绕代码的导航,重构——换句话说,许多功能扩展到简单的文本编辑之外,可能只有语法高亮显示。

Intellij 有一个 Scala 扩展,它利用了他们自己的 Scala 编译器的重新实现 - 他们需要它来更好地进行部分编译和智能感知工作,即使部分代码被破坏。从其他构建工具(例如 sbt 或 bloop)导入构建定义,从那时起 Intellij 不会回复任何外部内容(除非你使用像 "build with sbt" 这样的选项)。

Scala IDE 依靠 Scala 表示编译器实现智能感知。正如您在 scala-ide.org:

上看到的那样

The Scala IDE for Eclipse uses the Scala Presentation Compiler, a faster asynchronous version of the Scala Compiler. The presentation compiler only runs the phases up until and including the typer phase, that is, the first 4 of the 27 scala compilation phases. The IDE uses the presentation compiler to provide semantic features such as live error markers, inferred type hovers, and semantic highlighting. This document describes the key classes you need to know in order to understand how the Scala IDE uses the presentation compiler and provides some examples of interactions between the IDE and the presentation compiler.

其他所有 editor/IDE 都旨在使用语言服务器协议 - LSP 是 Microsoft 的发明,目的是在不同的编辑器中标准化一种支持语言的方式(尽管他们发明它是为了 VS Code),这将允许他们提供 IDE 功能。金属(来自 ScalaMeta Language Server) 是 Scala 的 LSP 实现。如您所见 here:

Code completions, type at point and parameter hints are implemented using the Scala presentation compiler, which is maintained by the Scala compiler team at Lightbend.

您可以使用 Scala Metals 扩展将其添加到 VS Code。

sbt、gradle、mill、fury、cbt 等是构建工具,它们使用类似 ivy2 或 coursier 的东西来解析和下载依赖项,然后使用 Zinc 增量编译器以提供能力(重新) 逐步构建普通编译器。构建工具可以 运行 测试、生成工件并将它们部署到存储库。

bloop 解决了以下问题:如果 JVM 很热,编译速度很快,而每次您终止构建时 JVM 都会变冷 tool/IDE。出于这个原因,您使用 nailgun 来保持一些 JVM 温暖,运行在后台执行构建任务。 bloop 本身不能生成配置,一般来说,它应该由其他构建工具生成,以加快开发过程中的编译速度。用于在后台与 bloop 服务器 运行ning 通信的协议是构建服务器协议 (bsp)。

Coursier 虽然主要用于依赖项解析,但也可用于 install scala programs。您可以安装的一些值得注意的程序是:

  • scalafmt - scala 格式化程序
  • ammonite - scala 的替代 REPL,它提供了很多不错的功能
  • scalafix - 用于提供自动代码迁移的代码重写工具

我放弃了在 table 中描述事物,因为它最好在图表上显示,但由于 SO 不支持视觉效果,我只求助于纯文本。