From 2fc22901730f35405d2bdfe33f88d7b3c6b14304 Mon Sep 17 00:00:00 2001 From: Ekaterina Babshukova Date: Sat, 5 Oct 2019 17:03:03 +0300 Subject: replace AST visitors with macro --- crates/ra_ide_api/src/display/navigation_target.rs | 59 ++++++----- crates/ra_ide_api/src/display/structure.rs | 116 +++++++++++---------- 2 files changed, 91 insertions(+), 84 deletions(-) (limited to 'crates/ra_ide_api/src/display') 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 @@ use hir::{AssocItem, FieldSource, HasSource, ModuleSource}; use ra_db::{FileId, SourceDatabase}; use ra_syntax::{ - algo::visit::{visitor, Visitor}, ast::{self, DocCommentsOwner}, - AstNode, AstPtr, SmolStr, + match_ast, AstNode, AstPtr, SmolStr, SyntaxKind::{self, NAME}, SyntaxNode, TextRange, }; @@ -308,19 +307,22 @@ pub(crate) fn docs_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> Option let parse = db.parse(symbol.file_id); let node = symbol.ptr.to_node(parse.tree().syntax()); - visitor() - .visit(|it: ast::FnDef| it.doc_comment_text()) - .visit(|it: ast::StructDef| it.doc_comment_text()) - .visit(|it: ast::EnumDef| it.doc_comment_text()) - .visit(|it: ast::TraitDef| it.doc_comment_text()) - .visit(|it: ast::Module| it.doc_comment_text()) - .visit(|it: ast::TypeAliasDef| it.doc_comment_text()) - .visit(|it: ast::ConstDef| it.doc_comment_text()) - .visit(|it: ast::StaticDef| it.doc_comment_text()) - .visit(|it: ast::RecordFieldDef| it.doc_comment_text()) - .visit(|it: ast::EnumVariant| it.doc_comment_text()) - .visit(|it: ast::MacroCall| it.doc_comment_text()) - .accept(&node)? + match_ast! { + match node { + ast::FnDef(it) => { it.doc_comment_text() }, + ast::StructDef(it) => { it.doc_comment_text() }, + ast::EnumDef(it) => { it.doc_comment_text() }, + ast::TraitDef(it) => { it.doc_comment_text() }, + ast::Module(it) => { it.doc_comment_text() }, + ast::TypeAliasDef(it) => { it.doc_comment_text() }, + ast::ConstDef(it) => { it.doc_comment_text() }, + ast::StaticDef(it) => { it.doc_comment_text() }, + ast::RecordFieldDef(it) => { it.doc_comment_text() }, + ast::EnumVariant(it) => { it.doc_comment_text() }, + ast::MacroCall(it) => { it.doc_comment_text() }, + _ => None, + } + } } /// Get a description of a symbol. @@ -330,16 +332,19 @@ pub(crate) fn description_from_symbol(db: &RootDatabase, symbol: &FileSymbol) -> let parse = db.parse(symbol.file_id); let node = symbol.ptr.to_node(parse.tree().syntax()); - visitor() - .visit(|node: ast::FnDef| node.short_label()) - .visit(|node: ast::StructDef| node.short_label()) - .visit(|node: ast::EnumDef| node.short_label()) - .visit(|node: ast::TraitDef| node.short_label()) - .visit(|node: ast::Module| node.short_label()) - .visit(|node: ast::TypeAliasDef| node.short_label()) - .visit(|node: ast::ConstDef| node.short_label()) - .visit(|node: ast::StaticDef| node.short_label()) - .visit(|node: ast::RecordFieldDef| node.short_label()) - .visit(|node: ast::EnumVariant| node.short_label()) - .accept(&node)? + match_ast! { + match node { + ast::FnDef(it) => { it.short_label() }, + ast::StructDef(it) => { it.short_label() }, + ast::EnumDef(it) => { it.short_label() }, + ast::TraitDef(it) => { it.short_label() }, + ast::Module(it) => { it.short_label() }, + ast::TypeAliasDef(it) => { it.short_label() }, + ast::ConstDef(it) => { it.short_label() }, + ast::StaticDef(it) => { it.short_label() }, + ast::RecordFieldDef(it) => { it.short_label() }, + ast::EnumVariant(it) => { it.short_label() }, + _ => None, + } + } } 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 @@ use crate::TextRange; use ra_syntax::{ - algo::visit::{visitor, Visitor}, ast::{self, AttrsOwner, NameOwner, TypeAscriptionOwner, TypeParamsOwner}, - AstNode, SourceFile, SyntaxKind, SyntaxNode, WalkEvent, + match_ast, AstNode, SourceFile, SyntaxKind, SyntaxNode, WalkEvent, }; #[derive(Debug, Clone)] @@ -101,63 +100,66 @@ fn structure_node(node: &SyntaxNode) -> Option { }) } - visitor() - .visit(|fn_def: ast::FnDef| { - let mut detail = String::from("fn"); - if let Some(type_param_list) = fn_def.type_param_list() { - collapse_ws(type_param_list.syntax(), &mut detail); - } - if let Some(param_list) = fn_def.param_list() { - collapse_ws(param_list.syntax(), &mut detail); - } - if let Some(ret_type) = fn_def.ret_type() { - detail.push_str(" "); - collapse_ws(ret_type.syntax(), &mut detail); - } - - decl_with_detail(fn_def, Some(detail)) - }) - .visit(decl::) - .visit(decl::) - .visit(decl::) - .visit(decl::) - .visit(decl::) - .visit(|td: ast::TypeAliasDef| { - let ty = td.type_ref(); - decl_with_type_ref(td, ty) - }) - .visit(decl_with_ascription::) - .visit(decl_with_ascription::) - .visit(decl_with_ascription::) - .visit(|im: ast::ImplBlock| { - let target_type = im.target_type()?; - let target_trait = im.target_trait(); - let label = match target_trait { - None => format!("impl {}", target_type.syntax().text()), - Some(t) => { - format!("impl {} for {}", t.syntax().text(), target_type.syntax().text(),) + match_ast! { + match node { + ast::FnDef(it) => { + let mut detail = String::from("fn"); + if let Some(type_param_list) = it.type_param_list() { + collapse_ws(type_param_list.syntax(), &mut detail); + } + if let Some(param_list) = it.param_list() { + collapse_ws(param_list.syntax(), &mut detail); + } + if let Some(ret_type) = it.ret_type() { + detail.push_str(" "); + collapse_ws(ret_type.syntax(), &mut detail); } - }; - let node = StructureNode { - parent: None, - label, - navigation_range: target_type.syntax().text_range(), - node_range: im.syntax().text_range(), - kind: im.syntax().kind(), - detail: None, - deprecated: false, - }; - Some(node) - }) - .visit(|mc: ast::MacroCall| { - let first_token = mc.syntax().first_token().unwrap(); - if first_token.text().as_str() != "macro_rules" { - return None; - } - decl(mc) - }) - .accept(&node)? + decl_with_detail(it, Some(detail)) + }, + ast::StructDef(it) => { decl(it) }, + ast::EnumDef(it) => { decl(it) }, + ast::EnumVariant(it) => { decl(it) }, + ast::TraitDef(it) => { decl(it) }, + ast::Module(it) => { decl(it) }, + ast::TypeAliasDef(it) => { + let ty = it.type_ref(); + decl_with_type_ref(it, ty) + }, + ast::RecordFieldDef(it) => { decl_with_ascription(it) }, + ast::ConstDef(it) => { decl_with_ascription(it) }, + ast::StaticDef(it) => { decl_with_ascription(it) }, + ast::ImplBlock(it) => { + let target_type = it.target_type()?; + let target_trait = it.target_trait(); + let label = match target_trait { + None => format!("impl {}", target_type.syntax().text()), + Some(t) => { + format!("impl {} for {}", t.syntax().text(), target_type.syntax().text(),) + } + }; + + let node = StructureNode { + parent: None, + label, + navigation_range: target_type.syntax().text_range(), + node_range: it.syntax().text_range(), + kind: it.syntax().kind(), + detail: None, + deprecated: false, + }; + Some(node) + }, + ast::MacroCall(it) => { + let first_token = it.syntax().first_token().unwrap(); + if first_token.text().as_str() != "macro_rules" { + return None; + } + decl(it) + }, + _ => None, + } + } } #[cfg(test)] -- cgit v1.2.3