aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/inlay_hints.ts
diff options
context:
space:
mode:
Diffstat (limited to 'editors/code/src/inlay_hints.ts')
-rw-r--r--editors/code/src/inlay_hints.ts52
1 files changed, 46 insertions, 6 deletions
diff --git a/editors/code/src/inlay_hints.ts b/editors/code/src/inlay_hints.ts
index 61db6b8d0..c23d6f738 100644
--- a/editors/code/src/inlay_hints.ts
+++ b/editors/code/src/inlay_hints.ts
@@ -5,6 +5,17 @@ import * as ra from './lsp_ext';
5import { Ctx, Disposable } from './ctx'; 5import { Ctx, Disposable } from './ctx';
6import { sendRequestWithRetry, isRustDocument, RustDocument, RustEditor, sleep } from './util'; 6import { sendRequestWithRetry, isRustDocument, RustDocument, RustEditor, sleep } from './util';
7 7
8interface InlayHintStyle {
9 decorationType: vscode.TextEditorDecorationType;
10 toDecoration(hint: ra.InlayHint, conv: lc.Protocol2CodeConverter): vscode.DecorationOptions;
11};
12
13interface InlayHintsStyles {
14 typeHints: InlayHintStyle;
15 paramHints: InlayHintStyle;
16 chainingHints: InlayHintStyle;
17}
18
8 19
9export function activateInlayHints(ctx: Ctx) { 20export function activateInlayHints(ctx: Ctx) {
10 const maybeUpdater = { 21 const maybeUpdater = {
@@ -19,6 +30,7 @@ export function activateInlayHints(ctx: Ctx) {
19 30
20 await sleep(100); 31 await sleep(100);
21 if (this.updater) { 32 if (this.updater) {
33 this.updater.updateInlayHintsStyles();
22 this.updater.syncCacheAndRenderHints(); 34 this.updater.syncCacheAndRenderHints();
23 } else { 35 } else {
24 this.updater = new HintsUpdater(ctx); 36 this.updater = new HintsUpdater(ctx);
@@ -39,11 +51,7 @@ export function activateInlayHints(ctx: Ctx) {
39 maybeUpdater.onConfigChange().catch(console.error); 51 maybeUpdater.onConfigChange().catch(console.error);
40} 52}
41 53
42const typeHints = createHintStyle("type"); 54function createHintStyle(hintKind: "type" | "parameter" | "chaining", smallerHints: boolean): InlayHintStyle {
43const paramHints = createHintStyle("parameter");
44const chainingHints = createHintStyle("chaining");
45
46function createHintStyle(hintKind: "type" | "parameter" | "chaining") {
47 // U+200C is a zero-width non-joiner to prevent the editor from forming a ligature 55 // U+200C is a zero-width non-joiner to prevent the editor from forming a ligature
48 // between code and type hints 56 // between code and type hints
49 const [pos, render] = ({ 57 const [pos, render] = ({
@@ -61,7 +69,7 @@ function createHintStyle(hintKind: "type" | "parameter" | "chaining") {
61 backgroundColor: bg, 69 backgroundColor: bg,
62 fontStyle: "normal", 70 fontStyle: "normal",
63 fontWeight: "normal", 71 fontWeight: "normal",
64 textDecoration: ";font-size:smaller", 72 textDecoration: smallerHints ? ";font-size:smaller" : "none",
65 }, 73 },
66 }), 74 }),
67 toDecoration(hint: ra.InlayHint, conv: lc.Protocol2CodeConverter): vscode.DecorationOptions { 75 toDecoration(hint: ra.InlayHint, conv: lc.Protocol2CodeConverter): vscode.DecorationOptions {
@@ -73,9 +81,23 @@ function createHintStyle(hintKind: "type" | "parameter" | "chaining") {
73 }; 81 };
74} 82}
75 83
84const smallHintsStyles = {
85 typeHints: createHintStyle("type", true),
86 paramHints: createHintStyle("parameter", true),
87 chainingHints: createHintStyle("chaining", true),
88};
89
90const biggerHintsStyles = {
91 typeHints: createHintStyle("type", false),
92 paramHints: createHintStyle("parameter", false),
93 chainingHints: createHintStyle("chaining", false),
94};
95
76class HintsUpdater implements Disposable { 96class HintsUpdater implements Disposable {
77 private sourceFiles = new Map<string, RustSourceFile>(); // map Uri -> RustSourceFile 97 private sourceFiles = new Map<string, RustSourceFile>(); // map Uri -> RustSourceFile
78 private readonly disposables: Disposable[] = []; 98 private readonly disposables: Disposable[] = [];
99 private pendingDisposeDecorations: undefined | InlayHintsStyles = undefined;
100 private inlayHintsStyles!: InlayHintsStyles;
79 101
80 constructor(private readonly ctx: Ctx) { 102 constructor(private readonly ctx: Ctx) {
81 vscode.window.onDidChangeVisibleTextEditors( 103 vscode.window.onDidChangeVisibleTextEditors(
@@ -100,6 +122,7 @@ class HintsUpdater implements Disposable {
100 } 122 }
101 )); 123 ));
102 124
125 this.updateInlayHintsStyles();
103 this.syncCacheAndRenderHints(); 126 this.syncCacheAndRenderHints();
104 } 127 }
105 128
@@ -114,6 +137,15 @@ class HintsUpdater implements Disposable {
114 this.syncCacheAndRenderHints(); 137 this.syncCacheAndRenderHints();
115 } 138 }
116 139
140 updateInlayHintsStyles() {
141 const inlayHintsStyles = this.ctx.config.inlayHints.smallerHints ? smallHintsStyles : biggerHintsStyles;
142
143 if (inlayHintsStyles !== this.inlayHintsStyles) {
144 this.pendingDisposeDecorations = this.inlayHintsStyles;
145 this.inlayHintsStyles = inlayHintsStyles;
146 }
147 }
148
117 syncCacheAndRenderHints() { 149 syncCacheAndRenderHints() {
118 this.sourceFiles.forEach((file, uri) => this.fetchHints(file).then(hints => { 150 this.sourceFiles.forEach((file, uri) => this.fetchHints(file).then(hints => {
119 if (!hints) return; 151 if (!hints) return;
@@ -161,12 +193,20 @@ class HintsUpdater implements Disposable {
161 } 193 }
162 194
163 private renderDecorations(editor: RustEditor, decorations: InlaysDecorations) { 195 private renderDecorations(editor: RustEditor, decorations: InlaysDecorations) {
196 const { typeHints, paramHints, chainingHints } = this.inlayHintsStyles;
197 if (this.pendingDisposeDecorations !== undefined) {
198 const { typeHints, paramHints, chainingHints } = this.pendingDisposeDecorations;
199 editor.setDecorations(typeHints.decorationType, []);
200 editor.setDecorations(paramHints.decorationType, []);
201 editor.setDecorations(chainingHints.decorationType, []);
202 }
164 editor.setDecorations(typeHints.decorationType, decorations.type); 203 editor.setDecorations(typeHints.decorationType, decorations.type);
165 editor.setDecorations(paramHints.decorationType, decorations.param); 204 editor.setDecorations(paramHints.decorationType, decorations.param);
166 editor.setDecorations(chainingHints.decorationType, decorations.chaining); 205 editor.setDecorations(chainingHints.decorationType, decorations.chaining);
167 } 206 }
168 207
169 private hintsToDecorations(hints: ra.InlayHint[]): InlaysDecorations { 208 private hintsToDecorations(hints: ra.InlayHint[]): InlaysDecorations {
209 const { typeHints, paramHints, chainingHints } = this.inlayHintsStyles;
170 const decorations: InlaysDecorations = { type: [], param: [], chaining: [] }; 210 const decorations: InlaysDecorations = { type: [], param: [], chaining: [] };
171 const conv = this.ctx.client.protocol2CodeConverter; 211 const conv = this.ctx.client.protocol2CodeConverter;
172 212