2133: Document match_ast! and use it in call_info r=matklad a=kjeremy

Suggested by @matklad in https://github.com/rust-analyzer/rust-analyzer/pull/2129#discussion_r340708660

Co-authored-by: kjeremy <kjeremy@gmail.com>
This commit is contained in:
bors[bot] 2019-10-30 19:05:09 +00:00 committed by GitHub
commit 998088876d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 9 deletions

View File

@ -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<FnCallNode> {
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 },
}
}
})
}

View File

@ -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 {