mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 13:25:09 +00:00
Add option to opt out from smaller inlay hints font size
This commit is contained in:
parent
a2ba0f4846
commit
9e5ef0ce72
2 changed files with 30 additions and 6 deletions
|
@ -115,6 +115,7 @@ export class Config {
|
||||||
typeHints: this.get<boolean>("inlayHints.typeHints"),
|
typeHints: this.get<boolean>("inlayHints.typeHints"),
|
||||||
parameterHints: this.get<boolean>("inlayHints.parameterHints"),
|
parameterHints: this.get<boolean>("inlayHints.parameterHints"),
|
||||||
chainingHints: this.get<boolean>("inlayHints.chainingHints"),
|
chainingHints: this.get<boolean>("inlayHints.chainingHints"),
|
||||||
|
smallerHints: this.get<boolean>("inlayHints.smallerHints"),
|
||||||
maxLength: this.get<null | number>("inlayHints.maxLength"),
|
maxLength: this.get<null | number>("inlayHints.maxLength"),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,11 @@ import * as ra from './lsp_ext';
|
||||||
import { Ctx, Disposable } from './ctx';
|
import { Ctx, Disposable } from './ctx';
|
||||||
import { sendRequestWithRetry, isRustDocument, RustDocument, RustEditor, sleep } from './util';
|
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) {
|
export function activateInlayHints(ctx: Ctx) {
|
||||||
const maybeUpdater = {
|
const maybeUpdater = {
|
||||||
|
@ -19,6 +24,7 @@ export function activateInlayHints(ctx: Ctx) {
|
||||||
|
|
||||||
await sleep(100);
|
await sleep(100);
|
||||||
if (this.updater) {
|
if (this.updater) {
|
||||||
|
this.updater.updateInlayHintsStyles();
|
||||||
this.updater.syncCacheAndRenderHints();
|
this.updater.syncCacheAndRenderHints();
|
||||||
} else {
|
} else {
|
||||||
this.updater = new HintsUpdater(ctx);
|
this.updater = new HintsUpdater(ctx);
|
||||||
|
@ -39,11 +45,7 @@ export function activateInlayHints(ctx: Ctx) {
|
||||||
maybeUpdater.onConfigChange().catch(console.error);
|
maybeUpdater.onConfigChange().catch(console.error);
|
||||||
}
|
}
|
||||||
|
|
||||||
const typeHints = createHintStyle("type");
|
function createHintStyle(ctx: Ctx, hintKind: "type" | "parameter" | "chaining"): InlayHintStyle {
|
||||||
const paramHints = createHintStyle("parameter");
|
|
||||||
const chainingHints = createHintStyle("chaining");
|
|
||||||
|
|
||||||
function createHintStyle(hintKind: "type" | "parameter" | "chaining") {
|
|
||||||
// U+200C is a zero-width non-joiner to prevent the editor from forming a ligature
|
// U+200C is a zero-width non-joiner to prevent the editor from forming a ligature
|
||||||
// between code and type hints
|
// between code and type hints
|
||||||
const [pos, render] = ({
|
const [pos, render] = ({
|
||||||
|
@ -52,6 +54,8 @@ function createHintStyle(hintKind: "type" | "parameter" | "chaining") {
|
||||||
chaining: ["after", (label: string) => `\u{200c}: ${label}`],
|
chaining: ["after", (label: string) => `\u{200c}: ${label}`],
|
||||||
} as const)[hintKind];
|
} as const)[hintKind];
|
||||||
|
|
||||||
|
const smallerHints = ctx.config.inlayHints.smallerHints;
|
||||||
|
|
||||||
const fg = new vscode.ThemeColor(`rust_analyzer.inlayHints.foreground.${hintKind}Hints`);
|
const fg = new vscode.ThemeColor(`rust_analyzer.inlayHints.foreground.${hintKind}Hints`);
|
||||||
const bg = new vscode.ThemeColor(`rust_analyzer.inlayHints.background.${hintKind}Hints`);
|
const bg = new vscode.ThemeColor(`rust_analyzer.inlayHints.background.${hintKind}Hints`);
|
||||||
return {
|
return {
|
||||||
|
@ -61,7 +65,7 @@ function createHintStyle(hintKind: "type" | "parameter" | "chaining") {
|
||||||
backgroundColor: bg,
|
backgroundColor: bg,
|
||||||
fontStyle: "normal",
|
fontStyle: "normal",
|
||||||
fontWeight: "normal",
|
fontWeight: "normal",
|
||||||
textDecoration: ";font-size:smaller",
|
textDecoration: smallerHints ? ";font-size:smaller" : "none",
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
toDecoration(hint: ra.InlayHint, conv: lc.Protocol2CodeConverter): vscode.DecorationOptions {
|
toDecoration(hint: ra.InlayHint, conv: lc.Protocol2CodeConverter): vscode.DecorationOptions {
|
||||||
|
@ -76,6 +80,11 @@ function createHintStyle(hintKind: "type" | "parameter" | "chaining") {
|
||||||
class HintsUpdater implements Disposable {
|
class HintsUpdater implements Disposable {
|
||||||
private sourceFiles = new Map<string, RustSourceFile>(); // map Uri -> RustSourceFile
|
private sourceFiles = new Map<string, RustSourceFile>(); // map Uri -> RustSourceFile
|
||||||
private readonly disposables: Disposable[] = [];
|
private readonly disposables: Disposable[] = [];
|
||||||
|
private inlayHintsStyles!: {
|
||||||
|
typeHints: InlayHintStyle;
|
||||||
|
paramHints: InlayHintStyle;
|
||||||
|
chainingHints: InlayHintStyle;
|
||||||
|
};
|
||||||
|
|
||||||
constructor(private readonly ctx: Ctx) {
|
constructor(private readonly ctx: Ctx) {
|
||||||
vscode.window.onDidChangeVisibleTextEditors(
|
vscode.window.onDidChangeVisibleTextEditors(
|
||||||
|
@ -100,6 +109,7 @@ class HintsUpdater implements Disposable {
|
||||||
}
|
}
|
||||||
));
|
));
|
||||||
|
|
||||||
|
this.updateInlayHintsStyles();
|
||||||
this.syncCacheAndRenderHints();
|
this.syncCacheAndRenderHints();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -114,6 +124,17 @@ class HintsUpdater implements Disposable {
|
||||||
this.syncCacheAndRenderHints();
|
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() {
|
syncCacheAndRenderHints() {
|
||||||
this.sourceFiles.forEach((file, uri) => this.fetchHints(file).then(hints => {
|
this.sourceFiles.forEach((file, uri) => this.fetchHints(file).then(hints => {
|
||||||
if (!hints) return;
|
if (!hints) return;
|
||||||
|
@ -161,12 +182,14 @@ class HintsUpdater implements Disposable {
|
||||||
}
|
}
|
||||||
|
|
||||||
private renderDecorations(editor: RustEditor, decorations: InlaysDecorations) {
|
private renderDecorations(editor: RustEditor, decorations: InlaysDecorations) {
|
||||||
|
const { typeHints, paramHints, chainingHints } = this.inlayHintsStyles;
|
||||||
editor.setDecorations(typeHints.decorationType, decorations.type);
|
editor.setDecorations(typeHints.decorationType, decorations.type);
|
||||||
editor.setDecorations(paramHints.decorationType, decorations.param);
|
editor.setDecorations(paramHints.decorationType, decorations.param);
|
||||||
editor.setDecorations(chainingHints.decorationType, decorations.chaining);
|
editor.setDecorations(chainingHints.decorationType, decorations.chaining);
|
||||||
}
|
}
|
||||||
|
|
||||||
private hintsToDecorations(hints: ra.InlayHint[]): InlaysDecorations {
|
private hintsToDecorations(hints: ra.InlayHint[]): InlaysDecorations {
|
||||||
|
const { typeHints, paramHints, chainingHints } = this.inlayHintsStyles;
|
||||||
const decorations: InlaysDecorations = { type: [], param: [], chaining: [] };
|
const decorations: InlaysDecorations = { type: [], param: [], chaining: [] };
|
||||||
const conv = this.ctx.client.protocol2CodeConverter;
|
const conv = this.ctx.client.protocol2CodeConverter;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue