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