diff options
| author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-04-27 08:31:35 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-27 08:31:35 +0000 |
| commit | 4af50de0abc81fff51caa2fbadfc31915e63e6a4 (patch) | |
| tree | 64a9e0b4102aa1dbec4ea890d200479709352bf5 | |
| parent | c5364ffde14eed2738b4a45cd004af07819967c5 (diff) | |
| parent | 0230f22d2a86ad7720b4a39e41a13111aa4b4789 (diff) | |
| download | rust-4af50de0abc81fff51caa2fbadfc31915e63e6a4.tar.gz rust-4af50de0abc81fff51caa2fbadfc31915e63e6a4.zip | |
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>
| -rw-r--r-- | crates/rust-analyzer/src/config.rs | 2 | ||||
| -rw-r--r-- | docs/user/generated_config.adoc | 5 | ||||
| -rw-r--r-- | editors/code/package-lock.json | 4 | ||||
| -rw-r--r-- | editors/code/package.json | 5 | ||||
| -rw-r--r-- | editors/code/src/config.ts | 1 | ||||
| -rw-r--r-- | editors/code/src/inlay_hints.ts | 52 |
6 files changed, 59 insertions, 10 deletions
diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index d81ee94ee4f..28bbbce197b 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -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. diff --git a/docs/user/generated_config.adoc b/docs/user/generated_config.adoc index e28423e9932..db3c5f7bbb1 100644 --- a/docs/user/generated_config.adoc +++ b/docs/user/generated_config.adoc @@ -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`):: + -- diff --git a/editors/code/package-lock.json b/editors/code/package-lock.json index 198c17556d2..4c2d1688174 100644 --- a/editors/code/package-lock.json +++ b/editors/code/package-lock.json @@ -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" }, diff --git a/editors/code/package.json b/editors/code/package.json index fa5632f9007..97d92e43c82 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -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, diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts index 82f0a0566a6..03f7d7cc348 100644 --- a/editors/code/src/config.ts +++ b/editors/code/src/config.ts @@ -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"), }; } diff --git a/editors/code/src/inlay_hints.ts b/editors/code/src/inlay_hints.ts index 61db6b8d0cb..c23d6f7384b 100644 --- a/editors/code/src/inlay_hints.ts +++ b/editors/code/src/inlay_hints.ts @@ -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; |
