aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/code_model_api.rs
diff options
context:
space:
mode:
authorJeremy A. Kolb <[email protected]>2019-01-23 21:22:10 +0000
committerJeremy A. Kolb <[email protected]>2019-01-23 21:32:04 +0000
commit576625f0a1c6e54075d173db7e691d75077ca677 (patch)
tree72c1fadf48265e7f5355eb72586de4bcdab43cec /crates/ra_hir/src/code_model_api.rs
parent6a6ce2bc959c346aed7a98c8509d1aa3d19ec8a9 (diff)
Add way of getting docs from the code model and use for completion
Diffstat (limited to 'crates/ra_hir/src/code_model_api.rs')
-rw-r--r--crates/ra_hir/src/code_model_api.rs37
1 files changed, 24 insertions, 13 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs
index 9ae620efd..333d117f1 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;
2 2
3use relative_path::RelativePathBuf; 3use relative_path::RelativePathBuf;
4use ra_db::{CrateId, FileId}; 4use ra_db::{CrateId, FileId};
5use ra_syntax::{ast::{self, AstNode, DocCommentsOwner}, TreeArc, SyntaxNode}; 5use ra_syntax::{ast::self, TreeArc, SyntaxNode};
6 6
7use crate::{ 7use crate::{
8 Name, DefId, Path, PerNs, ScopesWithSyntaxMapping, Ty, HirFileId, 8 Name, DefId, Path, PerNs, ScopesWithSyntaxMapping, Ty, HirFileId,
@@ -14,6 +14,7 @@ use crate::{
14 adt::VariantData, 14 adt::VariantData,
15 generics::GenericParams, 15 generics::GenericParams,
16 code_model_impl::def_id_to_ast, 16 code_model_impl::def_id_to_ast,
17 docs::{Documentation, Docs, docs_from_ast}
17}; 18};
18 19
19/// hir::Crate describes a single crate. It's the main interface with which 20/// hir::Crate describes a single crate. It's the main interface with which
@@ -208,6 +209,12 @@ impl Struct {
208 } 209 }
209} 210}
210 211
212impl Docs for Struct {
213 fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
214 docs_from_ast(&*self.source(db).1)
215 }
216}
217
211#[derive(Debug, Clone, PartialEq, Eq, Hash)] 218#[derive(Debug, Clone, PartialEq, Eq, Hash)]
212pub struct Enum { 219pub struct Enum {
213 pub(crate) def_id: DefId, 220 pub(crate) def_id: DefId,
@@ -239,6 +246,12 @@ impl Enum {
239 } 246 }
240} 247}
241 248
249impl Docs for Enum {
250 fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
251 docs_from_ast(&*self.source(db).1)
252 }
253}
254
242#[derive(Debug, Clone, PartialEq, Eq, Hash)] 255#[derive(Debug, Clone, PartialEq, Eq, Hash)]
243pub struct EnumVariant { 256pub struct EnumVariant {
244 pub(crate) def_id: DefId, 257 pub(crate) def_id: DefId,
@@ -281,6 +294,12 @@ impl EnumVariant {
281 } 294 }
282} 295}
283 296
297impl Docs for EnumVariant {
298 fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
299 docs_from_ast(&*self.source(db).1)
300 }
301}
302
284#[derive(Debug, Clone, PartialEq, Eq, Hash)] 303#[derive(Debug, Clone, PartialEq, Eq, Hash)]
285pub struct Function { 304pub struct Function {
286 pub(crate) def_id: DefId, 305 pub(crate) def_id: DefId,
@@ -352,19 +371,11 @@ impl Function {
352 pub fn generic_params(&self, db: &impl HirDatabase) -> Arc<GenericParams> { 371 pub fn generic_params(&self, db: &impl HirDatabase) -> Arc<GenericParams> {
353 db.generic_params(self.def_id) 372 db.generic_params(self.def_id)
354 } 373 }
374}
355 375
356 pub fn docs(&self, db: &impl HirDatabase) -> Option<String> { 376impl Docs for Function {
357 let def_loc = self.def_id.loc(db); 377 fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
358 let syntax = db.file_item(def_loc.source_item_id); 378 docs_from_ast(&*self.source(db).1)
359 let fn_def = ast::FnDef::cast(&syntax).expect("fn def should point to FnDef node");
360
361 // doc_comment_text unconditionally returns a String
362 let comments = fn_def.doc_comment_text();
363 if comments.is_empty() {
364 None
365 } else {
366 Some(comments)
367 }
368 } 379 }
369} 380}
370 381