diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-24 01:22:29 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-24 01:22:29 +0000 |
commit | 6a0a4a564accb12b48e703245655e3e3a0637445 (patch) | |
tree | 248097d8ceaded63601e3d663ef795e3bae55bfe /crates/ra_hir | |
parent | bf9cd6ee30b3044b61e99e24e82fad56d3965417 (diff) | |
parent | f87ce73579759fdb623f1d8d82880c0d6306746e (diff) |
Merge #621
621: Completion docs for code model r=kjeremy a=kjeremy
Adds a way to access documentation through the code model and exposes it to completions. Also allows us to document enum variants.
Co-authored-by: Jeremy A. Kolb <[email protected]>
Co-authored-by: Jeremy Kolb <[email protected]>
Diffstat (limited to 'crates/ra_hir')
-rw-r--r-- | crates/ra_hir/src/code_model_api.rs | 61 | ||||
-rw-r--r-- | crates/ra_hir/src/docs.rs | 36 | ||||
-rw-r--r-- | crates/ra_hir/src/lib.rs | 2 |
3 files changed, 86 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..3ff07bd60 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 | ||
3 | use relative_path::RelativePathBuf; | 3 | use relative_path::RelativePathBuf; |
4 | use ra_db::{CrateId, FileId}; | 4 | use ra_db::{CrateId, FileId}; |
5 | use ra_syntax::{ast::{self, AstNode, DocCommentsOwner}, TreeArc, SyntaxNode}; | 5 | use ra_syntax::{ast::self, TreeArc, SyntaxNode}; |
6 | 6 | ||
7 | use crate::{ | 7 | use 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 | ||
212 | impl 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)] |
212 | pub struct Enum { | 219 | pub 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 | ||
249 | impl 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)] |
243 | pub struct EnumVariant { | 256 | pub 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 | ||
297 | impl 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)] |
285 | pub struct Function { | 304 | pub 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> { | 376 | impl 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 | ||
@@ -383,6 +394,12 @@ impl Const { | |||
383 | } | 394 | } |
384 | } | 395 | } |
385 | 396 | ||
397 | impl Docs for Const { | ||
398 | fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> { | ||
399 | docs_from_ast(&*self.source(db).1) | ||
400 | } | ||
401 | } | ||
402 | |||
386 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 403 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
387 | pub struct Static { | 404 | pub struct Static { |
388 | pub(crate) def_id: DefId, | 405 | pub(crate) def_id: DefId, |
@@ -398,6 +415,12 @@ impl Static { | |||
398 | } | 415 | } |
399 | } | 416 | } |
400 | 417 | ||
418 | impl Docs for Static { | ||
419 | fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> { | ||
420 | docs_from_ast(&*self.source(db).1) | ||
421 | } | ||
422 | } | ||
423 | |||
401 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 424 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
402 | pub struct Trait { | 425 | pub struct Trait { |
403 | pub(crate) def_id: DefId, | 426 | pub(crate) def_id: DefId, |
@@ -417,6 +440,12 @@ impl Trait { | |||
417 | } | 440 | } |
418 | } | 441 | } |
419 | 442 | ||
443 | impl Docs for Trait { | ||
444 | fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> { | ||
445 | docs_from_ast(&*self.source(db).1) | ||
446 | } | ||
447 | } | ||
448 | |||
420 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | 449 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] |
421 | pub struct Type { | 450 | pub struct Type { |
422 | pub(crate) def_id: DefId, | 451 | pub(crate) def_id: DefId, |
@@ -435,3 +464,9 @@ impl Type { | |||
435 | db.generic_params(self.def_id) | 464 | db.generic_params(self.def_id) |
436 | } | 465 | } |
437 | } | 466 | } |
467 | |||
468 | impl Docs for Type { | ||
469 | fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> { | ||
470 | docs_from_ast(&*self.source(db).1) | ||
471 | } | ||
472 | } | ||
diff --git a/crates/ra_hir/src/docs.rs b/crates/ra_hir/src/docs.rs new file mode 100644 index 000000000..b1b47af9e --- /dev/null +++ b/crates/ra_hir/src/docs.rs | |||
@@ -0,0 +1,36 @@ | |||
1 | use ra_syntax::ast; | ||
2 | |||
3 | use crate::HirDatabase; | ||
4 | |||
5 | /// Holds documentation | ||
6 | #[derive(Debug, Clone)] | ||
7 | pub struct Documentation(String); | ||
8 | |||
9 | impl Documentation { | ||
10 | pub fn new(s: &str) -> Self { | ||
11 | Self(s.into()) | ||
12 | } | ||
13 | |||
14 | pub fn contents(&self) -> &str { | ||
15 | &self.0 | ||
16 | } | ||
17 | } | ||
18 | |||
19 | impl Into<String> for Documentation { | ||
20 | fn into(self) -> String { | ||
21 | self.contents().into() | ||
22 | } | ||
23 | } | ||
24 | |||
25 | pub trait Docs { | ||
26 | fn docs(&self, db: &impl HirDatabase) -> Option<Documentation>; | ||
27 | } | ||
28 | |||
29 | pub(crate) fn docs_from_ast(node: &impl ast::DocCommentsOwner) -> Option<Documentation> { | ||
30 | let comments = node.doc_comment_text(); | ||
31 | if comments.is_empty() { | ||
32 | None | ||
33 | } else { | ||
34 | Some(Documentation::new(&comments)) | ||
35 | } | ||
36 | } | ||
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index a861ee88e..f517f71e0 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs | |||
@@ -23,6 +23,7 @@ mod ty; | |||
23 | mod impl_block; | 23 | mod impl_block; |
24 | mod expr; | 24 | mod expr; |
25 | mod generics; | 25 | mod generics; |
26 | mod docs; | ||
26 | 27 | ||
27 | mod code_model_api; | 28 | mod code_model_api; |
28 | mod code_model_impl; | 29 | mod code_model_impl; |
@@ -45,6 +46,7 @@ pub use self::{ | |||
45 | ty::Ty, | 46 | ty::Ty, |
46 | impl_block::{ImplBlock, ImplItem}, | 47 | impl_block::{ImplBlock, ImplItem}, |
47 | code_model_impl::function::{FnScopes, ScopesWithSyntaxMapping}, | 48 | code_model_impl::function::{FnScopes, ScopesWithSyntaxMapping}, |
49 | docs::{Docs, Documentation} | ||
48 | }; | 50 | }; |
49 | 51 | ||
50 | pub use self::code_model_api::{ | 52 | pub use self::code_model_api::{ |