From b5404514834a27c682dc22d86bc5585c0cae3076 Mon Sep 17 00:00:00 2001 From: Jeremy Kolb Date: Tue, 22 Jan 2019 08:55:05 -0500 Subject: Move docs to Function --- crates/ra_hir/src/code_model_api.rs | 21 +++++++++++++++------ crates/ra_hir/src/code_model_impl/function.rs | 5 +---- crates/ra_ide_api/src/completion/completion_item.rs | 5 ++--- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index 57f405f4f..9ae620efd 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -2,7 +2,7 @@ use std::sync::Arc; use relative_path::RelativePathBuf; use ra_db::{CrateId, FileId}; -use ra_syntax::{ast, TreeArc, SyntaxNode}; +use ra_syntax::{ast::{self, AstNode, DocCommentsOwner}, TreeArc, SyntaxNode}; use crate::{ Name, DefId, Path, PerNs, ScopesWithSyntaxMapping, Ty, HirFileId, @@ -297,7 +297,6 @@ pub struct FnSignature { /// True if the first param is `self`. This is relevant to decide whether this /// can be called as a method. pub(crate) has_self_param: bool, - pub(crate) documentation: String, } impl FnSignature { @@ -318,10 +317,6 @@ impl FnSignature { pub fn has_self_param(&self) -> bool { self.has_self_param } - - pub fn documentation(&self) -> &String { - &self.documentation - } } impl Function { @@ -357,6 +352,20 @@ impl Function { pub fn generic_params(&self, db: &impl HirDatabase) -> Arc { db.generic_params(self.def_id) } + + pub fn docs(&self, db: &impl HirDatabase) -> Option { + let def_loc = self.def_id.loc(db); + let syntax = db.file_item(def_loc.source_item_id); + let fn_def = ast::FnDef::cast(&syntax).expect("fn def should point to FnDef node"); + + // doc_comment_text unconditionally returns a String + let comments = fn_def.doc_comment_text(); + if comments.is_empty() { + None + } else { + Some(comments) + } + } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] diff --git a/crates/ra_hir/src/code_model_impl/function.rs b/crates/ra_hir/src/code_model_impl/function.rs index c848f7a82..c68c6bfbf 100644 --- a/crates/ra_hir/src/code_model_impl/function.rs +++ b/crates/ra_hir/src/code_model_impl/function.rs @@ -2,7 +2,7 @@ mod scope; use std::sync::Arc; -use ra_syntax::{TreeArc, ast::{self, NameOwner, DocCommentsOwner}}; +use ra_syntax::{TreeArc, ast::{self, NameOwner}}; use crate::{ DefId, HirDatabase, Name, AsName, Function, FnSignature, Module, @@ -73,14 +73,11 @@ impl FnSignature { TypeRef::unit() }; - let comments = node.doc_comment_text(); - let sig = FnSignature { name, params, ret_type, has_self_param, - documentation: comments, }; Arc::new(sig) } diff --git a/crates/ra_ide_api/src/completion/completion_item.rs b/crates/ra_ide_api/src/completion/completion_item.rs index dfaadbb20..d70c36889 100644 --- a/crates/ra_ide_api/src/completion/completion_item.rs +++ b/crates/ra_ide_api/src/completion/completion_item.rs @@ -259,9 +259,8 @@ impl Builder { } self.insert_text_format = InsertTextFormat::Snippet; } - let sig = function.signature(ctx.db); - if !sig.documentation().is_empty() { - self.documentation = Some(sig.documentation().clone()); + if let Some(docs) = function.docs(ctx.db) { + self.documentation = Some(docs); } self.kind = Some(CompletionItemKind::Function); -- cgit v1.2.3