aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_lsp_server/src/markdown.rs
blob: f505755e866d681a36634966ce8b767e2058a9c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use ra_ide_api::Documentation;

pub(crate) fn sanitize_markdown(docs: Documentation) -> Documentation {
    let docs: String = docs.into();

    // Massage markdown
    let mut processed_lines = Vec::new();
    let mut in_code_block = false;
    for line in docs.lines() {
        if line.starts_with("```") {
            in_code_block = !in_code_block;
        }

        let line = if in_code_block && line.starts_with("```") && !line.contains("rust") {
            "```rust".into()
        } else {
            line.to_string()
        };

        processed_lines.push(line);
    }

    Documentation::new(&processed_lines.join("\n"))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_codeblock_adds_rust() {
        let comment = "```\nfn some_rust() {}\n```";
        assert_eq!(
            sanitize_markdown(Documentation::new(comment)).contents(),
            "```rust\nfn some_rust() {}\n```"
        );
    }
}