aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/commands
diff options
context:
space:
mode:
authorKirill Bulatov <[email protected]>2019-07-23 14:38:21 +0100
committerKirill Bulatov <[email protected]>2019-07-25 13:17:28 +0100
commit169e69d217600062f6299f7f9521f3f2776d0333 (patch)
treec7cb0782a1525a7b1355eac2dde80fc82492e59f /editors/code/src/commands
parent1d68e6171e68b9c95aec7a2e53cbf03ae8a672fe (diff)
Show type decorators
Diffstat (limited to 'editors/code/src/commands')
-rw-r--r--editors/code/src/commands/index.ts4
-rw-r--r--editors/code/src/commands/inlay_hints.ts142
2 files changed, 145 insertions, 1 deletions
diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts
index 194658497..d17f702e8 100644
--- a/editors/code/src/commands/index.ts
+++ b/editors/code/src/commands/index.ts
@@ -6,6 +6,7 @@ import * as onEnter from './on_enter';
6import * as parentModule from './parent_module'; 6import * as parentModule from './parent_module';
7import * as runnables from './runnables'; 7import * as runnables from './runnables';
8import * as syntaxTree from './syntaxTree'; 8import * as syntaxTree from './syntaxTree';
9import * as inlayHints from './inlay_hints';
9 10
10export { 11export {
11 analyzerStatus, 12 analyzerStatus,
@@ -15,5 +16,6 @@ export {
15 parentModule, 16 parentModule,
16 runnables, 17 runnables,
17 syntaxTree, 18 syntaxTree,
18 onEnter 19 onEnter,
20 inlayHints,
19}; 21};
diff --git a/editors/code/src/commands/inlay_hints.ts b/editors/code/src/commands/inlay_hints.ts
new file mode 100644
index 000000000..2780e9326
--- /dev/null
+++ b/editors/code/src/commands/inlay_hints.ts
@@ -0,0 +1,142 @@
1import * as vscode from 'vscode';
2import { DecorationOptions, Range, TextDocumentChangeEvent, TextDocumentContentChangeEvent, TextEditor } from 'vscode';
3import { TextDocumentIdentifier } from 'vscode-languageclient';
4import { Server } from '../server';
5
6interface InlayHintsParams {
7 textDocument: TextDocumentIdentifier;
8}
9
10interface InlayHint {
11 range: Range,
12 kind: string,
13 label: string,
14}
15
16const typeHintDecorationType = vscode.window.createTextEditorDecorationType({
17 after: {
18 color: new vscode.ThemeColor('ralsp.inlayHint'),
19 },
20});
21
22export class HintsUpdater {
23 private currentDecorations = new Map<string, DecorationOptions[]>();
24 private displayHints = true;
25
26 public async loadHints(editor: vscode.TextEditor | undefined): Promise<void> {
27 if (this.displayHints && editor !== undefined) {
28 await this.updateDecorationsFromServer(editor.document.uri.toString(), editor);
29 }
30 }
31
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> {
39 if (this.displayHints !== displayHints) {
40 this.displayHints = displayHints;
41 this.currentDecorations.clear();
42
43 if (displayHints) {
44 return this.updateHints();
45 } else {
46 const editor = vscode.window.activeTextEditor;
47 if (editor != null) {
48 return editor.setDecorations(typeHintDecorationType, [])
49 }
50 }
51 }
52 }
53
54 public async updateHints(cause?: TextDocumentChangeEvent): Promise<void> {
55 if (!this.displayHints) {
56 return;
57 }
58 const editor = vscode.window.activeTextEditor;
59 if (editor == null) {
60 return;
61 }
62 const document = cause == null ? editor.document : cause.document;
63 if (document.languageId !== 'rust') {
64 return;
65 }
66
67 const documentUri = document.uri.toString();
68 const documentDecorators = this.currentDecorations.get(documentUri) || [];
69
70 if (documentDecorators.length > 0) {
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 }
86 return await this.updateDecorationsFromServer(documentUri, editor);
87 }
88
89 private isEventInFile(documentLineCount: number, event: TextDocumentContentChangeEvent): boolean {
90 const eventText = event.text;
91 if (eventText.length === 0) {
92 return event.range.start.line <= documentLineCount || event.range.end.line <= documentLineCount;
93 } else {
94 return event.range.start.line <= documentLineCount && event.range.end.line <= documentLineCount;
95 }
96 }
97
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> {
123 const newHints = await this.queryHints(documentUri) || [];
124 const newDecorations = newHints.map(hint => (
125 {
126 range: hint.range,
127 renderOptions: { after: { contentText: `: ${hint.label}` } },
128 }
129 ));
130 this.currentDecorations.set(documentUri, newDecorations);
131 return editor.setDecorations(typeHintDecorationType, newDecorations);
132 }
133
134 private async queryHints(documentUri: string): Promise<InlayHint[] | null> {
135 const request: InlayHintsParams = { textDocument: { uri: documentUri } };
136 const client = Server.client;
137 return client.onReady().then(() => client.sendRequest<InlayHint[] | null>(
138 'rust-analyzer/inlayHints',
139 request
140 ));
141 }
142}