8617: Add option to opt out of smaller font size for inlay hints. r=SomeoneToIgnore a=jmederosalvarado

As requested on issue  this PR provides an option for users to opt out of the smaller font size for inlay hints. Part of .

Co-authored-by: Jorge Mederos Alvarado <jmederosalvarado@gmail.com>
This commit is contained in:
bors[bot] 2021-04-27 08:31:35 +00:00 committed by GitHub
commit 4af50de0ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 59 additions and 10 deletions

@ -145,6 +145,8 @@ config_data! {
inlayHints_parameterHints: bool = "true",
/// Whether to show inlay type hints for variables.
inlayHints_typeHints: bool = "true",
/// Whether inlay hints font size should be smaller than editor's font size.
inlayHints_smallerHints: bool = "true",
/// Whether to show `Debug` lens. Only applies when
/// `#rust-analyzer.lens.enable#` is set.

@ -234,6 +234,11 @@ site.
--
Whether to show inlay type hints for variables.
--
[[rust-analyzer.inlayHints.smallerHints]]rust-analyzer.inlayHints.smallerHints (default: `true`)::
+
--
Whether inlay hints font size should be smaller than editor's font size.
--
[[rust-analyzer.lens.debug]]rust-analyzer.lens.debug (default: `true`)::
+
--

@ -830,7 +830,6 @@
"dependencies": {
"anymatch": "~3.1.1",
"braces": "~3.0.2",
"fsevents": "~2.3.1",
"glob-parent": "~5.1.0",
"is-binary-path": "~2.1.0",
"is-glob": "~4.0.1",
@ -2680,9 +2679,6 @@
"resolved": "https://registry.npmjs.org/rollup/-/rollup-2.39.1.tgz",
"integrity": "sha512-9rfr0Z6j+vE+eayfNVFr1KZ+k+jiUl2+0e4quZafy1x6SFCjzFspfRSO2ZZQeWeX9noeDTUDgg6eCENiEPFvQg==",
"dev": true,
"dependencies": {
"fsevents": "~2.3.1"
},
"bin": {
"rollup": "dist/bin/rollup"
},

@ -653,6 +653,11 @@
"default": true,
"type": "boolean"
},
"rust-analyzer.inlayHints.smallerHints": {
"markdownDescription": "Whether inlay hints font size should be smaller than editor's font size.",
"default": true,
"type": "boolean"
},
"rust-analyzer.lens.debug": {
"markdownDescription": "Whether to show `Debug` lens. Only applies when\n`#rust-analyzer.lens.enable#` is set.",
"default": true,

@ -115,6 +115,7 @@ export class Config {
typeHints: this.get<boolean>("inlayHints.typeHints"),
parameterHints: this.get<boolean>("inlayHints.parameterHints"),
chainingHints: this.get<boolean>("inlayHints.chainingHints"),
smallerHints: this.get<boolean>("inlayHints.smallerHints"),
maxLength: this.get<null | number>("inlayHints.maxLength"),
};
}

@ -5,6 +5,17 @@ import * as ra from './lsp_ext';
import { Ctx, Disposable } from './ctx';
import { sendRequestWithRetry, isRustDocument, RustDocument, RustEditor, sleep } from './util';
interface InlayHintStyle {
decorationType: vscode.TextEditorDecorationType;
toDecoration(hint: ra.InlayHint, conv: lc.Protocol2CodeConverter): vscode.DecorationOptions;
};
interface InlayHintsStyles {
typeHints: InlayHintStyle;
paramHints: InlayHintStyle;
chainingHints: InlayHintStyle;
}
export function activateInlayHints(ctx: Ctx) {
const maybeUpdater = {
@ -19,6 +30,7 @@ export function activateInlayHints(ctx: Ctx) {
await sleep(100);
if (this.updater) {
this.updater.updateInlayHintsStyles();
this.updater.syncCacheAndRenderHints();
} else {
this.updater = new HintsUpdater(ctx);
@ -39,11 +51,7 @@ export function activateInlayHints(ctx: Ctx) {
maybeUpdater.onConfigChange().catch(console.error);
}
const typeHints = createHintStyle("type");
const paramHints = createHintStyle("parameter");
const chainingHints = createHintStyle("chaining");
function createHintStyle(hintKind: "type" | "parameter" | "chaining") {
function createHintStyle(hintKind: "type" | "parameter" | "chaining", smallerHints: boolean): InlayHintStyle {
// U+200C is a zero-width non-joiner to prevent the editor from forming a ligature
// between code and type hints
const [pos, render] = ({
@ -61,7 +69,7 @@ function createHintStyle(hintKind: "type" | "parameter" | "chaining") {
backgroundColor: bg,
fontStyle: "normal",
fontWeight: "normal",
textDecoration: ";font-size:smaller",
textDecoration: smallerHints ? ";font-size:smaller" : "none",
},
}),
toDecoration(hint: ra.InlayHint, conv: lc.Protocol2CodeConverter): vscode.DecorationOptions {
@ -73,9 +81,23 @@ function createHintStyle(hintKind: "type" | "parameter" | "chaining") {
};
}
const smallHintsStyles = {
typeHints: createHintStyle("type", true),
paramHints: createHintStyle("parameter", true),
chainingHints: createHintStyle("chaining", true),
};
const biggerHintsStyles = {
typeHints: createHintStyle("type", false),
paramHints: createHintStyle("parameter", false),
chainingHints: createHintStyle("chaining", false),
};
class HintsUpdater implements Disposable {
private sourceFiles = new Map<string, RustSourceFile>(); // map Uri -> RustSourceFile
private readonly disposables: Disposable[] = [];
private pendingDisposeDecorations: undefined | InlayHintsStyles = undefined;
private inlayHintsStyles!: InlayHintsStyles;
constructor(private readonly ctx: Ctx) {
vscode.window.onDidChangeVisibleTextEditors(
@ -100,6 +122,7 @@ class HintsUpdater implements Disposable {
}
));
this.updateInlayHintsStyles();
this.syncCacheAndRenderHints();
}
@ -114,6 +137,15 @@ class HintsUpdater implements Disposable {
this.syncCacheAndRenderHints();
}
updateInlayHintsStyles() {
const inlayHintsStyles = this.ctx.config.inlayHints.smallerHints ? smallHintsStyles : biggerHintsStyles;
if (inlayHintsStyles !== this.inlayHintsStyles) {
this.pendingDisposeDecorations = this.inlayHintsStyles;
this.inlayHintsStyles = inlayHintsStyles;
}
}
syncCacheAndRenderHints() {
this.sourceFiles.forEach((file, uri) => this.fetchHints(file).then(hints => {
if (!hints) return;
@ -161,12 +193,20 @@ class HintsUpdater implements Disposable {
}
private renderDecorations(editor: RustEditor, decorations: InlaysDecorations) {
const { typeHints, paramHints, chainingHints } = this.inlayHintsStyles;
if (this.pendingDisposeDecorations !== undefined) {
const { typeHints, paramHints, chainingHints } = this.pendingDisposeDecorations;
editor.setDecorations(typeHints.decorationType, []);
editor.setDecorations(paramHints.decorationType, []);
editor.setDecorations(chainingHints.decorationType, []);
}
editor.setDecorations(typeHints.decorationType, decorations.type);
editor.setDecorations(paramHints.decorationType, decorations.param);
editor.setDecorations(chainingHints.decorationType, decorations.chaining);
}
private hintsToDecorations(hints: ra.InlayHint[]): InlaysDecorations {
const { typeHints, paramHints, chainingHints } = this.inlayHintsStyles;
const decorations: InlaysDecorations = { type: [], param: [], chaining: [] };
const conv = this.ctx.client.protocol2CodeConverter;