about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLukas Wirth <lukastw97@gmail.com>2021-09-30 19:12:37 +0200
committerLukas Wirth <lukastw97@gmail.com>2021-09-30 19:12:37 +0200
commita33939565d40590f92d113aa81c0fb9fd0e4fe55 (patch)
tree7c5859e6bca69124f1317b742dedde5d9e1dccd1
parent529b7a4167aab91257da02277c00e5cba245babb (diff)
downloadrust-a33939565d40590f92d113aa81c0fb9fd0e4fe55.tar.gz
rust-a33939565d40590f92d113aa81c0fb9fd0e4fe55.zip
Simplify
-rw-r--r--crates/ide/src/syntax_highlighting.rs52
1 files changed, 23 insertions, 29 deletions
diff --git a/crates/ide/src/syntax_highlighting.rs b/crates/ide/src/syntax_highlighting.rs
index 6d352942cab..b4331fc09f4 100644
--- a/crates/ide/src/syntax_highlighting.rs
+++ b/crates/ide/src/syntax_highlighting.rs
@@ -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();
         }
     }