Skip to content

Commit d387296

Browse files
bors[bot]viorina
andauthored
Merge #1960
1960: Replace AST visitors with macro r=viorina a=viorina Fixes #1672. Co-authored-by: Ekaterina Babshukova <[email protected]>
2 parents ae6305b + 311dbb8 commit d387296

File tree

15 files changed

+349
-429
lines changed

15 files changed

+349
-429
lines changed

crates/ra_assists/src/assists/split_import.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ mod tests {
5151
fn split_import_works_with_trees() {
5252
check_assist(
5353
split_import,
54-
"use algo:<|>:visitor::{Visitor, visit}",
55-
"use algo::{<|>visitor::{Visitor, visit}}",
54+
"use crate:<|>:db::{RootDatabase, FileSymbol}",
55+
"use crate::{<|>db::{RootDatabase, FileSymbol}}",
5656
)
5757
}
5858

5959
#[test]
6060
fn split_import_target() {
61-
check_assist_target(split_import, "use algo::<|>visitor::{Visitor, visit}", "::");
61+
check_assist_target(split_import, "use crate::<|>db::{RootDatabase, FileSymbol}", "::");
6262
}
6363
}

crates/ra_ide_api/src/completion/complete_fn_param.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
//! FIXME: write short doc here
22
3-
use ra_syntax::{
4-
algo::visit::{visitor_ctx, VisitorCtx},
5-
ast, AstNode,
6-
};
3+
use ra_syntax::{ast, match_ast, AstNode};
74
use rustc_hash::FxHashMap;
85

96
use crate::completion::{CompletionContext, CompletionItem, CompletionKind, Completions};
@@ -19,10 +16,13 @@ pub(super) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext)
1916

2017
let mut params = FxHashMap::default();
2118
for node in ctx.token.parent().ancestors() {
22-
let _ = visitor_ctx(&mut params)
23-
.visit::<ast::SourceFile, _>(process)
24-
.visit::<ast::ItemList, _>(process)
25-
.accept(&node);
19+
match_ast! {
20+
match node {
21+
ast::SourceFile(it) => { process(it, &mut params) },
22+
ast::ItemList(it) => { process(it, &mut params) },
23+
_ => (),
24+
}
25+
}
2626
}
2727
params
2828
.into_iter()

