diff options
Diffstat (limited to 'crates/ra_analysis/src/descriptors/mod.rs')
-rw-r--r-- | crates/ra_analysis/src/descriptors/mod.rs | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/crates/ra_analysis/src/descriptors/mod.rs b/crates/ra_analysis/src/descriptors/mod.rs new file mode 100644 index 000000000..873eb47e4 --- /dev/null +++ b/crates/ra_analysis/src/descriptors/mod.rs | |||
@@ -0,0 +1,63 @@ | |||
1 | pub(crate) mod module; | ||
2 | |||
3 | use ra_syntax::{ | ||
4 | ast::{self, AstNode, NameOwner}, | ||
5 | text_utils::is_subrange, | ||
6 | }; | ||
7 | |||
8 | #[derive(Debug, Clone)] | ||
9 | pub struct FnDescriptor { | ||
10 | pub name: String, | ||
11 | pub label: String, | ||
12 | pub ret_type: Option<String>, | ||
13 | pub params: Vec<String>, | ||
14 | } | ||
15 | |||
16 | impl FnDescriptor { | ||
17 | pub fn new(node: ast::FnDef) -> Option<Self> { | ||
18 | let name = node.name()?.text().to_string(); | ||
19 | |||
20 | // Strip the body out for the label. | ||
21 | let label: String = if let Some(body) = node.body() { | ||
22 | let body_range = body.syntax().range(); | ||
23 | let label: String = node | ||
24 | .syntax() | ||
25 | .children() | ||
26 | .filter(|child| !is_subrange(body_range, child.range())) | ||
27 | .map(|node| node.text().to_string()) | ||
28 | .collect(); | ||
29 | label | ||
30 | } else { | ||
31 | node.syntax().text().to_string() | ||
32 | }; | ||
33 | |||
34 | let params = FnDescriptor::param_list(node); | ||
35 | let ret_type = node.ret_type().map(|r| r.syntax().text().to_string()); | ||
36 | |||
37 | Some(FnDescriptor { | ||
38 | name, | ||
39 | ret_type, | ||
40 | params, | ||
41 | label, | ||
42 | }) | ||
43 | } | ||
44 | |||
45 | fn param_list(node: ast::FnDef) -> Vec<String> { | ||
46 | let mut res = vec![]; | ||
47 | if let Some(param_list) = node.param_list() { | ||
48 | if let Some(self_param) = param_list.self_param() { | ||
49 | res.push(self_param.syntax().text().to_string()) | ||
50 | } | ||
51 | |||
52 | // Maybe use param.pat here? See if we can just extract the name? | ||
53 | //res.extend(param_list.params().map(|p| p.syntax().text().to_string())); | ||
54 | res.extend( | ||
55 | param_list | ||
56 | .params() | ||
57 | .filter_map(|p| p.pat()) | ||
58 | .map(|pat| pat.syntax().text().to_string()), | ||
59 | ); | ||
60 | } | ||
61 | res | ||
62 | } | ||
63 | } | ||