Skip to content

Commit 5e91f77

Browse files
authored
Merge pull request #5162 from dotty-staging/fix-vscode-compat
vscode-dotty: Fix hover functionality with old language server
2 parents 7336594 + ec8370f commit 5e91f77

File tree

4 files changed

+96
-32
lines changed

4 files changed

+96
-32
lines changed

vscode-dotty/package-lock.json

Lines changed: 34 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vscode-dotty/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@
5858
],
5959
"dependencies": {
6060
"child-process-promise": "^2.2.1",
61-
"vscode-languageclient": "^5.0.1",
62-
"vscode-languageserver": "^5.0.3"
61+
"compare-versions": "^3.4.0",
62+
"vscode-languageclient": "^5.1.0",
63+
"vscode-languageserver": "^5.1.0"
6364
},
6465
"devDependencies": {
66+
"@types/compare-versions": "^3.0.0",
6567
"@types/mocha": "^5.2.5",
6668
"@types/node": "^8.10.28",
6769
"typescript": "^2.9.2",

vscode-dotty/src/compat.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import * as vscode from 'vscode';
2+
3+
import { HoverRequest } from 'vscode-languageclient';
4+
import { MarkedString, LanguageClient, LanguageClientOptions, RevealOutputChannelOn,
5+
ServerOptions } from 'vscode-languageclient';
6+
7+
// Fix hover functionality when using this version of vscode-dotty with Dotty
8+
// Language Server 0.9 or earlier.
9+
// Without this, the displayed hover would be "[object Object]".
10+
export function enableOldServerWorkaround(client: LanguageClient): void {
11+
client.clientOptions.middleware = {
12+
provideHover: (document, position, token, next) => {
13+
// This code is adapted from HoverFeature#registerLanguageProvider in
14+
// https://github.com/Microsoft/vscode-languageserver-node/blob/master/client/src/client.ts
15+
return client
16+
.sendRequest(HoverRequest.type,
17+
client.code2ProtocolConverter.asTextDocumentPositionParams(document, position), token)
18+
.then(
19+
hover => {
20+
if (!hover) {
21+
return undefined
22+
}
23+
// The server is supposed to return string or { language: string,
24+
// value: string } or an array of those things, but instead it returns
25+
// an array of { value: string }. This used to work but with a recent
26+
// vscode-languageclient the user ends up seeing "[object Object]" as
27+
// the hover information.
28+
// We work around this by manually parsing the array of { value: string }
29+
// into a Hover.
30+
const contents = hover.contents as { value: string }[]
31+
let result: vscode.MarkdownString[] = []
32+
for (let element of contents) {
33+
let item = new vscode.MarkdownString()
34+
item.appendCodeblock(element.value, "scala")
35+
result.push(item)
36+
}
37+
return new vscode.Hover(result)
38+
},
39+
error => {
40+
client.logFailedRequest(HoverRequest.type, error)
41+
return Promise.resolve(null)
42+
}
43+
)
44+
}
45+
}
46+
}

vscode-dotty/src/extension.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@ import * as fs from 'fs';
44
import * as path from 'path';
55

66
import * as cpp from 'child-process-promise';
7+
import * as compareVersions from 'compare-versions';
78

89
import { ExtensionContext } from 'vscode';
910
import * as vscode from 'vscode';
1011
import { LanguageClient, LanguageClientOptions, RevealOutputChannelOn,
1112
ServerOptions } from 'vscode-languageclient';
13+
import { enableOldServerWorkaround } from './compat'
1214

1315
let extensionContext: ExtensionContext
1416
let outputChannel: vscode.OutputChannel
1517

1618
export function activate(context: ExtensionContext) {
1719
extensionContext = context
18-
outputChannel = vscode.window.createOutputChannel('Dotty Language Client');
20+
outputChannel = vscode.window.createOutputChannel("Dotty");
1921

2022
const sbtArtifact = "org.scala-sbt:sbt-launch:1.2.3"
2123
const buildSbtFile = `${vscode.workspace.rootPath}/build.sbt`
@@ -36,7 +38,7 @@ export function activate(context: ExtensionContext) {
3638
run({
3739
module: context.asAbsolutePath('out/src/passthrough-server.js'),
3840
args: [ port.toString() ]
39-
})
41+
}, false)
4042
})
4143

4244
} else {
@@ -72,11 +74,13 @@ function runLanguageServer(coursierPath: string, languageServerArtifactFile: str
7274
if (err) throw err
7375
else {
7476
const languageServerArtifact = data.toString().trim()
77+
const languageServerVersion = languageServerArtifact.split(":")[2]
78+
const isOldServer = compareVersions(languageServerVersion, "0.9.x") <= 0
7579
fetchWithCoursier(coursierPath, languageServerArtifact).then((languageServerClasspath) => {
7680
run({
7781
command: "java",
7882
args: ["-classpath", languageServerClasspath, "dotty.tools.languageserver.Main", "-stdio"]
79-
})
83+
}, isOldServer)
8084
})
8185
}
8286
})
@@ -149,7 +153,7 @@ function configureIDE(sbtClasspath: string, languageServerScalaVersion: string,
149153
})
150154
}
151155

152-
function run(serverOptions: ServerOptions) {
156+
function run(serverOptions: ServerOptions, isOldServer: boolean) {
153157
const clientOptions: LanguageClientOptions = {
154158
documentSelector: [
155159
{ language: 'scala', scheme: 'file', pattern: '**/*.scala' },
@@ -158,12 +162,13 @@ function run(serverOptions: ServerOptions) {
158162
synchronize: {
159163
configurationSection: 'dotty'
160164
},
165+
outputChannel: outputChannel,
161166
revealOutputChannelOn: RevealOutputChannelOn.Never
162167
}
163168

164-
outputChannel.dispose()
165-
166-
const client = new LanguageClient('dotty', 'Dotty Language Server', serverOptions, clientOptions);
169+
const client = new LanguageClient("dotty", "Dotty", serverOptions, clientOptions)
170+
if (isOldServer)
171+
enableOldServerWorkaround(client)
167172

168173
// Push the disposable to the context's subscriptions so that the
169174
// client can be deactivated on extension deactivation

0 commit comments

Comments
 (0)