diff options
Diffstat (limited to 'crates/ra_analysis/src/descriptors.rs')
-rw-r--r-- | crates/ra_analysis/src/descriptors.rs | 56 |
1 files changed, 55 insertions, 1 deletions
diff --git a/crates/ra_analysis/src/descriptors.rs b/crates/ra_analysis/src/descriptors.rs index 0731b5572..faf945a41 100644 --- a/crates/ra_analysis/src/descriptors.rs +++ b/crates/ra_analysis/src/descriptors.rs | |||
@@ -4,7 +4,8 @@ use std::{ | |||
4 | use relative_path::RelativePathBuf; | 4 | use relative_path::RelativePathBuf; |
5 | use ra_syntax::{ | 5 | use ra_syntax::{ |
6 | SmolStr, | 6 | SmolStr, |
7 | ast::{self, NameOwner}, | 7 | ast::{self, NameOwner, AstNode}, |
8 | text_utils::is_subrange | ||
8 | }; | 9 | }; |
9 | use { | 10 | use { |
10 | FileId, | 11 | FileId, |
@@ -218,3 +219,56 @@ fn resolve_submodule( | |||
218 | } | 219 | } |
219 | (points_to, problem) | 220 | (points_to, problem) |
220 | } | 221 | } |
222 | |||
223 | #[derive(Debug, Clone)] | ||
224 | pub struct FnDescriptor { | ||
225 | pub name: String, | ||
226 | pub label : String, | ||
227 | pub ret_type: Option<String>, | ||
228 | pub params: Vec<String>, | ||
229 | } | ||
230 | |||
231 | impl 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 | } \ No newline at end of file | ||