From a3472f8fe16a359f9ea7a12e7b4920894921421e Mon Sep 17 00:00:00 2001 From: "Jeremy A. Kolb" Date: Tue, 22 Jan 2019 18:20:40 -0500 Subject: Move label from hir to ide_api --- .../ra_ide_api/src/completion/completion_item.rs | 27 ++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'crates/ra_ide_api/src/completion') 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 @@ use hir::PerNs; use crate::completion::completion_context::CompletionContext; -use ra_syntax::TextRange; +use ra_syntax::{ + ast::{self, AstNode}, + TextRange +}; use ra_text_edit::TextEdit; /// `CompletionItem` describes a single completion variant in the editor pop-up. @@ -263,7 +266,7 @@ impl Builder { self.documentation = Some(docs); } - if let Some(label) = function.label(ctx.db) { + if let Some(label) = function_label(ctx, function) { self.detail = Some(label); } @@ -303,6 +306,26 @@ impl Into> for Completions { } } +fn function_label(ctx: &CompletionContext, function: hir::Function) -> Option { + let node = function.source(ctx.db).1; + + let label: String = if let Some(body) = node.body() { + let body_range = body.syntax().range(); + let label: String = node + .syntax() + .children() + .filter(|child| !child.range().is_subrange(&body_range)) // Filter out body + .filter(|child| ast::Comment::cast(child).is_none()) // Filter out comments + .map(|node| node.text().to_string()) + .collect(); + label + } else { + node.syntax().text().to_string() + }; + + Some(label.trim().to_owned()) +} + #[cfg(test)] pub(crate) fn check_completion(test_name: &str, code: &str, kind: CompletionKind) { use crate::mock_analysis::{single_file_with_position, analysis_and_position}; -- cgit v1.2.3