aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/src/descriptors/function/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_analysis/src/descriptors/function/mod.rs')
-rw-r--r--crates/ra_analysis/src/descriptors/function/mod.rs83
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..bb68b0ce7
--- /dev/null
+++ b/crates/ra_analysis/src/descriptors/function/mod.rs
@@ -0,0 +1,83 @@
1pub(super) mod imp;
2mod scope;
3
4use ra_syntax::{
5 ast::{self, AstNode, NameOwner}
6};
7
8use crate::{
9 FileId,
10 syntax_ptr::SyntaxPtr
11};
12
13pub(crate) use self::scope::{FnScopes, resolve_local_name};
14
15
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
17pub(crate) struct FnId(SyntaxPtr);
18
19impl 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)]
28pub struct FnDescriptor {
29 pub name: String,
30 pub label: String,
31 pub ret_type: Option<String>,
32 pub params: Vec<String>,
33}
34
35impl 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