Skip to content

Commit 0230f22

Browse files
Fix how and when old inlay hint decorations are disposed
1 parent c4dba40 commit 0230f22

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

editors/code/src/inlay_hints.ts

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ interface InlayHintStyle {
1010
toDecoration(hint: ra.InlayHint, conv: lc.Protocol2CodeConverter): vscode.DecorationOptions;
1111
};
1212

13+
interface InlayHintsStyles {
14+
typeHints: InlayHintStyle;
15+
paramHints: InlayHintStyle;
16+
chainingHints: InlayHintStyle;
17+
}
18+
1319

1420
export function activateInlayHints(ctx: Ctx) {
1521
const maybeUpdater = {
@@ -45,7 +51,7 @@ export function activateInlayHints(ctx: Ctx) {
4551
maybeUpdater.onConfigChange().catch(console.error);
4652
}
4753

48-
function createHintStyle(ctx: Ctx, hintKind: "type" | "parameter" | "chaining"): InlayHintStyle {
54+
function createHintStyle(hintKind: "type" | "parameter" | "chaining", smallerHints: boolean): InlayHintStyle {
4955
// U+200C is a zero-width non-joiner to prevent the editor from forming a ligature
5056
// between code and type hints
5157
const [pos, render] = ({
@@ -54,8 +60,6 @@ function createHintStyle(ctx: Ctx, hintKind: "type" | "parameter" | "chaining"):
5460
chaining: ["after", (label: string) => `\u{200c}: ${label}`],
5561
} as const)[hintKind];
5662

57-
const smallerHints = ctx.config.inlayHints.smallerHints;
58-
5963
const fg = new vscode.ThemeColor(`rust_analyzer.inlayHints.foreground.${hintKind}Hints`);
6064
const bg = new vscode.ThemeColor(`rust_analyzer.inlayHints.background.${hintKind}Hints`);
6165
return {
@@ -77,14 +81,23 @@ function createHintStyle(ctx: Ctx, hintKind: "type" | "parameter" | "chaining"):
7781
};
7882
}
7983

84+
const smallHintsStyles = {
85+
typeHints: createHintStyle("type", true),
86+
paramHints: createHintStyle("parameter", true),
87+
chainingHints: createHintStyle("chaining", true),
88+
};
89+
90+
const biggerHintsStyles = {
91+
typeHints: createHintStyle("type", false),
92+
paramHints: createHintStyle("parameter", false),
93+
chainingHints: createHintStyle("chaining", false),
94+
};
95+
8096
class HintsUpdater implements Disposable {
8197
private sourceFiles = new Map<string, RustSourceFile>(); // map Uri -> RustSourceFile
8298
private readonly disposables: Disposable[] = [];
83-
private inlayHintsStyles!: {
84-
typeHints: InlayHintStyle;
85-
paramHints: InlayHintStyle;
86-
chainingHints: InlayHintStyle;
87-
};
99+
private pendingDisposeDecorations: undefined | InlayHintsStyles = undefined;
100+
private inlayHintsStyles!: InlayHintsStyles;
88101

89102
constructor(private readonly ctx: Ctx) {
90103
vscode.window.onDidChangeVisibleTextEditors(
@@ -125,14 +138,12 @@ class HintsUpdater implements Disposable {
125138
}
126139

127140
updateInlayHintsStyles() {
128-
this.inlayHintsStyles?.typeHints.decorationType.dispose();
129-
this.inlayHintsStyles?.paramHints.decorationType.dispose();
130-
this.inlayHintsStyles?.chainingHints.decorationType.dispose();
131-
this.inlayHintsStyles = {
132-
typeHints: createHintStyle(this.ctx, "type"),
133-
paramHints: createHintStyle(this.ctx, "parameter"),
134-
chainingHints: createHintStyle(this.ctx, "chaining"),
135-
};
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+
}
136147
}
137148

138149
syncCacheAndRenderHints() {
@@ -183,6 +194,12 @@ class HintsUpdater implements Disposable {
183194

184195
private renderDecorations(editor: RustEditor, decorations: InlaysDecorations) {
185196
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+
}
186203
editor.setDecorations(typeHints.decorationType, decorations.type);
187204
editor.setDecorations(paramHints.decorationType, decorations.param);
188205
editor.setDecorations(chainingHints.decorationType, decorations.chaining);

0 commit comments

Comments
 (0)