aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/inlay_hints.ts
diff options
context:
space:
mode:
authorVeetaha <[email protected]>2020-02-16 23:47:14 +0000
committerVeetaha <[email protected]>2020-02-17 00:07:11 +0000
commitbd113623a02dc253549464667af8931e2ff378bc (patch)
treea3fabd75ef0e1b5b60381d62f761c94daf2d1299 /editors/code/src/inlay_hints.ts
parent31ae64644864257b2375167df56c0b2e3839a9fd (diff)
vscode: minor refactorings
Diffstat (limited to 'editors/code/src/inlay_hints.ts')
-rw-r--r--editors/code/src/inlay_hints.ts45
1 files changed, 24 insertions, 21 deletions
diff --git a/editors/code/src/inlay_hints.ts b/editors/code/src/inlay_hints.ts
index 12d7ddf0d..3896878cd 100644
--- a/editors/code/src/inlay_hints.ts
+++ b/editors/code/src/inlay_hints.ts
@@ -38,7 +38,7 @@ interface InlayHintsParams {
38 38
39interface InlayHint { 39interface InlayHint {
40 range: vscode.Range; 40 range: vscode.Range;
41 kind: string; 41 kind: "TypeHint" | "ParameterHint";
42 label: string; 42 label: string;
43} 43}
44 44
@@ -55,7 +55,7 @@ const parameterHintDecorationType = vscode.window.createTextEditorDecorationType
55}); 55});
56 56
57class HintsUpdater { 57class HintsUpdater {
58 private pending: Map<string, vscode.CancellationTokenSource> = new Map(); 58 private pending = new Map<string, vscode.CancellationTokenSource>();
59 private ctx: Ctx; 59 private ctx: Ctx;
60 private enabled: boolean; 60 private enabled: boolean;
61 61
@@ -64,30 +64,36 @@ class HintsUpdater {
64 this.enabled = ctx.config.displayInlayHints; 64 this.enabled = ctx.config.displayInlayHints;
65 } 65 }
66 66
67 async setEnabled(enabled: boolean) { 67 async setEnabled(enabled: boolean): Promise<void> {
68 if (this.enabled == enabled) return; 68 if (this.enabled == enabled) return;
69 this.enabled = enabled; 69 this.enabled = enabled;
70 70
71 if (this.enabled) { 71 if (this.enabled) {
72 await this.refresh(); 72 return await this.refresh();
73 } else {
74 this.allEditors.forEach(it => {
75 this.setTypeDecorations(it, []);
76 this.setParameterDecorations(it, []);
77 });
78 } 73 }
74 this.allEditors.forEach(it => {
75 this.setTypeDecorations(it, []);
76 this.setParameterDecorations(it, []);
77 });
79 } 78 }
80 79
81 async refresh() { 80 async refresh() {
82 if (!this.enabled) return; 81 if (!this.enabled) return;
83 const promises = this.allEditors.map(it => this.refreshEditor(it)); 82 await Promise.all(this.allEditors.map(it => this.refreshEditor(it)));
84 await Promise.all(promises); 83 }
84
85 private get allEditors(): vscode.TextEditor[] {
86 return vscode.window.visibleTextEditors.filter(
87 editor => editor.document.languageId === 'rust',
88 );
85 } 89 }
86 90
87 private async refreshEditor(editor: vscode.TextEditor): Promise<void> { 91 private async refreshEditor(editor: vscode.TextEditor): Promise<void> {
88 const newHints = await this.queryHints(editor.document.uri.toString()); 92 const newHints = await this.queryHints(editor.document.uri.toString());
89 if (newHints == null) return; 93 if (newHints == null) return;
90 const newTypeDecorations = newHints.filter(hint => hint.kind === 'TypeHint') 94
95 const newTypeDecorations = newHints
96 .filter(hint => hint.kind === 'TypeHint')
91 .map(hint => ({ 97 .map(hint => ({
92 range: hint.range, 98 range: hint.range,
93 renderOptions: { 99 renderOptions: {
@@ -98,7 +104,8 @@ class HintsUpdater {
98 })); 104 }));
99 this.setTypeDecorations(editor, newTypeDecorations); 105 this.setTypeDecorations(editor, newTypeDecorations);
100 106
101 const newParameterDecorations = newHints.filter(hint => hint.kind === 'ParameterHint') 107 const newParameterDecorations = newHints
108 .filter(hint => hint.kind === 'ParameterHint')
102 .map(hint => ({ 109 .map(hint => ({
103 range: hint.range, 110 range: hint.range,
104 renderOptions: { 111 renderOptions: {
@@ -110,12 +117,6 @@ class HintsUpdater {
110 this.setParameterDecorations(editor, newParameterDecorations); 117 this.setParameterDecorations(editor, newParameterDecorations);
111 } 118 }
112 119
113 private get allEditors(): vscode.TextEditor[] {
114 return vscode.window.visibleTextEditors.filter(
115 editor => editor.document.languageId === 'rust',
116 );
117 }
118
119 private setTypeDecorations( 120 private setTypeDecorations(
120 editor: vscode.TextEditor, 121 editor: vscode.TextEditor,
121 decorations: vscode.DecorationOptions[], 122 decorations: vscode.DecorationOptions[],
@@ -139,12 +140,14 @@ class HintsUpdater {
139 private async queryHints(documentUri: string): Promise<InlayHint[] | null> { 140 private async queryHints(documentUri: string): Promise<InlayHint[] | null> {
140 const client = this.ctx.client; 141 const client = this.ctx.client;
141 if (!client) return null; 142 if (!client) return null;
143
142 const request: InlayHintsParams = { 144 const request: InlayHintsParams = {
143 textDocument: { uri: documentUri }, 145 textDocument: { uri: documentUri },
144 }; 146 };
145 const tokenSource = new vscode.CancellationTokenSource(); 147 const tokenSource = new vscode.CancellationTokenSource();
146 const prev = this.pending.get(documentUri); 148 const prevHintsRequest = this.pending.get(documentUri);
147 if (prev) prev.cancel(); 149 prevHintsRequest?.cancel();
150
148 this.pending.set(documentUri, tokenSource); 151 this.pending.set(documentUri, tokenSource);
149 try { 152 try {
150 return await sendRequestWithRetry<InlayHint[] | null>( 153 return await sendRequestWithRetry<InlayHint[] | null>(