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