From 9e5ef0ce723caa3270f9595ba738aa756a8804a9 Mon Sep 17 00:00:00 2001 From: Jorge Mederos Alvarado Date: Wed, 21 Apr 2021 15:48:57 -0400 Subject: Add option to opt out from smaller inlay hints font size --- editors/code/src/config.ts | 1 + editors/code/src/inlay_hints.ts | 35 +++++++++++++++++++++++++++++------ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts index 82f0a0566..03f7d7cc3 100644 --- a/editors/code/src/config.ts +++ b/editors/code/src/config.ts @@ -115,6 +115,7 @@ export class Config { typeHints: this.get("inlayHints.typeHints"), parameterHints: this.get("inlayHints.parameterHints"), chainingHints: this.get("inlayHints.chainingHints"), + smallerHints: this.get("inlayHints.smallerHints"), maxLength: this.get("inlayHints.maxLength"), }; } diff --git a/editors/code/src/inlay_hints.ts b/editors/code/src/inlay_hints.ts index 61db6b8d0..aa7221454 100644 --- a/editors/code/src/inlay_hints.ts +++ b/editors/code/src/inlay_hints.ts @@ -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(); // 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; -- cgit v1.2.3