Add option to opt out from smaller inlay hints font size

This commit is contained in:
Jorge Mederos Alvarado 2021-04-21 15:48:57 -04:00
parent a2ba0f4846
commit 9e5ef0ce72
2 changed files with 30 additions and 6 deletions

View File

@ -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"),
};
}

View File

@ -5,6 +5,11 @@ 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;
};
export function activateInlayHints(ctx: Ctx) {
const maybeUpdater = {
@ -19,6 +24,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 +45,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(ctx: Ctx, hintKind: "type" | "parameter" | "chaining"): 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] = ({
@ -52,6 +54,8 @@ function createHintStyle(hintKind: "type" | "parameter" | "chaining") {
chaining: ["after", (label: string) => `\u{200c}: ${label}`],
} as const)[hintKind];
const smallerHints = ctx.config.inlayHints.smallerHints;
const fg = new vscode.ThemeColor(`rust_analyzer.inlayHints.foreground.${hintKind}Hints`);
const bg = new vscode.ThemeColor(`rust_analyzer.inlayHints.background.${hintKind}Hints`);
return {
@ -61,7 +65,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 {
@ -76,6 +80,11 @@ function createHintStyle(hintKind: "type" | "parameter" | "chaining") {
class HintsUpdater implements Disposable {
private sourceFiles = new Map<string, RustSourceFile>(); // map Uri -> RustSourceFile
private readonly disposables: Disposable[] = [];
private inlayHintsStyles!: {
typeHints: InlayHintStyle;
paramHints: InlayHintStyle;
chainingHints: InlayHintStyle;
};
constructor(private readonly ctx: Ctx) {
vscode.window.onDidChangeVisibleTextEditors(
@ -100,6 +109,7 @@ class HintsUpdater implements Disposable {
}
));
this.updateInlayHintsStyles();
this.syncCacheAndRenderHints();
}
@ -114,6 +124,17 @@ class HintsUpdater implements Disposable {
this.syncCacheAndRenderHints();
}
updateInlayHintsStyles() {
this.inlayHintsStyles?.typeHints.decorationType.dispose();
this.inlayHintsStyles?.paramHints.decorationType.dispose();
this.inlayHintsStyles?.chainingHints.decorationType.dispose();
this.inlayHintsStyles = {
typeHints: createHintStyle(this.ctx, "type"),
paramHints: createHintStyle(this.ctx, "parameter"),
chainingHints: createHintStyle(this.ctx, "chaining"),
};
}
syncCacheAndRenderHints() {
this.sourceFiles.forEach((file, uri) => this.fetchHints(file).then(hints => {
if (!hints) return;
@ -161,12 +182,14 @@ class HintsUpdater implements Disposable {
}
private renderDecorations(editor: RustEditor, decorations: InlaysDecorations) {
const { typeHints, paramHints, chainingHints } = this.inlayHintsStyles;
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;