From f7b8ae1ee7793bd9b34ecea27037a01159cd5d7a Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Wed, 24 Jul 2019 17:15:12 +0300 Subject: Simplify the hints display --- editors/code/src/commands/inlay_hints.ts | 59 ++++---------------------------- editors/code/src/extension.ts | 1 - 2 files changed, 6 insertions(+), 54 deletions(-) diff --git a/editors/code/src/commands/inlay_hints.ts b/editors/code/src/commands/inlay_hints.ts index 2780e9326..cc6620d36 100644 --- a/editors/code/src/commands/inlay_hints.ts +++ b/editors/code/src/commands/inlay_hints.ts @@ -1,5 +1,5 @@ import * as vscode from 'vscode'; -import { DecorationOptions, Range, TextDocumentChangeEvent, TextDocumentContentChangeEvent, TextEditor } from 'vscode'; +import { Range, TextDocumentChangeEvent, TextDocumentContentChangeEvent, TextEditor } from 'vscode'; import { TextDocumentIdentifier } from 'vscode-languageclient'; import { Server } from '../server'; @@ -20,7 +20,6 @@ const typeHintDecorationType = vscode.window.createTextEditorDecorationType({ }); export class HintsUpdater { - private currentDecorations = new Map(); private displayHints = true; public async loadHints(editor: vscode.TextEditor | undefined): Promise { @@ -29,16 +28,9 @@ export class HintsUpdater { } } - public dropHints(document: vscode.TextDocument) { - if (this.displayHints) { - this.currentDecorations.delete(document.uri.toString()); - } - } - public async toggleHintsDisplay(displayHints: boolean): Promise { if (this.displayHints !== displayHints) { this.displayHints = displayHints; - this.currentDecorations.clear(); if (displayHints) { return this.updateHints(); @@ -64,26 +56,12 @@ export class HintsUpdater { return; } - const documentUri = document.uri.toString(); - const documentDecorators = this.currentDecorations.get(documentUri) || []; - - if (documentDecorators.length > 0) { - // FIXME a dbg! in the handlers.rs of the server causes - // an endless storm of events with `cause.contentChanges` with the dbg messages, why? - const changesFromFile = cause !== undefined ? cause.contentChanges.filter(changeEvent => this.isEventInFile(document.lineCount, changeEvent)) : []; - if (changesFromFile.length === 0) { - return; - } - - const firstShiftedLine = this.getFirstShiftedLine(changesFromFile); - if (firstShiftedLine !== null) { - const unchangedDecorations = documentDecorators.filter(decoration => decoration.range.start.line < firstShiftedLine); - if (unchangedDecorations.length !== documentDecorators.length) { - await editor.setDecorations(typeHintDecorationType, unchangedDecorations); - } - } + // If the dbg! macro is used in the lsp-server, an endless stream of events with `cause.contentChanges` with the dbg messages. + // Should not be a real situation, but better to filter such things out. + if (cause !== undefined && cause.contentChanges.filter(changeEvent => this.isEventInFile(document.lineCount, changeEvent)).length === 0) { + return; } - return await this.updateDecorationsFromServer(documentUri, editor); + return await this.updateDecorationsFromServer(document.uri.toString(), editor); } private isEventInFile(documentLineCount: number, event: TextDocumentContentChangeEvent): boolean { @@ -95,30 +73,6 @@ export class HintsUpdater { } } - private getFirstShiftedLine(changeEvents: TextDocumentContentChangeEvent[]): number | null { - let topmostUnshiftedLine: number | null = null; - - changeEvents - .filter(event => this.isShiftingChange(event)) - .forEach(event => { - const shiftedLineNumber = event.range.start.line; - if (topmostUnshiftedLine === null || topmostUnshiftedLine > shiftedLineNumber) { - topmostUnshiftedLine = shiftedLineNumber; - } - }); - - return topmostUnshiftedLine; - } - - private isShiftingChange(event: TextDocumentContentChangeEvent) { - const eventText = event.text; - if (eventText.length === 0) { - return !event.range.isSingleLine; - } else { - return eventText.indexOf('\n') >= 0 || eventText.indexOf('\r') >= 0; - } - } - private async updateDecorationsFromServer(documentUri: string, editor: TextEditor): Promise { const newHints = await this.queryHints(documentUri) || []; const newDecorations = newHints.map(hint => ( @@ -127,7 +81,6 @@ export class HintsUpdater { renderOptions: { after: { contentText: `: ${hint.label}` } }, } )); - this.currentDecorations.set(documentUri, newDecorations); return editor.setDecorations(typeHintDecorationType, newDecorations); } diff --git a/editors/code/src/extension.ts b/editors/code/src/extension.ts index a0b897385..3965b881a 100644 --- a/editors/code/src/extension.ts +++ b/editors/code/src/extension.ts @@ -154,7 +154,6 @@ export function activate(context: vscode.ExtensionContext) { hintsUpdater.loadHints(vscode.window.activeTextEditor).then(() => { vscode.window.onDidChangeActiveTextEditor(editor => hintsUpdater.loadHints(editor)); vscode.workspace.onDidChangeTextDocument(e => hintsUpdater.updateHints(e)); - vscode.workspace.onDidCloseTextDocument(document => hintsUpdater.dropHints(document)); vscode.workspace.onDidChangeConfiguration(_ => hintsUpdater.toggleHintsDisplay(Server.config.displayInlayHints)); }); } -- cgit v1.2.3