Skip to content

Commit d58a3a2

Browse files
bors[bot]Veetaha
andauthored
Merge #5885
5885: Make inlay hints colors more configurable r=matklad a=Veetaha **[BREAKING CHANGE]** Tackles #5337 (comment) and generally related to #5337. Added `foreground/background` color configurations with optional more specific overrides `foreground.(type|parameter|chaining)Hints`. One problem I see is that the config keys are long and don't fit into the on-hover hints in the `settings.json` file entirely... <details> <summary>Demo</summary> ![demo](https://user-images.githubusercontent.com/36276403/91238334-77fc3b00-e745-11ea-836b-2822015ece98.gif) </details> Co-authored-by: Veetaha <veetaha2@gmail.com>
2 parents ad60b4e + 5b9a882 commit d58a3a2

File tree

3 files changed

+117
-49
lines changed

3 files changed

+117
-49
lines changed

docs/user/manual.adoc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,30 @@ include::./generated_assists.adoc[]
367367

368368
== Editor Features
369369
=== VS Code
370+
371+
==== Color configurations
372+
373+
It is possible to change the foreground/background color of inlay hints. Just add this to your
374+
`settings.json`:
375+
376+
[source,jsonc]
377+
----
378+
{
379+
"workbench.colorCustomizations": {
380+
// Name of the theme you are currently using
381+
"[Default Dark+]": {
382+
"rust_analyzer.inlayHints.foreground": "#868686f0",
383+
"rust_analyzer.inlayHints.background": "#3d3d3d48",
384+
385+
// Overrides for specific kinds of inlay hints
386+
"rust_analyzer.inlayHints.foreground.typeHints": "#fdb6fdf0",
387+
"rust_analyzer.inlayHints.foreground.paramHints": "#fdb6fdf0",
388+
"rust_analyzer.inlayHints.background.chainingHints": "#6b0c0c81"
389+
}
390+
}
391+
}
392+
----
393+
370394
==== Special `when` clause context for keybindings.
371395
You may use `inRustProject` context to configure keybindings for rust projects only. For example:
372396
[source,json]

