Merge #2133
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:
commit
998088876d
@ -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 },
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user