6470: Restore semantic token flickering workaround removed in #5697 r=kjeremy a=charlespierce

Closes #6452 

Info
-----
* As discussed in #6452, the `Error('busy')` workaround for semantic token flickering was removed because the underlying issue was believed to be fixed in VS Code.
* It turns out that the fix isn't yet complete, so this caused flickering of the semantic highlighting when making rapid edits (e.g. typing quickly).
* This PR restores that workaround and makes it slightly more robust, covering all areas of semantic token middleware.

Changes
-----
* Added middleware functions for `provideDocumentSemanticTokens`, `provideDocumentSemanticTokensEdits`, and `provideDocumentRangeSemanticTokens` to match the 3 possible middleware hooks defined in https://github.com/microsoft/vscode-languageserver-node/blob/master/client/src/common/semanticTokens.ts#L33
* Each intercepts a `null` or `undefined` return and throws an error with the message `busy` instead, which prevents the tokens from being removed and re-added (causing the flickering behavior)

Tested
-----
* Tested locally that the flickering behavior is gone.
* There don't appear to be any significant tests of the VS Code plugin side of things, other than that it loads. Is there somewhere I can / should add tests to cover this behavior?

Co-authored-by: Charles Pierce <cpierce.grad@gmail.com>
This commit is contained in:
bors[bot] 2020-11-05 13:09:22 +00:00 committed by GitHub
commit 7709b6a2d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,6 +2,7 @@ import * as lc from 'vscode-languageclient/node';
import * as vscode from 'vscode';
import * as ra from '../src/lsp_ext';
import * as Is from 'vscode-languageclient/lib/common/utils/is';
import { DocumentSemanticsTokensSignature, DocumentSemanticsTokensEditsSignature, DocumentRangeSemanticTokensSignature } from 'vscode-languageclient/lib/common/semanticTokens';
import { assert } from './util';
function renderCommand(cmd: ra.CommandLink) {
@ -18,6 +19,13 @@ function renderHoverActions(actions: ra.CommandLinkGroup[]): vscode.MarkdownStri
return result;
}
// Workaround for https://github.com/microsoft/vscode-languageserver-node/issues/576
async function semanticHighlightingWorkaround<R, F extends (...args: any[]) => vscode.ProviderResult<R>>(next: F, ...args: Parameters<F>): Promise<R> {
const res = await next(...args);
if (res == null) throw new Error('busy');
return res;
}
export function createClient(serverPath: string, cwd: string): lc.LanguageClient {
// '.' Is the fallback if no folder is open
// TODO?: Workspace folders support Uri's (eg: file://test.txt).
@ -41,6 +49,15 @@ export function createClient(serverPath: string, cwd: string): lc.LanguageClient
diagnosticCollectionName: "rustc",
traceOutputChannel,
middleware: {
provideDocumentSemanticTokens(document: vscode.TextDocument, token: vscode.CancellationToken, next: DocumentSemanticsTokensSignature): vscode.ProviderResult<vscode.SemanticTokens> {
return semanticHighlightingWorkaround(next, document, token);
},
provideDocumentSemanticTokensEdits(document: vscode.TextDocument, previousResultId: string, token: vscode.CancellationToken, next: DocumentSemanticsTokensEditsSignature): vscode.ProviderResult<vscode.SemanticTokensEdits | vscode.SemanticTokens> {
return semanticHighlightingWorkaround(next, document, previousResultId, token);
},
provideDocumentRangeSemanticTokens(document: vscode.TextDocument, range: vscode.Range, token: vscode.CancellationToken, next: DocumentRangeSemanticTokensSignature): vscode.ProviderResult<vscode.SemanticTokens> {
return semanticHighlightingWorkaround(next, document, range, token);
},
async provideHover(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken, _next: lc.ProvideHoverSignature) {
return client.sendRequest(lc.HoverRequest.type, client.code2ProtocolConverter.asTextDocumentPositionParams(document, position), token).then(
(result) => {