mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-15 01:17:27 +00:00
Merge #2559
2559: Add some granularity to syntax highlighting. r=matklad a=omerbenamram Hi, I wanted to start using `rust-analyzer` a bit more frequently - one of the main blockers for me so far was the highlighting. I just discovered it's possible to override the default colors with `ralsp.<something>` setting without waiting for #2061! However, the current implementation was lumping a bunch of different tokens into `type` and `literal`. The golden standard IMO is what Clion is currently doing (and is my current daily driver for rust). Clion allows users to control the coloring for specific literal kinds, and the default is to distinguish between them (numerics get a different color from strings, and special colors for bytestrings). I've also splitted the builtin types, which are also allowed to be highlighted speratly. My goal is to match the default experience I'm getting with clion. The only blockers now I think is that `rust-analyzer` doesn't corrently infer types in some situations, so the highlighting information is incorrect in those cases. This is what it looks like so far (with colors overriden to match clion's theme): ![image](https://user-images.githubusercontent.com/2467993/70848219-ccd97900-1e76-11ea-89e1-2e467cfcc9fb.png) If there are any other changes you feel is necessary let me know. I did leave the default colors to match the current behavior, since I'm not familiar with the colors for this theme, I added some random (different) colors in the test to check that it indeed was working. Co-authored-by: Omer Ben-Amram <omerbenamram@gmail.com>
This commit is contained in:
commit
3e8f9eb6c4
5 changed files with 98 additions and 18 deletions
|
@ -10,8 +10,10 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
|
|||
.builtin { color: #DD6718; }
|
||||
.text { color: #DCDCCC; }
|
||||
.type { color: #7CB8BB; }
|
||||
.type\.param { color: #20999D; }
|
||||
.attribute { color: #94BFF3; }
|
||||
.literal { color: #BFEBBF; }
|
||||
.literal\.numeric { color: #6A8759; }
|
||||
.macro { color: #94BFF3; }
|
||||
.variable { color: #DCDCCC; }
|
||||
.variable\.mut { color: #DCDCCC; text-decoration: underline; }
|
||||
|
@ -22,36 +24,36 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
|
|||
</style>
|
||||
<pre><code><span class="attribute">#</span><span class="attribute">[</span><span class="attribute">derive</span><span class="attribute">(</span><span class="attribute">Clone</span><span class="attribute">,</span><span class="attribute"> </span><span class="attribute">Debug</span><span class="attribute">)</span><span class="attribute">]</span>
|
||||
<span class="keyword">struct</span> <span class="type">Foo</span> {
|
||||
<span class="keyword">pub</span> <span class="field">x</span>: <span class="type">i32</span>,
|
||||
<span class="keyword">pub</span> <span class="field">y</span>: <span class="type">i32</span>,
|
||||
<span class="keyword">pub</span> <span class="field">x</span>: <span class="type.builtin">i32</span>,
|
||||
<span class="keyword">pub</span> <span class="field">y</span>: <span class="type.builtin">i32</span>,
|
||||
}
|
||||
|
||||
<span class="keyword">fn</span> <span class="function">foo</span><<span class="type">T</span>>() -> <span class="type">T</span> {
|
||||
<span class="keyword">fn</span> <span class="function">foo</span><<span class="type.param">T</span>>() -> <span class="type.param">T</span> {
|
||||
<span class="macro">unimplemented</span><span class="macro">!</span>();
|
||||
<span class="function">foo</span>::<<span class="type">i32</span>>();
|
||||
<span class="function">foo</span>::<<span class="type.builtin">i32</span>>();
|
||||
}
|
||||
|
||||
<span class="comment">// comment</span>
|
||||
<span class="keyword">fn</span> <span class="function">main</span>() {
|
||||
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"Hello, {}!"</span>, <span class="literal">92</span>);
|
||||
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"Hello, {}!"</span>, <span class="literal.numeric">92</span>);
|
||||
|
||||
<span class="keyword">let</span> <span class="keyword">mut</span> <span class="variable.mut">vec</span> = <span class="text">Vec</span>::<span class="text">new</span>();
|
||||
<span class="keyword.control">if</span> <span class="keyword">true</span> {
|
||||
<span class="variable.mut">vec</span>.<span class="text">push</span>(<span class="type">Foo</span> { <span class="field">x</span>: <span class="literal">0</span>, <span class="field">y</span>: <span class="literal">1</span> });
|
||||
<span class="variable.mut">vec</span>.<span class="text">push</span>(<span class="type">Foo</span> { <span class="field">x</span>: <span class="literal.numeric">0</span>, <span class="field">y</span>: <span class="literal.numeric">1</span> });
|
||||
}
|
||||
<span class="keyword.unsafe">unsafe</span> { <span class="variable.mut">vec</span>.<span class="text">set_len</span>(<span class="literal">0</span>); }
|
||||
<span class="keyword.unsafe">unsafe</span> { <span class="variable.mut">vec</span>.<span class="text">set_len</span>(<span class="literal.numeric">0</span>); }
|
||||
|
||||
<span class="keyword">let</span> <span class="keyword">mut</span> <span class="variable.mut">x</span> = <span class="literal">42</span>;
|
||||
<span class="keyword">let</span> <span class="keyword">mut</span> <span class="variable.mut">x</span> = <span class="literal.numeric">42</span>;
|
||||
<span class="keyword">let</span> <span class="variable.mut">y</span> = &<span class="keyword">mut</span> <span class="variable.mut">x</span>;
|
||||
<span class="keyword">let</span> <span class="variable">z</span> = &<span class="variable.mut">y</span>;
|
||||
|
||||
<span class="variable.mut">y</span>;
|
||||
}
|
||||
|
||||
<span class="keyword">enum</span> <span class="type">E</span><<span class="type">X</span>> {
|
||||
<span class="constant">V</span>(<span class="type">X</span>)
|
||||
<span class="keyword">enum</span> <span class="type">E</span><<span class="type.param">X</span>> {
|
||||
<span class="constant">V</span>(<span class="type.param">X</span>)
|
||||
}
|
||||
|
||||
<span class="keyword">impl</span><<span class="type">X</span>> <span class="type">E</span><<span class="type">X</span>> {
|
||||
<span class="keyword">fn</span> <span class="function">new</span><<span class="type">T</span>>() -> <span class="type">E</span><<span class="type">T</span>> {}
|
||||
<span class="keyword">impl</span><<span class="type.param">X</span>> <span class="type">E</span><<span class="type.param">X</span>> {
|
||||
<span class="keyword">fn</span> <span class="function">new</span><<span class="type.param">T</span>>() -> <span class="type">E</span><<span class="type.param">T</span>> {}
|
||||
}</code></pre>
|
|
@ -10,8 +10,10 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
|
|||
.builtin { color: #DD6718; }
|
||||
.text { color: #DCDCCC; }
|
||||
.type { color: #7CB8BB; }
|
||||
.type\.param { color: #20999D; }
|
||||
.attribute { color: #94BFF3; }
|
||||
.literal { color: #BFEBBF; }
|
||||
.literal\.numeric { color: #6A8759; }
|
||||
.macro { color: #94BFF3; }
|
||||
.variable { color: #DCDCCC; }
|
||||
.variable\.mut { color: #DCDCCC; text-decoration: underline; }
|
||||
|
|
|
@ -108,14 +108,17 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
|
|||
match name_kind {
|
||||
Some(name_kind) => highlight_name(db, name_kind),
|
||||
None => name.syntax().parent().map_or("function", |x| match x.kind() {
|
||||
TYPE_PARAM | STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_ALIAS_DEF => "type",
|
||||
STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_ALIAS_DEF => "type",
|
||||
TYPE_PARAM => "type.param",
|
||||
RECORD_FIELD_DEF => "field",
|
||||
_ => "function",
|
||||
}),
|
||||
}
|
||||
}
|
||||
INT_NUMBER | FLOAT_NUMBER | CHAR | BYTE => "literal",
|
||||
LIFETIME => "parameter",
|
||||
INT_NUMBER | FLOAT_NUMBER => "literal.numeric",
|
||||
BYTE => "literal.byte",
|
||||
CHAR => "literal.char",
|
||||
LIFETIME => "type.lifetime",
|
||||
T![unsafe] => "keyword.unsafe",
|
||||
k if is_control_keyword(k) => "keyword.control",
|
||||
k if k.is_keyword() => "keyword",
|
||||
|
@ -224,8 +227,9 @@ fn highlight_name(db: &RootDatabase, name_kind: NameKind) -> &'static str {
|
|||
Def(hir::ModuleDef::Static(_)) => "constant",
|
||||
Def(hir::ModuleDef::Trait(_)) => "type",
|
||||
Def(hir::ModuleDef::TypeAlias(_)) => "type",
|
||||
Def(hir::ModuleDef::BuiltinType(_)) => "type",
|
||||
SelfType(_) | TypeParam(_) => "type",
|
||||
Def(hir::ModuleDef::BuiltinType(_)) => "type.builtin",
|
||||
SelfType(_) => "type.self",
|
||||
TypeParam(_) => "type.param",
|
||||
Local(local) => {
|
||||
if local.is_mut(db) {
|
||||
"variable.mut"
|
||||
|
@ -255,8 +259,10 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
|
|||
.builtin { color: #DD6718; }
|
||||
.text { color: #DCDCCC; }
|
||||
.type { color: #7CB8BB; }
|
||||
.type\\.param { color: #20999D; }
|
||||
.attribute { color: #94BFF3; }
|
||||
.literal { color: #BFEBBF; }
|
||||
.literal\\.numeric { color: #6A8759; }
|
||||
.macro { color: #94BFF3; }
|
||||
.variable { color: #DCDCCC; }
|
||||
.variable\\.mut { color: #DCDCCC; text-decoration: underline; }
|
||||
|
|
|
@ -437,6 +437,33 @@
|
|||
"highContrast": "#B5CEA8"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "ralsp.literal.numeric",
|
||||
"description": "Color for numeric literals",
|
||||
"defaults": {
|
||||
"dark": "#BECEA8",
|
||||
"light": "#09885A",
|
||||
"highContrast": "#B5CEA8"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "ralsp.literal.char",
|
||||
"description": "Color for character literals",
|
||||
"defaults": {
|
||||
"dark": "#BECEA8",
|
||||
"light": "#09885A",
|
||||
"highContrast": "#B5CEA8"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "ralsp.literal.byte",
|
||||
"description": "Color for byte literals",
|
||||
"defaults": {
|
||||
"dark": "#BECEA8",
|
||||
"light": "#09885A",
|
||||
"highContrast": "#B5CEA8"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "ralsp.macro",
|
||||
"description": "Color for macros",
|
||||
|
@ -457,7 +484,43 @@
|
|||
},
|
||||
{
|
||||
"id": "ralsp.type",
|
||||
"description": "Color for types",
|
||||
"description": "Color for other types (traits, aliases..)",
|
||||
"defaults": {
|
||||
"dark": "#4EC9B0",
|
||||
"light": "#267F99",
|
||||
"highContrast": "#4EC9B0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "ralsp.type.builtin",
|
||||
"description": "Color for built-in types (&str, bool, u16, u32)",
|
||||
"defaults": {
|
||||
"dark": "#4EC9B0",
|
||||
"light": "#267F99",
|
||||
"highContrast": "#4EC9B0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "ralsp.type.lifetime",
|
||||
"description": "Color for `Self` param type",
|
||||
"defaults": {
|
||||
"dark": "#4EC9B0",
|
||||
"light": "#267F99",
|
||||
"highContrast": "#4EC9B0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "ralsp.type.self",
|
||||
"description": "Color for `Self` param type",
|
||||
"defaults": {
|
||||
"dark": "#4EC9B0",
|
||||
"light": "#267F99",
|
||||
"highContrast": "#4EC9B0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "ralsp.type.param",
|
||||
"description": "Color for type parameters",
|
||||
"defaults": {
|
||||
"dark": "#4EC9B0",
|
||||
"light": "#267F99",
|
||||
|
|
|
@ -53,10 +53,17 @@ export class Highlighter {
|
|||
decoration('parameter'),
|
||||
decoration('constant'),
|
||||
decoration('type'),
|
||||
decoration('type.self'),
|
||||
decoration('type.generic'),
|
||||
decoration('type.param'),
|
||||
decoration('type.lifetime'),
|
||||
decoration('builtin'),
|
||||
decoration('text'),
|
||||
decoration('attribute'),
|
||||
decoration('literal'),
|
||||
decoration('literal.numeric'),
|
||||
decoration('literal.char'),
|
||||
decoration('literal.byte'),
|
||||
decoration('macro'),
|
||||
decoration('variable'),
|
||||
decoration('variable.mut', 'underline'),
|
||||
|
|
Loading…
Reference in a new issue