Skip to content

Commit 9e5ef0c

Browse files
Add option to opt out from smaller inlay hints font size
1 parent a2ba0f4 commit 9e5ef0c

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

editors/code/src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export class Config {
115115
typeHints: this.get<boolean>("inlayHints.typeHints"),
116116
parameterHints: this.get<boolean>("inlayHints.parameterHints"),
117117
chainingHints: this.get<boolean>("inlayHints.chainingHints"),
118+
smallerHints: this.get<boolean>("inlayHints.smallerHints"),
118119
maxLength: this.get<null | number>("inlayHints.maxLength"),
119120
};
120121
}

editors/code/src/inlay_hints.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import * as ra from './lsp_ext';
55
import { Ctx, Disposable } from './ctx';
66
import { sendRequestWithRetry, isRustDocument, RustDocument, RustEditor, sleep } from './util';
77

8+
interface InlayHintStyle {
9+
decorationType: vscode.TextEditorDecorationType;
10+
toDecoration(hint: ra.InlayHint, conv: lc.Protocol2CodeConverter): vscode.DecorationOptions;
11+
};
12+
813

914
export function activateInlayHints(ctx: Ctx) {
1015
const maybeUpdater = {
@@ -19,6 +24,7 @@ export function activateInlayHints(ctx: Ctx) {
1924

2025
await sleep(100);
2126
if (this.updater) {
27+
this.updater.updateInlayHintsStyles();
2228
this.updater.syncCacheAndRenderHints();
2329
} else {
2430
this.updater = new HintsUpdater(ctx);
@@ -39,11 +45,7 @@ export function activateInlayHints(ctx: Ctx) {
3945
maybeUpdater.onConfigChange().catch(console.error);
4046
}
4147

42-
const typeHints = createHintStyle("type");
43-
const paramHints = createHintStyle("parameter");
44-
const chainingHints = createHintStyle("chaining");
45-
46-
function createHintStyle(hintKind: "type" | "parameter" | "chaining") {
48+
function createHintStyle(ctx: Ctx, hintKind: "type" | "parameter" | "chaining"): InlayHintStyle {
4749
// U+200C is a zero-width non-joiner to prevent the editor from forming a ligature
4850
// between code and type hints
4951
const [pos, render] = ({
@@ -52,6 +54,8 @@ function createHintStyle(hintKind: "type" | "parameter" | "chaining") {
5254
chaining: ["after", (label: string) => `\u{200c}: ${label}`],
5355
} as const)[hintKind];
5456

57+
const smallerHints = ctx.config.inlayHints.smallerHints;
58+
5559
const fg = new vscode.ThemeColor(`rust_analyzer.inlayHints.foreground.${hintKind}Hints`);
5660
const bg = new vscode.ThemeColor(`rust_analyzer.inlayHints.background.${hintKind}Hints`);
5761
return {
@@ -61,7 +65,7 @@ function createHintStyle(hintKind: "type" | "parameter" | "chaining") {
6165
backgroundColor: bg,
6266
fontStyle: "normal",
6367
fontWeight: "normal",
64-
textDecoration: ";font-size:smaller",
68+
textDecoration: smallerHints ? ";font-size:smaller" : "none",
6569
},
6670
}),
6771
toDecoration(hint: ra.InlayHint, conv: lc.Protocol2CodeConverter): vscode.DecorationOptions {
@@ -76,6 +80,11 @@ function createHintStyle(hintKind: "type" | "parameter" | "chaining") {
7680
class HintsUpdater implements Disposable {
7781
private sourceFiles = new Map<string, RustSourceFile>(); // map Uri -> RustSourceFile
7882
private readonly disposables: Disposable[] = [];
83+
private inlayHintsStyles!: {
84+
typeHints: InlayHintStyle;
85+
paramHints: InlayHintStyle;
86+
chainingHints: InlayHintStyle;
87+
};
7988

8089
constructor(private readonly ctx: Ctx) {
8190
vscode.window.onDidChangeVisibleTextEditors(
@@ -100,6 +109,7 @@ class HintsUpdater implements Disposable {
100109
}
101110
));
102111

112+
this.updateInlayHintsStyles();
103113
this.syncCacheAndRenderHints();
104114
}
105115

@@ -114,6 +124,17 @@ class HintsUpdater implements Disposable {
114124
this.syncCacheAndRenderHints();
115125
}
116126

127+
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+
};
136+
}
137+
117138
syncCacheAndRenderHints() {
118139
this.sourceFiles.forEach((file, uri) => this.fetchHints(file).then(hints => {
119140
if (!hints) return;
@@ -161,12 +182,14 @@ class HintsUpdater implements Disposable {
161182
}
162183

163184
private renderDecorations(editor: RustEditor, decorations: InlaysDecorations) {
185+
const { typeHints, paramHints, chainingHints } = this.inlayHintsStyles;
164186
editor.setDecorations(typeHints.decorationType, decorations.type);
165187
editor.setDecorations(paramHints.decorationType, decorations.param);
166188
editor.setDecorations(chainingHints.decorationType, decorations.chaining);
167189
}
168190

169191
private hintsToDecorations(hints: ra.InlayHint[]): InlaysDecorations {
192+
const { typeHints, paramHints, chainingHints } = this.inlayHintsStyles;
170193
const decorations: InlaysDecorations = { type: [], param: [], chaining: [] };
171194
const conv = this.ctx.client.protocol2CodeConverter;
172195

0 commit comments

Comments
 (0)