]> git.lizzy.rs Git - rust.git/commitdiff
Add option to opt out from smaller inlay hints font size
authorJorge Mederos Alvarado <jmederosalvarado@gmail.com>
Wed, 21 Apr 2021 19:48:57 +0000 (15:48 -0400)
committerJorge Mederos Alvarado <jmederosalvarado@gmail.com>
Tue, 27 Apr 2021 00:29:54 +0000 (20:29 -0400)
editors/code/src/config.ts
editors/code/src/inlay_hints.ts

index 82f0a0566a60bb7a28bf1e1b4340798f60d7661a..03f7d7cc3489011431e277013968bce12b7017e1 100644 (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"),
         };
     }
index 61db6b8d0cb762914a48275c5309e8ce4a2a8ae6..aa7221454790b37c4c81d4b824274083efb132a6 100644 (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;