diff options
Diffstat (limited to 'crates/ra_hir/src/code_model_api.rs')
-rw-r--r-- | crates/ra_hir/src/code_model_api.rs | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index 9ae620efd..21ca36265 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs | |||
@@ -366,6 +366,28 @@ impl Function { | |||
366 | Some(comments) | 366 | Some(comments) |
367 | } | 367 | } |
368 | } | 368 | } |
369 | |||
370 | pub fn label(&self, db: &impl HirDatabase) -> Option<String> { | ||
371 | let def_loc = self.def_id.loc(db); | ||
372 | let syntax = db.file_item(def_loc.source_item_id); | ||
373 | let node = ast::FnDef::cast(&syntax).expect("fn def should point to FnDef node"); | ||
374 | |||
375 | let label: String = if let Some(body) = node.body() { | ||
376 | let body_range = body.syntax().range(); | ||
377 | let label: String = node | ||
378 | .syntax() | ||
379 | .children() | ||
380 | .filter(|child| !child.range().is_subrange(&body_range)) // Filter out body | ||
381 | .filter(|child| ast::Comment::cast(child).is_none()) // Filter out comments | ||
382 | .map(|node| node.text().to_string()) | ||
383 | .collect(); | ||
384 | label | ||
385 | } else { | ||
386 | node.syntax().text().to_string() | ||
387 | }; | ||
388 | |||
389 | Some(label.trim().to_owned()) | ||
390 | } | ||
369 | } | 391 | } |
370 | 392 | ||
371 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 393 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |