From 7ad55e976c3f88e92075379c8a4c1f413665b458 Mon Sep 17 00:00:00 2001 From: kjeremy Date: Wed, 30 Oct 2019 14:38:45 -0400 Subject: Document match_ast! --- crates/ra_syntax/src/lib.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs index c315ba552..5dcb6a95a 100644 --- a/crates/ra_syntax/src/lib.rs +++ b/crates/ra_syntax/src/lib.rs @@ -160,6 +160,20 @@ impl SourceFile { } } +/// Matches a `SyntaxNode` against an `ast` type. +/// +/// # Example: +/// +/// ```ignore +/// match_ast! { +/// match node { +/// ast::CallExpr(it) => { ... }, +/// ast::MethodCallExpr(it) => { ... }, +/// ast::MacroCall(it) => { ... }, +/// _ => None, +/// } +/// } +/// ``` #[macro_export] macro_rules! match_ast { (match $node:ident { -- cgit v1.2.3 From 4d17658940ec73554dfef799b22e8829ab5ad61a Mon Sep 17 00:00:00 2001 From: kjeremy Date: Wed, 30 Oct 2019 14:39:05 -0400 Subject: Use match_ast! in FnCallNode::with_node --- crates/ra_ide_api/src/call_info.rs | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/crates/ra_ide_api/src/call_info.rs b/crates/ra_ide_api/src/call_info.rs index e494f5620..3572825b5 100644 --- a/crates/ra_ide_api/src/call_info.rs +++ b/crates/ra_ide_api/src/call_info.rs @@ -4,7 +4,7 @@ use ra_db::SourceDatabase; use ra_syntax::{ algo::ancestors_at_offset, ast::{self, ArgListOwner}, - AstNode, SyntaxNode, TextUnit, + match_ast, AstNode, SyntaxNode, TextUnit, }; use test_utils::tested_by; @@ -91,14 +91,13 @@ enum FnCallNode { impl FnCallNode { fn with_node(syntax: &SyntaxNode, offset: TextUnit) -> Option { ancestors_at_offset(syntax, offset).find_map(|node| { - if let Some(expr) = ast::CallExpr::cast(node.clone()) { - Some(FnCallNode::CallExpr(expr)) - } else if let Some(expr) = ast::MethodCallExpr::cast(node.clone()) { - Some(FnCallNode::MethodCallExpr(expr)) - } else if let Some(expr) = ast::MacroCall::cast(node) { - Some(FnCallNode::MacroCallExpr(expr)) - } else { - None + match_ast! { + match node { + ast::CallExpr(it) => { Some(FnCallNode::CallExpr(it)) }, + ast::MethodCallExpr(it) => { Some(FnCallNode::MethodCallExpr(it)) }, + ast::MacroCall(it) => { Some(FnCallNode::MacroCallExpr(it)) }, + _ => { None }, + } } }) } -- cgit v1.2.3