aboutsummaryrefslogtreecommitdiff
path: root/crates/rust-analyzer/src
diff options
context:
space:
mode:
authorLeón Orell Valerian Liehr <[email protected]>2020-08-26 02:02:55 +0100
committerLeón Orell Valerian Liehr <[email protected]>2020-08-26 14:55:06 +0100
commit63caef372ad96a4cfc6aeafa52218c426daf7f2a (patch)
tree7108ca9d9307eed6a8d7c0b9c5bd4d45e48a8812 /crates/rust-analyzer/src
parentd58a3a277a1778ec33e492e958b52869510c1239 (diff)
Improve support for code block attributes
Diffstat (limited to 'crates/rust-analyzer/src')
-rw-r--r--crates/rust-analyzer/src/markdown.rs81
1 files changed, 71 insertions, 10 deletions
diff --git a/crates/rust-analyzer/src/markdown.rs b/crates/rust-analyzer/src/markdown.rs
index 76bef45cc..968ea55f0 100644
--- a/crates/rust-analyzer/src/markdown.rs
+++ b/crates/rust-analyzer/src/markdown.rs
@@ -1,22 +1,32 @@
1//! Transforms markdown 1//! Transforms markdown
2 2
3const RUSTDOC_FENCE: &str = "```";
4const RUSTDOC_CODE_BLOCK_ATTRIBUTES_RUST_SPECIFIC: &[&str] =
5 &["", "rust", "should_panic", "ignore", "no_run", "compile_fail", "edition2015", "edition2018"];
6
3pub(crate) fn format_docs(src: &str) -> String { 7pub(crate) fn format_docs(src: &str) -> String {
4 let mut processed_lines = Vec::new(); 8 let mut processed_lines = Vec::new();
5 let mut in_code_block = false; 9 let mut in_code_block = false;
6 for line in src.lines() { 10 let mut is_rust = false;
7 if in_code_block && code_line_ignored_by_rustdoc(line) { 11
12 for mut line in src.lines() {
13 if in_code_block && is_rust && code_line_ignored_by_rustdoc(line) {
8 continue; 14 continue;
9 } 15 }
10 16
11 if line.starts_with("```") { 17 if let Some(header) = line.strip_prefix(RUSTDOC_FENCE) {
12 in_code_block ^= true 18 in_code_block ^= true;
13 }
14 19
15 let line = if in_code_block && line.starts_with("```") && !line.contains("rust") { 20 if in_code_block {
16 "```rust" 21 is_rust = header
17 } else { 22 .split(',')
18 line 23 .all(|sub| RUSTDOC_CODE_BLOCK_ATTRIBUTES_RUST_SPECIFIC.contains(&sub.trim()));
19 }; 24
25 if is_rust {
26 line = "```rust";
27 }
28 }
29 }
20 30
21 processed_lines.push(line); 31 processed_lines.push(line);
22 } 32 }
@@ -39,6 +49,30 @@ mod tests {
39 } 49 }
40 50
41 #[test] 51 #[test]
52 fn test_format_docs_handles_plain_text() {
53 let comment = "```text\nthis is plain text\n```";
54 assert_eq!(format_docs(comment), "```text\nthis is plain text\n```");
55 }
56
57 #[test]
58 fn test_format_docs_handles_non_rust() {
59 let comment = "```sh\nsupposedly shell code\n```";
60 assert_eq!(format_docs(comment), "```sh\nsupposedly shell code\n```");
61 }
62
63 #[test]
64 fn test_format_docs_handles_rust_alias() {
65 let comment = "```ignore\nlet z = 55;\n```";
66 assert_eq!(format_docs(comment), "```rust\nlet z = 55;\n```");
67 }
68
69 #[test]
70 fn test_format_docs_handles_complex_code_block_attrs() {
71 let comment = "```rust,no_run\nlet z = 55;\n```";
72 assert_eq!(format_docs(comment), "```rust\nlet z = 55;\n```");
73 }
74
75 #[test]
42 fn test_format_docs_skips_comments_in_rust_block() { 76 fn test_format_docs_skips_comments_in_rust_block() {
43 let comment = 77 let comment =
44 "```rust\n # skip1\n# skip2\n#stay1\nstay2\n#\n #\n # \n #\tskip3\n\t#\t\n```"; 78 "```rust\n # skip1\n# skip2\n#stay1\nstay2\n#\n #\n # \n #\tskip3\n\t#\t\n```";
@@ -46,6 +80,16 @@ mod tests {
46 } 80 }
47 81
48 #[test] 82 #[test]
83 fn test_format_docs_does_not_skip_lines_if_plain_text() {
84 let comment =
85 "```text\n # stay1\n# stay2\n#stay3\nstay4\n#\n #\n # \n #\tstay5\n\t#\t\n```";
86 assert_eq!(
87 format_docs(comment),
88 "```text\n # stay1\n# stay2\n#stay3\nstay4\n#\n #\n # \n #\tstay5\n\t#\t\n```",
89 );
90 }
91
92 #[test]
49 fn test_format_docs_keeps_comments_outside_of_rust_block() { 93 fn test_format_docs_keeps_comments_outside_of_rust_block() {
50 let comment = " # stay1\n# stay2\n#stay3\nstay4\n#\n #\n # \n #\tstay5\n\t#\t"; 94 let comment = " # stay1\n# stay2\n#stay3\nstay4\n#\n #\n # \n #\tstay5\n\t#\t";
51 assert_eq!(format_docs(comment), comment); 95 assert_eq!(format_docs(comment), comment);
@@ -72,4 +116,21 @@ let a = 1;
72 "```rust\nfn main(){}\n```\nSome comment.\n```rust\nlet a = 1;\n```" 116 "```rust\nfn main(){}\n```\nSome comment.\n```rust\nlet a = 1;\n```"
73 ); 117 );
74 } 118 }
119
120 #[test]
121 fn test_code_blocks_in_comments_marked_as_text() {
122 let comment = r#"```text
123filler
124text
125```
126Some comment.
127```
128let a = 1;
129```"#;
130
131 assert_eq!(
132 format_docs(comment),
133 "```text\nfiller\ntext\n```\nSome comment.\n```rust\nlet a = 1;\n```"
134 );
135 }
75} 136}