This commit is contained in:
Lukas Wirth 2021-09-30 19:12:37 +02:00
parent 529b7a4167
commit a33939565d

View file

@ -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;
@ -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,
@ -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,
@ -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(),
}
}
@ -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;
}
}
}
}
@ -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() {
@ -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();
}
}