Skip to content

Commit 8f5e088

Browse files
authored
Merge pull request #11535 from lrytz/ij-bsp
Documentation for using IntelliJ and Metals
2 parents 04d367e + 05ffb1c commit 8f5e088

File tree

5 files changed

+137
-62
lines changed

5 files changed

+137
-62
lines changed

docs/docs/contributing/tools/ide.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
layout: doc-page
3+
title: Using an IDE
4+
---
5+
6+
You can use either Metals (VS Code, vim) or IntelliJ IDEA as described on the
7+
[IDE Support](../../usage/ide-support.md) page to work on the Scala 3 codebase. There are however
8+
a few additional considerations to take into account.
9+
10+
11+
## Bootstrapping Projects
12+
13+
The sbt build for dotty implements bootstrapping within the same build, so each component has
14+
two projects:
15+
16+
```
17+
sbt:scala3> projects
18+
...
19+
[info] scala3-compiler
20+
[info] scala3-compiler-bootstrapped
21+
...
22+
```
23+
24+
These duplicated projects can be confusing and cause issues in IDEs.
25+
26+
When using Metals, the `-bootstrapped` projects are not exported.
27+
28+
In IntelliJ IDEA, we recommend importing the dotty codebase through BSP as described on the
29+
[IDE Support page](../../usage/ide-support.md), then the `-bootstrapped` projects are not exported.
30+
31+
32+
## Scala Version warning in Metals
33+
34+
When using VS Code, Metals might show a warning that the Scala version (`3.0.0-[...]-NIGHTLY`)
35+
is not supported. The reason is that the dotty repository sometimes uses a nightly build as
36+
reference compiler. The IDE experience is going to be limited in this case (semantic features will
37+
only within single files).

docs/docs/usage/ide-support.md

Lines changed: 62 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,66 @@
11
---
22
layout: doc-page
3-
title: "IDE support for Dotty"
3+
title: "IDE support for Scala 3"
44
---
55

