Skip to content

Commit 4520002

Browse files
committed
Unleash macro 2.0 in hightlight and more
1 parent a193666 commit 4520002

File tree

12 files changed

+49
-31
lines changed

12 files changed

+49
-31
lines changed

crates/hir/src/semantics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ to_def_impls![
768768
(crate::TypeParam, ast::TypeParam, type_param_to_def),
769769
(crate::LifetimeParam, ast::LifetimeParam, lifetime_param_to_def),
770770
(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),
772772
(crate::Local, ast::IdentPat, bind_pat_to_def),
773773
(crate::Local, ast::SelfParam, self_param_to_def),
774774
(crate::Label, ast::Label, label_to_def),

crates/hir/src/semantics/source_to_def.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,7 @@ impl SourceToDefCtx<'_, '_> {
191191
}
192192

193193
// FIXME: use DynMap as well?
194-
pub(super) fn macro_rules_to_def(
195-
&mut self,
196-
src: InFile<ast::MacroRules>,
197-
) -> Option<MacroDefId> {
194+
pub(super) fn macro_to_def(&mut self, src: InFile<ast::Macro>) -> Option<MacroDefId> {
198195
let file_ast_id = self.db.ast_id_map(src.file_id).ast_id(&src.value);
199196
let ast_id = AstId::new(src.file_id, file_ast_id.upcast());
200197
let kind = MacroDefKind::Declarative(ast_id);

crates/ide/src/doc_links.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ pub(crate) fn doc_owner_to_def(
161161
ast::Variant(it) => sema.to_def(&it)?.into(),
162162
ast::Trait(it) => sema.to_def(&it)?.into(),
163163
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),
165165
ast::TupleField(it) => return sema.to_def(&it).map(Definition::Field),
166166
ast::RecordField(it) => return sema.to_def(&it).map(Definition::Field),
167167
_ => return None,

crates/ide/src/file_structure.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ fn structure_node(node: &SyntaxNode) -> Option<StructureNode> {
172172
};
173173
Some(node)
174174
},
175-
ast::MacroRules(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Macro)),
175+
ast::Macro(it) => decl(it, StructureNodeKind::SymbolKind(SymbolKind::Macro)),
176176
_ => None,
177177
}
178178
}

crates/ide/src/move_item.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ fn find_ancestors(item: SyntaxElement, direction: Direction, range: TextRange) -
6666
SyntaxKind::STATIC,
6767
SyntaxKind::CONST,
6868
SyntaxKind::MACRO_RULES,
69+
SyntaxKind::MACRO_DEF,
6970
];
7071

7172
let ancestor = once(root.clone())

crates/ide/src/syntax_highlighting.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod injector;
55

66
mod highlight;
77
mod format;
8-
mod macro_rules;
8+
mod macro_;
99
mod inject;
1010

