Skip to content

Commit 3e1e3b9

Browse files
committed
Color attribute functions
1 parent cffa70b commit 3e1e3b9

File tree

7 files changed

+24
-4
lines changed

7 files changed

+24
-4
lines changed

crates/ra_ide/src/snapshots/highlighting.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
.keyword.unsafe { color: #BC8383; font-weight: bold; }
2828
.control { font-style: italic; }
2929
</style>
30-
<pre><code><span class="attribute">#[derive(Clone, Debug)]</span>
30+
<pre><code><span class="attribute">#[</span><span class="function attribute">derive</span><span class="attribute">(Clone, Debug)]</span>
3131
<span class="keyword">struct</span> <span class="struct declaration">Foo</span> {
3232
<span class="keyword">pub</span> <span class="field declaration">x</span>: <span class="builtin_type">i32</span>,
3333
<span class="keyword">pub</span> <span class="field declaration">y</span>: <span class="builtin_type">i32</span>,

crates/ra_ide/src/syntax_highlighting.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,9 @@ fn highlight_element(
361361
}
362362

363363
// Highlight references like the definitions they resolve to
364-
NAME_REF if element.ancestors().any(|it| it.kind() == ATTR) => return None,
364+
NAME_REF if element.ancestors().any(|it| it.kind() == ATTR) => {
365+
Highlight::from(HighlightTag::Function) | HighlightModifier::Attribute
366+
}
365367
NAME_REF => {
366368
let name_ref = element.into_node().and_then(ast::NameRef::cast).unwrap();
367369
match classify_name_ref(sema, &name_ref) {

crates/ra_ide/src/syntax_highlighting/tags.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ pub enum HighlightTag {
4545
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
4646
#[repr(u8)]
4747
pub enum HighlightModifier {
48+
/// Used to differentiate individual elements within attributes.
49+
Attribute = 0,
4850
/// Used with keywords like `if` and `break`.
49-
ControlFlow = 0,
51+
ControlFlow,
5052
/// `foo` in `fn foo(x: i32)` is a definition, `foo` in `foo(90 + 2)` is
5153
/// not.
5254
Definition,
@@ -95,6 +97,7 @@ impl fmt::Display for HighlightTag {
9597

9698
impl HighlightModifier {
9799
const ALL: &'static [HighlightModifier] = &[
100+
HighlightModifier::Attribute,
98101
HighlightModifier::ControlFlow,
99102
HighlightModifier::Definition,
100103
HighlightModifier::Mutable,
@@ -103,6 +106,7 @@ impl HighlightModifier {
103106

104107
fn as_str(self) -> &'static str {
105108
match self {
109+
HighlightModifier::Attribute => "attribute",
106110
HighlightModifier::ControlFlow => "control",
107111
HighlightModifier::Definition => "declaration",
108112
HighlightModifier::Mutable => "mutable",

crates/rust-analyzer/src/semantic_tokens.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ define_semantic_token_modifiers![
6767
(CONTROL_FLOW, "controlFlow"),
6868
(MUTABLE, "mutable"),
6969
(UNSAFE, "unsafe"),
70+
(ATTRIBUTE_MODIFIER, "attribute"),
7071
];
7172

7273
#[derive(Default)]

crates/rust-analyzer/src/to_proto.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ fn semantic_token_type_and_modifiers(
296296

297297
for modifier in highlight.modifiers.iter() {
298298
let modifier = match modifier {
299+
HighlightModifier::Attribute => semantic_tokens::ATTRIBUTE_MODIFIER,
299300
HighlightModifier::Definition => lsp_types::SemanticTokenModifier::DECLARATION,
300301
HighlightModifier::ControlFlow => semantic_tokens::CONTROL_FLOW,
301302
HighlightModifier::Mutable => semantic_tokens::MUTABLE,

editors/code/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,10 @@
583583
}
584584
],
585585
"semanticTokenModifiers": [
586+
{
587+
"id": "attribute",
588+
"description": "Style for elements within attributes"
589+
},
586590
{
587591
"id": "constant",
588592
"description": "Style for compile-time constants"
@@ -607,6 +611,9 @@
607611
"attribute": [
608612
"meta.attribute.rust"
609613
],
614+
"function.attribute": [
615+
"entity.name.function.attribute.rust"
616+
],
610617
"builtinType": [
611618
"support.type.primitive.rust"
612619
],

editors/code/rust.tmGrammar.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,13 @@
7575
{
7676
"comment": "Attribute",
7777
"name": "meta.attribute.rust",
78-
"begin": "#\\!?\\[",
78+
"begin": "#\\!?\\[(\\w*)",
7979
"end": "\\]",
80+
"captures": {
81+
"1": {
82+
"name": "entity.name.function.attribute.rust"
83+
}
84+
},
8085
"patterns": [
8186
{
8287
"include": "#string_literal"

0 commit comments

Comments
 (0)