From b88ba007cc2631799c2334753a7de807c548685e Mon Sep 17 00:00:00 2001 From: Jeremy Kolb Date: Tue, 29 Jan 2019 21:39:09 -0500 Subject: Pass Documentation up to LSP and add "rust" to our codeblocks there --- crates/ra_lsp_server/src/conv.rs | 19 +++++++------ crates/ra_lsp_server/src/lib.rs | 1 + crates/ra_lsp_server/src/main_loop/handlers.rs | 11 +++----- crates/ra_lsp_server/src/markdown.rs | 38 ++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 15 deletions(-) create mode 100644 crates/ra_lsp_server/src/markdown.rs (limited to 'crates/ra_lsp_server/src') diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs index 8c87f5195..c033ecdea 100644 --- a/crates/ra_lsp_server/src/conv.rs +++ b/crates/ra_lsp_server/src/conv.rs @@ -87,13 +87,6 @@ impl ConvWith for CompletionItem { None }; - let documentation = self.documentation().map(|value| { - Documentation::MarkupContent(MarkupContent { - kind: MarkupKind::Markdown, - value: value.to_string(), - }) - }); - let mut res = lsp_types::CompletionItem { label: self.label().to_string(), detail: self.detail().map(|it| it.to_string()), @@ -101,7 +94,7 @@ impl ConvWith for CompletionItem { kind: self.kind().map(|it| it.conv()), text_edit: Some(text_edit), additional_text_edits, - documentation: documentation, + documentation: self.documentation().map(|it| it.conv()), ..Default::default() }; res.insert_text_format = Some(match self.insert_text_format() { @@ -160,6 +153,16 @@ impl ConvWith for Range { } } +impl Conv for ra_ide_api::Documentation { + type Output = lsp_types::Documentation; + fn conv(self) -> Documentation { + Documentation::MarkupContent(MarkupContent { + kind: MarkupKind::Markdown, + value: crate::markdown::sanitize_markdown(self).into(), + }) + } +} + impl ConvWith for TextEdit { type Ctx = LineIndex; type Output = Vec; diff --git a/crates/ra_lsp_server/src/lib.rs b/crates/ra_lsp_server/src/lib.rs index f93d4b37d..5b5f3b948 100644 --- a/crates/ra_lsp_server/src/lib.rs +++ b/crates/ra_lsp_server/src/lib.rs @@ -2,6 +2,7 @@ mod caps; mod cargo_target_spec; mod conv; mod main_loop; +mod markdown; mod project_model; pub mod req; mod server_world; diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index 9478ebfb8..4f75f9a22 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -1,7 +1,7 @@ use gen_lsp_server::ErrorCode; use lsp_types::{ CodeActionResponse, CodeLens, Command, Diagnostic, DiagnosticSeverity, - DocumentFormattingParams, DocumentHighlight, DocumentSymbol, Documentation, FoldingRange, + DocumentFormattingParams, DocumentHighlight, DocumentSymbol, FoldingRange, FoldingRangeKind, FoldingRangeParams, Hover, HoverContents, Location, MarkupContent, MarkupKind, ParameterInformation, ParameterLabel, Position, PrepareRenameResponse, Range, RenameParams, SignatureInformation, SymbolInformation, TextDocumentIdentifier, TextEdit, @@ -401,12 +401,9 @@ pub fn handle_signature_help( documentation: None, }) .collect(); - let documentation = call_info.doc.map(|value| { - Documentation::MarkupContent(MarkupContent { - kind: MarkupKind::Markdown, - value, - }) - }); + + let documentation = call_info.doc.map(|it| it.conv()); + let sig_info = SignatureInformation { label: call_info.label, documentation, diff --git a/crates/ra_lsp_server/src/markdown.rs b/crates/ra_lsp_server/src/markdown.rs new file mode 100644 index 000000000..f505755e8 --- /dev/null +++ b/crates/ra_lsp_server/src/markdown.rs @@ -0,0 +1,38 @@ +use ra_ide_api::Documentation; + +pub(crate) fn sanitize_markdown(docs: Documentation) -> Documentation { + let docs: String = docs.into(); + + // Massage markdown + let mut processed_lines = Vec::new(); + let mut in_code_block = false; + for line in docs.lines() { + if line.starts_with("```") { + in_code_block = !in_code_block; + } + + let line = if in_code_block && line.starts_with("```") && !line.contains("rust") { + "```rust".into() + } else { + line.to_string() + }; + + processed_lines.push(line); + } + + Documentation::new(&processed_lines.join("\n")) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_codeblock_adds_rust() { + let comment = "```\nfn some_rust() {}\n```"; + assert_eq!( + sanitize_markdown(Documentation::new(comment)).contents(), + "```rust\nfn some_rust() {}\n```" + ); + } +} -- cgit v1.2.3