From c1cb5953820f26d4d0a614650bc8c50cbc5a3ce6 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Tue, 15 Dec 2020 15:37:37 +0100 Subject: Move to upstream `macro_rules!` model --- crates/ide/src/file_structure.rs | 14 ++---- crates/ide/src/references.rs | 2 +- crates/ide/src/syntax_highlighting.rs | 52 ++++++++++++---------- .../test_data/highlight_doctest.html | 2 +- .../test_data/highlight_strings.html | 6 +-- .../test_data/highlighting.html | 6 +-- 6 files changed, 41 insertions(+), 41 deletions(-) (limited to 'crates/ide') diff --git a/crates/ide/src/file_structure.rs b/crates/ide/src/file_structure.rs index 415795e8c..c51531391 100644 --- a/crates/ide/src/file_structure.rs +++ b/crates/ide/src/file_structure.rs @@ -150,13 +150,7 @@ fn structure_node(node: &SyntaxNode) -> Option { }; Some(node) }, - ast::MacroCall(it) => { - match it.path().and_then(|it| it.segment()).and_then(|it| it.name_ref()) { - Some(path_segment) if path_segment.text() == "macro_rules" - => decl(it), - _ => None, - } - }, + ast::MacroRules(it) => decl(it), _ => None, } } @@ -380,7 +374,7 @@ fn very_obsolete() {} label: "mc", navigation_range: 284..286, node_range: 271..303, - kind: MACRO_CALL, + kind: MACRO_RULES, detail: None, deprecated: false, }, @@ -389,7 +383,7 @@ fn very_obsolete() {} label: "mcexp", navigation_range: 334..339, node_range: 305..356, - kind: MACRO_CALL, + kind: MACRO_RULES, detail: None, deprecated: false, }, @@ -398,7 +392,7 @@ fn very_obsolete() {} label: "mcexp", navigation_range: 387..392, node_range: 358..409, - kind: MACRO_CALL, + kind: MACRO_RULES, detail: None, deprecated: false, }, diff --git a/crates/ide/src/references.rs b/crates/ide/src/references.rs index 66f0f7950..675957fff 100644 --- a/crates/ide/src/references.rs +++ b/crates/ide/src/references.rs @@ -770,7 +770,7 @@ fn foo() { } "#, expect![[r#" - m1 MACRO_CALL FileId(0) 0..46 29..31 Other + m1 MACRO_RULES FileId(0) 0..46 29..31 Other FileId(0) 63..65 StructLiteral FileId(0) 73..75 StructLiteral diff --git a/crates/ide/src/syntax_highlighting.rs b/crates/ide/src/syntax_highlighting.rs index f5c6eabef..990b0f7d9 100644 --- a/crates/ide/src/syntax_highlighting.rs +++ b/crates/ide/src/syntax_highlighting.rs @@ -74,6 +74,7 @@ pub(crate) fn highlight( let mut stack = HighlightedRangeStack::new(); let mut current_macro_call: Option = None; + let mut current_macro_rules: Option = None; let mut format_string_highlighter = FormatStringHighlighter::default(); let mut macro_rules_highlighter = MacroRulesHighlighter::default(); let mut inside_attribute = false; @@ -106,28 +107,26 @@ pub(crate) fn highlight( binding_hash: None, }); } - if let Some(name) = mc.is_macro_rules() { - macro_rules_highlighter.init(); - if let Some((highlight, binding_hash)) = highlight_element( - &sema, - &mut bindings_shadow_count, - syntactic_name_ref_highlighting, - name.syntax().clone().into(), - ) { - stack.add(HighlightedRange { - range: name.syntax().text_range(), - highlight, - binding_hash, - }); - } - } current_macro_call = Some(mc.clone()); continue; } WalkEvent::Leave(Some(mc)) => { - assert!(current_macro_call == Some(mc)); + assert_eq!(current_macro_call, Some(mc)); current_macro_call = None; format_string_highlighter = FormatStringHighlighter::default(); + } + _ => (), + } + + match event.clone().map(|it| it.into_node().and_then(ast::MacroRules::cast)) { + WalkEvent::Enter(Some(mac)) => { + macro_rules_highlighter.init(); + current_macro_rules = Some(mac); + continue; + } + WalkEvent::Leave(Some(mac)) => { + assert_eq!(current_macro_rules, Some(mac)); + current_macro_rules = None; macro_rules_highlighter = MacroRulesHighlighter::default(); } _ => (), @@ -163,6 +162,12 @@ pub(crate) fn highlight( let range = element.text_range(); + if current_macro_rules.is_some() { + if let Some(tok) = element.as_token() { + macro_rules_highlighter.advance(tok); + } + } + let element_to_highlight = if current_macro_call.is_some() && element.kind() != COMMENT { // Inside a macro -- expand it first let token = match element.clone().into_token() { @@ -173,9 +178,6 @@ pub(crate) fn highlight( let parent = token.parent(); format_string_highlighter.check_for_format_string(&parent); - if let Some(tok) = element.as_token() { - macro_rules_highlighter.advance(tok); - } // We only care Name and Name_ref match (token.kind(), parent.kind()) { @@ -386,10 +388,14 @@ impl HighlightedRangeStack { let mut res = self.stack.pop().unwrap(); res.sort_by_key(|range| range.range.start()); // Check that ranges are sorted and disjoint - assert!(res - .iter() - .zip(res.iter().skip(1)) - .all(|(left, right)| left.range.end() <= right.range.start())); + for (left, right) in res.iter().zip(res.iter().skip(1)) { + assert!( + left.range.end() <= right.range.start(), + "left: {:#?}, right: {:#?}", + left, + right + ); + } res } } diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html b/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html index 396fd46fb..920956b51 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_doctest.html @@ -95,7 +95,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd /// ``` /// noop!(1); /// ``` -macro_rules! noop { +macro_rules! noop { ($expr:expr) => { $expr } diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html b/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html index d398e1ec8..c843b5085 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html @@ -35,13 +35,13 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .unresolved_reference { color: #FC5555; text-decoration: wavy underline; } -
macro_rules! println {
+
macro_rules! println {
     ($($arg:tt)*) => ({
         $crate::io::_print($crate::format_args_nl!($($arg)*));
     })
 }
-#[rustc_builtin_macro]
-macro_rules! format_args_nl {
+#[rustc_builtin_macro]
+macro_rules! format_args_nl {
     ($fmt:expr) => {{ /* compiler built-in */ }};
     ($fmt:expr, $($args:tt)*) => {{ /* compiler built-in */ }};
 }
diff --git a/crates/ide/src/syntax_highlighting/test_data/highlighting.html b/crates/ide/src/syntax_highlighting/test_data/highlighting.html
index a3b4f20e8..0569cf1e5 100644
--- a/crates/ide/src/syntax_highlighting/test_data/highlighting.html
+++ b/crates/ide/src/syntax_highlighting/test_data/highlighting.html
@@ -125,7 +125,7 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
     let bar = foobar();
 }
 
-macro_rules! def_fn {
+macro_rules! def_fn {
     ($($tt:tt)*) => {$($tt)*}
 }
 
@@ -135,13 +135,13 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
     }
 }
 
-macro_rules! noop {
+macro_rules! noop {
     ($expr:expr) => {
         $expr
     }
 }
 
-macro_rules! keyword_frag {
+macro_rules! keyword_frag {
     ($type:ty) => ($type)
 }
 
-- 
cgit v1.2.3