diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-10-05 15:54:25 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2019-10-05 15:54:25 +0100 |
commit | d3872964f88a0d751c428c150bb40d8b4f4c89a9 (patch) | |
tree | 14b7045119a1918118c18857cc094c914284705c /crates/ra_ide_api/src/display | |
parent | ae6305b90c80eb919cfde985cba66975b6222ed2 (diff) | |
parent | 311dbb854536dd526cdbcadc6d270f9a37e4b816 (diff) |
Merge #1960
1960: Replace AST visitors with macro r=viorina a=viorina
Fixes #1672.
Co-authored-by: Ekaterina Babshukova <[email protected]>
Diffstat (limited to 'crates/ra_ide_api/src/display')
-rw-r--r-- | crates/ra_ide_api/src/display/navigation_target.rs | 59 | ||||
-rw-r--r-- | crates/ra_ide_api/src/display/structure.rs | 116 |
2 files changed, 91 insertions, 84 deletions
diff --git a/crates/ra_ide_api/src/display/navigation_target.rs b/crates/ra_ide_api/src/display/navigation_target.rs index 60ae802c0..d0b1a8a2a 100644 --- a/crates/ra_ide_api/src/display/navigation_target.rs +++ b/crates/ra_ide_api/src/display/navigation_target.rs | |||
@@ -3,9 +3,8 @@ | |||
3 | use hir::{AssocItem, FieldSource, HasSource, ModuleSource}; | 3 | use hir::{AssocItem, FieldSource, HasSource, ModuleSource}; |
4 | use ra_db::{FileId, SourceDatabase}; | 4 | use ra_db::{FileId, SourceDatabase}; |
5 | use ra_syntax::{ | 5 | use ra_syntax::{ |
6 | algo::visit::{visitor, Visitor}, | ||
7 | ast::{self, DocCommentsOwner}, | 6 | ast::{self, DocCommentsOwner}, |
8 | AstNode, AstPtr, SmolStr, | 7 | match_ast, AstNode, AstPtr, SmolStr, |
9 | SyntaxKind::{self, NAME}, | 8 | SyntaxKind::{self, NAME}, |
10 | SyntaxNode, TextRange, | 9 | SyntaxNode, TextRange, |
11 | }; | 10 | }; |
@@ -308,19 +307,22 @@ pub(crate) fn docs_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option | |||
308 | let parse = db.parse(symbol.file_id); | 307 | let parse = db.parse(symbol.file_id); |
309 | let node = symbol.ptr.to_node(parse.tree().syntax()); | 308 | let node = symbol.ptr.to_node(parse.tree().syntax()); |
310 | 309 | ||
311 | visitor() | 310 | match_ast! { |
312 | .visit(|it: ast::FnDef| it.doc_comment_text()) | 311 | match node { |
313 | .visit(|it: ast::StructDef| it.doc_comment_text()) | 312 | ast::FnDef(it) => { it.doc_comment_text() }, |
314 | .visit(|it: ast::EnumDef| it.doc_comment_text()) | 313 | ast::StructDef(it) => { it.doc_comment_text() }, |
315 | .visit(|it: ast::TraitDef| it.doc_comment_text()) | 314 | ast::EnumDef(it) => { it.doc_comment_text() }, |
316 | .visit(|it: ast::Module| it.doc_comment_text()) | 315 | ast::TraitDef(it) => { it.doc_comment_text() }, |
317 | .visit(|it: ast::TypeAliasDef| it.doc_comment_text()) | 316 | ast::Module(it) => { it.doc_comment_text() }, |
318 | .visit(|it: ast::ConstDef| it.doc_comment_text()) | 317 | ast::TypeAliasDef(it) => { it.doc_comment_text() }, |
319 | .visit(|it: ast::StaticDef| it.doc_comment_text()) | 318 | ast::ConstDef(it) => { it.doc_comment_text() }, |
320 | .visit(|it: ast::RecordFieldDef| it.doc_comment_text()) | 319 | ast::StaticDef(it) => { it.doc_comment_text() }, |
321 | .visit(|it: ast::EnumVariant| it.doc_comment_text()) | 320 | ast::RecordFieldDef(it) => { it.doc_comment_text() }, |
322 | .visit(|it: ast::MacroCall| it.doc_comment_text()) | 321 | ast::EnumVariant(it) => { it.doc_comment_text() }, |
323 | .accept(&node)? | 322 | ast::MacroCall(it) => { it.doc_comment_text() }, |
323 | _ => None, | ||
324 | } | ||
325 | } | ||
324 | } | 326 | } |
325 | 327 | ||
326 | /// Get a description of a symbol. | 328 | /// Get a description of a symbol. |
@@ -330,16 +332,19 @@ pub(crate) fn description_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> | |||
330 | let parse = db.parse(symbol.file_id); | 332 | let parse = db.parse(symbol.file_id); |
331 | let node = symbol.ptr.to_node(parse.tree().syntax()); | 333 | let node = symbol.ptr.to_node(parse.tree().syntax()); |
332 | 334 | ||
333 | visitor() | 335 | match_ast! { |
334 | .visit(|node: ast::FnDef| node.short_label()) | 336 | match node { |
335 | .visit(|node: ast::StructDef| node.short_label()) | 337 | ast::FnDef(it) => { it.short_label() }, |
336 | .visit(|node: ast::EnumDef| node.short_label()) | 338 | ast::StructDef(it) => { it.short_label() }, |
337 | .visit(|node: ast::TraitDef| node.short_label()) | 339 | ast::EnumDef(it) => { it.short_label() }, |
338 | .visit(|node: ast::Module| node.short_label()) | 340 | ast::TraitDef(it) => { it.short_label() }, |
339 | .visit(|node: ast::TypeAliasDef| node.short_label()) | 341 | ast::Module(it) => { it.short_label() }, |
340 | .visit(|node: ast::ConstDef| node.short_label()) | 342 | ast::TypeAliasDef(it) => { it.short_label() }, |
341 | .visit(|node: ast::StaticDef| node.short_label()) | 343 | ast::ConstDef(it) => { it.short_label() }, |
342 | .visit(|node: ast::RecordFieldDef| node.short_label()) | 344 | ast::StaticDef(it) => { it.short_label() }, |
343 | .visit(|node: ast::EnumVariant| node.short_label()) | 345 | ast::RecordFieldDef(it) => { it.short_label() }, |
344 | .accept(&node)? | 346 | ast::EnumVariant(it) => { it.short_label() }, |
347 | _ => None, | ||
348 | } | ||
349 | } | ||
345 | } | 350 | } |
diff --git a/crates/ra_ide_api/src/display/structure.rs b/crates/ra_ide_api/src/display/structure.rs index 8815df747..ddd8b7b20 100644 --- a/crates/ra_ide_api/src/display/structure.rs +++ b/crates/ra_ide_api/src/display/structure.rs | |||
@@ -3,9 +3,8 @@ | |||
3 | use crate::TextRange; | 3 | use crate::TextRange; |
4 | 4 | ||
5 | use ra_syntax::{ | 5 | use ra_syntax::{ |
6 | algo::visit::{visitor, Visitor}, | ||
7 | ast::{self, AttrsOwner, NameOwner, TypeAscriptionOwner, TypeParamsOwner}, | 6 | ast::{self, AttrsOwner, NameOwner, TypeAscriptionOwner, TypeParamsOwner}, |
8 | AstNode, SourceFile, SyntaxKind, SyntaxNode, WalkEvent, | 7 | match_ast, AstNode, SourceFile, SyntaxKind, SyntaxNode, WalkEvent, |
9 | }; | 8 | }; |
10 | 9 | ||
11 | #[derive(Debug, Clone)] | 10 | #[derive(Debug, Clone)] |
@@ -101,63 +100,66 @@ fn structure_node(node: &SyntaxNode) -> Option<StructureNode> { | |||
101 | }) | 100 | }) |
102 | } | 101 | } |
103 | 102 | ||
104 | visitor() | 103 | match_ast! { |
105 | .visit(|fn_def: ast::FnDef| { | 104 | match node { |
106 | let mut detail = String::from("fn"); | 105 | ast::FnDef(it) => { |
107 | if let Some(type_param_list) = fn_def.type_param_list() { | 106 | let mut detail = String::from("fn"); |
108 | collapse_ws(type_param_list.syntax(), &mut detail); | 107 | if let Some(type_param_list) = it.type_param_list() { |
109 | } | 108 | collapse_ws(type_param_list.syntax(), &mut detail); |
110 | if let Some(param_list) = fn_def.param_list() { | 109 | } |
111 | collapse_ws(param_list.syntax(), &mut detail); | 110 | if let Some(param_list) = it.param_list() { |
112 | } | 111 | collapse_ws(param_list.syntax(), &mut detail); |
113 | if let Some(ret_type) = fn_def.ret_type() { | 112 | } |
114 | detail.push_str(" "); | 113 | if let Some(ret_type) = it.ret_type() { |
115 | collapse_ws(ret_type.syntax(), &mut detail); | 114 | detail.push_str(" "); |
116 | } | 115 | collapse_ws(ret_type.syntax(), &mut detail); |
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(),) | ||
139 | } | 116 | } |
140 | }; | ||
141 | 117 | ||
142 | let node = StructureNode { | 118 | decl_with_detail(it, Some(detail)) |
143 | parent: None, | 119 | }, |
144 | label, | 120 | ast::StructDef(it) => { decl(it) }, |
145 | navigation_range: target_type.syntax().text_range(), | 121 | ast::EnumDef(it) => { decl(it) }, |
146 | node_range: im.syntax().text_range(), | 122 | ast::EnumVariant(it) => { decl(it) }, |
147 | kind: im.syntax().kind(), | 123 | ast::TraitDef(it) => { decl(it) }, |
148 | detail: None, | 124 | ast::Module(it) => { decl(it) }, |
149 | deprecated: false, | 125 | ast::TypeAliasDef(it) => { |
150 | }; | 126 | let ty = it.type_ref(); |
151 | Some(node) | 127 | decl_with_type_ref(it, ty) |
152 | }) | 128 | }, |
153 | .visit(|mc: ast::MacroCall| { | 129 | ast::RecordFieldDef(it) => { decl_with_ascription(it) }, |
154 | let first_token = mc.syntax().first_token().unwrap(); | 130 | ast::ConstDef(it) => { decl_with_ascription(it) }, |
155 | if first_token.text().as_str() != "macro_rules" { | 131 | ast::StaticDef(it) => { decl_with_ascription(it) }, |
156 | return None; | 132 | ast::ImplBlock(it) => { |
157 | } | 133 | let target_type = it.target_type()?; |
158 | decl(mc) | 134 | let target_trait = it.target_trait(); |
159 | }) | 135 | let label = match target_trait { |
160 | .accept(&node)? | 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 | } | ||
161 | } | 163 | } |
162 | 164 | ||
163 | #[cfg(test)] | 165 | #[cfg(test)] |