]> git.lizzy.rs Git - rust.git/commitdiff
Move label from hir to ide_api
authorJeremy A. Kolb <jkolb@ara.com>
Tue, 22 Jan 2019 23:20:40 +0000 (18:20 -0500)
committerJeremy A. Kolb <jkolb@ara.com>
Tue, 22 Jan 2019 23:20:40 +0000 (18:20 -0500)
crates/ra_hir/src/code_model_api.rs
crates/ra_ide_api/src/completion/completion_item.rs

index 21ca36265d18829fb8c13843571d942b17c50a1d..9ae620efd4c0647cb40ec277b07d34d0cb4de181 100644 (file)
@@ -366,28 +366,6 @@ pub fn docs(&self, db: &impl HirDatabase) -> Option<String> {
             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)]
index b720a138210bcb2e5f76684119e2d310aafc7278..680fd8d1b5d3d45bcdafbd0e10950dc34ad63bcf 100644 (file)
@@ -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 @@ pub(super) fn from_function(
             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 @@ fn into(self) -> Vec<CompletionItem> {
     }
 }
 
+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)]
 pub(crate) fn check_completion(test_name: &str, code: &str, kind: CompletionKind) {
     use crate::mock_analysis::{single_file_with_position, analysis_and_position};