diff options
author | Kirill Bulatov <[email protected]> | 2019-07-24 15:15:12 +0100 |
---|---|---|
committer | Kirill Bulatov <[email protected]> | 2019-07-25 13:17:28 +0100 |
commit | f7b8ae1ee7793bd9b34ecea27037a01159cd5d7a (patch) | |
tree | 089f1e5dafc83b9fa63391d06bc274acef297361 /editors | |
parent | 169e69d217600062f6299f7f9521f3f2776d0333 (diff) |
Simplify the hints display
Diffstat (limited to 'editors')
-rw-r--r-- | editors/code/src/commands/inlay_hints.ts | 59 | ||||
-rw-r--r-- | 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 @@ | |||
1 | import * as vscode from 'vscode'; | 1 | import * as vscode from 'vscode'; |
2 | import { DecorationOptions, Range, TextDocumentChangeEvent, TextDocumentContentChangeEvent, TextEditor } from 'vscode'; | 2 | import { Range, TextDocumentChangeEvent, TextDocumentContentChangeEvent, TextEditor } from 'vscode'; |
3 | import { TextDocumentIdentifier } from 'vscode-languageclient'; | 3 | import { TextDocumentIdentifier } from 'vscode-languageclient'; |
4 | import { Server } from '../server'; | 4 | import { Server } from '../server'; |
5 | 5 | ||
@@ -20,7 +20,6 @@ const typeHintDecorationType = vscode.window.createTextEditorDecorationType({ | |||
20 | }); | 20 | }); |
21 | 21 | ||
22 | export class HintsUpdater { | 22 | export class HintsUpdater { |
23 | private currentDecorations = new Map<string, DecorationOptions[]>(); | ||
24 | private displayHints = true; | 23 | private displayHints = true; |
25 | 24 | ||
26 | public async loadHints(editor: vscode.TextEditor | undefined): Promise<void> { | 25 | public async loadHints(editor: vscode.TextEditor | undefined): Promise<void> { |
@@ -29,16 +28,9 @@ export class HintsUpdater { | |||
29 | } | 28 | } |
30 | } | 29 | } |
31 | 30 | ||
32 | public dropHints(document: vscode.TextDocument) { | ||
33 | if (this.displayHints) { | ||
34 | this.currentDecorations.delete(document.uri.toString()); | ||
35 | } | ||
36 | } | ||
37 | |||
38 | public async toggleHintsDisplay(displayHints: boolean): Promise<void> { | 31 | public async toggleHintsDisplay(displayHints: boolean): Promise<void> { |
39 | if (this.displayHints !== displayHints) { | 32 | if (this.displayHints !== displayHints) { |
40 | this.displayHints = displayHints; | 33 | this.displayHints = displayHints; |
41 | this.currentDecorations.clear(); | ||
42 | 34 | ||
43 | if (displayHints) { | 35 | if (displayHints) { |
44 | return this.updateHints(); | 36 | return this.updateHints(); |
@@ -64,26 +56,12 @@ export class HintsUpdater { | |||
64 | return; | 56 | return; |
65 | } | 57 | } |
66 | 58 | ||
67 | const documentUri = document.uri.toString(); | 59 | // If the dbg! macro is used in the lsp-server, an endless stream of events with `cause.contentChanges` with the dbg messages. |
68 | const documentDecorators = this.currentDecorations.get(documentUri) || []; | 60 | // Should not be a real situation, but better to filter such things out. |
69 | 61 | if (cause !== undefined && cause.contentChanges.filter(changeEvent => this.isEventInFile(document.lineCount, changeEvent)).length === 0) { | |
70 | if (documentDecorators.length > 0) { | 62 | return; |
71 | // FIXME a dbg! in the handlers.rs of the server causes | ||
72 | // an endless storm of events with `cause.contentChanges` with the dbg messages, why? | ||
73 | const changesFromFile = cause !== undefined ? cause.contentChanges.filter(changeEvent => this.isEventInFile(document.lineCount, changeEvent)) : []; | ||
74 | if (changesFromFile.length === 0) { | ||
75 | return; | ||
76 | } | ||
77 | |||
78 | const firstShiftedLine = this.getFirstShiftedLine(changesFromFile); | ||
79 | if (firstShiftedLine !== null) { | ||
80 | const unchangedDecorations = documentDecorators.filter(decoration => decoration.range.start.line < firstShiftedLine); | ||
81 | if (unchangedDecorations.length !== documentDecorators.length) { | ||
82 | await editor.setDecorations(typeHintDecorationType, unchangedDecorations); | ||
83 | } | ||
84 | } | ||
85 | } | 63 | } |
86 | return await this.updateDecorationsFromServer(documentUri, editor); | 64 | return await this.updateDecorationsFromServer(document.uri.toString(), editor); |
87 | } | 65 | } |
88 | 66 | ||
89 | private isEventInFile(documentLineCount: number, event: TextDocumentContentChangeEvent): boolean { | 67 | private isEventInFile(documentLineCount: number, event: TextDocumentContentChangeEvent): boolean { |
@@ -95,30 +73,6 @@ export class HintsUpdater { | |||
95 | } | 73 | } |
96 | } | 74 | } |
97 | 75 | ||
98 | private getFirstShiftedLine(changeEvents: TextDocumentContentChangeEvent[]): number | null { | ||
99 | let topmostUnshiftedLine: number | null = null; | ||
100 | |||
101 | changeEvents | ||
102 | .filter(event => this.isShiftingChange(event)) | ||
103 | .forEach(event => { | ||
104 | const shiftedLineNumber = event.range.start.line; | ||
105 | if (topmostUnshiftedLine === null || topmostUnshiftedLine > shiftedLineNumber) { | ||
106 | topmostUnshiftedLine = shiftedLineNumber; | ||
107 | } | ||
108 | }); | ||
109 | |||
110 | return topmostUnshiftedLine; | ||
111 | } | ||
112 | |||
113 | private isShiftingChange(event: TextDocumentContentChangeEvent) { | ||
114 | const eventText = event.text; | ||
115 | if (eventText.length === 0) { | ||
116 | return !event.range.isSingleLine; | ||
117 | } else { | ||
118 | return eventText.indexOf('\n') >= 0 || eventText.indexOf('\r') >= 0; | ||
119 | } | ||
120 | } | ||
121 | |||
122 | private async updateDecorationsFromServer(documentUri: string, editor: TextEditor): Promise<void> { | 76 | private async updateDecorationsFromServer(documentUri: string, editor: TextEditor): Promise<void> { |
123 | const newHints = await this.queryHints(documentUri) || []; | 77 | const newHints = await this.queryHints(documentUri) || []; |
124 | const newDecorations = newHints.map(hint => ( | 78 | const newDecorations = newHints.map(hint => ( |
@@ -127,7 +81,6 @@ export class HintsUpdater { | |||
127 | renderOptions: { after: { contentText: `: ${hint.label}` } }, | 81 | renderOptions: { after: { contentText: `: ${hint.label}` } }, |
128 | } | 82 | } |
129 | )); | 83 | )); |
130 | this.currentDecorations.set(documentUri, newDecorations); | ||
131 | return editor.setDecorations(typeHintDecorationType, newDecorations); | 84 | return editor.setDecorations(typeHintDecorationType, newDecorations); |
132 | } | 85 | } |
133 | 86 | ||
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) { | |||
154 | hintsUpdater.loadHints(vscode.window.activeTextEditor).then(() => { | 154 | hintsUpdater.loadHints(vscode.window.activeTextEditor).then(() => { |
155 | vscode.window.onDidChangeActiveTextEditor(editor => hintsUpdater.loadHints(editor)); | 155 | vscode.window.onDidChangeActiveTextEditor(editor => hintsUpdater.loadHints(editor)); |
156 | vscode.workspace.onDidChangeTextDocument(e => hintsUpdater.updateHints(e)); | 156 | vscode.workspace.onDidChangeTextDocument(e => hintsUpdater.updateHints(e)); |
157 | vscode.workspace.onDidCloseTextDocument(document => hintsUpdater.dropHints(document)); | ||
158 | vscode.workspace.onDidChangeConfiguration(_ => hintsUpdater.toggleHintsDisplay(Server.config.displayInlayHints)); | 157 | vscode.workspace.onDidChangeConfiguration(_ => hintsUpdater.toggleHintsDisplay(Server.config.displayInlayHints)); |
159 | }); | 158 | }); |
160 | } | 159 | } |