diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-10-30 19:05:09 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2019-10-30 19:05:09 +0000 |
commit | 998088876d91b7602068f8209a61918d4a8a8fe7 (patch) | |
tree | 12837dc698294e3673496130372359ef6e6c3ef8 /crates | |
parent | ce3173469c9dc9c59de903dfe33a6156168d0325 (diff) | |
parent | 4d17658940ec73554dfef799b22e8829ab5ad61a (diff) |
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 <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_ide_api/src/call_info.rs | 17 | ||||
-rw-r--r-- | crates/ra_syntax/src/lib.rs | 14 |
2 files changed, 22 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; | |||
4 | use ra_syntax::{ | 4 | use ra_syntax::{ |
5 | algo::ancestors_at_offset, | 5 | algo::ancestors_at_offset, |
6 | ast::{self, ArgListOwner}, | 6 | ast::{self, ArgListOwner}, |
7 | AstNode, SyntaxNode, TextUnit, | 7 | match_ast, AstNode, SyntaxNode, TextUnit, |
8 | }; | 8 | }; |
9 | use test_utils::tested_by; | 9 | use test_utils::tested_by; |
10 | 10 | ||
@@ -91,14 +91,13 @@ enum FnCallNode { | |||
91 | impl FnCallNode { | 91 | impl FnCallNode { |
92 | fn with_node(syntax: &SyntaxNode, offset: TextUnit) -> Option<FnCallNode> { | 92 | fn with_node(syntax: &SyntaxNode, offset: TextUnit) -> Option<FnCallNode> { |
93 | ancestors_at_offset(syntax, offset).find_map(|node| { | 93 | ancestors_at_offset(syntax, offset).find_map(|node| { |
94 | if let Some(expr) = ast::CallExpr::cast(node.clone()) { | 94 | match_ast! { |
95 | Some(FnCallNode::CallExpr(expr)) | 95 | match node { |
96 | } else if let Some(expr) = ast::MethodCallExpr::cast(node.clone()) { | 96 | ast::CallExpr(it) => { Some(FnCallNode::CallExpr(it)) }, |
97 | Some(FnCallNode::MethodCallExpr(expr)) | 97 | ast::MethodCallExpr(it) => { Some(FnCallNode::MethodCallExpr(it)) }, |
98 | } else if let Some(expr) = ast::MacroCall::cast(node) { | 98 | ast::MacroCall(it) => { Some(FnCallNode::MacroCallExpr(it)) }, |
99 | Some(FnCallNode::MacroCallExpr(expr)) | 99 | _ => { None }, |
100 | } else { | 100 | } |
101 | None | ||
102 | } | 101 | } |
103 | }) | 102 | }) |
104 | } | 103 | } |
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 { | |||
160 | } | 160 | } |
161 | } | 161 | } |
162 | 162 | ||
163 | /// Matches a `SyntaxNode` against an `ast` type. | ||
164 | /// | ||
165 | /// # Example: | ||
166 | /// | ||
167 | /// ```ignore | ||
168 | /// match_ast! { | ||
169 | /// match node { | ||
170 | /// ast::CallExpr(it) => { ... }, | ||
171 | /// ast::MethodCallExpr(it) => { ... }, | ||
172 | /// ast::MacroCall(it) => { ... }, | ||
173 | /// _ => None, | ||
174 | /// } | ||
175 | /// } | ||
176 | /// ``` | ||
163 | #[macro_export] | 177 | #[macro_export] |
164 | macro_rules! match_ast { | 178 | macro_rules! match_ast { |
165 | (match $node:ident { | 179 | (match $node:ident { |