auto_lsp_core/utils.rs
1/// Calls a method on the first node that matches any of the provided types.
2/// Returns the result of the method call.
3///
4/// # Example
5///
6/// ```rust, ignore
7/// use ast::generated::{FunctionDefinition, ClassDefinition};
8/// use auto_lsp::dispatch_once;
9///
10/// /* ... */
11/// let result = dispatch_once!(node.lower(), [
12/// FunctionDefinition => return_something(db, param),
13/// ClassDefinition => return_something(db, param)
14/// ]);
15/// ```
16#[macro_export]
17macro_rules! dispatch_once {
18 ($node:expr, [$($ty:ty => $method:ident($($param:expr),*)),*]) => {
19 $(
20 if let Some(n) = $node.downcast_ref::<$ty>() {
21 return n.$method($($param),*);
22 };
23 )*
24 };
25}
26
27/// Calls a method on any node that matches any of the provided types.
28/// Unlike dispatch_once, it will not return.
29///
30/// # Example
31///
32/// ```rust, ignore
33/// use ast::generated::{FunctionDefinition, ClassDefinition};
34/// use auto_lsp::dispatch;
35///
36/// /* ... */
37/// dispatch!(node.lower(), [
38/// FunctionDefinition => get_something(db, param),
39/// ClassDefinition => get_something(db, param)
40/// ]);
41/// ```
42#[macro_export]
43macro_rules! dispatch {
44 ($node:expr, [$($ty:ty => $method:ident($($param:expr),*)),*]) => {
45 $(
46 if let Some(n) = $node.downcast_ref::<$ty>() {
47 n.$method($($param),*)?;
48 };
49 )*
50 };
51}