Skip to content

minor: Simplify #10404

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 2 commits into from
Sep 30, 2021
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
52 changes: 23 additions & 29 deletions crates/ide/src/syntax_highlighting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ fn traverse(
let mut bindings_shadow_count: FxHashMap<Name, u32> = FxHashMap::default();

let mut current_macro_call: Option<ast::MacroCall> = None;
let mut current_attr_macro_call = None;
let mut current_attr_call = None;
let mut current_macro: Option<ast::Macro> = None;
let mut macro_highlighter = MacroHighlighter::default();
let mut inside_attribute = false;
Expand Down Expand Up @@ -236,7 +236,7 @@ fn traverse(
},
ast::Item(item) => {
if sema.is_attr_macro_call(&item) {
current_attr_macro_call = Some(item);
current_attr_call = Some(item);
}
},
ast::Attr(__) => inside_attribute = true,
Expand All @@ -257,8 +257,8 @@ fn traverse(
macro_highlighter = MacroHighlighter::default();
},
ast::Item(item) => {
if current_attr_macro_call == Some(item) {
current_attr_macro_call = None;
if current_attr_call == Some(item) {
current_attr_call = None;
}
},
ast::Attr(__) => inside_attribute = false,
Expand Down Expand Up @@ -287,34 +287,28 @@ fn traverse(
}
}

let element_to_highlight = if current_macro_call.is_some() && element.kind() != COMMENT {
let descend_token = (current_macro_call.is_some() || current_attr_call.is_some())
&& element.kind() != COMMENT;
let element_to_highlight = if descend_token {
// Inside a macro -- expand it first
let token = match element.clone().into_token() {
Some(it) if it.parent().map_or(false, |it| it.kind() == TOKEN_TREE) => it,
_ => continue,
};
let token = sema.descend_into_macros(token.clone());
match token.parent() {
Some(parent) => {
// We only care Name and Name_ref
match (token.kind(), parent.kind()) {
(IDENT, NAME | NAME_REF) => parent.into(),
_ => token.into(),
Some(it) if current_macro_call.is_some() => {
let not_in_tt = it.parent().map_or(true, |it| it.kind() != TOKEN_TREE);
if not_in_tt {
continue;
}
it
}
None => token.into(),
}
} else if current_attr_macro_call.is_some() {
let token = match element.clone().into_token() {
Some(it) => it,
_ => continue,
};
let token = sema.descend_into_macros(token.clone());
let token = sema.descend_into_macros(token);
match token.parent() {
Some(parent) => {
// We only care Name and Name_ref
match (token.kind(), parent.kind()) {
(IDENT, NAME | NAME_REF) => parent.into(),
(T![ident], NAME | NAME_REF) => parent.into(),
(T![self] | T![super] | T![crate], NAME_REF) => parent.into(),
_ => token.into(),
}
}
Expand All @@ -324,11 +318,12 @@ fn traverse(
element.clone()
};

if let Some(token) = element.as_token().cloned().and_then(ast::String::cast) {
if let Some(token) = element.into_token().and_then(ast::String::cast) {
if token.is_raw() {
let expanded = element_to_highlight.as_token().unwrap().clone();
if inject::ra_fixture(hl, sema, token, expanded).is_some() {
continue;
if let Some(expanded) = element_to_highlight.as_token() {
if inject::ra_fixture(hl, sema, token, expanded.clone()).is_some() {
continue;
}
}
}
}
Expand All @@ -351,7 +346,7 @@ fn traverse(
hl.add(HlRange { range, highlight, binding_hash });
}

if let Some(string) = element_to_highlight.as_token().cloned().and_then(ast::String::cast) {
if let Some(string) = element_to_highlight.into_token().and_then(ast::String::cast) {
highlight_format_string(hl, &string, range);
// Highlight escape sequences
if let Some(char_ranges) = string.char_ranges() {
Expand All @@ -376,9 +371,8 @@ fn macro_call_range(macro_call: &ast::MacroCall) -> Option<TextRange> {
let range_start = name_ref.syntax().text_range().start();
let mut range_end = name_ref.syntax().text_range().end();
for sibling in path.syntax().siblings_with_tokens(Direction::Next) {
match sibling.kind() {
T![!] | IDENT => range_end = sibling.text_range().end(),
_ => (),
if let T![!] | T![ident] = sibling.kind() {
range_end = sibling.text_range().end();
}
}

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 @@ -81,6 +81,7 @@ macro_rules! define_semantic_token_modifiers {
SemanticTokenModifier::ABSTRACT,
SemanticTokenModifier::DEPRECATED,
SemanticTokenModifier::READONLY,
SemanticTokenModifier::DEFAULT_LIBRARY,
$($ident),*
];
};
Expand Down