aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/src/descriptors.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_analysis/src/descriptors.rs')
-rw-r--r--crates/ra_analysis/src/descriptors.rs68
1 files changed, 61 insertions, 7 deletions
diff --git a/crates/ra_analysis/src/descriptors.rs b/crates/ra_analysis/src/descriptors.rs
index 0731b5572..8d9f38ca5 100644
--- a/crates/ra_analysis/src/descriptors.rs
+++ b/crates/ra_analysis/src/descriptors.rs
@@ -4,14 +4,15 @@ use std::{
4use relative_path::RelativePathBuf; 4use relative_path::RelativePathBuf;
5use ra_syntax::{ 5use ra_syntax::{
6 SmolStr, 6 SmolStr,
7 ast::{self, NameOwner}, 7 ast::{self, NameOwner, AstNode},
8 text_utils::is_subrange
8}; 9};
9use { 10use crate::{
10 FileId, 11 FileId,
11 imp::FileResolverImp, 12 imp::FileResolverImp,
12}; 13};
13 14
14#[derive(Debug, Hash)] 15#[derive(Debug, PartialEq, Eq, Hash)]
15pub struct ModuleDescriptor { 16pub struct ModuleDescriptor {
16 pub submodules: Vec<Submodule> 17 pub submodules: Vec<Submodule>
17} 18}
@@ -42,7 +43,7 @@ pub struct Submodule {
42 pub name: SmolStr, 43 pub name: SmolStr,
43} 44}
44 45
45#[derive(Hash, Debug)] 46#[derive(Debug, PartialEq, Eq, Hash)]
46pub(crate) struct ModuleTreeDescriptor { 47pub(crate) struct ModuleTreeDescriptor {
47 nodes: Vec<NodeData>, 48 nodes: Vec<NodeData>,
48 links: Vec<LinkData>, 49 links: Vec<LinkData>,
@@ -51,7 +52,7 @@ pub(crate) struct ModuleTreeDescriptor {
51 52
52#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] 53#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
53struct Node(usize); 54struct Node(usize);
54#[derive(Hash, Debug)] 55#[derive(Hash, Debug, PartialEq, Eq)]
55struct NodeData { 56struct NodeData {
56 file_id: FileId, 57 file_id: FileId,
57 links: Vec<Link>, 58 links: Vec<Link>,
@@ -60,7 +61,7 @@ struct NodeData {
60 61
61#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] 62#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
62pub(crate) struct Link(usize); 63pub(crate) struct Link(usize);
63#[derive(Hash, Debug)] 64#[derive(Hash, Debug, PartialEq, Eq)]
64struct LinkData { 65struct LinkData {
65 owner: Node, 66 owner: Node,
66 name: SmolStr, 67 name: SmolStr,
@@ -69,7 +70,7 @@ struct LinkData {
69} 70}
70 71
71 72
72#[derive(Clone, Debug, Hash)] 73#[derive(Clone, Debug, Hash, PartialEq, Eq)]
73pub enum Problem { 74pub enum Problem {
74 UnresolvedModule { 75 UnresolvedModule {
75 candidate: RelativePathBuf, 76 candidate: RelativePathBuf,
@@ -218,3 +219,56 @@ fn resolve_submodule(
218 } 219 }
219 (points_to, problem) 220 (points_to, problem)
220} 221}
222
223#[derive(Debug, Clone)]
224pub struct FnDescriptor {
225 pub name: String,
226 pub label : String,
227 pub ret_type: Option<String>,
228 pub params: Vec<String>,
229}
230
231impl FnDescriptor {
232 pub fn new(node: ast::FnDef) -> Option<Self> {
233 let name = node.name()?.text().to_string();
234
235 // Strip the body out for the label.
236 let label : String = if let Some(body) = node.body() {
237 let body_range = body.syntax().range();
238 let label : String = node.syntax().children()
239 .filter(|child| !is_subrange(body_range, child.range()))
240 .map(|node| node.text().to_string())
241 .collect();
242 label
243 } else {
244 node.syntax().text().to_string()
245 };
246
247 let params = FnDescriptor::param_list(node);
248 let ret_type = node.ret_type().map(|r| r.syntax().text().to_string());
249
250 Some(FnDescriptor {
251 name,
252 ret_type,
253 params,
254 label
255 })
256 }
257
258 fn param_list(node: ast::FnDef) -> Vec<String> {
259 let mut res = vec![];
260 if let Some(param_list) = node.param_list() {
261 if let Some(self_param) = param_list.self_param() {
262 res.push(self_param.syntax().text().to_string())
263 }
264
265 // Maybe use param.pat here? See if we can just extract the name?
266 //res.extend(param_list.params().map(|p| p.syntax().text().to_string()));
267 res.extend(param_list.params()
268 .filter_map(|p| p.pat())
269 .map(|pat| pat.syntax().text().to_string())
270 );
271 }
272 res
273 }
274}