mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 04:44:57 +00:00
Add docs to struct fields
This commit is contained in:
parent
55906341a6
commit
7a0bc2dd64
6 changed files with 26 additions and 8 deletions
|
@ -216,6 +216,15 @@ impl StructField {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Docs for StructField {
|
||||||
|
fn docs(&self, db: &impl HirDatabase) -> Option<Documentation> {
|
||||||
|
match self.source(db).1 {
|
||||||
|
FieldSource::Named(named) => docs_from_ast(&*named),
|
||||||
|
FieldSource::Pos(..) => return None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
pub struct Struct {
|
pub struct Struct {
|
||||||
pub(crate) id: StructId,
|
pub(crate) id: StructId,
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use hir::{Ty, AdtDef};
|
use hir::{Ty, AdtDef, Docs};
|
||||||
|
|
||||||
use crate::completion::{CompletionContext, Completions, CompletionItem, CompletionItemKind};
|
use crate::completion::{CompletionContext, Completions, CompletionItem, CompletionItemKind};
|
||||||
use crate::completion::completion_item::CompletionKind;
|
use crate::completion::completion_item::CompletionKind;
|
||||||
|
@ -38,6 +38,7 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: Ty)
|
||||||
)
|
)
|
||||||
.kind(CompletionItemKind::Field)
|
.kind(CompletionItemKind::Field)
|
||||||
.detail(field.ty(ctx.db).subst(substs).to_string())
|
.detail(field.ty(ctx.db).subst(substs).to_string())
|
||||||
|
.set_documentation(field.docs(ctx.db))
|
||||||
.add_to(acc);
|
.add_to(acc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -107,7 +108,10 @@ mod tests {
|
||||||
check_ref_completion(
|
check_ref_completion(
|
||||||
"struct_field_completion_self",
|
"struct_field_completion_self",
|
||||||
r"
|
r"
|
||||||
struct A { the_field: (u32,) }
|
struct A {
|
||||||
|
/// This is the_field
|
||||||
|
the_field: (u32,)
|
||||||
|
}
|
||||||
impl A {
|
impl A {
|
||||||
fn foo(self) {
|
fn foo(self) {
|
||||||
self.<|>
|
self.<|>
|
||||||
|
|
|
@ -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
|
creator: insta@0.5.2
|
||||||
expression: kind_completions
|
expression: kind_completions
|
||||||
source: crates/ra_ide_api/src/completion/completion_item.rs
|
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(
|
detail: Some(
|
||||||
"(u32,)"
|
"(u32,)"
|
||||||
),
|
),
|
||||||
documentation: None,
|
documentation: Some(
|
||||||
|
Documentation(
|
||||||
|
"This is the_field"
|
||||||
|
)
|
||||||
|
),
|
||||||
lookup: None,
|
lookup: None,
|
||||||
insert_text: None,
|
insert_text: None,
|
||||||
insert_text_format: PlainText,
|
insert_text_format: PlainText,
|
||||||
source_range: [121; 121),
|
source_range: [187; 187),
|
||||||
text_edit: None
|
text_edit: None
|
||||||
},
|
},
|
||||||
CompletionItem {
|
CompletionItem {
|
||||||
|
@ -36,7 +40,7 @@ source: crates/ra_ide_api/src/completion/completion_item.rs
|
||||||
"foo()$0"
|
"foo()$0"
|
||||||
),
|
),
|
||||||
insert_text_format: Snippet,
|
insert_text_format: Snippet,
|
||||||
source_range: [121; 121),
|
source_range: [187; 187),
|
||||||
text_edit: None
|
text_edit: None
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
|
@ -2248,6 +2248,7 @@ impl ToOwned for NamedFieldDef {
|
||||||
impl ast::VisibilityOwner for NamedFieldDef {}
|
impl ast::VisibilityOwner for NamedFieldDef {}
|
||||||
impl ast::NameOwner for NamedFieldDef {}
|
impl ast::NameOwner for NamedFieldDef {}
|
||||||
impl ast::AttrsOwner for NamedFieldDef {}
|
impl ast::AttrsOwner for NamedFieldDef {}
|
||||||
|
impl ast::DocCommentsOwner for NamedFieldDef {}
|
||||||
impl NamedFieldDef {
|
impl NamedFieldDef {
|
||||||
pub fn type_ref(&self) -> Option<&TypeRef> {
|
pub fn type_ref(&self) -> Option<&TypeRef> {
|
||||||
super::child_opt(self)
|
super::child_opt(self)
|
||||||
|
|
|
@ -268,7 +268,7 @@ Grammar(
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
"NamedFieldDefList": (collections: [["fields", "NamedFieldDef"]]),
|
"NamedFieldDefList": (collections: [["fields", "NamedFieldDef"]]),
|
||||||
"NamedFieldDef": ( traits: ["VisibilityOwner", "NameOwner", "AttrsOwner"], options: ["TypeRef"] ),
|
"NamedFieldDef": ( traits: ["VisibilityOwner", "NameOwner", "AttrsOwner", "DocCommentsOwner"], options: ["TypeRef"] ),
|
||||||
"PosFieldList": (collections: [["fields", "PosField"]]),
|
"PosFieldList": (collections: [["fields", "PosField"]]),
|
||||||
"PosField": ( traits: ["VisibilityOwner", "AttrsOwner"], options: ["TypeRef"]),
|
"PosField": ( traits: ["VisibilityOwner", "AttrsOwner"], options: ["TypeRef"]),
|
||||||
"EnumDef": ( traits: [
|
"EnumDef": ( traits: [
|
||||||
|
|
|
@ -250,7 +250,7 @@ fn n_attached_trivias<'a>(
|
||||||
) -> usize {
|
) -> usize {
|
||||||
match kind {
|
match kind {
|
||||||
CONST_DEF | TYPE_DEF | STRUCT_DEF | ENUM_DEF | ENUM_VARIANT | FN_DEF | TRAIT_DEF
|
CONST_DEF | TYPE_DEF | STRUCT_DEF | ENUM_DEF | ENUM_VARIANT | FN_DEF | TRAIT_DEF
|
||||||
| MODULE => {
|
| MODULE | NAMED_FIELD_DEF => {
|
||||||
let mut res = 0;
|
let mut res = 0;
|
||||||
for (i, (kind, text)) in trivias.enumerate() {
|
for (i, (kind, text)) in trivias.enumerate() {
|
||||||
match kind {
|
match kind {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue