From a2ba0f48467aede5a051bb1e7ac0384d8f5b7e8f Mon Sep 17 00:00:00 2001 From: Jorge Mederos Alvarado Date: Wed, 21 Apr 2021 15:09:41 -0400 Subject: add option to package.json --- editors/code/package-lock.json | 4 ---- editors/code/package.json | 5 +++++ 2 files changed, 5 insertions(+), 4 deletions(-) (limited to 'editors') diff --git a/editors/code/package-lock.json b/editors/code/package-lock.json index 198c17556..4c2d16881 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 fa5632f90..97d92e43c 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, -- cgit v1.2.3 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(-) (limited to 'editors') 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 From 0230f22d2a86ad7720b4a39e41a13111aa4b4789 Mon Sep 17 00:00:00 2001 From: Jorge Mederos Alvarado Date: Mon, 26 Apr 2021 20:26:36 -0400 Subject: Fix how and when old inlay hint decorations are disposed --- editors/code/src/inlay_hints.ts | 49 +++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 16 deletions(-) (limited to 'editors') diff --git a/editors/code/src/inlay_hints.ts b/editors/code/src/inlay_hints.ts index aa7221454..c23d6f738 100644 --- a/editors/code/src/inlay_hints.ts +++ b/editors/code/src/inlay_hints.ts @@ -10,6 +10,12 @@ interface InlayHintStyle { toDecoration(hint: ra.InlayHint, conv: lc.Protocol2CodeConverter): vscode.DecorationOptions; }; +interface InlayHintsStyles { + typeHints: InlayHintStyle; + paramHints: InlayHintStyle; + chainingHints: InlayHintStyle; +} + export function activateInlayHints(ctx: Ctx) { const maybeUpdater = { @@ -45,7 +51,7 @@ export function activateInlayHints(ctx: Ctx) { maybeUpdater.onConfigChange().catch(console.error); } -function createHintStyle(ctx: Ctx, hintKind: "type" | "parameter" | "chaining"): InlayHintStyle { +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] = ({ @@ -54,8 +60,6 @@ function createHintStyle(ctx: Ctx, 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 { @@ -77,14 +81,23 @@ function createHintStyle(ctx: Ctx, 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(); // map Uri -> RustSourceFile private readonly disposables: Disposable[] = []; - private inlayHintsStyles!: { - typeHints: InlayHintStyle; - paramHints: InlayHintStyle; - chainingHints: InlayHintStyle; - }; + private pendingDisposeDecorations: undefined | InlayHintsStyles = undefined; + private inlayHintsStyles!: InlayHintsStyles; constructor(private readonly ctx: Ctx) { vscode.window.onDidChangeVisibleTextEditors( @@ -125,14 +138,12 @@ class HintsUpdater implements Disposable { } 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"), - }; + const inlayHintsStyles = this.ctx.config.inlayHints.smallerHints ? smallHintsStyles : biggerHintsStyles; + + if (inlayHintsStyles !== this.inlayHintsStyles) { + this.pendingDisposeDecorations = this.inlayHintsStyles; + this.inlayHintsStyles = inlayHintsStyles; + } } syncCacheAndRenderHints() { @@ -183,6 +194,12 @@ 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); -- cgit v1.2.3