Move label from hir to ide_api

This commit is contained in:
Jeremy A. Kolb 2019-01-22 18:20:40 -05:00
parent 070a980224
commit a3472f8fe1
2 changed files with 25 additions and 24 deletions

View file

@ -366,28 +366,6 @@ impl Function {
Some(comments) Some(comments)
} }
} }
pub fn label(&self, db: &impl HirDatabase) -> Option<String> {
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)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]

View file

@ -1,7 +1,10 @@
use hir::PerNs; use hir::PerNs;
use crate::completion::completion_context::CompletionContext; use crate::completion::completion_context::CompletionContext;
use ra_syntax::TextRange; use ra_syntax::{
ast::{self, AstNode},
TextRange
};
use ra_text_edit::TextEdit; use ra_text_edit::TextEdit;
/// `CompletionItem` describes a single completion variant in the editor pop-up. /// `CompletionItem` describes a single completion variant in the editor pop-up.
@ -263,7 +266,7 @@ impl Builder {
self.documentation = Some(docs); self.documentation = Some(docs);
} }
if let Some(label) = function.label(ctx.db) { if let Some(label) = function_label(ctx, function) {
self.detail = Some(label); self.detail = Some(label);
} }
@ -303,6 +306,26 @@ impl Into<Vec<CompletionItem>> for Completions {
} }
} }
fn function_label(ctx: &CompletionContext, function: hir::Function) -> Option<String> {
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)] #[cfg(test)]
pub(crate) fn check_completion(test_name: &str, code: &str, kind: CompletionKind) { pub(crate) fn check_completion(test_name: &str, code: &str, kind: CompletionKind) {
use crate::mock_analysis::{single_file_with_position, analysis_and_position}; use crate::mock_analysis::{single_file_with_position, analysis_and_position};