From 1c11d7b1d87501687cda5efae9188e883ea3b92c Mon Sep 17 00:00:00 2001 From: Roman Stoliar Date: Tue, 30 Jul 2019 03:46:38 +0300 Subject: Hide comments in rust section of doc comments --- crates/ra_lsp_server/src/markdown.rs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'crates/ra_lsp_server/src/markdown.rs') diff --git a/crates/ra_lsp_server/src/markdown.rs b/crates/ra_lsp_server/src/markdown.rs index e382eee90..905e8c90b 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 && line.trim_start().starts_with("# ") { + continue; + } + if line.starts_with("```") { in_code_block ^= true } @@ -22,8 +26,20 @@ 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```"; + 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"; + assert_eq!(format_docs(comment), comment); } } -- cgit v1.2.3