Skip to content

vscode-dotty: Fix hover functionality with old language server #5162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 34 additions & 23 deletions vscode-dotty/package-lock.json

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

6 changes: 4 additions & 2 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",
"vscode-languageclient": "^5.0.1",
"vscode-languageserver": "^5.0.3"
"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)
}
)
}
}
}
19 changes: 12 additions & 7 deletions vscode-dotty/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ 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

export function activate(context: ExtensionContext) {
extensionContext = context
outputChannel = vscode.window.createOutputChannel('Dotty Language Client');
outputChannel = vscode.window.createOutputChannel("Dotty");

const sbtArtifact = "org.scala-sbt:sbt-launch:1.2.3"
const buildSbtFile = `${vscode.workspace.rootPath}/build.sbt`
Expand All @@ -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 @@ -158,12 +162,13 @@ function run(serverOptions: ServerOptions) {
synchronize: {
configurationSection: 'dotty'
},
outputChannel: outputChannel,
revealOutputChannelOn: RevealOutputChannelOn.Never
}

outputChannel.dispose()

const client = new LanguageClient('dotty', 'Dotty Language Server', serverOptions, clientOptions);
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