aboutsummaryrefslogtreecommitdiff
path: root/editors/code
diff options
context:
space:
mode:
Diffstat (limited to 'editors/code')
-rw-r--r--editors/code/src/commands/index.ts4
-rw-r--r--editors/code/src/commands/inlay_hints.ts88
-rw-r--r--editors/code/src/extension.ts12
3 files changed, 72 insertions, 32 deletions
diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts
index d17f702e8..c194bd2ea 100644
--- a/editors/code/src/commands/index.ts
+++ b/editors/code/src/commands/index.ts
@@ -1,12 +1,12 @@
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';
6import * as parentModule from './parent_module'; 7import * as parentModule from './parent_module';
7import * as runnables from './runnables'; 8import * as runnables from './runnables';
8import * as syntaxTree from './syntaxTree'; 9import * as syntaxTree from './syntaxTree';
9import * as inlayHints from './inlay_hints';
10 10
11export { 11export {
12 analyzerStatus, 12 analyzerStatus,
@@ -17,5 +17,5 @@ export {
17 runnables, 17 runnables,
18 syntaxTree, 18 syntaxTree,
19 onEnter, 19 onEnter,
20 inlayHints, 20 inlayHints
21}; 21};
diff --git a/editors/code/src/commands/inlay_hints.ts b/editors/code/src/commands/inlay_hints.ts
index cc6620d36..056d7c8e3 100644
--- a/editors/code/src/commands/inlay_hints.ts
+++ b/editors/code/src/commands/inlay_hints.ts
@@ -1,5 +1,10 @@
1import * as vscode from 'vscode'; 1import * as vscode from 'vscode';
2import { Range, TextDocumentChangeEvent, TextDocumentContentChangeEvent, TextEditor } from 'vscode'; 2import {
3 Range,
4 TextDocumentChangeEvent,
5 TextDocumentContentChangeEvent,
6 TextEditor
7} from 'vscode';
3import { TextDocumentIdentifier } from 'vscode-languageclient'; 8import { TextDocumentIdentifier } from 'vscode-languageclient';
4import { Server } from '../server'; 9import { Server } from '../server';
5 10
@@ -8,23 +13,28 @@ interface InlayHintsParams {
8} 13}
9 14
10interface InlayHint { 15interface InlayHint {
11 range: Range, 16 range: Range;
12 kind: string, 17 kind: string;
13 label: string, 18 label: string;
14} 19}
15 20
16const typeHintDecorationType = vscode.window.createTextEditorDecorationType({ 21const typeHintDecorationType = vscode.window.createTextEditorDecorationType({
17 after: { 22 after: {
18 color: new vscode.ThemeColor('ralsp.inlayHint'), 23 color: new vscode.ThemeColor('ralsp.inlayHint')
19 }, 24 }
20}); 25});
21 26
22export class HintsUpdater { 27export class HintsUpdater {
23 private displayHints = true; 28 private displayHints = true;
24 29
25 public async loadHints(editor: vscode.TextEditor | undefined): Promise<void> { 30 public async loadHints(
31 editor: vscode.TextEditor | undefined
32 ): Promise<void> {
26 if (this.displayHints && editor !== undefined) { 33 if (this.displayHints && editor !== undefined) {
27 await this.updateDecorationsFromServer(editor.document.uri.toString(), editor); 34 await this.updateDecorationsFromServer(
35 editor.document.uri.toString(),
36 editor
37 );
28 } 38 }
29 } 39 }
30 40
@@ -37,7 +47,7 @@ export class HintsUpdater {
37 } else { 47 } else {
38 const editor = vscode.window.activeTextEditor; 48 const editor = vscode.window.activeTextEditor;
39 if (editor != null) { 49 if (editor != null) {
40 return editor.setDecorations(typeHintDecorationType, []) 50 return editor.setDecorations(typeHintDecorationType, []);
41 } 51 }
42 } 52 }
43 } 53 }
@@ -58,38 +68,62 @@ export class HintsUpdater {
58 68
59 // If the dbg! macro is used in the lsp-server, an endless stream of events with `cause.contentChanges` with the dbg messages. 69 // If the dbg! macro is used in the lsp-server, an endless stream of events with `cause.contentChanges` with the dbg messages.
60 // Should not be a real situation, but better to filter such things out. 70 // Should not be a real situation, but better to filter such things out.
61 if (cause !== undefined && cause.contentChanges.filter(changeEvent => this.isEventInFile(document.lineCount, changeEvent)).length === 0) { 71 if (
72 cause !== undefined &&
73 cause.contentChanges.filter(changeEvent =>
74 this.isEventInFile(document.lineCount, changeEvent)
75 ).length === 0
76 ) {
62 return; 77 return;
63 } 78 }
64 return await this.updateDecorationsFromServer(document.uri.toString(), editor); 79 return await this.updateDecorationsFromServer(
80 document.uri.toString(),
81 editor
82 );
65 } 83 }
66 84
67 private isEventInFile(documentLineCount: number, event: TextDocumentContentChangeEvent): boolean { 85 private isEventInFile(
86 documentLineCount: number,
87 event: TextDocumentContentChangeEvent
88 ): boolean {
68 const eventText = event.text; 89 const eventText = event.text;
69 if (eventText.length === 0) { 90 if (eventText.length === 0) {
70 return event.range.start.line <= documentLineCount || event.range.end.line <= documentLineCount; 91 return (
92 event.range.start.line <= documentLineCount ||
93 event.range.end.line <= documentLineCount
94 );
71 } else { 95 } else {
72 return event.range.start.line <= documentLineCount && event.range.end.line <= documentLineCount; 96 return (
97 event.range.start.line <= documentLineCount &&
98 event.range.end.line <= documentLineCount
99 );
73 } 100 }
74 } 101 }
75 102
76 private async updateDecorationsFromServer(documentUri: string, editor: TextEditor): Promise<void> { 103 private async updateDecorationsFromServer(
77 const newHints = await this.queryHints(documentUri) || []; 104 documentUri: string,
78 const newDecorations = newHints.map(hint => ( 105 editor: TextEditor
79 { 106 ): Promise<void> {
80 range: hint.range, 107 const newHints = (await this.queryHints(documentUri)) || [];
81 renderOptions: { after: { contentText: `: ${hint.label}` } }, 108 const newDecorations = newHints.map(hint => ({
82 } 109 range: hint.range,
83 )); 110 renderOptions: { after: { contentText: `: ${hint.label}` } }
111 }));
84 return editor.setDecorations(typeHintDecorationType, newDecorations); 112 return editor.setDecorations(typeHintDecorationType, newDecorations);
85 } 113 }
86 114
87 private async queryHints(documentUri: string): Promise<InlayHint[] | null> { 115 private async queryHints(documentUri: string): Promise<InlayHint[] | null> {
88 const request: InlayHintsParams = { textDocument: { uri: documentUri } }; 116 const request: InlayHintsParams = {
117 textDocument: { uri: documentUri }
118 };
89 const client = Server.client; 119 const client = Server.client;
90 return client.onReady().then(() => client.sendRequest<InlayHint[] | null>( 120 return client
91 'rust-analyzer/inlayHints', 121 .onReady()
92 request 122 .then(() =>
93 )); 123 client.sendRequest<InlayHint[] | null>(
124 'rust-analyzer/inlayHints',
125 request
126 )
127 );
94 } 128 }
95} 129}
diff --git a/editors/code/src/extension.ts b/editors/code/src/extension.ts
index 3965b881a..2ec3a2b35 100644
--- a/editors/code/src/extension.ts
+++ b/editors/code/src/extension.ts
@@ -152,9 +152,15 @@ export function activate(context: vscode.ExtensionContext) {
152 if (Server.config.displayInlayHints) { 152 if (Server.config.displayInlayHints) {
153 const hintsUpdater = new HintsUpdater(); 153 const hintsUpdater = new HintsUpdater();
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 =>
156 vscode.workspace.onDidChangeTextDocument(e => hintsUpdater.updateHints(e)); 156 hintsUpdater.loadHints(editor)
157 vscode.workspace.onDidChangeConfiguration(_ => hintsUpdater.toggleHintsDisplay(Server.config.displayInlayHints)); 157 );
158 vscode.workspace.onDidChangeTextDocument(e =>
159 hintsUpdater.updateHints(e)
160 );
161 vscode.workspace.onDidChangeConfiguration(_ =>
162 hintsUpdater.toggleHintsDisplay(Server.config.displayInlayHints)
163 );
158 }); 164 });
159 } 165 }
160} 166}