aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/commands
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-12-30 19:10:40 +0000
committerAleksey Kladov <[email protected]>2019-12-30 19:10:40 +0000
commit7b199f6a4b7a947d1dad6a74b2b88758497d4efa (patch)
tree8fbc8dca5fc5171b179afe1c47f841895cb5b5c6 /editors/code/src/commands
parent3d008a78d0ab1d43629326d58d4b2a157303dd00 (diff)
Hints are not commands
Diffstat (limited to 'editors/code/src/commands')
-rw-r--r--editors/code/src/commands/index.ts2
-rw-r--r--editors/code/src/commands/inlay_hints.ts113
2 files changed, 0 insertions, 115 deletions
diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts
index 4a2e8e4db..89af4be90 100644
--- a/editors/code/src/commands/index.ts
+++ b/editors/code/src/commands/index.ts
@@ -10,7 +10,6 @@ import { onEnter } from './on_enter';
10import { parentModule } from './parent_module'; 10import { parentModule } from './parent_module';
11import { syntaxTree } from './syntax_tree'; 11import { syntaxTree } from './syntax_tree';
12import { expandMacro } from './expand_macro'; 12import { expandMacro } from './expand_macro';
13import * as inlayHints from './inlay_hints';
14import { run, runSingle } from './runnables'; 13import { run, runSingle } from './runnables';
15 14
16function collectGarbage(ctx: Ctx): Cmd { 15function collectGarbage(ctx: Ctx): Cmd {
@@ -38,7 +37,6 @@ export {
38 parentModule, 37 parentModule,
39 syntaxTree, 38 syntaxTree,
40 onEnter, 39 onEnter,
41 inlayHints,
42 collectGarbage, 40 collectGarbage,
43 run, 41 run,
44 runSingle, 42 runSingle,
diff --git a/editors/code/src/commands/inlay_hints.ts b/editors/code/src/commands/inlay_hints.ts
deleted file mode 100644
index 6d00152f8..000000000
--- a/editors/code/src/commands/inlay_hints.ts
+++ /dev/null
@@ -1,113 +0,0 @@
1import * as vscode from 'vscode';
2import * as lc from 'vscode-languageclient';
3import { Server } from '../server';
4
5interface InlayHintsParams {
6 textDocument: lc.TextDocumentIdentifier;
7}
8
9interface InlayHint {
10 range: vscode.Range;
11 kind: string;
12 label: string;
13}
14
15const typeHintDecorationType = vscode.window.createTextEditorDecorationType({
16 after: {
17 color: new vscode.ThemeColor('ralsp.inlayHint'),
18 },
19});
20
21export class HintsUpdater {
22 private displayHints = true;
23
24 public async toggleHintsDisplay(displayHints: boolean): Promise<void> {
25 if (this.displayHints !== displayHints) {
26 this.displayHints = displayHints;
27 return this.refreshVisibleEditorsHints(
28 displayHints ? undefined : [],
29 );
30 }
31 }
32
33 public async refreshHintsForVisibleEditors(
34 cause?: vscode.TextDocumentChangeEvent,
35 ): Promise<void> {
36 if (!this.displayHints) return;
37
38 if (
39 cause !== undefined &&
40 (cause.contentChanges.length === 0 ||
41 !this.isRustDocument(cause.document))
42 ) {
43 return;
44 }
45 return this.refreshVisibleEditorsHints();
46 }
47
48 private async refreshVisibleEditorsHints(
49 newDecorations?: vscode.DecorationOptions[],
50 ) {
51 const promises: Array<Promise<void>> = [];
52
53 for (const rustEditor of vscode.window.visibleTextEditors.filter(
54 editor => this.isRustDocument(editor.document),
55 )) {
56 if (newDecorations !== undefined) {
57 promises.push(
58 Promise.resolve(
59 rustEditor.setDecorations(
60 typeHintDecorationType,
61 newDecorations,
62 ),
63 ),
64 );
65 } else {
66 promises.push(this.updateDecorationsFromServer(rustEditor));
67 }
68 }
69
70 for (const promise of promises) {
71 await promise;
72 }
73 }
74
75 private isRustDocument(document: vscode.TextDocument): boolean {
76 return document && document.languageId === 'rust';
77 }
78
79 private async updateDecorationsFromServer(
80 editor: vscode.TextEditor,
81 ): Promise<void> {
82 const newHints = await this.queryHints(editor.document.uri.toString());
83 if (newHints !== null) {
84 const newDecorations = newHints.map(hint => ({
85 range: hint.range,
86 renderOptions: {
87 after: {
88 contentText: `: ${hint.label}`,
89 },
90 },
91 }));
92 return editor.setDecorations(
93 typeHintDecorationType,
94 newDecorations,
95 );
96 }
97 }
98
99 private async queryHints(documentUri: string): Promise<InlayHint[] | null> {
100 const request: InlayHintsParams = {
101 textDocument: { uri: documentUri },
102 };
103 const client = Server.client;
104 return client
105 .onReady()
106 .then(() =>
107 client.sendRequest<InlayHint[] | null>(
108 'rust-analyzer/inlayHints',
109 request,
110 ),
111 );
112 }
113}