aboutsummaryrefslogtreecommitdiff
path: root/editors
diff options
context:
space:
mode:
authorJorge Mederos Alvarado <[email protected]>2021-04-21 20:48:57 +0100
committerJorge Mederos Alvarado <[email protected]>2021-04-27 01:29:54 +0100
commit9e5ef0ce723caa3270f9595ba738aa756a8804a9 (patch)
tree11eb0ba8ce2d23277c740c954f01b83d6ad2d079 /editors
parenta2ba0f48467aede5a051bb1e7ac0384d8f5b7e8f (diff)
Add option to opt out from smaller inlay hints font size
Diffstat (limited to 'editors')
-rw-r--r--editors/code/src/config.ts1
-rw-r--r--editors/code/src/inlay_hints.ts35
2 files changed, 30 insertions, 6 deletions
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 {
115 typeHints: this.get<boolean>("inlayHints.typeHints"), 115 typeHints: this.get<boolean>("inlayHints.typeHints"),
116 parameterHints: this.get<boolean>("inlayHints.parameterHints"), 116 parameterHints: this.get<boolean>("inlayHints.parameterHints"),
117 chainingHints: this.get<boolean>("inlayHints.chainingHints"), 117 chainingHints: this.get<boolean>("inlayHints.chainingHints"),
118 smallerHints: this.get<boolean>("inlayHints.smallerHints"),
118 maxLength: this.get<null | number>("inlayHints.maxLength"), 119 maxLength: this.get<null | number>("inlayHints.maxLength"),
119 }; 120 };
120 } 121 }
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';
5import { Ctx, Disposable } from './ctx'; 5import { Ctx, Disposable } from './ctx';
6import { sendRequestWithRetry, isRustDocument, RustDocument, RustEditor, sleep } from './util'; 6import { sendRequestWithRetry, isRustDocument, RustDocument, RustEditor, sleep } from './util';
7 7
8interface InlayHintStyle {
9 decorationType: vscode.TextEditorDecorationType;
10 toDecoration(hint: ra.InlayHint, conv: lc.Protocol2CodeConverter): vscode.DecorationOptions;
11};
12
8 13
9export function activateInlayHints(ctx: Ctx) { 14export function activateInlayHints(ctx: Ctx) {
10 const maybeUpdater = { 15 const maybeUpdater = {
@@ -19,6 +24,7 @@ export function activateInlayHints(ctx: Ctx) {
19 24
20 await sleep(100); 25 await sleep(100);
21 if (this.updater) { 26 if (this.updater) {
27 this.updater.updateInlayHintsStyles();
22 this.updater.syncCacheAndRenderHints(); 28 this.updater.syncCacheAndRenderHints();
23 } else { 29 } else {
24 this.updater = new HintsUpdater(ctx); 30 this.updater = new HintsUpdater(ctx);
@@ -39,11 +45,7 @@ export function activateInlayHints(ctx: Ctx) {
39 maybeUpdater.onConfigChange().catch(console.error); 45 maybeUpdater.onConfigChange().catch(console.error);
40} 46}
41 47
42const typeHints = createHintStyle("type"); 48function createHintStyle(ctx: Ctx, hintKind: "type" | "parameter" | "chaining"): InlayHintStyle {
43const paramHints = createHintStyle("parameter");
44const chainingHints = createHintStyle("chaining");
45
46function createHintStyle(hintKind: "type" | "parameter" | "chaining") {
47 // U+200C is a zero-width non-joiner to prevent the editor from forming a ligature 49 // U+200C is a zero-width non-joiner to prevent the editor from forming a ligature
48 // between code and type hints 50 // between code and type hints
49 const [pos, render] = ({ 51 const [pos, render] = ({
@@ -52,6 +54,8 @@ function createHintStyle(hintKind: "type" | "parameter" | "chaining") {
52 chaining: ["after", (label: string) => `\u{200c}: ${label}`], 54 chaining: ["after", (label: string) => `\u{200c}: ${label}`],
53 } as const)[hintKind]; 55 } as const)[hintKind];
54 56
57 const smallerHints = ctx.config.inlayHints.smallerHints;
58
55 const fg = new vscode.ThemeColor(`rust_analyzer.inlayHints.foreground.${hintKind}Hints`); 59 const fg = new vscode.ThemeColor(`rust_analyzer.inlayHints.foreground.${hintKind}Hints`);
56 const bg = new vscode.ThemeColor(`rust_analyzer.inlayHints.background.${hintKind}Hints`); 60 const bg = new vscode.ThemeColor(`rust_analyzer.inlayHints.background.${hintKind}Hints`);
57 return { 61 return {
@@ -61,7 +65,7 @@ function createHintStyle(hintKind: "type" | "parameter" | "chaining") {
61 backgroundColor: bg, 65 backgroundColor: bg,
62 fontStyle: "normal", 66 fontStyle: "normal",
63 fontWeight: "normal", 67 fontWeight: "normal",
64 textDecoration: ";font-size:smaller", 68 textDecoration: smallerHints ? ";font-size:smaller" : "none",
65 }, 69 },
66 }), 70 }),
67 toDecoration(hint: ra.InlayHint, conv: lc.Protocol2CodeConverter): vscode.DecorationOptions { 71 toDecoration(hint: ra.InlayHint, conv: lc.Protocol2CodeConverter): vscode.DecorationOptions {
@@ -76,6 +80,11 @@ function createHintStyle(hintKind: "type" | "parameter" | "chaining") {
76class HintsUpdater implements Disposable { 80class HintsUpdater implements Disposable {
77 private sourceFiles = new Map<string, RustSourceFile>(); // map Uri -> RustSourceFile 81 private sourceFiles = new Map<string, RustSourceFile>(); // map Uri -> RustSourceFile
78 private readonly disposables: Disposable[] = []; 82 private readonly disposables: Disposable[] = [];
83 private inlayHintsStyles!: {
84 typeHints: InlayHintStyle;
85 paramHints: InlayHintStyle;
86 chainingHints: InlayHintStyle;
87 };
79 88
80 constructor(private readonly ctx: Ctx) { 89 constructor(private readonly ctx: Ctx) {
81 vscode.window.onDidChangeVisibleTextEditors( 90 vscode.window.onDidChangeVisibleTextEditors(
@@ -100,6 +109,7 @@ class HintsUpdater implements Disposable {
100 } 109 }
101 )); 110 ));
102 111
112 this.updateInlayHintsStyles();
103 this.syncCacheAndRenderHints(); 113 this.syncCacheAndRenderHints();
104 } 114 }
105 115
@@ -114,6 +124,17 @@ class HintsUpdater implements Disposable {
114 this.syncCacheAndRenderHints(); 124 this.syncCacheAndRenderHints();
115 } 125 }
116 126
127 updateInlayHintsStyles() {
128 this.inlayHintsStyles?.typeHints.decorationType.dispose();
129 this.inlayHintsStyles?.paramHints.decorationType.dispose();
130 this.inlayHintsStyles?.chainingHints.decorationType.dispose();
131 this.inlayHintsStyles = {
132 typeHints: createHintStyle(this.ctx, "type"),
133 paramHints: createHintStyle(this.ctx, "parameter"),
134 chainingHints: createHintStyle(this.ctx, "chaining"),
135 };
136 }
137
117 syncCacheAndRenderHints() { 138 syncCacheAndRenderHints() {
118 this.sourceFiles.forEach((file, uri) => this.fetchHints(file).then(hints => { 139 this.sourceFiles.forEach((file, uri) => this.fetchHints(file).then(hints => {
119 if (!hints) return; 140 if (!hints) return;
@@ -161,12 +182,14 @@ class HintsUpdater implements Disposable {
161 } 182 }
162 183
163 private renderDecorations(editor: RustEditor, decorations: InlaysDecorations) { 184 private renderDecorations(editor: RustEditor, decorations: InlaysDecorations) {
185 const { typeHints, paramHints, chainingHints } = this.inlayHintsStyles;
164 editor.setDecorations(typeHints.decorationType, decorations.type); 186 editor.setDecorations(typeHints.decorationType, decorations.type);
165 editor.setDecorations(paramHints.decorationType, decorations.param); 187 editor.setDecorations(paramHints.decorationType, decorations.param);
166 editor.setDecorations(chainingHints.decorationType, decorations.chaining); 188 editor.setDecorations(chainingHints.decorationType, decorations.chaining);
167 } 189 }
168 190
169 private hintsToDecorations(hints: ra.InlayHint[]): InlaysDecorations { 191 private hintsToDecorations(hints: ra.InlayHint[]): InlaysDecorations {
192 const { typeHints, paramHints, chainingHints } = this.inlayHintsStyles;
170 const decorations: InlaysDecorations = { type: [], param: [], chaining: [] }; 193 const decorations: InlaysDecorations = { type: [], param: [], chaining: [] };
171 const conv = this.ctx.client.protocol2CodeConverter; 194 const conv = this.ctx.client.protocol2CodeConverter;
172 195