aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir/src/code_model_api.rs22
-rw-r--r--crates/ra_ide_api/src/completion/completion_item.rs27
2 files changed, 25 insertions, 24 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs
index 21ca36265..9ae620efd 100644
--- a/crates/ra_hir/src/code_model_api.rs
+++ b/crates/ra_hir/src/code_model_api.rs
@@ -366,28 +366,6 @@ 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 }
391} 369}
392 370
393#[derive(Debug, Clone, PartialEq, Eq, Hash)] 371#[derive(Debug, Clone, PartialEq, Eq, Hash)]
diff --git a/crates/ra_ide_api/src/completion/completion_item.rs b/crates/ra_ide_api/src/completion/completion_item.rs
index b720a1382..680fd8d1b 100644
--- a/crates/ra_ide_api/src/completion/completion_item.rs
+++ b/crates/ra_ide_api/src/completion/completion_item.rs
@@ -1,7 +1,10 @@
1use hir::PerNs; 1use hir::PerNs;
2 2
3use crate::completion::completion_context::CompletionContext; 3use crate::completion::completion_context::CompletionContext;
4use ra_syntax::TextRange; 4use ra_syntax::{
5 ast::{self, AstNode},
6 TextRange
7};
5use ra_text_edit::TextEdit; 8use ra_text_edit::TextEdit;
6 9
7/// `CompletionItem` describes a single completion variant in the editor pop-up. 10/// `CompletionItem` describes a single completion variant in the editor pop-up.
@@ -263,7 +266,7 @@ impl Builder {
263 self.documentation = Some(docs); 266 self.documentation = Some(docs);
264 } 267 }
265 268
266 if let Some(label) = function.label(ctx.db) { 269 if let Some(label) = function_label(ctx, function) {
267 self.detail = Some(label); 270 self.detail = Some(label);
268 } 271 }
269 272
@@ -303,6 +306,26 @@ impl Into<Vec<CompletionItem>> for Completions {
303 } 306 }
304} 307}
305 308
309fn function_label(ctx: &CompletionContext, function: hir::Function) -> Option<String> {
310 let node = function.source(ctx.db).1;
311
312 let label: String = if let Some(body) = node.body() {
313 let body_range = body.syntax().range();
314 let label: String = node
315 .syntax()
316 .children()
317 .filter(|child| !child.range().is_subrange(&body_range)) // Filter out body
318 .filter(|child| ast::Comment::cast(child).is_none()) // Filter out comments
319 .map(|node| node.text().to_string())
320 .collect();
321 label
322 } else {
323 node.syntax().text().to_string()
324 };
325
326 Some(label.trim().to_owned())
327}
328
306#[cfg(test)] 329#[cfg(test)]
307pub(crate) fn check_completion(test_name: &str, code: &str, kind: CompletionKind) { 330pub(crate) fn check_completion(test_name: &str, code: &str, kind: CompletionKind) {
308 use crate::mock_analysis::{single_file_with_position, analysis_and_position}; 331 use crate::mock_analysis::{single_file_with_position, analysis_and_position};