Skip to content

Commit

Permalink
vscode-dotty: Fix hover functionality with old language server
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
smarter committed Sep 26, 2018
1 parent eb61770 commit ec8370f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 3 deletions.
11 changes: 11 additions & 0 deletions vscode-dotty/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions vscode-dotty/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@
],
"dependencies": {
"child-process-promise": "^2.2.1",
"compare-versions": "^3.4.0",
"vscode-languageclient": "^5.1.0",
"vscode-languageserver": "^5.1.0"
},
"devDependencies": {
"@types/compare-versions": "^3.0.0",
"@types/mocha": "^5.2.5",
"@types/node": "^8.10.28",
"typescript": "^2.9.2",
Expand Down
46 changes: 46 additions & 0 deletions vscode-dotty/src/compat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import * as vscode from 'vscode';

import { HoverRequest } from 'vscode-languageclient';
import { MarkedString, LanguageClient, LanguageClientOptions, RevealOutputChannelOn,
ServerOptions } from 'vscode-languageclient';

// Fix hover functionality when using this version of vscode-dotty with Dotty
// Language Server 0.9 or earlier.
// Without this, the displayed hover would be "[object Object]".
export function enableOldServerWorkaround(client: LanguageClient): void {
client.clientOptions.middleware = {
provideHover: (document, position, token, next) => {
// This code is adapted from HoverFeature#registerLanguageProvider in
// https://github.com/Microsoft/vscode-languageserver-node/blob/master/client/src/client.ts
return client
.sendRequest(HoverRequest.type,
client.code2ProtocolConverter.asTextDocumentPositionParams(document, position), token)
.then(
hover => {
if (!hover) {
return undefined
}
// The server is supposed to return string or { language: string,
// value: string } or an array of those things, but instead it returns
// an array of { value: string }. This used to work but with a recent
// vscode-languageclient the user ends up seeing "[object Object]" as
// the hover information.
// We work around this by manually parsing the array of { value: string }
// into a Hover.
const contents = hover.contents as { value: string }[]
let result: vscode.MarkdownString[] = []
for (let element of contents) {
let item = new vscode.MarkdownString()
item.appendCodeblock(element.value, "scala")
result.push(item)
}
return new vscode.Hover(result)
},
error => {
client.logFailedRequest(HoverRequest.type, error)
return Promise.resolve(null)
}
)
}
}
}
12 changes: 9 additions & 3 deletions vscode-dotty/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import * as fs from 'fs';
import * as path from 'path';

import * as cpp from 'child-process-promise';
import * as compareVersions from 'compare-versions';

import { ExtensionContext } from 'vscode';
import * as vscode from 'vscode';
import { LanguageClient, LanguageClientOptions, RevealOutputChannelOn,
ServerOptions } from 'vscode-languageclient';
import { enableOldServerWorkaround } from './compat'

let extensionContext: ExtensionContext
let outputChannel: vscode.OutputChannel
Expand Down Expand Up @@ -36,7 +38,7 @@ export function activate(context: ExtensionContext) {
run({
module: context.asAbsolutePath('out/src/passthrough-server.js'),
args: [ port.toString() ]
})
}, false)
})

} else {
Expand Down Expand Up @@ -72,11 +74,13 @@ function runLanguageServer(coursierPath: string, languageServerArtifactFile: str
if (err) throw err
else {
const languageServerArtifact = data.toString().trim()
const languageServerVersion = languageServerArtifact.split(":")[2]
const isOldServer = compareVersions(languageServerVersion, "0.9.x") <= 0
fetchWithCoursier(coursierPath, languageServerArtifact).then((languageServerClasspath) => {
run({
command: "java",
args: ["-classpath", languageServerClasspath, "dotty.tools.languageserver.Main", "-stdio"]
})
}, isOldServer)
})
}
})
Expand Down Expand Up @@ -149,7 +153,7 @@ function configureIDE(sbtClasspath: string, languageServerScalaVersion: string,
})
}

function run(serverOptions: ServerOptions) {
function run(serverOptions: ServerOptions, isOldServer: boolean) {
const clientOptions: LanguageClientOptions = {
documentSelector: [
{ language: 'scala', scheme: 'file', pattern: '**/*.scala' },
Expand All @@ -163,6 +167,8 @@ function run(serverOptions: ServerOptions) {
}

const client = new LanguageClient("dotty", "Dotty", serverOptions, clientOptions)
if (isOldServer)
enableOldServerWorkaround(client)

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

0 comments on commit ec8370f

Please sign in to comment.