aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_lsp_server/src/markdown.rs
blob: e382eee90c9032351a22ee2621f2131cf3e1dd6c (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
pub(crate) fn mark_fenced_blocks_as_rust(src: &str) -> String {
    let mut processed_lines = Vec::new();
    let mut in_code_block = false;
    for line in src.lines() {
        if line.starts_with("```") {
            in_code_block ^= true
        }

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

        processed_lines.push(line);
    }
    processed_lines.join("\n")
}

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

    #[test]
    fn test_codeblock_adds_rust() {
        let comment = "```\nfn some_rust() {}\n```";
        assert_eq!(mark_fenced_blocks_as_rust(comment), "```rust\nfn some_rust() {}\n```");
    }
}