forked from scala/scala3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
4 changed files
with
68 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters