aboutsummaryrefslogtreecommitdiff
path: root/editors/code
diff options
context:
space:
mode:
authorJorge Mederos Alvarado <[email protected]>2021-04-27 01:26:36 +0100
committerJorge Mederos Alvarado <[email protected]>2021-04-27 01:29:54 +0100
commit0230f22d2a86ad7720b4a39e41a13111aa4b4789 (patch)
tree64a9e0b4102aa1dbec4ea890d200479709352bf5 /editors/code
parentc4dba4077f748ce16c91d369b98cbb23e59633c6 (diff)
Fix how and when old inlay hint decorations are disposed
Diffstat (limited to 'editors/code')
-rw-r--r--editors/code/src/inlay_hints.ts49
1 files changed, 33 insertions, 16 deletions
diff --git a/editors/code/src/inlay_hints.ts b/editors/code/src/inlay_hints.ts
index aa7221454..c23d6f738 100644
--- a/editors/code/src/inlay_hints.ts
+++ b/editors/code/src/inlay_hints.ts
@@ -10,6 +10,12 @@ interface InlayHintStyle {
10 toDecoration(hint: ra.InlayHint, conv: lc.Protocol2CodeConverter): vscode.DecorationOptions; 10 toDecoration(hint: ra.InlayHint, conv: lc.Protocol2CodeConverter): vscode.DecorationOptions;
11}; 11};
12 12
13interface InlayHintsStyles {
14 typeHints: InlayHintStyle;
15 paramHints: InlayHintStyle;
16 chainingHints: InlayHintStyle;
17}
18
13 19
14export function activateInlayHints(ctx: Ctx) { 20export function activateInlayHints(ctx: Ctx) {
15 const maybeUpdater = { 21 const maybeUpdater = {
@@ -45,7 +51,7 @@ export function activateInlayHints(ctx: Ctx) {
45 maybeUpdater.onConfigChange().catch(console.error); 51 maybeUpdater.onConfigChange().catch(console.error);
46} 52}
47 53
48function createHintStyle(ctx: Ctx, hintKind: "type" | "parameter" | "chaining"): InlayHintStyle { 54function createHintStyle(hintKind: "type" | "parameter" | "chaining", smallerHints: boolean): InlayHintStyle {
49 // 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
50 // between code and type hints 56 // between code and type hints
51 const [pos, render] = ({ 57 const [pos, render] = ({
@@ -54,8 +60,6 @@ function createHintStyle(ctx: Ctx, hintKind: "type" | "parameter" | "chaining"):
54 chaining: ["after", (label: string) => `\u{200c}: ${label}`], 60 chaining: ["after", (label: string) => `\u{200c}: ${label}`],
55 } as const)[hintKind]; 61 } as const)[hintKind];
56 62
57 const smallerHints = ctx.config.inlayHints.smallerHints;
58
59 const fg = new vscode.ThemeColor(`rust_analyzer.inlayHints.foreground.${hintKind}Hints`); 63 const fg = new vscode.ThemeColor(`rust_analyzer.inlayHints.foreground.${hintKind}Hints`);
60 const bg = new vscode.ThemeColor(`rust_analyzer.inlayHints.background.${hintKind}Hints`); 64 const bg = new vscode.ThemeColor(`rust_analyzer.inlayHints.background.${hintKind}Hints`);
61 return { 65 return {
@@ -77,14 +81,23 @@ function createHintStyle(ctx: Ctx, hintKind: "type" | "parameter" | "chaining"):
77 }; 81 };
78} 82}
79 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
80class HintsUpdater implements Disposable { 96class HintsUpdater implements Disposable {
81 private sourceFiles = new Map<string, RustSourceFile>(); // map Uri -> RustSourceFile 97 private sourceFiles = new Map<string, RustSourceFile>(); // map Uri -> RustSourceFile
82 private readonly disposables: Disposable[] = []; 98 private readonly disposables: Disposable[] = [];
83 private inlayHintsStyles!: { 99 private pendingDisposeDecorations: undefined | InlayHintsStyles = undefined;
84 typeHints: InlayHintStyle; 100 private inlayHintsStyles!: InlayHintsStyles;
85 paramHints: InlayHintStyle;
86 chainingHints: InlayHintStyle;
87 };
88 101
89 constructor(private readonly ctx: Ctx) { 102 constructor(private readonly ctx: Ctx) {
90 vscode.window.onDidChangeVisibleTextEditors( 103 vscode.window.onDidChangeVisibleTextEditors(
@@ -125,14 +138,12 @@ class HintsUpdater implements Disposable {
125 } 138 }
126 139
127 updateInlayHintsStyles() { 140 updateInlayHintsStyles() {
128 this.inlayHintsStyles?.typeHints.decorationType.dispose(); 141 const inlayHintsStyles = this.ctx.config.inlayHints.smallerHints ? smallHintsStyles : biggerHintsStyles;
129 this.inlayHintsStyles?.paramHints.decorationType.dispose(); 142
130 this.inlayHintsStyles?.chainingHints.decorationType.dispose(); 143 if (inlayHintsStyles !== this.inlayHintsStyles) {
131 this.inlayHintsStyles = { 144 this.pendingDisposeDecorations = this.inlayHintsStyles;
132 typeHints: createHintStyle(this.ctx, "type"), 145 this.inlayHintsStyles = inlayHintsStyles;
133 paramHints: createHintStyle(this.ctx, "parameter"), 146 }
134 chainingHints: createHintStyle(this.ctx, "chaining"),
135 };
136 } 147 }
137 148
138 syncCacheAndRenderHints() { 149 syncCacheAndRenderHints() {
@@ -183,6 +194,12 @@ class HintsUpdater implements Disposable {
183 194
184 private renderDecorations(editor: RustEditor, decorations: InlaysDecorations) { 195 private renderDecorations(editor: RustEditor, decorations: InlaysDecorations) {
185 const { typeHints, paramHints, chainingHints } = this.inlayHintsStyles; 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 }
186 editor.setDecorations(typeHints.decorationType, decorations.type); 203 editor.setDecorations(typeHints.decorationType, decorations.type);
187 editor.setDecorations(paramHints.decorationType, decorations.param); 204 editor.setDecorations(paramHints.decorationType, decorations.param);
188 editor.setDecorations(chainingHints.decorationType, decorations.chaining); 205 editor.setDecorations(chainingHints.decorationType, decorations.chaining);