diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs index 6b3be444fd..59c5e15823 100644 --- a/crates/ra_lsp_server/src/conv.rs +++ b/crates/ra_lsp_server/src/conv.rs @@ -171,7 +171,7 @@ impl Conv for ra_ide_api::Documentation { fn conv(self) -> Documentation { Documentation::MarkupContent(MarkupContent { kind: MarkupKind::Markdown, - value: crate::markdown::mark_fenced_blocks_as_rust(self.as_str()), + value: crate::markdown::format_docs(self.as_str()), }) } } diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index 9006aa316f..ee48d0cb8a 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -485,7 +485,7 @@ pub fn handle_hover( let res = Hover { contents: HoverContents::Markup(MarkupContent { kind: MarkupKind::Markdown, - value: info.info.to_markup(), + value: crate::markdown::format_docs(&info.info.to_markup()), }), range: Some(range), }; diff --git a/crates/ra_lsp_server/src/markdown.rs b/crates/ra_lsp_server/src/markdown.rs index e382eee90c..53cf24482d 100644 --- a/crates/ra_lsp_server/src/markdown.rs +++ b/crates/ra_lsp_server/src/markdown.rs @@ -1,7 +1,11 @@ -pub(crate) fn mark_fenced_blocks_as_rust(src: &str) -> String { +pub(crate) fn format_docs(src: &str) -> String { let mut processed_lines = Vec::new(); let mut in_code_block = false; for line in src.lines() { + if in_code_block && code_line_ignored_by_rustdoc(line) { + continue; + } + if line.starts_with("```") { in_code_block ^= true } @@ -17,13 +21,31 @@ pub(crate) fn mark_fenced_blocks_as_rust(src: &str) -> String { processed_lines.join("\n") } +fn code_line_ignored_by_rustdoc(line: &str) -> bool { + let trimmed = line.trim(); + trimmed == "#" || trimmed.starts_with("# ") || trimmed.starts_with("#\t") +} + #[cfg(test)] mod tests { use super::*; #[test] - fn test_codeblock_adds_rust() { + fn test_format_docs_adds_rust() { let comment = "```\nfn some_rust() {}\n```"; - assert_eq!(mark_fenced_blocks_as_rust(comment), "```rust\nfn some_rust() {}\n```"); + assert_eq!(format_docs(comment), "```rust\nfn some_rust() {}\n```"); + } + + #[test] + fn test_format_docs_skips_comments_in_rust_block() { + let comment = + "```rust\n # skip1\n# skip2\n#stay1\nstay2\n#\n #\n # \n #\tskip3\n\t#\t\n```"; + assert_eq!(format_docs(comment), "```rust\n#stay1\nstay2\n```"); + } + + #[test] + fn test_format_docs_keeps_comments_outside_of_rust_block() { + let comment = " # stay1\n# stay2\n#stay3\nstay4\n#\n #\n # \n #\tstay5\n\t#\t"; + assert_eq!(format_docs(comment), comment); } }