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/conv.rs | 2 +- crates/ra_lsp_server/src/main_loop/handlers.rs | 2 +- crates/ra_lsp_server/src/markdown.rs | 22 +++++++++++++++++++--- 3 files changed, 21 insertions(+), 5 deletions(-) (limited to 'crates') diff --git a/crates/ra_lsp_server/src/conv.rs b/crates/ra_lsp_server/src/conv.rs index 6b3be444f..59c5e1582 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 9006aa316..ee48d0cb8 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 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 From facc7a35a5ce5cc8be0d03bedd4e00959a03f851 Mon Sep 17 00:00:00 2001 From: Roman Stoliar Date: Tue, 30 Jul 2019 21:25:51 +0300 Subject: Fixed review comments --- crates/ra_lsp_server/src/markdown.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'crates') diff --git a/crates/ra_lsp_server/src/markdown.rs b/crates/ra_lsp_server/src/markdown.rs index 905e8c90b..53cf24482 100644 --- a/crates/ra_lsp_server/src/markdown.rs +++ b/crates/ra_lsp_server/src/markdown.rs @@ -2,7 +2,7 @@ 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("# ") { + if in_code_block && code_line_ignored_by_rustdoc(line) { continue; } @@ -21,6 +21,11 @@ pub(crate) fn format_docs(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::*; @@ -33,13 +38,14 @@ mod tests { #[test] fn test_format_docs_skips_comments_in_rust_block() { - let comment = "```rust\n # skip1\n# skip2\n#stay1\nstay2\n```"; + 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"; + let comment = " # stay1\n# stay2\n#stay3\nstay4\n#\n #\n # \n #\tstay5\n\t#\t"; assert_eq!(format_docs(comment), comment); } } -- cgit v1.2.3