Skip to content

Commit d959c91

Browse files
Merge #4556
4556: More highlighting improvements r=matthewjasper a=matthewjasper * Separate `true` and `false` from keywords (this matches the Textmate grammar). * Handle more cases in `highlight_name_by_syntax`. Co-authored-by: Matthew Jasper <mjjasper1@gmail.com>
2 parents 7e86262 + 1895888 commit d959c91

File tree

10 files changed

+33
-12
lines changed

10 files changed

+33
-12
lines changed

crates/ra_ide/src/snapshots/highlight_injection.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
.type_param { color: #DFAF8F; }
1818
.attribute { color: #94BFF3; }
1919
.numeric_literal { color: #BFEBBF; }
20+
.bool_literal { color: #BFE6EB; }
2021
.macro { color: #94BFF3; }
2122
.module { color: #AFD8AF; }
2223
.variable { color: #DCDCCC; }

crates/ra_ide/src/snapshots/highlight_strings.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
.type_param { color: #DFAF8F; }
1818
.attribute { color: #94BFF3; }
1919
.numeric_literal { color: #BFEBBF; }
20+
.bool_literal { color: #BFE6EB; }
2021
.macro { color: #94BFF3; }
2122
.module { color: #AFD8AF; }
2223
.variable { color: #DCDCCC; }

crates/ra_ide/src/snapshots/highlighting.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
.type_param { color: #DFAF8F; }
1818
.attribute { color: #94BFF3; }
1919
.numeric_literal { color: #BFEBBF; }
20+
.bool_literal { color: #BFE6EB; }
2021
.macro { color: #94BFF3; }
2122
.module { color: #AFD8AF; }
2223
.variable { color: #DCDCCC; }
@@ -64,7 +65,7 @@
6465
<span class="macro">println!</span>(<span class="string_literal">"Hello, {}!"</span>, <span class="numeric_literal">92</span>);
6566

6667
<span class="keyword">let</span> <span class="keyword">mut</span> <span class="variable declaration mutable">vec</span> = <span class="unresolved_reference">Vec</span>::<span class="unresolved_reference">new</span>();
67-
<span class="keyword control">if</span> <span class="keyword">true</span> {
68+
<span class="keyword control">if</span> <span class="bool_literal">true</span> {
6869
<span class="keyword">let</span> <span class="variable declaration">x</span> = <span class="numeric_literal">92</span>;
6970
<span class="variable mutable">vec</span>.<span class="unresolved_reference">push</span>(<span class="struct">Foo</span> { <span class="field">x</span>, <span class="field">y</span>: <span class="numeric_literal">1</span> });
7071
}

crates/ra_ide/src/snapshots/rainbow_highlighting.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
.type_param { color: #DFAF8F; }
1818
.attribute { color: #94BFF3; }
1919
.numeric_literal { color: #BFEBBF; }
20+
.bool_literal { color: #BFE6EB; }
2021
.macro { color: #94BFF3; }
2122
.module { color: #AFD8AF; }
2223
.variable { color: #DCDCCC; }

crates/ra_ide/src/syntax_highlighting.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ fn highlight_element(
413413
| T![in] => h | HighlightModifier::ControlFlow,
414414
T![for] if !is_child_of_impl(element) => h | HighlightModifier::ControlFlow,
415415
T![unsafe] => h | HighlightModifier::Unsafe,
416+
T![true] | T![false] => HighlightTag::BoolLiteral.into(),
416417
_ => h,
417418
}
418419
}
@@ -480,23 +481,31 @@ fn highlight_name(db: &RootDatabase, def: Definition) -> Highlight {
480481
}
481482

482483
fn highlight_name_by_syntax(name: ast::Name) -> Highlight {
483-
let default = HighlightTag::Function.into();
484+
let default = HighlightTag::UnresolvedReference;
484485

485486
let parent = match name.syntax().parent() {
486487
Some(it) => it,
487-
_ => return default,
488+
_ => return default.into(),
488489
};
489490

490-
match parent.kind() {
491-
STRUCT_DEF => HighlightTag::Struct.into(),
492-
ENUM_DEF => HighlightTag::Enum.into(),
493-
UNION_DEF => HighlightTag::Union.into(),
494-
TRAIT_DEF => HighlightTag::Trait.into(),
495-
TYPE_ALIAS_DEF => HighlightTag::TypeAlias.into(),
496-
TYPE_PARAM => HighlightTag::TypeParam.into(),
497-
RECORD_FIELD_DEF => HighlightTag::Field.into(),
491+
let tag = match parent.kind() {
492+
STRUCT_DEF => HighlightTag::Struct,
493+
ENUM_DEF => HighlightTag::Enum,
494+
UNION_DEF => HighlightTag::Union,
495+
TRAIT_DEF => HighlightTag::Trait,
496+
TYPE_ALIAS_DEF => HighlightTag::TypeAlias,
497+
TYPE_PARAM => HighlightTag::TypeParam,
498+
RECORD_FIELD_DEF => HighlightTag::Field,
499+
MODULE => HighlightTag::Module,
500+
FN_DEF => HighlightTag::Function,
501+
CONST_DEF => HighlightTag::Constant,
502+
STATIC_DEF => HighlightTag::Static,
503+
ENUM_VARIANT => HighlightTag::EnumVariant,
504+
BIND_PAT => HighlightTag::Local,
498505
_ => default,
499-
}
506+
};
507+
508+
tag.into()
500509
}
501510

502511
fn highlight_injection(

crates/ra_ide/src/syntax_highlighting/html.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
7676
.type_param { color: #DFAF8F; }
7777
.attribute { color: #94BFF3; }
7878
.numeric_literal { color: #BFEBBF; }
79+
.bool_literal { color: #BFE6EB; }
7980
.macro { color: #94BFF3; }
8081
.module { color: #AFD8AF; }
8182
.variable { color: #DCDCCC; }

crates/ra_ide/src/syntax_highlighting/tags.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub struct HighlightModifiers(u32);
1515
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
1616
pub enum HighlightTag {
1717
Attribute,
18+
BoolLiteral,
1819
BuiltinType,
1920
ByteLiteral,
2021
CharLiteral,
@@ -60,6 +61,7 @@ impl HighlightTag {
6061
fn as_str(self) -> &'static str {
6162
match self {
6263
HighlightTag::Attribute => "attribute",
64+
HighlightTag::BoolLiteral => "bool_literal",
6365
HighlightTag::BuiltinType => "builtin_type",
6466
HighlightTag::ByteLiteral => "byte_literal",
6567
HighlightTag::CharLiteral => "char_literal",

crates/rust-analyzer/src/semantic_tokens.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ macro_rules! define_semantic_token_types {
3636

3737
define_semantic_token_types![
3838
(ATTRIBUTE, "attribute"),
39+
(BOOLEAN, "boolean"),
3940
(BUILTIN_TYPE, "builtinType"),
4041
(ENUM_MEMBER, "enumMember"),
4142
(LIFETIME, "lifetime"),

crates/rust-analyzer/src/to_proto.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ fn semantic_token_type_and_modifiers(
295295
HighlightTag::ByteLiteral | HighlightTag::NumericLiteral => {
296296
lsp_types::SemanticTokenType::NUMBER
297297
}
298+
HighlightTag::BoolLiteral => semantic_tokens::BOOLEAN,
298299
HighlightTag::CharLiteral | HighlightTag::StringLiteral => {
299300
lsp_types::SemanticTokenType::STRING
300301
}

editors/code/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,9 @@
644644
"function.attribute": [
645645
"entity.name.function.attribute.rust"
646646
],
647+
"boolean": [
648+
"constant.language.boolean.rust"
649+
],
647650
"builtinType": [
648651
"support.type.primitive.rust"
649652
],

0 commit comments

Comments
 (0)