Skip to content

Commit ec8370f

Browse files
committed
vscode-dotty: Fix hover functionality with old language server
The current unreleased version of vscode-dotty depends on a new version of vscode-languageclient which parses hover messages in a stricter way, and Dotty Language Server <= 0.9.x sends incorrect hover messages due to a bug in lsp4j. The end result is that when using the current vscode-dotty on a project with an old version of dotty, the hover shows "[object Object]" instead of showing the type and documentation. This commit works around this by manually parsing the hover message when the server version is too old.
1 parent eb61770 commit ec8370f

File tree

4 files changed

+68
-3
lines changed

4 files changed

+68
-3
lines changed

vscode-dotty/package-lock.json

Lines changed: 11 additions & 0 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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@
5858
],
5959
"dependencies": {
6060
"child-process-promise": "^2.2.1",
61+
"compare-versions": "^3.4.0",
6162
"vscode-languageclient": "^5.1.0",
6263
"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: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ 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
@@ -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' },
@@ -163,6 +167,8 @@ function run(serverOptions: ServerOptions) {
163167
}
164168

165169
const client = new LanguageClient("dotty", "Dotty", serverOptions, clientOptions)
170+
if (isOldServer)
171+
enableOldServerWorkaround(client)
166172

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

0 commit comments

Comments
 (0)