1111
mod html;
@@ -24,8 +24,8 @@ use syntax::{
2424

2525
use crate::{
2626
syntax_highlighting::{
27-
format::highlight_format_string, highlights::Highlights,
28-
macro_rules::MacroRulesHighlighter, tags::Highlight,
27+
format::highlight_format_string, highlights::Highlights, macro_::MacroHighlighter,
28+
tags::Highlight,
2929
},
3030
FileId, HlMod, HlTag,
3131
};
@@ -93,8 +93,8 @@ fn traverse(
9393
let mut bindings_shadow_count: FxHashMap<Name, u32> = FxHashMap::default();
9494

9595
let mut current_macro_call: Option<ast::MacroCall> = None;
96-
let mut current_macro_rules: Option<ast::MacroRules> = None;
97-
let mut macro_rules_highlighter = MacroRulesHighlighter::default();
96+
let mut current_macro: Option<ast::Macro> = None;
97+
let mut macro_highlighter = MacroHighlighter::default();
9898
let mut inside_attribute = false;
9999

100100
// Walk all nodes, keeping track of whether we are inside a macro or not.
@@ -129,16 +129,16 @@ fn traverse(
129129
_ => (),
130130
}
131131

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)) {
133133
WalkEvent::Enter(Some(mac)) => {
134-
macro_rules_highlighter.init();
135-
current_macro_rules = Some(mac);
134+
macro_highlighter.init();
135+
current_macro = Some(mac);
136136
continue;
137137
}
138138
WalkEvent::Leave(Some(mac)) => {
139-
assert_eq!(current_macro_rules, Some(mac));
140-
current_macro_rules = None;
141-
macro_rules_highlighter = MacroRulesHighlighter::default();
139+
assert_eq!(current_macro, Some(mac));
140+
current_macro = None;
141+
macro_highlighter = MacroHighlighter::default();
142142
}
143143
_ => (),
144144
}
@@ -164,9 +164,9 @@ fn traverse(
164164

165165
let range = element.text_range();
166166

167-
if current_macro_rules.is_some() {
167+
if current_macro.is_some() {
168168
if let Some(tok) = element.as_token() {
169-
macro_rules_highlighter.advance(tok);
169+
macro_highlighter.advance(tok);
170170
}
171171
}
172172

@@ -200,7 +200,7 @@ fn traverse(
200200
}
201201
}
202202

203-
if let Some(_) = macro_rules_highlighter.highlight(element_to_highlight.clone()) {
203+
if let Some(_) = macro_highlighter.highlight(element_to_highlight.clone()) {
204204
continue;
205205
}
206206

crates/ide/src/syntax_highlighting/inject.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,7 @@ fn doc_attributes<'node>(
109109
ast::Impl(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::SelfType(def))),
110110
ast::RecordField(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Field(def))),
111111
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))),
113-
// ast::MacroDef(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))),
112+
ast::Macro(it) => sema.to_def(&it).map(|def| (def.attrs(sema.db), Definition::Macro(def))),
114113
// ast::Use(it) => sema.to_def(&it).map(|def| (Box::new(it) as _, def.attrs(sema.db))),
115114
_ => return None
116115
}

crates/ide/src/syntax_highlighting/macro_rules.rs renamed to crates/ide/src/syntax_highlighting/macro_.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ use syntax::{SyntaxElement, SyntaxKind, SyntaxToken, TextRange, T};
44
use crate::{HlRange, HlTag};
55

66
#[derive(Default)]
7-
pub(super) struct MacroRulesHighlighter {
7+
pub(super) struct MacroHighlighter {
88
state: Option<MacroMatcherParseState>,
99
}
1010

11-
impl MacroRulesHighlighter {
11+
impl MacroHighlighter {
1212
pub(super) fn init(&mut self) {
1313
self.state = Some(MacroMatcherParseState::default());
1414
}
1515

1616
pub(super) fn advance(&mut self, token: &SyntaxToken) {
1717
if let Some(state) = self.state.as_mut() {
18-
update_macro_rules_state(state, token);
18+
update_macro_state(state, token);
1919
}
2020
}
2121

@@ -74,9 +74,9 @@ impl RuleState {
7474
}
7575
}
7676

77-
fn update_macro_rules_state(state: &mut MacroMatcherParseState, tok: &SyntaxToken) {
77+
fn update_macro_state(state: &mut MacroMatcherParseState, tok: &SyntaxToken) {
7878
if !state.in_invoc_body {
79-
if tok.kind() == T!['{'] {
79+
if tok.kind() == T!['{'] || tok.kind() == T!['('] {
8080
state.in_invoc_body = true;
8181
}
8282
return;

crates/ide/src/syntax_highlighting/test_data/highlighting.html

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<span class="keyword">mod</span> <span class="module declaration">inner</span> <span class="brace">{</span><span class="brace">}</span>
4242

4343
<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>
4545

4646
<span class="comment">// Needed for function consuming vs normal</span>
4747
<span class="keyword">pub</span> <span class="keyword">mod</span> <span class="module declaration">marker</span> <span class="brace">{</span>
@@ -158,6 +158,16 @@
158158
<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>
159159
<span class="brace">}</span>
160160

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+
161171
<span class="comment">// comment</span>
162172
<span class="keyword">fn</span> <span class="function declaration">main</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span>
163173
<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>

crates/ide/src/syntax_highlighting/tests.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,16 @@ macro_rules! keyword_frag {
129129
($type:ty) => ($type)
130130
}
131131
132+
macro with_args($i:ident) {
133+
$i
134+
}
135+
136+
macro without_args {
137+
($i:ident) => {
138+
$i
139+
}
140+
}
141+
132142
// comment
133143
fn main() {
134144
println!("Hello, {}!", 92);

0 commit comments

Comments
 (0)