Skip to content

Commit 4af50de

Browse files
Merge #8617
8617: Add option to opt out of smaller font size for inlay hints. r=SomeoneToIgnore a=jmederosalvarado As requested on issue #6883 this PR provides an option for users to opt out of the smaller font size for inlay hints. Part of #6883. Co-authored-by: Jorge Mederos Alvarado <jmederosalvarado@gmail.com>
2 parents c5364ff + 0230f22 commit 4af50de

File tree

6 files changed

+59
-10
lines changed

6 files changed

+59
-10
lines changed

crates/rust-analyzer/src/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ config_data! {
145145
inlayHints_parameterHints: bool = "true",
146146
/// Whether to show inlay type hints for variables.
147147
inlayHints_typeHints: bool = "true",
148+
/// Whether inlay hints font size should be smaller than editor's font size.
149+
inlayHints_smallerHints: bool = "true",
148150

149151
/// Whether to show `Debug` lens. Only applies when
150152
/// `#rust-analyzer.lens.enable#` is set.

docs/user/generated_config.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,11 @@ site.
234234
--
235235
Whether to show inlay type hints for variables.
236236
--
237+
[[rust-analyzer.inlayHints.smallerHints]]rust-analyzer.inlayHints.smallerHints (default: `true`)::
238+
+
239+
--
240+
Whether inlay hints font size should be smaller than editor's font size.
241+
--
237242
[[rust-analyzer.lens.debug]]rust-analyzer.lens.debug (default: `true`)::
238243
+
239244
--

editors/code/package-lock.json

Lines changed: 0 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

editors/code/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,11 @@
653653
"default": true,
654654
"type": "boolean"
655655
},
656+
"rust-analyzer.inlayHints.smallerHints": {
657+
"markdownDescription": "Whether inlay hints font size should be smaller than editor's font size.",
658+
"default": true,
659+
"type": "boolean"
660+
},
656661
"rust-analyzer.lens.debug": {
657662
"markdownDescription": "Whether to show `Debug` lens. Only applies when\n`#rust-analyzer.lens.enable#` is set.",
658663
"default": true,

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: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ 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+
13+
interface InlayHintsStyles {
14+
typeHints: InlayHintStyle;
15+
paramHints: InlayHintStyle;
16+
chainingHints: InlayHintStyle;
17+
}
18+
819

920
export function activateInlayHints(ctx: Ctx) {
1021
const maybeUpdater = {
@@ -19,6 +30,7 @@ export function activateInlayHints(ctx: Ctx) {
1930

2031
await sleep(100);
2132
if (this.updater) {
33+
this.updater.updateInlayHintsStyles();
2234
this.updater.syncCacheAndRenderHints();
2335
} else {
2436
this.updater = new HintsUpdater(ctx);
@@ -39,11 +51,7 @@ export function activateInlayHints(ctx: Ctx) {
3951
maybeUpdater.onConfigChange().catch(console.error);
4052
}
4153

42-
const typeHints = createHintStyle("type");
43-
const paramHints = createHintStyle("parameter");
44-
const chainingHints = createHintStyle("chaining");
45-
46-
function createHintStyle(hintKind: "type" | "parameter" | "chaining") {
54+
function createHintStyle(hintKind: "type" | "parameter" | "chaining", smallerHints: boolean): InlayHintStyle {
4755
// U+200C is a zero-width non-joiner to prevent the editor from forming a ligature
4856
// between code and type hints
4957
const [pos, render] = ({
@@ -61,7 +69,7 @@ function createHintStyle(hintKind: "type" | "parameter" | "chaining") {
6169
backgroundColor: bg,
6270
fontStyle: "normal",
6371
fontWeight: "normal",
64-
textDecoration: ";font-size:smaller",
72+
textDecoration: smallerHints ? ";font-size:smaller" : "none",
6573
},
6674
}),
6775
toDecoration(hint: ra.InlayHint, conv: lc.Protocol2CodeConverter): vscode.DecorationOptions {
@@ -73,9 +81,23 @@ function createHintStyle(hintKind: "type" | "parameter" | "chaining") {
7381
};
7482
}
7583

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+
7696
class HintsUpdater implements Disposable {
7797
private sourceFiles = new Map<string, RustSourceFile>(); // map Uri -> RustSourceFile
7898
private readonly disposables: Disposable[] = [];
99+
private pendingDisposeDecorations: undefined | InlayHintsStyles = undefined;
100+
private inlayHintsStyles!: InlayHintsStyles;
79101

80102
constructor(private readonly ctx: Ctx) {
81103
vscode.window.onDidChangeVisibleTextEditors(
@@ -100,6 +122,7 @@ class HintsUpdater implements Disposable {
100122
}
101123
));
102124

125+
this.updateInlayHintsStyles();
103126
this.syncCacheAndRenderHints();
104127
}
105128

@@ -114,6 +137,15 @@ class HintsUpdater implements Disposable {
114137
this.syncCacheAndRenderHints();
115138
}
116139

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+
117149
syncCacheAndRenderHints() {
118150
this.sourceFiles.forEach((file, uri) => this.fetchHints(file).then(hints => {
119151
if (!hints) return;
@@ -161,12 +193,20 @@ class HintsUpdater implements Disposable {
161193
}
162194

163195
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+
}
164203
editor.setDecorations(typeHints.decorationType, decorations.type);
165204
editor.setDecorations(paramHints.decorationType, decorations.param);
166205
editor.setDecorations(chainingHints.decorationType, decorations.chaining);
167206
}
168207

169208
private hintsToDecorations(hints: ra.InlayHint[]): InlaysDecorations {
209+
const { typeHints, paramHints, chainingHints } = this.inlayHintsStyles;
170210
const decorations: InlaysDecorations = { type: [], param: [], chaining: [] };
171211
const conv = this.ctx.client.protocol2CodeConverter;
172212

0 commit comments

Comments
 (0)