From 7a0bc2dd6402d681223be4198ec68ee7bb65d9ee Mon Sep 17 00:00:00 2001 From: "Jeremy A. Kolb" Date: Fri, 25 Jan 2019 14:29:56 -0500 Subject: Add docs to struct fields --- crates/ra_hir/src/code_model_api.rs | 9 +++++++++ crates/ra_ide_api/src/completion/complete_dot.rs | 8 ++++++-- .../completion_item__struct_field_completion_self.snap | 12 ++++++++---- crates/ra_syntax/src/ast/generated.rs | 1 + crates/ra_syntax/src/grammar.ron | 2 +- crates/ra_syntax/src/parser_impl/event.rs | 2 +- 6 files changed, 26 insertions(+), 8 deletions(-) diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index 1e88bff8b..41bac05c6 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -216,6 +216,15 @@ impl StructField { } } +impl Docs for StructField { + fn docs(&self, db: &impl HirDatabase) -> Option { + match self.source(db).1 { + FieldSource::Named(named) => docs_from_ast(&*named), + FieldSource::Pos(..) => return None, + } + } +} + #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct Struct { pub(crate) id: StructId, diff --git a/crates/ra_ide_api/src/completion/complete_dot.rs b/crates/ra_ide_api/src/completion/complete_dot.rs index 060a46c5e..bad51cc51 100644 --- a/crates/ra_ide_api/src/completion/complete_dot.rs +++ b/crates/ra_ide_api/src/completion/complete_dot.rs @@ -1,4 +1,4 @@ -use hir::{Ty, AdtDef}; +use hir::{Ty, AdtDef, Docs}; use crate::completion::{CompletionContext, Completions, CompletionItem, CompletionItemKind}; use crate::completion::completion_item::CompletionKind; @@ -38,6 +38,7 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty) ) .kind(CompletionItemKind::Field) .detail(field.ty(ctx.db).subst(substs).to_string()) + .set_documentation(field.docs(ctx.db)) .add_to(acc); } } @@ -107,7 +108,10 @@ mod tests { check_ref_completion( "struct_field_completion_self", r" - struct A { the_field: (u32,) } + struct A { + /// This is the_field + the_field: (u32,) + } impl A { fn foo(self) { self.<|> diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion_self.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion_self.snap index 80e8f3df5..580e92a90 100644 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion_self.snap +++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__struct_field_completion_self.snap @@ -1,5 +1,5 @@ --- -created: "2019-01-23T13:19:23.501297515+00:00" +created: "2019-01-25T19:27:09.519688600+00:00" creator: insta@0.5.2 expression: kind_completions source: crates/ra_ide_api/src/completion/completion_item.rs @@ -14,11 +14,15 @@ source: crates/ra_ide_api/src/completion/completion_item.rs detail: Some( "(u32,)" ), - documentation: None, + documentation: Some( + Documentation( + "This is the_field" + ) + ), lookup: None, insert_text: None, insert_text_format: PlainText, - source_range: [121; 121), + source_range: [187; 187), text_edit: None }, CompletionItem { @@ -36,7 +40,7 @@ source: crates/ra_ide_api/src/completion/completion_item.rs "foo()$0" ), insert_text_format: Snippet, - source_range: [121; 121), + source_range: [187; 187), text_edit: None } ] diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index 7da19d782..e936c2a6d 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs @@ -2248,6 +2248,7 @@ impl ToOwned for NamedFieldDef { impl ast::VisibilityOwner for NamedFieldDef {} impl ast::NameOwner for NamedFieldDef {} impl ast::AttrsOwner for NamedFieldDef {} +impl ast::DocCommentsOwner for NamedFieldDef {} impl NamedFieldDef { pub fn type_ref(&self) -> Option<&TypeRef> { super::child_opt(self) diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron index cb5d6cde8..a92d9bbda 100644 --- a/crates/ra_syntax/src/grammar.ron +++ b/crates/ra_syntax/src/grammar.ron @@ -268,7 +268,7 @@ Grammar( ] ), "NamedFieldDefList": (collections: [["fields", "NamedFieldDef"]]), - "NamedFieldDef": ( traits: ["VisibilityOwner", "NameOwner", "AttrsOwner"], options: ["TypeRef"] ), + "NamedFieldDef": ( traits: ["VisibilityOwner", "NameOwner", "AttrsOwner", "DocCommentsOwner"], options: ["TypeRef"] ), "PosFieldList": (collections: [["fields", "PosField"]]), "PosField": ( traits: ["VisibilityOwner", "AttrsOwner"], options: ["TypeRef"]), "EnumDef": ( traits: [ diff --git a/crates/ra_syntax/src/parser_impl/event.rs b/crates/ra_syntax/src/parser_impl/event.rs index bd77fee89..33e10ef85 100644 --- a/crates/ra_syntax/src/parser_impl/event.rs +++ b/crates/ra_syntax/src/parser_impl/event.rs @@ -250,7 +250,7 @@ fn n_attached_trivias<'a>( ) -> usize { match kind { CONST_DEF | TYPE_DEF | STRUCT_DEF | ENUM_DEF | ENUM_VARIANT | FN_DEF | TRAIT_DEF - | MODULE => { + | MODULE | NAMED_FIELD_DEF => { let mut res = 0; for (i, (kind, text)) in trivias.enumerate() { match kind { -- cgit v1.2.3