aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/commands
diff options
context:
space:
mode:
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.ts104
2 files changed, 107 insertions, 1 deletions
diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts
index 194658497..c194bd2ea 100644
--- a/editors/code/src/commands/index.ts
+++ b/editors/code/src/commands/index.ts
@@ -1,5 +1,6 @@
1import * as analyzerStatus from './analyzer_status'; 1import * as analyzerStatus from './analyzer_status';
2import * as applySourceChange from './apply_source_change'; 2import * as applySourceChange from './apply_source_change';
3import * as inlayHints from './inlay_hints';
3import * as joinLines from './join_lines'; 4import * as joinLines from './join_lines';
4import * as matchingBrace from './matching_brace'; 5import * as matchingBrace from './matching_brace';
5import * as onEnter from './on_enter'; 6import * as onEnter from './on_enter';
@@ -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..8154af8dc
--- /dev/null
+++ b/editors/code/src/commands/inlay_hints.ts
@@ -0,0 +1,104 @@
1import * as vscode from 'vscode';
2import { Range, TextDocumentChangeEvent, 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 displayHints = true;
24
25 public async loadHints(
26 editor: vscode.TextEditor | undefined
27 ): Promise<void> {
28 if (
29 this.displayHints &&
30 editor !== undefined &&
31 this.isRustDocument(editor.document)
32 ) {
33 await this.updateDecorationsFromServer(
34 editor.document.uri.toString(),
35 editor
36 );
37 }
38 }
39
40 public async toggleHintsDisplay(displayHints: boolean): Promise<void> {
41 if (this.displayHints !== displayHints) {
42 this.displayHints = displayHints;
43
44 if (displayHints) {
45 return this.updateHints();
46 } else {
47 const editor = vscode.window.activeTextEditor;
48 if (editor != null) {
49 return editor.setDecorations(typeHintDecorationType, []);
50 }
51 }
52 }
53 }
54
55 public async updateHints(cause?: TextDocumentChangeEvent): Promise<void> {
56 if (!this.displayHints) {
57 return;
58 }
59 const editor = vscode.window.activeTextEditor;
60 if (editor == null) {
61 return;
62 }
63 const document = cause == null ? editor.document : cause.document;
64 if (!this.isRustDocument(document)) {
65 return;
66 }
67
68 return await this.updateDecorationsFromServer(
69 document.uri.toString(),
70 editor
71 );
72 }
73
74 private isRustDocument(document: vscode.TextDocument): boolean {
75 return document && document.languageId === 'rust';
76 }
77
78 private async updateDecorationsFromServer(
79 documentUri: string,
80 editor: TextEditor
81 ): Promise<void> {
82 const newHints = (await this.queryHints(documentUri)) || [];
83 const newDecorations = newHints.map(hint => ({
84 range: hint.range,
85 renderOptions: { after: { contentText: `: ${hint.label}` } }
86 }));
87 return editor.setDecorations(typeHintDecorationType, newDecorations);
88 }
89
90 private async queryHints(documentUri: string): Promise<InlayHint[] | null> {
91 const request: InlayHintsParams = {
92 textDocument: { uri: documentUri }
93 };
94 const client = Server.client;
95 return client
96 .onReady()
97 .then(() =>
98 client.sendRequest<InlayHint[] | null>(
99 'rust-analyzer/inlayHints',
100 request
101 )
102 );
103 }
104}