Skip to content

Color attribute functions #4474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/ra_ide/src/snapshots/highlighting.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
.keyword.unsafe { color: #BC8383; font-weight: bold; }
.control { font-style: italic; }
</style>
<pre><code><span class="attribute">#[derive(Clone, Debug)]</span>
<pre><code><span class="attribute">#[</span><span class="function attribute">derive</span><span class="attribute">(Clone, Debug)]</span>
<span class="keyword">struct</span> <span class="struct declaration">Foo</span> {
<span class="keyword">pub</span> <span class="field declaration">x</span>: <span class="builtin_type">i32</span>,
<span class="keyword">pub</span> <span class="field declaration">y</span>: <span class="builtin_type">i32</span>,
Expand Down
4 changes: 3 additions & 1 deletion crates/ra_ide/src/syntax_highlighting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,9 @@ fn highlight_element(
}

// Highlight references like the definitions they resolve to
NAME_REF if element.ancestors().any(|it| it.kind() == ATTR) => return None,
NAME_REF if element.ancestors().any(|it| it.kind() == ATTR) => {
Highlight::from(HighlightTag::Function) | HighlightModifier::Attribute
Copy link
Member

@matklad matklad May 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess, this gives a nicer looks, but semantically this feels wrong. Like, it's not a function, it's a part of attribute syntax.

Maybe we should introduce something like HighlightTag::AttrKey here? Not that it really matters though.

}
NAME_REF => {
let name_ref = element.into_node().and_then(ast::NameRef::cast).unwrap();
match classify_name_ref(sema, &name_ref) {
Expand Down
6 changes: 5 additions & 1 deletion crates/ra_ide/src/syntax_highlighting/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ pub enum HighlightTag {
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[repr(u8)]
pub enum HighlightModifier {
/// Used to differentiate individual elements within attributes.
Attribute = 0,
/// Used with keywords like `if` and `break`.
ControlFlow = 0,
ControlFlow,
/// `foo` in `fn foo(x: i32)` is a definition, `foo` in `foo(90 + 2)` is
/// not.
Definition,
Expand Down Expand Up @@ -95,6 +97,7 @@ impl fmt::Display for HighlightTag {

impl HighlightModifier {
const ALL: &'static [HighlightModifier] = &[
HighlightModifier::Attribute,
HighlightModifier::ControlFlow,
HighlightModifier::Definition,
HighlightModifier::Mutable,
Expand All @@ -103,6 +106,7 @@ impl HighlightModifier {

fn as_str(self) -> &'static str {
match self {
HighlightModifier::Attribute => "attribute",
HighlightModifier::ControlFlow => "control",
HighlightModifier::Definition => "declaration",
HighlightModifier::Mutable => "mutable",
Expand Down
1 change: 1 addition & 0 deletions crates/rust-analyzer/src/semantic_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ define_semantic_token_modifiers![
(CONTROL_FLOW, "controlFlow"),
(MUTABLE, "mutable"),
(UNSAFE, "unsafe"),
(ATTRIBUTE_MODIFIER, "attribute"),
];

#[derive(Default)]
Expand Down
1 change: 1 addition & 0 deletions crates/rust-analyzer/src/to_proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ fn semantic_token_type_and_modifiers(

for modifier in highlight.modifiers.iter() {
let modifier = match modifier {
HighlightModifier::Attribute => semantic_tokens::ATTRIBUTE_MODIFIER,
HighlightModifier::Definition => lsp_types::SemanticTokenModifier::DECLARATION,
HighlightModifier::ControlFlow => semantic_tokens::CONTROL_FLOW,
HighlightModifier::Mutable => semantic_tokens::MUTABLE,
Expand Down
7 changes: 7 additions & 0 deletions editors/code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,10 @@
}
],
"semanticTokenModifiers": [
{
"id": "attribute",
"description": "Style for elements within attributes"
},
{
"id": "constant",
"description": "Style for compile-time constants"
Expand Down Expand Up @@ -630,6 +634,9 @@
"attribute": [
"meta.attribute.rust"
],
"function.attribute": [
"entity.name.function.attribute.rust"
],
"builtinType": [
"support.type.primitive.rust"
],
Expand Down
7 changes: 6 additions & 1 deletion editors/code/rust.tmGrammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,13 @@
{
"comment": "Attribute",
"name": "meta.attribute.rust",
"begin": "#\\!?\\[",
"begin": "#\\!?\\[(\\w*)",
"end": "\\]",
"captures": {
"1": {
"name": "entity.name.function.attribute.rust"
}
},
"patterns": [
{
"include": "#string_literal"
Expand Down