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 --- crates/ra_hir/src/code_model_api.rs | 22 ------------------ .../ra_ide_api/src/completion/completion_item.rs | 27 ++++++++++++++++++++-- 2 files changed, 25 insertions(+), 24 deletions(-) (limited to 'crates') 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 { Some(comments) } } - - pub fn label(&self, db: &impl HirDatabase) -> Option { - let def_loc = self.def_id.loc(db); - let syntax = db.file_item(def_loc.source_item_id); - let node = ast::FnDef::cast(&syntax).expect("fn def should point to FnDef node"); - - 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()) - } } #[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 @@ 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