10404: minor: Simplify r=Veykril a=Veykril

Fixes https://github.com/rust-analyzer/rust-analyzer/issues/10405
bors r+

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
bors[bot] 2021-09-30 18:38:39 +00:00 committed by GitHub
commit 8bf2d4fe62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 29 deletions

View File

@ -197,7 +197,7 @@ fn traverse(
let mut bindings_shadow_count: FxHashMap<Name, u32> = FxHashMap::default(); let mut bindings_shadow_count: FxHashMap<Name, u32> = FxHashMap::default();
let mut current_macro_call: Option<ast::MacroCall> = None; 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 current_macro: Option<ast::Macro> = None;
let mut macro_highlighter = MacroHighlighter::default(); let mut macro_highlighter = MacroHighlighter::default();
let mut inside_attribute = false; let mut inside_attribute = false;
@ -236,7 +236,7 @@ fn traverse(
}, },
ast::Item(item) => { ast::Item(item) => {
if sema.is_attr_macro_call(&item) { if sema.is_attr_macro_call(&item) {
current_attr_macro_call = Some(item); current_attr_call = Some(item);
} }
}, },
ast::Attr(__) => inside_attribute = true, ast::Attr(__) => inside_attribute = true,
@ -257,8 +257,8 @@ fn traverse(
macro_highlighter = MacroHighlighter::default(); macro_highlighter = MacroHighlighter::default();
}, },
ast::Item(item) => { ast::Item(item) => {
if current_attr_macro_call == Some(item) { if current_attr_call == Some(item) {
current_attr_macro_call = None; current_attr_call = None;
} }
}, },
ast::Attr(__) => inside_attribute = false, 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 // Inside a macro -- expand it first
let token = match element.clone().into_token() { let token = match element.clone().into_token() {
Some(it) if it.parent().map_or(false, |it| it.kind() == TOKEN_TREE) => it, Some(it) if current_macro_call.is_some() => {
_ => continue, let not_in_tt = it.parent().map_or(true, |it| it.kind() != TOKEN_TREE);
}; if not_in_tt {
let token = sema.descend_into_macros(token.clone()); continue;
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(),
} }
it
} }
None => token.into(),
}
} else if current_attr_macro_call.is_some() {
let token = match element.clone().into_token() {
Some(it) => it, Some(it) => it,
_ => continue, _ => continue,
}; };
let token = sema.descend_into_macros(token.clone()); let token = sema.descend_into_macros(token);
match token.parent() { match token.parent() {
Some(parent) => { Some(parent) => {
// We only care Name and Name_ref // We only care Name and Name_ref
match (token.kind(), parent.kind()) { 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(), _ => token.into(),
} }
} }
@ -324,11 +318,12 @@ fn traverse(
element.clone() 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() { if token.is_raw() {
let expanded = element_to_highlight.as_token().unwrap().clone(); if let Some(expanded) = element_to_highlight.as_token() {
if inject::ra_fixture(hl, sema, token, expanded).is_some() { if inject::ra_fixture(hl, sema, token, expanded.clone()).is_some() {
continue; continue;
}
} }
} }
} }
@ -351,7 +346,7 @@ fn traverse(
hl.add(HlRange { range, highlight, binding_hash }); 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_format_string(hl, &string, range);
// Highlight escape sequences // Highlight escape sequences
if let Some(char_ranges) = string.char_ranges() { 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 range_start = name_ref.syntax().text_range().start();
let mut range_end = name_ref.syntax().text_range().end(); let mut range_end = name_ref.syntax().text_range().end();
for sibling in path.syntax().siblings_with_tokens(Direction::Next) { for sibling in path.syntax().siblings_with_tokens(Direction::Next) {
match sibling.kind() { if let T![!] | T![ident] = sibling.kind() {
T![!] | IDENT => range_end = sibling.text_range().end(), range_end = sibling.text_range().end();
_ => (),
} }
} }

View File

@ -81,6 +81,7 @@ macro_rules! define_semantic_token_modifiers {
SemanticTokenModifier::ABSTRACT, SemanticTokenModifier::ABSTRACT,
SemanticTokenModifier::DEPRECATED, SemanticTokenModifier::DEPRECATED,
SemanticTokenModifier::READONLY, SemanticTokenModifier::READONLY,
SemanticTokenModifier::DEFAULT_LIBRARY,
$($ident),* $($ident),*
]; ];
}; };