From 576625f0a1c6e54075d173db7e691d75077ca677 Mon Sep 17 00:00:00 2001 From: "Jeremy A. Kolb" Date: Wed, 23 Jan 2019 16:22:10 -0500 Subject: Add way of getting docs from the code model and use for completion --- crates/ra_ide_api/src/completion/complete_path.rs | 5 ++++- crates/ra_ide_api/src/completion/completion_item.rs | 13 +++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) (limited to 'crates/ra_ide_api/src/completion') diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs index 6bed299d2..aeb226847 100644 --- a/crates/ra_ide_api/src/completion/complete_path.rs +++ b/crates/ra_ide_api/src/completion/complete_path.rs @@ -2,6 +2,8 @@ use crate::{ completion::{CompletionItem, CompletionItemKind, Completions, CompletionKind, CompletionContext}, }; +use hir::Docs; + pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { let (path, module) = match (&ctx.path_prefix, &ctx.module) { (Some(path), Some(module)) => (path.clone(), module), @@ -27,13 +29,14 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { hir::Def::Enum(e) => { e.variants(ctx.db) .into_iter() - .for_each(|(variant_name, _variant)| { + .for_each(|(variant_name, variant)| { CompletionItem::new( CompletionKind::Reference, ctx.source_range(), variant_name.to_string(), ) .kind(CompletionItemKind::EnumVariant) + .set_documentation(variant.docs(ctx.db)) .add_to(acc) }); } diff --git a/crates/ra_ide_api/src/completion/completion_item.rs b/crates/ra_ide_api/src/completion/completion_item.rs index e3bf82304..8e0be4c4b 100644 --- a/crates/ra_ide_api/src/completion/completion_item.rs +++ b/crates/ra_ide_api/src/completion/completion_item.rs @@ -1,4 +1,4 @@ -use hir::PerNs; +use hir::{Docs, Documentation, PerNs}; use crate::completion::completion_context::CompletionContext; use ra_syntax::{ @@ -19,7 +19,7 @@ pub struct CompletionItem { label: String, kind: Option, detail: Option, - documentation: Option, + documentation: Option, lookup: Option, insert_text: Option, insert_text_format: InsertTextFormat, @@ -98,7 +98,7 @@ impl CompletionItem { } /// A doc-comment pub fn documentation(&self) -> Option<&str> { - self.documentation.as_ref().map(|it| it.as_str()) + self.documentation.as_ref().map(|it| it.contents()) } /// What string is used for filtering. pub fn lookup(&self) -> &str { @@ -137,7 +137,7 @@ pub(crate) struct Builder { insert_text: Option, insert_text_format: InsertTextFormat, detail: Option, - documentation: Option, + documentation: Option, lookup: Option, kind: Option, text_edit: Option, @@ -197,10 +197,10 @@ impl Builder { self } #[allow(unused)] - pub(crate) fn documentation(self, docs: impl Into) -> Builder { + pub(crate) fn documentation(self, docs: Documentation) -> Builder { self.set_documentation(Some(docs)) } - pub(crate) fn set_documentation(mut self, docs: Option>) -> Builder { + pub(crate) fn set_documentation(mut self, docs: Option) -> Builder { self.documentation = docs.map(Into::into); self } @@ -265,6 +265,7 @@ impl Builder { } self.insert_text_format = InsertTextFormat::Snippet; } + if let Some(docs) = function.docs(ctx.db) { self.documentation = Some(docs); } -- cgit v1.2.3 From 7b548de63405a07c1a4aa6aedb3e1993fe6b1e6c Mon Sep 17 00:00:00 2001 From: "Jeremy A. Kolb" Date: Wed, 23 Jan 2019 17:46:14 -0500 Subject: Add a bunch of new documentation to completions --- .../ra_ide_api/src/completion/completion_item.rs | 30 ++++++++++++---------- .../completion_item__reference_completion.snap | 20 ++++++++++----- 2 files changed, 30 insertions(+), 20 deletions(-) (limited to 'crates/ra_ide_api/src/completion') diff --git a/crates/ra_ide_api/src/completion/completion_item.rs b/crates/ra_ide_api/src/completion/completion_item.rs index 8e0be4c4b..18c151932 100644 --- a/crates/ra_ide_api/src/completion/completion_item.rs +++ b/crates/ra_ide_api/src/completion/completion_item.rs @@ -210,35 +210,35 @@ impl Builder { resolution: &hir::Resolution, ) -> Builder { let resolved = resolution.def_id.map(|d| d.resolve(ctx.db)); - let kind = match resolved { + let (kind, docs) = match resolved { PerNs { types: Some(hir::Def::Module(..)), .. - } => CompletionItemKind::Module, + } => (CompletionItemKind::Module, None), PerNs { - types: Some(hir::Def::Struct(..)), + types: Some(hir::Def::Struct(s)), .. - } => CompletionItemKind::Struct, + } => (CompletionItemKind::Struct, s.docs(ctx.db)), PerNs { - types: Some(hir::Def::Enum(..)), + types: Some(hir::Def::Enum(e)), .. - } => CompletionItemKind::Enum, + } => (CompletionItemKind::Enum, e.docs(ctx.db)), PerNs { - types: Some(hir::Def::Trait(..)), + types: Some(hir::Def::Trait(t)), .. - } => CompletionItemKind::Trait, + } => (CompletionItemKind::Trait, t.docs(ctx.db)), PerNs { - types: Some(hir::Def::Type(..)), + types: Some(hir::Def::Type(t)), .. - } => CompletionItemKind::TypeAlias, + } => (CompletionItemKind::TypeAlias, t.docs(ctx.db)), PerNs { - values: Some(hir::Def::Const(..)), + values: Some(hir::Def::Const(c)), .. - } => CompletionItemKind::Const, + } => (CompletionItemKind::Const, c.docs(ctx.db)), PerNs { - values: Some(hir::Def::Static(..)), + values: Some(hir::Def::Static(s)), .. - } => CompletionItemKind::Static, + } => (CompletionItemKind::Static, s.docs(ctx.db)), PerNs { values: Some(hir::Def::Function(function)), .. @@ -246,6 +246,8 @@ impl Builder { _ => return self, }; self.kind = Some(kind); + self.documentation = docs; + self } diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__reference_completion.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__reference_completion.snap index 0180a4f44..9779541b3 100644 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__reference_completion.snap +++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__reference_completion.snap @@ -1,6 +1,6 @@ --- -created: "2019-01-22T14:45:00.717917+00:00" -creator: insta@0.4.0 +created: "2019-01-23T21:14:09.186661600+00:00" +creator: insta@0.5.1 expression: kind_completions source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" --- @@ -12,11 +12,15 @@ source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" EnumVariant ), detail: None, - documentation: None, + documentation: Some( + Documentation( + "This is foo" + ) + ), lookup: None, insert_text: None, insert_text_format: PlainText, - source_range: [47; 47), + source_range: [109; 109), text_edit: None }, CompletionItem { @@ -26,11 +30,15 @@ source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" EnumVariant ), detail: None, - documentation: None, + documentation: Some( + Documentation( + "Use when we need an `i32`" + ) + ), lookup: None, insert_text: None, insert_text_format: PlainText, - source_range: [47; 47), + source_range: [109; 109), text_edit: None } ] -- cgit v1.2.3 From 30f0736e646fe7cf4e133c78f0417e5bd844c525 Mon Sep 17 00:00:00 2001 From: "Jeremy A. Kolb" Date: Wed, 23 Jan 2019 18:50:46 -0500 Subject: Fix snap --- crates/ra_ide_api/src/completion/complete_path.rs | 8 +++++++- .../snapshots/completion_item__reference_completion.snap | 10 +++++----- 2 files changed, 12 insertions(+), 6 deletions(-) (limited to 'crates/ra_ide_api/src/completion') diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs index aeb226847..e44b76c4a 100644 --- a/crates/ra_ide_api/src/completion/complete_path.rs +++ b/crates/ra_ide_api/src/completion/complete_path.rs @@ -119,7 +119,13 @@ mod tests { "reference_completion", " //- /lib.rs - enum E { Foo, Bar(i32) } + /// An enum + enum E { + /// Foo Variant + Foo, + /// Bar Variant with i32 + Bar(i32) + } fn foo() { let _ = E::<|> } ", ); diff --git a/crates/ra_ide_api/src/completion/snapshots/completion_item__reference_completion.snap b/crates/ra_ide_api/src/completion/snapshots/completion_item__reference_completion.snap index 9779541b3..e46f7807b 100644 --- a/crates/ra_ide_api/src/completion/snapshots/completion_item__reference_completion.snap +++ b/crates/ra_ide_api/src/completion/snapshots/completion_item__reference_completion.snap @@ -1,5 +1,5 @@ --- -created: "2019-01-23T21:14:09.186661600+00:00" +created: "2019-01-23T23:49:43.278245900+00:00" creator: insta@0.5.1 expression: kind_completions source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" @@ -14,13 +14,13 @@ source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" detail: None, documentation: Some( Documentation( - "This is foo" + "Foo Variant" ) ), lookup: None, insert_text: None, insert_text_format: PlainText, - source_range: [109; 109), + source_range: [116; 116), text_edit: None }, CompletionItem { @@ -32,13 +32,13 @@ source: "crates\\ra_ide_api\\src\\completion\\completion_item.rs" detail: None, documentation: Some( Documentation( - "Use when we need an `i32`" + "Bar Variant with i32" ) ), lookup: None, insert_text: None, insert_text_format: PlainText, - source_range: [109; 109), + source_range: [116; 116), text_edit: None } ] -- cgit v1.2.3