crates/ra_ide_api/src/completion/complete_keyword.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
//! FIXME: write short doc here
22
33
use ra_syntax::{
4-
algo::visit::{visitor, Visitor},
54
ast::{self, LoopBodyOwner},
6-
AstNode,
5+
match_ast, AstNode,
76
SyntaxKind::*,
87
SyntaxToken,
98
};
@@ -84,12 +83,15 @@ fn is_in_loop_body(leaf: &SyntaxToken) -> bool {
8483
if node.kind() == FN_DEF || node.kind() == LAMBDA_EXPR {
8584
break;
8685
}
87-
let loop_body = visitor()
88-
.visit::<ast::ForExpr, _>(|it| it.loop_body())
89-
.visit::<ast::WhileExpr, _>(|it| it.loop_body())
90-
.visit::<ast::LoopExpr, _>(|it| it.loop_body())
91-
.accept(&node);
92-
if let Some(Some(body)) = loop_body {
86+
let loop_body = match_ast! {
87+
match node {
88+
ast::ForExpr(it) => { it.loop_body() },
89+
ast::WhileExpr(it) => { it.loop_body() },
90+
ast::LoopExpr(it) => { it.loop_body() },
91+
_ => None,
92+
}
93+
};
94+
if let Some(body) = loop_body {
9395
if leaf.text_range().is_subrange(&body.syntax().text_range()) {
9496
return true;
9597
}

crates/ra_ide_api/src/display/navigation_target.rs

+32-27
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
use hir::{AssocItem, FieldSource, HasSource, ModuleSource};
44
use ra_db::{FileId, SourceDatabase};
55
use ra_syntax::{
6-
algo::visit::{visitor, Visitor},
76
ast::{self, DocCommentsOwner},
8-
AstNode, AstPtr, SmolStr,
7+
match_ast, AstNode, AstPtr, SmolStr,
98
SyntaxKind::{self, NAME},
109
SyntaxNode, TextRange,
1110
};
@@ -308,19 +307,22 @@ pub(crate) fn docs_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option
308307
let parse = db.parse(symbol.file_id);
309308
let node = symbol.ptr.to_node(parse.tree().syntax());
310309

311-
visitor()
312-
.visit(|it: ast::FnDef| it.doc_comment_text())
313-
.visit(|it: ast::StructDef| it.doc_comment_text())
314-
.visit(|it: ast::EnumDef| it.doc_comment_text())
315-
.visit(|it: ast::TraitDef| it.doc_comment_text())
316-
.visit(|it: ast::Module| it.doc_comment_text())
317-
.visit(|it: ast::TypeAliasDef| it.doc_comment_text())
318-
.visit(|it: ast::ConstDef| it.doc_comment_text())
319-
.visit(|it: ast::StaticDef| it.doc_comment_text())
320-
.visit(|it: ast::RecordFieldDef| it.doc_comment_text())
321-
.visit(|it: ast::EnumVariant| it.doc_comment_text())
322-
.visit(|it: ast::MacroCall| it.doc_comment_text())
323-
.accept(&node)?
310+
match_ast! {
311+
match node {
312+
ast::FnDef(it) => { it.doc_comment_text() },
313+
ast::StructDef(it) => { it.doc_comment_text() },
314+
ast::EnumDef(it) => { it.doc_comment_text() },
315+
ast::TraitDef(it) => { it.doc_comment_text() },
316+
ast::Module(it) => { it.doc_comment_text() },
317+
ast::TypeAliasDef(it) => { it.doc_comment_text() },
318+
ast::ConstDef(it) => { it.doc_comment_text() },
319+
ast::StaticDef(it) => { it.doc_comment_text() },
320+
ast::RecordFieldDef(it) => { it.doc_comment_text() },
321+
ast::EnumVariant(it) => { it.doc_comment_text() },
322+
ast::MacroCall(it) => { it.doc_comment_text() },
323+
_ => None,
324+
}
325+
}
324326
}
325327

326328
/// Get a description of a symbol.
@@ -330,16 +332,19 @@ pub(crate) fn description_from_symbol(db: &RootDatabase, symbol: &FileSymbol) ->
330332
let parse = db.parse(symbol.file_id);
331333
let node = symbol.ptr.to_node(parse.tree().syntax());
332334

333-
visitor()
334-
.visit(|node: ast::FnDef| node.short_label())
335-
.visit(|node: ast::StructDef| node.short_label())
336-
.visit(|node: ast::EnumDef| node.short_label())
337-
.visit(|node: ast::TraitDef| node.short_label())
338-
.visit(|node: ast::Module| node.short_label())
339-
.visit(|node: ast::TypeAliasDef| node.short_label())
340-
.visit(|node: ast::ConstDef| node.short_label())
341-
.visit(|node: ast::StaticDef| node.short_label())
342-
.visit(|node: ast::RecordFieldDef| node.short_label())
343-
.visit(|node: ast::EnumVariant| node.short_label())
344-
.accept(&node)?
335+
match_ast! {
336+
match node {
337+
ast::FnDef(it) => { it.short_label() },
338+
ast::StructDef(it) => { it.short_label() },
339+
ast::EnumDef(it) => { it.short_label() },
340+
ast::TraitDef(it) => { it.short_label() },
341+
ast::Module(it) => { it.short_label() },
342+
ast::TypeAliasDef(it) => { it.short_label() },
343+
ast::ConstDef(it) => { it.short_label() },
344+
ast::StaticDef(it) => { it.short_label() },
345+
ast::RecordFieldDef(it) => { it.short_label() },
346+
ast::EnumVariant(it) => { it.short_label() },
347+
_ => None,
348+
}
349+
}
345350
}

crates/ra_ide_api/src/display/structure.rs

+59-57
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
use crate::TextRange;
44

55
use ra_syntax::{
6-
algo::visit::{visitor, Visitor},
76
ast::{self, AttrsOwner, NameOwner, TypeAscriptionOwner, TypeParamsOwner},
8-
AstNode, SourceFile, SyntaxKind, SyntaxNode, WalkEvent,
7+
match_ast, AstNode, SourceFile, SyntaxKind, SyntaxNode, WalkEvent,
98
};
109

1110
#[derive(Debug, Clone)]
@@ -101,63 +100,66 @@ fn structure_node(node: &SyntaxNode) -> Option<StructureNode> {
101100
})
102101
}
103102

104-
visitor()
105-
.visit(|fn_def: ast::FnDef| {
106-
let mut detail = String::from("fn");
107-
if let Some(type_param_list) = fn_def.type_param_list() {
108-
collapse_ws(type_param_list.syntax(), &mut detail);
109-
}
110-
if let Some(param_list) = fn_def.param_list() {
111-
collapse_ws(param_list.syntax(), &mut detail);
112-
}
113-
if let Some(ret_type) = fn_def.ret_type() {
114-
detail.push_str(" ");
115-
collapse_ws(ret_type.syntax(), &mut detail);
116-
}
117-
118-
decl_with_detail(fn_def, Some(detail))
119-
})
120-
.visit(decl::<ast::StructDef>)
121-
.visit(decl::<ast::EnumDef>)
122-
.visit(decl::<ast::EnumVariant>)
123-
.visit(decl::<ast::TraitDef>)
124-
.visit(decl::<ast::Module>)
125-
.visit(|td: ast::TypeAliasDef| {
126-
let ty = td.type_ref();
127-
decl_with_type_ref(td, ty)
128-
})
129-
.visit(decl_with_ascription::<ast::RecordFieldDef>)
130-
.visit(decl_with_ascription::<ast::ConstDef>)
131-
.visit(decl_with_ascription::<ast::StaticDef>)
132-
.visit(|im: ast::ImplBlock| {
133-
let target_type = im.target_type()?;
134-
let target_trait = im.target_trait();
135-
let label = match target_trait {
136-
None => format!("impl {}", target_type.syntax().text()),
137-
Some(t) => {
138-
format!("impl {} for {}", t.syntax().text(), target_type.syntax().text(),)
103+
match_ast! {
104+
match node {
105+
ast::FnDef(it) => {
106+
let mut detail = String::from("fn");
107+
if let Some(type_param_list) = it.type_param_list() {
108+
collapse_ws(type_param_list.syntax(), &mut detail);
109+
}
110+
if let Some(param_list) = it.param_list() {
111+
collapse_ws(param_list.syntax(), &mut detail);
112+
}
113+
if let Some(ret_type) = it.ret_type() {
114+
detail.push_str(" ");
115+
collapse_ws(ret_type.syntax(), &mut detail);
139116
}
140-
};
141117

142-
let node = StructureNode {
143-
parent: None,
144-
label,
145-
navigation_range: target_type.syntax().text_range(),
146-
node_range: im.syntax().text_range(),
147-
kind: im.syntax().kind(),
148-
detail: None,
149-
deprecated: false,
150-
};
151-
Some(node)
152-
})
153-
.visit(|mc: ast::MacroCall| {
154-
let first_token = mc.syntax().first_token().unwrap();
155-
if first_token.text().as_str() != "macro_rules" {
156-
return None;
157-
}
158-
decl(mc)
159-
})
160-
.accept(&node)?
118+
decl_with_detail(it, Some(detail))
119+
},
120+
ast::StructDef(it) => { decl(it) },
121+
ast::EnumDef(it) => { decl(it) },
122+
ast::EnumVariant(it) => { decl(it) },
123+
ast::TraitDef(it) => { decl(it) },
124+
ast::Module(it) => { decl(it) },
125+
ast::TypeAliasDef(it) => {
126+
let ty = it.type_ref();
127+
decl_with_type_ref(it, ty)
128+
},
129+
ast::RecordFieldDef(it) => { decl_with_ascription(it) },
130+
ast::ConstDef(it) => { decl_with_ascription(it) },
131+
ast::StaticDef(it) => { decl_with_ascription(it) },
132+
ast::ImplBlock(it) => {
133+
let target_type = it.target_type()?;
134+
let target_trait = it.target_trait();
135+
let label = match target_trait {
136+
None => format!("impl {}", target_type.syntax().text()),
137+
Some(t) => {
138+
format!("impl {} for {}", t.syntax().text(), target_type.syntax().text(),)
139+
}
140+
};
141+
142+
let node = StructureNode {
143+
parent: None,
144+
label,
145+
navigation_range: target_type.syntax().text_range(),
146+
node_range: it.syntax().text_range(),
147+
kind: it.syntax().kind(),
148+
detail: None,
149+
deprecated: false,
150+
};
151+
Some(node)
152+
},
153+
ast::MacroCall(it) => {
154+
let first_token = it.syntax().first_token().unwrap();
155+
if first_token.text().as_str() != "macro_rules" {
156+
return None;
157+
}
158+
decl(it)
159+
},
160+
_ => None,
161+
}
162+
}
161163
}
162164

163165
#[cfg(test)]

0 commit comments

Comments
 (0)