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 = {
await sleep(100);
if (this.updater) {
+ this.updater.updateInlayHintsStyles();
this.updater.syncCacheAndRenderHints();
} else {
this.updater = new HintsUpdater(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] = ({
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 {
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 {
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(
}
));
+ this.updateInlayHintsStyles();
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() {
this.sourceFiles.forEach((file, uri) => this.fetchHints(file).then(hints => {
if (!hints) return;
}
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;