From a0cdb4fe4485681109080fea97b96329266b3b1f Mon Sep 17 00:00:00 2001 From: Tad Hardesty Date: Sun, 14 Jul 2019 20:18:33 -0700 Subject: [PATCH] Return documentation with completion results --- src/dreammaker/preprocessor.rs | 9 +++++++++ src/langserver/completion.rs | 18 +++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/dreammaker/preprocessor.rs b/src/dreammaker/preprocessor.rs index 53f80afe..eedba07b 100644 --- a/src/dreammaker/preprocessor.rs +++ b/src/dreammaker/preprocessor.rs @@ -32,6 +32,15 @@ pub enum Define { }, } +impl Define { + pub fn docs(&self) -> &DocCollection { + match self { + Define::Constant { docs, .. } => docs, + Define::Function { docs, .. } => docs, + } + } +} + impl fmt::Display for Define { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { let subst = match self { diff --git a/src/langserver/completion.rs b/src/langserver/completion.rs index 1fdbb067..2432dfd8 100644 --- a/src/langserver/completion.rs +++ b/src/langserver/completion.rs @@ -29,11 +29,12 @@ pub fn item_var(ty: TypeRef, name: &str, var: &TypeVar) -> CompletionItem { label: name.to_owned(), kind: Some(CompletionItemKind::Field), detail: Some(detail), + documentation: item_documentation(&var.value.docs), .. Default::default() } } -pub fn item_proc(ty: TypeRef, name: &str, _proc: &TypeProc) -> CompletionItem { +pub fn item_proc(ty: TypeRef, name: &str, proc: &TypeProc) -> CompletionItem { CompletionItem { label: name.to_owned(), kind: Some(if ty.is_root() { @@ -44,10 +45,22 @@ pub fn item_proc(ty: TypeRef, name: &str, _proc: &TypeProc) -> CompletionItem { CompletionItemKind::Method }), detail: Some(ty.pretty_path().to_owned()), + documentation: item_documentation(&proc.main_value().docs), ..Default::default() } } +pub fn item_documentation(docs: &dm::docs::DocCollection) -> Option { + if docs.is_empty() { + return None; + } + + Some(Documentation::MarkupContent(MarkupContent { + kind: MarkupKind::Markdown, + value: docs.text(), + })) +} + pub fn items_ty<'a>( results: &mut Vec, skip: &mut HashSet<(&str, &'a String)>, @@ -195,6 +208,7 @@ impl<'a, W: io::ResponseWrite> Engine<'a, W> { results.push(CompletionItem { label: child.name.to_owned(), kind: Some(CompletionItemKind::Class), + documentation: item_documentation(&child.docs), .. Default::default() }); } @@ -282,6 +296,7 @@ impl<'a, W: io::ResponseWrite> Engine<'a, W> { results.push(CompletionItem { label: child.name.to_owned(), kind: Some(CompletionItemKind::Class), + documentation: item_documentation(&child.docs), .. Default::default() }); } @@ -382,6 +397,7 @@ impl<'a, W: io::ResponseWrite> Engine<'a, W> { label: name.to_owned(), kind: Some(CompletionItemKind::Constant), detail: Some(format!("{}", define)), + documentation: item_documentation(define.docs()), .. Default::default() }); }