aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_lsp_server/src/markdown.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_lsp_server/src/markdown.rs')
-rw-r--r--crates/ra_lsp_server/src/markdown.rs28
1 files changed, 25 insertions, 3 deletions
diff --git a/crates/ra_lsp_server/src/markdown.rs b/crates/ra_lsp_server/src/markdown.rs
index e382eee90..53cf24482 100644
--- a/crates/ra_lsp_server/src/markdown.rs
+++ b/crates/ra_lsp_server/src/markdown.rs
@@ -1,7 +1,11 @@
1pub(crate) fn mark_fenced_blocks_as_rust(src: &str) -> String { 1pub(crate) fn format_docs(src: &str) -> String {
2 let mut processed_lines = Vec::new(); 2 let mut processed_lines = Vec::new();
3 let mut in_code_block = false; 3 let mut in_code_block = false;
4 for line in src.lines() { 4 for line in src.lines() {
5 if in_code_block && code_line_ignored_by_rustdoc(line) {
6 continue;
7 }
8
5 if line.starts_with("```") { 9 if line.starts_with("```") {
6 in_code_block ^= true 10 in_code_block ^= true
7 } 11 }
@@ -17,13 +21,31 @@ pub(crate) fn mark_fenced_blocks_as_rust(src: &str) -> String {
17 processed_lines.join("\n") 21 processed_lines.join("\n")
18} 22}
19 23
24fn code_line_ignored_by_rustdoc(line: &str) -> bool {
25 let trimmed = line.trim();
26 trimmed == "#" || trimmed.starts_with("# ") || trimmed.starts_with("#\t")
27}
28
20#[cfg(test)] 29#[cfg(test)]
21mod tests { 30mod tests {
22 use super::*; 31 use super::*;
23 32
24 #[test] 33 #[test]
25 fn test_codeblock_adds_rust() { 34 fn test_format_docs_adds_rust() {
26 let comment = "```\nfn some_rust() {}\n```"; 35 let comment = "```\nfn some_rust() {}\n```";
27 assert_eq!(mark_fenced_blocks_as_rust(comment), "```rust\nfn some_rust() {}\n```"); 36 assert_eq!(format_docs(comment), "```rust\nfn some_rust() {}\n```");
37 }
38
39 #[test]
40 fn test_format_docs_skips_comments_in_rust_block() {
41 let comment =
42 "```rust\n # skip1\n# skip2\n#stay1\nstay2\n#\n #\n # \n #\tskip3\n\t#\t\n```";
43 assert_eq!(format_docs(comment), "```rust\n#stay1\nstay2\n```");
44 }
45
46 #[test]
47 fn test_format_docs_keeps_comments_outside_of_rust_block() {
48 let comment = " # stay1\n# stay2\n#stay3\nstay4\n#\n #\n # \n #\tstay5\n\t#\t";
49 assert_eq!(format_docs(comment), comment);
28 } 50 }
29} 51}