6-
Dotty comes built-in with the Dotty Language Server, an implementation of the
7-
[Language Server Protocol](https://github.com/Microsoft/language-server-protocol),
8-
which means that any editor that implements the LSP can be used as a Dotty IDE.
9-
Currently, the only IDE we officially support is
10-
[Visual Studio Code](https://code.visualstudio.com/).
11-
12-
Prerequisites
13-
============
14-
To use this in your own Scala project, you must first get it to compile with
15-
Dotty, please follow the instructions at https://github.com/scala/scala3-example-project
16-
17-
Usage
18-
=====
19-
1. Install [Visual Studio Code](https://code.visualstudio.com/).
20-
2. Make sure `code`, the binary for Visual Studio Code, is on your `$PATH`, this
21-
is the case if you can start the IDE by running `code` in a terminal. This
22-
is the default on all systems except Mac where you'll need to follow these
23-
instructions: https://code.visualstudio.com/docs/setup/mac#_command-line
24-
3. In your project, run:
25-
```shell
26-
sbt launchIDE
27-
```
28-
29-
Status
30-
======
31-
32-
## Fully supported features:
33-
- Typechecking as you type to show compiler errors/warnings
34-
- Type information on hover
35-
- Go to definition (in the current project)
36-
- Find all references
37-
- Documentation on hover
38-
- [Worksheet mode](worksheet-mode.md)
39-
40-
## Partially working features:
41-
- Completion
42-
- Renaming
43-
- Go to definition in external projects
44-
45-
## Unimplemented features:
46-
- Formatting code (requires integrating with scalafmt)
47-
- Quick fixes (probably by integrating with scalafix)
48-
49-
## Current limitations, to be fixed:
50-
- Projects should be compiled with sbt before starting the IDE, this is
51-
automatically done for you if you run `sbt launchIDE`.
52-
- Once the IDE is started, source files that are not opened in the IDE
53-
should not be modified in some other editor, the IDE won't pick up
54-
these changes.
55-
- Not all compiler errors/warnings are displayed, just those occurring
56-
during typechecking.
57-
58-
59-
Feedback
60-
========
61-
Please report issues on https://github.com/lampepfl/dotty/issues,
62-
you can also come chat with use on the
63-
[Dotty gitter channel](https://gitter.im/lampepfl/dotty)!
6+
IDE support for Scala 3 is available in IDEs based on [Scala Metals](https://scalameta.org/metals/)
7+
(e.g., Visual Studio Code, vim) and in [IntelliJ IDEA](https://www.jetbrains.com/idea/).
8+
9+
10+
## Using Visual Studio Code
11+
12+
To use Visual Studio Code on a Scala 3 project, ensure you have the
13+
[Metals](https://scalameta.org/metals/docs/editors/vscode.html) plugin installed. Then open the
14+
project directory in VS code and click the "Import build" button in notification.
15+
16+
17+
### Under the Hood
18+
19+
VS Code implements semantic features (such as completions, "go to definition")
20+
using the [Language Server Protocol (LSP)](https://github.com/Microsoft/language-server-protocol),
21+
so it needs a language server implementation. Metals is the implementation of LSP for Scala. It
22+
extracts semantic information from [semanticdb](https://scalameta.org/docs/semanticdb/guide.html),
23+
which is generated directly by the Scala 3 compiler.
24+
25+
You can read more about Scala 3 support in Metals in
26+
[this blog post](https://medium.com/virtuslab/introduction-to-metals-with-scala-3-79ebf3120a95).
27+
28+
To communicate with the build tool (e.g., to import the project, trigger builds, run tests),
29+
Metals uses the [Build Server Protocol (BSP)](https://build-server-protocol.github.io/). The
30+
default BSP implementation used by metals is [Bloop](https://scalacenter.github.io/bloop/), which
31+
supports Scala 3 projects. Alternatively,
32+
[sbt can be used as a BSP server](https://scalameta.org/metals/blog/2020/11/06/sbt-BSP-support.html)
33+
as it directly implements BSP since version 1.4.
34+
35+
36+
## Using IntelliJ IDEA
37+
38+
IntelliJ has its own implementation for semantic features, so it does not use Metals or the
39+
Language Server Protocol (LSP).
40+
41+
In order to import a project into IntelliJ there are two possibilities:
42+
- Use the built-in feature to import sbt builds
43+
- Use IntelliJ's support for the
44+
[Build Server Protocol (BSP)](https://www.jetbrains.com/help/idea/bsp-support.html)
45+
46+
47+
### Importing the sbt build
48+
49+
To use IntelliJ's sbt import, go to "File" - "Open..." and select your project's `build.sbt` file.
50+
51+
In this mode, IntelliJ starts sbt with a custom plugin to extract the project structure. After
52+
importing, IntelliJ no longer interacts with other sbt sessions. Building and running the project
53+
within the IDE is done by separate processes.
54+
55+
56+
### Importing the project using BSP
57+
58+
To import a project using BSP, go to "File" - "New" - "Project from Existing Sources" and select
59+
the project directory. In the upcoming dialog select "BSP" to import the project. You may be asked
60+
to choose between "sbt" and "sbt with Bloop", the recommended option is "sbt".
61+
62+
If the project import fails ("Problem executing BSP job"), navigate to your project in a terminal
63+
and just start `sbt`. Once sbt is running, open the "bsp" tab in IntelliJ click the "Reload" button.
64+
65+
When using IntelliJ's BSP mode, build and run commands from the IDE are executed through sbt, so
66+
they have the same effect as building or running the project through sbt in the terminal.

docs/sidebar.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ sidebar:
209209
url: docs/contributing/debugging.html
210210
- title: IDEs and Tools
211211
subsection:
212+
- title: IDEs
213+
url: docs/contributing/tools/ide.html
212214
- title: Mill
213215
url: docs/contributing/tools/mill.html
214216
- title: Scalafix

project/Build.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,8 @@ object Build {
283283
)
284284

285285
// Settings used when compiling dotty with a non-bootstrapped dotty
286-
lazy val commonBootstrappedSettings = commonDottySettings ++ Seq(
286+
lazy val commonBootstrappedSettings = commonDottySettings ++ NoBloopExport.settings ++ Seq(
287+
bspEnabled := false,
287288
unmanagedSourceDirectories in Compile += baseDirectory.value / "src-bootstrapped",
288289

289290
version := dottyVersion,
@@ -419,9 +420,10 @@ object Build {
419420
),
420421

421422
// For convenience, change the baseDirectory when running the compiler
422-
baseDirectory in (Compile, run) := baseDirectory.value / "..",
423+
(Compile / forkOptions) := (Compile / forkOptions).value.withWorkingDirectory((ThisBuild / baseDirectory).value),
424+
(Compile / run / forkOptions) := (Compile / run / forkOptions).value.withWorkingDirectory((ThisBuild / baseDirectory).value),
423425
// And when running the tests
424-
baseDirectory in Test := baseDirectory.value / "..",
426+
(Test / forkOptions) := (Test / forkOptions).value.withWorkingDirectory((ThisBuild / baseDirectory).value),
425427

426428
test in Test := {
427429
// Exclude VulpixMetaTests

project/NoBloopExport.scala

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import sbt._
2+
import Keys._
3+
4+
/* With <3 from scala-js */
5+
object NoBloopExport {
6+
private lazy val bloopGenerateKey: Option[TaskKey[Option[File]]] = {
7+
val optBloopKeysClass: Option[Class[_]] = try {
8+
Some(Class.forName("bloop.integrations.sbt.BloopKeys"))
9+
} catch {
10+
case _: ClassNotFoundException => None
11+
}
12+
13+
optBloopKeysClass.map { bloopKeysClass =>
14+
val bloopGenerateGetter = bloopKeysClass.getMethod("bloopGenerate")
15+
bloopGenerateGetter.invoke(null).asInstanceOf[TaskKey[Option[File]]]
16+
}
17+
}
18+
19+
/** Settings to prevent the project from being exported to IDEs. */
20+
lazy val settings: Seq[Setting[_]] = {
21+
bloopGenerateKey match {
22+
case None =>
23+
Nil
24+
case Some(key) =>
25+
Seq(
26+
key in Compile := None,
27+
key in Test := None,
28+
)
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)