]> git.lizzy.rs Git - rust.git/blob - crates/rust-analyzer/src/markdown.rs
Merge #7777
[rust.git] / crates / rust-analyzer / src / markdown.rs
1 //! Transforms markdown
2
3 const RUSTDOC_FENCE: &str = "```";
4 const RUSTDOC_CODE_BLOCK_ATTRIBUTES_RUST_SPECIFIC: &[&str] = &[
5     "",
6     "rust",
7     "should_panic",
8     "ignore",
9     "no_run",
10     "compile_fail",
11     "edition2015",
12     "edition2018",
13     "edition2021",
14 ];
15
16 pub(crate) fn format_docs(src: &str) -> String {
17     let mut processed_lines = Vec::new();
18     let mut in_code_block = false;
19     let mut is_rust = false;
20
21     for mut line in src.lines() {
22         if in_code_block && is_rust && code_line_ignored_by_rustdoc(line) {
23             continue;
24         }
25
26         if let Some(header) = line.strip_prefix(RUSTDOC_FENCE) {
27             in_code_block ^= true;
28
29             if in_code_block {
30                 is_rust = header
31                     .split(',')
32                     .all(|sub| RUSTDOC_CODE_BLOCK_ATTRIBUTES_RUST_SPECIFIC.contains(&sub.trim()));
33
34                 if is_rust {
35                     line = "```rust";
36                 }
37             }
38         }
39
40         processed_lines.push(line);
41     }
42     processed_lines.join("\n")
43 }
44
45 fn code_line_ignored_by_rustdoc(line: &str) -> bool {
46     let trimmed = line.trim();
47     trimmed == "#" || trimmed.starts_with("# ") || trimmed.starts_with("#\t")
48 }
49
50 #[cfg(test)]
51 mod tests {
52     use super::*;
53
54     #[test]
55     fn test_format_docs_adds_rust() {
56         let comment = "```\nfn some_rust() {}\n```";
57         assert_eq!(format_docs(comment), "```rust\nfn some_rust() {}\n```");
58     }
59
60     #[test]
61     fn test_format_docs_handles_plain_text() {
62         let comment = "```text\nthis is plain text\n```";
63         assert_eq!(format_docs(comment), "```text\nthis is plain text\n```");
64     }
65
66     #[test]
67     fn test_format_docs_handles_non_rust() {
68         let comment = "```sh\nsupposedly shell code\n```";
69         assert_eq!(format_docs(comment), "```sh\nsupposedly shell code\n```");
70     }
71
72     #[test]
73     fn test_format_docs_handles_rust_alias() {
74         let comment = "```ignore\nlet z = 55;\n```";
75         assert_eq!(format_docs(comment), "```rust\nlet z = 55;\n```");
76     }
77
78     #[test]
79     fn test_format_docs_handles_complex_code_block_attrs() {
80         let comment = "```rust,no_run\nlet z = 55;\n```";
81         assert_eq!(format_docs(comment), "```rust\nlet z = 55;\n```");
82     }
83
84     #[test]
85     fn test_format_docs_skips_comments_in_rust_block() {
86         let comment =
87             "```rust\n # skip1\n# skip2\n#stay1\nstay2\n#\n #\n   #    \n #\tskip3\n\t#\t\n```";
88         assert_eq!(format_docs(comment), "```rust\n#stay1\nstay2\n```");
89     }
90
91     #[test]
92     fn test_format_docs_does_not_skip_lines_if_plain_text() {
93         let comment =
94             "```text\n # stay1\n# stay2\n#stay3\nstay4\n#\n #\n   #    \n #\tstay5\n\t#\t\n```";
95         assert_eq!(
96             format_docs(comment),
97             "```text\n # stay1\n# stay2\n#stay3\nstay4\n#\n #\n   #    \n #\tstay5\n\t#\t\n```",
98         );
99     }
100
101     #[test]
102     fn test_format_docs_keeps_comments_outside_of_rust_block() {
103         let comment = " # stay1\n# stay2\n#stay3\nstay4\n#\n #\n   #    \n #\tstay5\n\t#\t";
104         assert_eq!(format_docs(comment), comment);
105     }
106
107     #[test]
108     fn test_format_docs_preserves_newlines() {
109         let comment = "this\nis\nmultiline";
110         assert_eq!(format_docs(comment), comment);
111     }
112
113     #[test]
114     fn test_code_blocks_in_comments_marked_as_rust() {
115         let comment = r#"```rust
116 fn main(){}
117 ```
118 Some comment.
119 ```
120 let a = 1;
121 ```"#;
122
123         assert_eq!(
124             format_docs(comment),
125             "```rust\nfn main(){}\n```\nSome comment.\n```rust\nlet a = 1;\n```"
126         );
127     }
128
129     #[test]
130     fn test_code_blocks_in_comments_marked_as_text() {
131         let comment = r#"```text
132 filler
133 text
134 ```
135 Some comment.
136 ```
137 let a = 1;
138 ```"#;
139
140         assert_eq!(
141             format_docs(comment),
142             "```text\nfiller\ntext\n```\nSome comment.\n```rust\nlet a = 1;\n```"
143         );
144     }
145 }