editors/code/package.json

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -712,14 +712,77 @@
712712
],
713713
"colors": [
714714
{
715-
"id": "rust_analyzer.inlayHint",
716-
"description": "Color for inlay hints",
715+
"id": "rust_analyzer.inlayHints.foreground",
716+
"description": "Foreground color of inlay hints (is overriden by more specific rust_analyzer.inlayHints.foreground.* configurations)",
717717
"defaults": {
718718
"dark": "#A0A0A0F0",
719719
"light": "#747474",
720720
"highContrast": "#BEBEBE"
721721
}
722722
},
723+
{
724+
"id": "rust_analyzer.inlayHints.background",
725+
"description": "Background color of inlay hints (is overriden by more specific rust_analyzer.inlayHints.background.* configurations)",
726+
"defaults": {
727+
"dark": "#11223300",
728+
"light": "#11223300",
729+
"highContrast": "#11223300"
730+
}
731+
},
732+
{
733+
"id": "rust_analyzer.inlayHints.foreground.typeHints",
734+
"description": "Foreground color of inlay type hints for variables (overrides rust_analyzer.inlayHints.foreground)",
735+
"defaults": {
736+
"dark": "rust_analyzer.inlayHints.foreground",
737+
"light": "rust_analyzer.inlayHints.foreground",
738+
"highContrast": "rust_analyzer.inlayHints.foreground"
739+
}
740+
},
741+
{
742+
"id": "rust_analyzer.inlayHints.foreground.chainingHints",
743+
"description": "Foreground color of inlay type hints for method chains (overrides rust_analyzer.inlayHints.foreground)",
744+
"defaults": {
745+
"dark": "rust_analyzer.inlayHints.foreground",
746+
"light": "rust_analyzer.inlayHints.foreground",
747+
"highContrast": "rust_analyzer.inlayHints.foreground"
748+
}
749+
},
750+
{
751+
"id": "rust_analyzer.inlayHints.foreground.parameterHints",
752+
"description": "Foreground color of function parameter name inlay hints at the call site (overrides rust_analyzer.inlayHints.foreground)",
753+
"defaults": {
754+
"dark": "rust_analyzer.inlayHints.foreground",
755+
"light": "rust_analyzer.inlayHints.foreground",
756+
"highContrast": "rust_analyzer.inlayHints.foreground"
757+
}
758+
},
759+
{
760+
"id": "rust_analyzer.inlayHints.background.typeHints",
761+
"description": "Background color of inlay type hints for variables (overrides rust_analyzer.inlayHints.background)",
762+
"defaults": {
763+
"dark": "rust_analyzer.inlayHints.background",
764+
"light": "rust_analyzer.inlayHints.background",
765+
"highContrast": "rust_analyzer.inlayHints.background"
766+
}
767+
},
768+
{
769+
"id": "rust_analyzer.inlayHints.background.chainingHints",
770+
"description": "Background color of inlay type hints for method chains (overrides rust_analyzer.inlayHints.background)",
771+
"defaults": {
772+
"dark": "rust_analyzer.inlayHints.background",
773+
"light": "rust_analyzer.inlayHints.background",
774+
"highContrast": "rust_analyzer.inlayHints.background"
775+
}
776+
},
777+
{
778+
"id": "rust_analyzer.inlayHints.background.parameterHints",
779+
"description": "Background color of function parameter name inlay hints at the call site (overrides rust_analyzer.inlayHints.background)",
780+
"defaults": {
781+
"dark": "rust_analyzer.inlayHints.background",
782+
"light": "rust_analyzer.inlayHints.background",
783+
"highContrast": "rust_analyzer.inlayHints.background"
784+
}
785+
},
723786
{
724787
"id": "rust_analyzer.syntaxTreeBorder",
725788
"description": "Color of the border displayed in the Rust source code for the selected syntax node (see \"Show Syntax Tree\" command)",

editors/code/src/inlay_hints.ts

Lines changed: 28 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -39,54 +39,35 @@ export function activateInlayHints(ctx: Ctx) {
3939
maybeUpdater.onConfigChange();
4040
}
4141

42-
43-
const typeHints = {
44-
decorationType: vscode.window.createTextEditorDecorationType({
45-
after: {
46-
color: new vscode.ThemeColor('rust_analyzer.inlayHint'),
47-
fontStyle: "normal",
48-
}
49-
}),
50-
51-
toDecoration(hint: ra.InlayHint.TypeHint, conv: lc.Protocol2CodeConverter): vscode.DecorationOptions {
52-
return {
53-
range: conv.asRange(hint.range),
54-
renderOptions: { after: { contentText: `: ${hint.label}` } }
55-
};
56-
}
57-
};
58-
59-
const paramHints = {
60-
decorationType: vscode.window.createTextEditorDecorationType({
61-
before: {
62-
color: new vscode.ThemeColor('rust_analyzer.inlayHint'),
63-
fontStyle: "normal",
64-
}
65-
}),
66-
67-
toDecoration(hint: ra.InlayHint.ParamHint, conv: lc.Protocol2CodeConverter): vscode.DecorationOptions {
68-
return {
69-
range: conv.asRange(hint.range),
70-
renderOptions: { before: { contentText: `${hint.label}: ` } }
71-
};
72-
}
73-
};
74-
75-
const chainingHints = {
76-
decorationType: vscode.window.createTextEditorDecorationType({
77-
after: {
78-
color: new vscode.ThemeColor('rust_analyzer.inlayHint'),
79-
fontStyle: "normal",
42+
const typeHints = createHintStyle("type");
43+
const paramHints = createHintStyle("parameter");
44+
const chainingHints = createHintStyle("chaining");
45+
46+
function createHintStyle(hintKind: "type" | "parameter" | "chaining") {
47+
const [pos, render] = ({
48+
type: ["after", (label: string) => `: ${label}`],
49+
parameter: ["before", (label: string) => `${label}: `],
50+
chaining: ["after", (label: string) => `: ${label}`],
51+
} as const)[hintKind];
52+
53+
const fg = new vscode.ThemeColor(`rust_analyzer.inlayHints.foreground.${hintKind}Hints`);
54+
const bg = new vscode.ThemeColor(`rust_analyzer.inlayHints.background.${hintKind}Hints`);
55+
return {
56+
decorationType: vscode.window.createTextEditorDecorationType({
57+
[pos]: {
58+
color: fg,
59+
backgroundColor: bg,
60+
fontStyle: "normal",
61+
},
62+
}),
63+
toDecoration(hint: ra.InlayHint, conv: lc.Protocol2CodeConverter): vscode.DecorationOptions {
64+
return {
65+
range: conv.asRange(hint.range),
66+
renderOptions: { [pos]: { contentText: render(hint.label) } }
67+
};
8068
}
81-
}),
82-
83-
toDecoration(hint: ra.InlayHint.ChainingHint, conv: lc.Protocol2CodeConverter): vscode.DecorationOptions {
84-
return {
85-
range: conv.asRange(hint.range),
86-
renderOptions: { after: { contentText: ` ${hint.label}` } }
87-
};
88-
}
89-
};
69+
};
70+
}
9071

9172
class HintsUpdater implements Disposable {
9273
private sourceFiles = new Map<string, RustSourceFile>(); // map Uri -> RustSourceFile

0 commit comments

Comments
 (0)