diff options
author | Aleksey Kladov <[email protected]> | 2018-10-31 07:56:31 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-10-31 07:56:31 +0000 |
commit | b67295134bf5c518b39bc88abbe1bc5b9d7d3baf (patch) | |
tree | a507432668deef7b216cb7c6360620619e904060 /crates/ra_analysis/src/descriptors/function/mod.rs | |
parent | 3068af79ffd23656ff2d46ac92d9192ce6c813fb (diff) |
Move FnDescriptors to analyzer
Diffstat (limited to 'crates/ra_analysis/src/descriptors/function/mod.rs')
-rw-r--r-- | crates/ra_analysis/src/descriptors/function/mod.rs | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/crates/ra_analysis/src/descriptors/function/mod.rs b/crates/ra_analysis/src/descriptors/function/mod.rs new file mode 100644 index 000000000..687413ddc --- /dev/null +++ b/crates/ra_analysis/src/descriptors/function/mod.rs | |||
@@ -0,0 +1,83 @@ | |||
1 | pub(super) mod imp; | ||
2 | mod scope; | ||
3 | |||
4 | use ra_syntax::{ | ||
5 | ast::{self, AstNode, NameOwner} | ||
6 | }; | ||
7 | |||
8 | use crate::{ | ||
9 | FileId, | ||
10 | syntax_ptr::SyntaxPtr | ||
11 | }; | ||
12 | |||
13 | pub(crate) use self::scope::FnScopes; | ||
14 | |||
15 | |||
16 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
17 | pub(crate) struct FnId(SyntaxPtr); | ||
18 | |||
19 | impl FnId { | ||
20 | pub(crate) fn new(file_id: FileId, fn_def: ast::FnDef) -> FnId { | ||
21 | let ptr = SyntaxPtr::new(file_id, fn_def.syntax()); | ||
22 | FnId(ptr) | ||
23 | } | ||
24 | } | ||
25 | |||
26 | |||
27 | #[derive(Debug, Clone)] | ||
28 | pub struct FnDescriptor { | ||
29 | pub name: String, | ||
30 | pub label: String, | ||
31 | pub ret_type: Option<String>, | ||
32 | pub params: Vec<String>, | ||
33 | } | ||
34 | |||
35 | impl FnDescriptor { | ||
36 | pub fn new(node: ast::FnDef) -> Option<Self> { | ||
37 | let name = node.name()?.text().to_string(); | ||
38 | |||
39 | // Strip the body out for the label. | ||
40 | let label: String = if let Some(body) = node.body() { | ||
41 | let body_range = body.syntax().range(); | ||
42 | let label: String = node | ||
43 | .syntax() | ||
44 | .children() | ||
45 | .filter(|child| !child.range().is_subrange(&body_range)) | ||
46 | .map(|node| node.text().to_string()) | ||
47 | .collect(); | ||
48 | label | ||
49 | } else { | ||
50 | node.syntax().text().to_string() | ||
51 | }; | ||
52 | |||
53 | let params = FnDescriptor::param_list(node); | ||
54 | let ret_type = node.ret_type().map(|r| r.syntax().text().to_string()); | ||
55 | |||
56 | Some(FnDescriptor { | ||
57 | name, | ||
58 | ret_type, | ||
59 | params, | ||
60 | label, | ||
61 | }) | ||
62 | } | ||
63 | |||
64 | fn param_list(node: ast::FnDef) -> Vec<String> { | ||
65 | let mut res = vec![]; | ||
66 | if let Some(param_list) = node.param_list() { | ||
67 | if let Some(self_param) = param_list.self_param() { | ||
68 | res.push(self_param.syntax().text().to_string()) | ||
69 | } | ||
70 | |||
71 | // Maybe use param.pat here? See if we can just extract the name? | ||
72 | //res.extend(param_list.params().map(|p| p.syntax().text().to_string())); | ||
73 | res.extend( | ||
74 | param_list | ||
75 | .params() | ||
76 | .filter_map(|p| p.pat()) | ||
77 | .map(|pat| pat.syntax().text().to_string()), | ||
78 | ); | ||
79 | } | ||
80 | res | ||
81 | } | ||
82 | } | ||
83 | |||