Skip to content

Commit 920fb74

Browse files
committed
Let VSCode start the language server
We used to start the language server with `launchIDE`. However, this means that the language server is dependent of sbt being running and configured, which is not a good user experience for newcomers. For newcomers, we want the language server to start when they create a new Scala file in an empty directory, for instance, which means that no build is already configured. This commits takes us closer to this experience by using `load-plugin` to inject the Dotty plugin. This means that the Dotty plugin can now be loaded even if there's no build configured, which happens if the user opens VSCode in an empty directory, for instance.
1 parent f7d5fe4 commit 920fb74

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

vscode-dotty/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
],
2525
"main": "./out/src/extension",
2626
"activationEvents": [
27+
"onLanguage:scala",
2728
"workspaceContains:.dotty-ide.json"
2829
],
2930
"languages": [
@@ -50,7 +51,7 @@
5051
"vscode:prepublish": "npm install && ./node_modules/.bin/tsc -p ./",
5152
"compile": "./node_modules/.bin/tsc -p ./",
5253
"test": "node ./node_modules/vscode/bin/test",
53-
"postinstall": "node ./node_modules/vscode/bin/install && curl -L -o out/coursier https://github.com/coursier/coursier/raw/v1.0.0/coursier"
54+
"postinstall": "node ./node_modules/vscode/bin/install && curl -L -o out/coursier https://github.com/coursier/coursier/raw/v1.0.0/coursier && curl -L -o out/load-plugin.jar https://github.com/scalacenter/load-plugin/releases/download/v0.1.0/load-plugin_2.12-0.1.0.jar"
5455
},
5556
"extensionDependencies": [
5657
"daltonjorge.scala"

vscode-dotty/src/extension.ts

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,9 @@ export function activate(context: ExtensionContext) {
1717
outputChannel = vscode.window.createOutputChannel('Dotty Language Client');
1818

1919
const artifactFile = `${vscode.workspace.rootPath}/.dotty-ide-artifact`
20+
const defaultArtifact = "ch.epfl.lamp:dotty-language-server_0.8:0.8.0-bin-SNAPSHOT"
2021
fs.readFile(artifactFile, (err, data) => {
21-
if (err) {
22-
outputChannel.append(`Unable to parse ${artifactFile}`)
23-
throw err
24-
}
25-
const artifact = data.toString().trim()
22+
const artifact = err ? defaultArtifact : data.toString().trim()
2623

2724
if (process.env['DLS_DEV_MODE']) {
2825
const portFile = `${vscode.workspace.rootPath}/.dotty-ide-dev-port`
@@ -77,15 +74,51 @@ function fetchAndRun(artifact: string) {
7774
throw new Error(msg)
7875
}
7976

80-
run({
81-
command: "java",
82-
args: ["-classpath", classPath, "dotty.tools.languageserver.Main", "-stdio"]
77+
configureIDE().then((res) => {
78+
run({
79+
command: "java",
80+
args: ["-classpath", classPath, "dotty.tools.languageserver.Main", "-stdio"]
81+
})
8382
})
8483
})
8584
return coursierPromise
8685
})
8786
}
8887

88+
function configureIDE() {
89+
const coursierPath = path.join(extensionContext.extensionPath, './out/coursier');
90+
const loadPluginPath = path.join(extensionContext.extensionPath, './out/load-plugin.jar');
91+
92+
return vscode.window.withProgress({
93+
location: vscode.ProgressLocation.Window,
94+
title: 'Configuring IDE...'
95+
}, (progress) => {
96+
97+
const sbtPromise =
98+
cpp.spawn("java", [
99+
"-jar", coursierPath,
100+
"launch",
101+
"org.scala-sbt:sbt-launch:1.1.2", "--",
102+
"apply -cp " + loadPluginPath + " ch.epfl.scala.loadplugin.LoadPlugin",
103+
"set every scalaVersion := \"0.8.0-bin-SNAPSHOT\"",
104+
"load-plugin ch.epfl.lamp:sbt-dotty:0.2.0-SNAPSHOT dotty.tools.sbtplugin.DottyPlugin",
105+
"load-plugin ch.epfl.lamp:sbt-dotty:0.2.0-SNAPSHOT dotty.tools.sbtplugin.DottyIDEPlugin",
106+
"configureIDE"
107+
])
108+
const sbtProc = sbtPromise.childProcess
109+
110+
sbtProc.on('close', (code: number) => {
111+
if (code != 0) {
112+
let msg = "Configuring the IDE failed."
113+
outputChannel.append(msg)
114+
throw new Error(msg)
115+
}
116+
})
117+
118+
return sbtPromise;
119+
})
120+
}
121+
89122
function run(serverOptions: ServerOptions) {
90123
const clientOptions: LanguageClientOptions = {
91124
documentSelector: ['scala'],

0 commit comments

Comments
 (0)