aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdwin Cheng <[email protected]>2021-03-27 05:48:15 +0000
committerEdwin Cheng <[email protected]>2021-03-27 06:42:49 +0000
commit4520002b63b5a27e7676822aecfb2d435bf36e5a (patch)
treed59641111ce77db0117714180ae1d448056cf3c6
parenta193666361f6ea9725b927a35f5baf77da713c0a (diff)
Unleash macro 2.0 in hightlight and more
-rw-r--r--crates/hir/src/semantics.rs2
-rw-r--r--crates/hir/src/semantics/source_to_def.rs5
-rw-r--r--crates/ide/src/doc_links.rs2
-rw-r--r--crates/ide/src/file_structure.rs2
-rw-r--r--crates/ide/src/move_item.rs1
-rw-r--r--crates/ide/src/syntax_highlighting.rs28
-rw-r--r--crates/ide/src/syntax_highlighting/inject.rs3
-rw-r--r--crates/ide/src/syntax_highlighting/macro_.rs (renamed from crates/ide/src/syntax_highlighting/macro_rules.rs)10
-rw-r--r--crates/ide/src/syntax_highlighting/test_data/highlighting.html12
-rw-r--r--crates/ide/src/syntax_highlighting/tests.rs10
-rw-r--r--crates/ide_db/src/defs.rs2
-rw-r--r--crates/ide_db/src/symbol_index.rs3
12 files changed, 49 insertions, 31 deletions
diff --git a/crates/hir/src/semantics.rs b/crates/hir/src/semantics.rs
index 3ff135f41..d3caeef4e 100644
--- a/crates/hir/src/semantics.rs
+++ b/crates/hir/src/semantics.rs
@@ -768,7 +768,7 @@ to_def_impls![
768 (crate::TypeParam, ast::TypeParam, type_param_to_def), 768 (crate::TypeParam, ast::TypeParam, type_param_to_def),
769 (crate::LifetimeParam, ast::LifetimeParam, lifetime_param_to_def), 769 (crate::LifetimeParam, ast::LifetimeParam, lifetime_param_to_def),
770 (crate::ConstParam, ast::ConstParam, const_param_to_def), 770 (crate::ConstParam, ast::ConstParam, const_param_to_def),
771 (crate::MacroDef, ast::MacroRules, macro_rules_to_def), 771 (crate::MacroDef, ast::Macro, macro_to_def),
772 (crate::Local, ast::IdentPat, bind_pat_to_def), 772 (crate::Local, ast::IdentPat, bind_pat_to_def),
773 (crate::Local, ast::SelfParam, self_param_to_def), 773 (crate::Local, ast::SelfParam, self_param_to_def),
774 (crate::Label, ast::Label, label_to_def), 774 (crate::Label, ast::Label, label_to_def),
diff --git a/crates/hir/src/semantics/source_to_def.rs b/crates/hir/src/semantics/source_to_def.rs
index 762809fcd..9a5a2255f 100644
--- a/crates/hir/src/semantics/source_to_def.rs
+++ b/crates/hir/src/semantics/source_to_def.rs
@@ -191,10 +191,7 @@ impl SourceToDefCtx<'_, '_> {
191 } 191 }
192 192
193 // FIXME: use DynMap as well? 193 // FIXME: use DynMap as well?
194 pub(super) fn macro_rules_to_def( 194 pub(super) fn macro_to_def(&mut self, src: InFile<ast::Macro>) -> Option<MacroDefId> {
195 &mut self,
196 src: InFile<ast::MacroRules>,
197 ) -> Option<MacroDefId> {
198 let file_ast_id = self.db.ast_id_map(src.file_id).ast_id(&src.value); 195 let file_ast_id = self.db.ast_id_map(src.file_id).ast_id(&src.value);
199 let ast_id = AstId::new(src.file_id, file_ast_id.upcast()); 196 let ast_id = AstId::new(src.file_id, file_ast_id.upcast());
200 let kind = MacroDefKind::Declarative(ast_id); 197 let kind = MacroDefKind::Declarative(ast_id);
diff --git a/crates/ide/src/doc_links.rs b/crates/ide/src/doc_links.rs
index 0cee741ac..9301cdeff 100644
--- a/crates/ide/src/doc_links.rs
+++ b/crates/ide/src/doc_links.rs
@@ -161,7 +161,7 @@ pub(crate) fn doc_owner_to_def(
161 ast::Variant(it) => sema.to_def(&it)?.into(), 161 ast::Variant(it) => sema.to_def(&it)?.into(),
162 ast::Trait(it) => sema.to_def(&it)?.into(), 162 ast::Trait(it) => sema.to_def(&it)?.into(),
163 ast::Impl(it) => return sema.to_def(&it).map(Definition::SelfType), 163 ast::Impl(it) => return sema.to_def(&it).map(Definition::SelfType),
164 ast::MacroRules(it) => return sema.to_def(&it).map(Definition::Macro), 164 ast::Macro(it) => return sema.to_def(&it).map(Definition::Macro),
165 ast::TupleField(it) => return sema.to_def(&it).map(Definition::Field), 165 ast::TupleField(it) => return sema.to_def(&it).map(Definition::Field),
166 ast::RecordField(it) => return sema.to_def(&it).map(Definition::Field), 166 ast::RecordField(it) => return sema.to_def(&it).map(Definition::Field),
167 _ => return None, 167 _ => return None,
diff --git a/crates/ide/src/file_structure.rs b/crates/ide/src/file_structure.rs
index 9f879a66e..2c898eae8 100644
--- a/crates/ide/src/file_structure.rs
+++ b/crates/ide/src/file_structure.rs
@@ -172,7 +172,7 @@ fn structure_node(node: &SyntaxNode) -> Option<StructureNode> {
172 }; 172 };
173 Some(node) 173 Some(node)
174 }, 174 },
175 ast::MacroRules(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Macro)), 175 ast::Macro(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Macro)),
176 _ => None, 176 _ => None,
177 } 177 }
178 } 178 }
diff --git a/crates/ide/src/move_item.rs b/crates/ide/src/move_item.rs
index 48690b073..05fa8fc13 100644
--- a/crates/ide/src/move_item.rs
+++ b/crates/ide/src/move_item.rs
@@ -66,6 +66,7 @@ fn find_ancestors(item: SyntaxElement, direction: Direction, range: TextRange) -
66 SyntaxKind::STATIC, 66 SyntaxKind::STATIC,
67 SyntaxKind::CONST, 67 SyntaxKind::CONST,
68 SyntaxKind::MACRO_RULES, 68 SyntaxKind::MACRO_RULES,
69 SyntaxKind::MACRO_DEF,
69 ]; 70 ];
70 71
71 let ancestor = once(root.clone()) 72 let ancestor = once(root.clone())
diff --git a/crates/ide/src/syntax_highlighting.rs b/crates/ide/src/syntax_highlighting.rs
index e25b698e0..67a10766b 100644
--- a/crates/ide/src/syntax_highlighting.rs
+++ b/crates/ide/src/syntax_highlighting.rs
@@ -5,7 +5,7 @@ mod injector;
5 5
6mod highlight; 6mod highlight;
7mod format; 7mod format;
8mod macro_rules; 8mod macro_;
9mod inject; 9mod inject;
10 10
11mod html; 11mod html;
@@ -24,8 +24,8 @@ use syntax::{
24 24
25use crate::{ 25use crate::{
26 syntax_highlighting::{ 26 syntax_highlighting::{
27 format::highlight_format_string, highlights::Highlights, 27 format::highlight_format_string, highlights::Highlights, macro_::MacroHighlighter,
28 macro_rules::MacroRulesHighlighter, tags::Highlight, 28 tags::Highlight,
29 }, 29 },
30 FileId, HlMod, HlTag, 30 FileId, HlMod, HlTag,
31}; 31};
@@ -93,8 +93,8 @@ fn traverse(
93 let mut bindings_shadow_count: FxHashMap<Name, u32> = FxHashMap::default(); 93 let mut bindings_shadow_count: FxHashMap<Name, u32> = FxHashMap::default();
94 94
95 let mut current_macro_call: Option<ast::MacroCall> = None; 95 let mut current_macro_call: Option<ast::MacroCall> = None;
96 let mut current_macro_rules: Option<ast::MacroRules> = None; 96 let mut current_macro: Option<ast::Macro> = None;
97 let mut macro_rules_highlighter = MacroRulesHighlighter::default(); 97 let mut macro_highlighter = MacroHighlighter::default();
98 let mut inside_attribute = false; 98 let mut inside_attribute = false;
99 99
100 // Walk all nodes, keeping track of whether we are inside a macro or not. 100 // Walk all nodes, keeping track of whether we are inside a macro or not.
@@ -129,16 +129,16 @@ fn traverse(
129 _ => (), 129 _ => (),
130 } 130 }
131 131
132 match event.clone().map(|it| it.into_node().and_then(ast::MacroRules::cast)) { 132 match event.clone().map(|it| it.into_node().and_then(ast::Macro::cast)) {
133 WalkEvent::Enter(Some(mac)) => { 133 WalkEvent::Enter(Some(mac)) => {
134 macro_rules_highlighter.init(); 134 macro_highlighter.init();
135 current_macro_rules = Some(mac); 135 current_macro = Some(mac);
136 continue; 136 continue;
137 } 137 }
138 WalkEvent::Leave(Some(mac)) => { 138 WalkEvent::Leave(Some(mac)) => {
139 assert_eq!(current_macro_rules, Some(mac)); 139 assert_eq!(current_macro, Some(mac));
140 current_macro_rules = None; 140 current_macro = None;
141 macro_rules_highlighter = MacroRulesHighlighter::default(); 141 macro_highlighter = MacroHighlighter::default();
142 } 142 }
143 _ => (), 143 _ => (),
144 } 144 }
@@ -164,9 +164,9 @@ fn traverse(
164 164
165 let range = element.text_range(); 165 let range = element.text_range();
166 166
167 if current_macro_rules.is_some() { 167 if current_macro.is_some() {
168 if let Some(tok) = element.as_token() { 168 if let Some(tok) = element.as_token() {
169 macro_rules_highlighter.advance(tok); 169 macro_highlighter.advance(tok);
170 } 170 }
171 } 171 }
172 172
@@ -200,7 +200,7 @@ fn traverse(
200 } 200 }
201 } 201 }
202 202
203 if let Some(_) = macro_rules_highlighter.highlight(element_to_highlight.clone()) { 203 if let Some(_) = macro_highlighter.highlight(element_to_highlight.clone()) {
204 continue; 204 continue;
205 } 205 }
206 206
diff --git a/crates/ide/src/syntax_highlighting/inject.rs b/crates/ide/src/syntax_highlighting/inject.rs
index 38bf49348..963c3fb59 100644
--- a/crates/ide/src/syntax_highlighting/inject.rs
+++ b/crates/ide/src/syntax_highlighting/inject.rs
@@ -109,8 +109,7 @@ fn doc_attributes<'node>(
109 ast::Impl(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::SelfType(def))), 109 ast::Impl(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::SelfType(def))),
110 ast::RecordField(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Field(def))), 110 ast::RecordField(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Field(def))),
111 ast::TupleField(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Field(def))), 111 ast::TupleField(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Field(def))),
112 ast::MacroRules(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Macro(def))), 112 ast::Macro(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Macro(def))),
113 // ast::MacroDef(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))),
114 // ast::Use(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))), 113 // ast::Use(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))),
115 _ => return None 114 _ => return None
116 } 115 }
diff --git a/crates/ide/src/syntax_highlighting/macro_rules.rs b/crates/ide/src/syntax_highlighting/macro_.rs
index 44620e912..819704294 100644
--- a/crates/ide/src/syntax_highlighting/macro_rules.rs
+++ b/crates/ide/src/syntax_highlighting/macro_.rs
@@ -4,18 +4,18 @@ use syntax::{SyntaxElement, SyntaxKind, SyntaxToken, TextRange, T};
4use crate::{HlRange, HlTag}; 4use crate::{HlRange, HlTag};
5 5
6#[derive(Default)] 6#[derive(Default)]
7pub(super) struct MacroRulesHighlighter { 7pub(super) struct MacroHighlighter {
8 state: Option<MacroMatcherParseState>, 8 state: Option<MacroMatcherParseState>,
9} 9}
10 10
11impl MacroRulesHighlighter { 11impl MacroHighlighter {
12 pub(super) fn init(&mut self) { 12 pub(super) fn init(&mut self) {
13 self.state = Some(MacroMatcherParseState::default()); 13 self.state = Some(MacroMatcherParseState::default());
14 } 14 }
15 15
16 pub(super) fn advance(&mut self, token: &SyntaxToken) { 16 pub(super) fn advance(&mut self, token: &SyntaxToken) {
17 if let Some(state) = self.state.as_mut() { 17 if let Some(state) = self.state.as_mut() {
18 update_macro_rules_state(state, token); 18 update_macro_state(state, token);
19 } 19 }
20 } 20 }
21 21
@@ -74,9 +74,9 @@ impl RuleState {
74 } 74 }
75} 75}
76 76
77fn update_macro_rules_state(state: &mut MacroMatcherParseState, tok: &SyntaxToken) { 77fn update_macro_state(state: &mut MacroMatcherParseState, tok: &SyntaxToken) {
78 if !state.in_invoc_body { 78 if !state.in_invoc_body {
79 if tok.kind() == T!['{'] { 79 if tok.kind() == T!['{'] || tok.kind() == T!['('] {
80 state.in_invoc_body = true; 80 state.in_invoc_body = true;
81 } 81 }
82 return; 82 return;
diff --git a/crates/ide/src/syntax_highlighting/test_data/highlighting.html b/crates/ide/src/syntax_highlighting/test_data/highlighting.html
index 8b2dd3b70..1eaa7b75b 100644
--- a/crates/ide/src/syntax_highlighting/test_data/highlighting.html
+++ b/crates/ide/src/syntax_highlighting/test_data/highlighting.html
@@ -41,7 +41,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
41<span class="keyword">mod</span> <span class="module declaration">inner</span> <span class="brace">{</span><span class="brace">}</span> 41<span class="keyword">mod</span> <span class="module declaration">inner</span> <span class="brace">{</span><span class="brace">}</span>
42 42
43<span class="attribute attribute">#</span><span class="attribute attribute">[</span><span class="function attribute">rustc_builtin_macro</span><span class="attribute attribute">]</span> 43<span class="attribute attribute">#</span><span class="attribute attribute">[</span><span class="function attribute">rustc_builtin_macro</span><span class="attribute attribute">]</span>
44<span class="keyword">macro</span> <span class="unresolved_reference declaration">Copy</span> <span class="brace">{</span><span class="brace">}</span> 44<span class="keyword">macro</span> <span class="macro declaration">Copy</span> <span class="brace">{</span><span class="brace">}</span>
45 45
46<span class="comment">// Needed for function consuming vs normal</span> 46<span class="comment">// Needed for function consuming vs normal</span>
47<span class="keyword">pub</span> <span class="keyword">mod</span> <span class="module declaration">marker</span> <span class="brace">{</span> 47<span class="keyword">pub</span> <span class="keyword">mod</span> <span class="module declaration">marker</span> <span class="brace">{</span>
@@ -158,6 +158,16 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
158 <span class="parenthesis">(</span><span class="punctuation">$</span>type<span class="colon">:</span>ty<span class="parenthesis">)</span> <span class="operator">=</span><span class="angle">&gt;</span> <span class="parenthesis">(</span><span class="punctuation">$</span>type<span class="parenthesis">)</span> 158 <span class="parenthesis">(</span><span class="punctuation">$</span>type<span class="colon">:</span>ty<span class="parenthesis">)</span> <span class="operator">=</span><span class="angle">&gt;</span> <span class="parenthesis">(</span><span class="punctuation">$</span>type<span class="parenthesis">)</span>
159<span class="brace">}</span> 159<span class="brace">}</span>
160 160
161<span class="keyword">macro</span> <span class="macro declaration">with_args</span><span class="parenthesis">(</span><span class="punctuation">$</span>i<span class="colon">:</span>ident<span class="parenthesis">)</span> <span class="brace">{</span>
162 <span class="punctuation">$</span>i
163<span class="brace">}</span>
164
165<span class="keyword">macro</span> <span class="macro declaration">without_args</span> <span class="brace">{</span>
166 <span class="parenthesis">(</span><span class="punctuation">$</span>i<span class="colon">:</span>ident<span class="parenthesis">)</span> <span class="operator">=</span><span class="angle">&gt;</span> <span class="brace">{</span>
167 <span class="punctuation">$</span>i
168 <span class="brace">}</span>
169<span class="brace">}</span>
170
161<span class="comment">// comment</span> 171<span class="comment">// comment</span>
162<span class="keyword">fn</span> <span class="function declaration">main</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span> 172<span class="keyword">fn</span> <span class="function declaration">main</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span>
163 <span class="macro">println!</span><span class="parenthesis">(</span><span class="string_literal">"Hello, {}!"</span><span class="comma">,</span> <span class="numeric_literal">92</span><span class="parenthesis">)</span><span class="semicolon">;</span> 173 <span class="macro">println!</span><span class="parenthesis">(</span><span class="string_literal">"Hello, {}!"</span><span class="comma">,</span> <span class="numeric_literal">92</span><span class="parenthesis">)</span><span class="semicolon">;</span>
diff --git a/crates/ide/src/syntax_highlighting/tests.rs b/crates/ide/src/syntax_highlighting/tests.rs
index 7b2922b0d..369ae0972 100644
--- a/crates/ide/src/syntax_highlighting/tests.rs
+++ b/crates/ide/src/syntax_highlighting/tests.rs
@@ -129,6 +129,16 @@ macro_rules! keyword_frag {
129 ($type:ty) => ($type) 129 ($type:ty) => ($type)
130} 130}
131 131
132macro with_args($i:ident) {
133 $i
134}
135
136macro without_args {
137 ($i:ident) => {
138 $i
139 }
140}
141
132// comment 142// comment
133fn main() { 143fn main() {
134 println!("Hello, {}!", 92); 144 println!("Hello, {}!", 92);
diff --git a/crates/ide_db/src/defs.rs b/crates/ide_db/src/defs.rs
index ab23dd7ac..0d9808d24 100644
--- a/crates/ide_db/src/defs.rs
+++ b/crates/ide_db/src/defs.rs
@@ -227,7 +227,7 @@ impl NameClass {
227 let def: hir::TypeAlias = sema.to_def(&it)?; 227 let def: hir::TypeAlias = sema.to_def(&it)?;
228 Some(NameClass::Definition(Definition::ModuleDef(def.into()))) 228 Some(NameClass::Definition(Definition::ModuleDef(def.into())))
229 }, 229 },
230 ast::MacroRules(it) => { 230 ast::Macro(it) => {
231 let def = sema.to_def(&it)?; 231 let def = sema.to_def(&it)?;
232 Some(NameClass::Definition(Definition::Macro(def))) 232 Some(NameClass::Definition(Definition::Macro(def)))
233 }, 233 },
diff --git a/crates/ide_db/src/symbol_index.rs b/crates/ide_db/src/symbol_index.rs
index 35e382b5c..da427d686 100644
--- a/crates/ide_db/src/symbol_index.rs
+++ b/crates/ide_db/src/symbol_index.rs
@@ -438,7 +438,7 @@ fn to_symbol(node: &SyntaxNode) -> Option<(SmolStr, SyntaxNodePtr, TextRange)> {
438 ast::TypeAlias(it) => decl(it), 438 ast::TypeAlias(it) => decl(it),
439 ast::Const(it) => decl(it), 439 ast::Const(it) => decl(it),
440 ast::Static(it) => decl(it), 440 ast::Static(it) => decl(it),
441 ast::MacroRules(it) => decl(it), 441 ast::Macro(it) => decl(it),
442 ast::Union(it) => decl(it), 442 ast::Union(it) => decl(it),
443 _ => None, 443 _ => None,
444 } 444 }
@@ -458,6 +458,7 @@ fn to_file_symbol(node: &SyntaxNode, file_id: FileId) -> Option<FileSymbol> {
458 CONST => FileSymbolKind::Const, 458 CONST => FileSymbolKind::Const,
459 STATIC => FileSymbolKind::Static, 459 STATIC => FileSymbolKind::Static,
460 MACRO_RULES => FileSymbolKind::Macro, 460 MACRO_RULES => FileSymbolKind::Macro,
461 MACRO_DEF => FileSymbolKind::Macro,
461 UNION => FileSymbolKind::Union, 462 UNION => FileSymbolKind::Union,
462 kind => unreachable!("{:?}", kind), 463 kind => unreachable!("{:?}", kind),
463 }, 464 },