summaryrefslogtreecommitdiff
path: root/support/texlab/src/folding/latex_env.rs
blob: ced2567b413e1793c5ef893816c161e8638591e5 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use crate::{
    feature::{FeatureProvider, FeatureRequest},
    protocol::{FoldingRange, FoldingRangeKind, FoldingRangeParams},
    syntax::SyntaxNode,
    workspace::DocumentContent,
};
use async_trait::async_trait;

#[derive(Debug, PartialEq, Eq, Clone, Copy, Default)]
pub struct LatexEnvironmentFoldingProvider;

#[async_trait]
impl FeatureProvider for LatexEnvironmentFoldingProvider {
    type Params = FoldingRangeParams;
    type Output = Vec<FoldingRange>;

    async fn execute<'a>(&'a self, req: &'a FeatureRequest<Self::Params>) -> Self::Output {
        let mut foldings = Vec::new();
        if let DocumentContent::Latex(table) = &req.current().content {
            for env in &table.environments {
                let left_node = &table[env.left.parent];
                let right_node = &table[env.right.parent];
                let folding = FoldingRange {
                    start_line: left_node.end().line,
                    start_character: Some(left_node.end().character),
                    end_line: right_node.start().line,
                    end_character: Some(right_node.start().character),
                    kind: Some(FoldingRangeKind::Region),
                };
                foldings.push(folding);
            }
        }
        foldings
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::feature::FeatureTester;
    use indoc::indoc;

    #[tokio::test]
    async fn multiline() {
        let actual_foldings = FeatureTester::new()
            .file(
                "main.tex",
                indoc!(
                    r#"
                        \begin{foo}
                        \end{foo}
                    "#
                ),
            )
            .main("main.tex")
            .test_folding(LatexEnvironmentFoldingProvider)
            .await;

        let expected_foldings = vec![FoldingRange {
            start_line: 0,
            start_character: Some(11),
            end_line: 1,
            end_character: Some(0),
            kind: Some(FoldingRangeKind::Region),
        }];

        assert_eq!(actual_foldings, expected_foldings);
    }

    #[tokio::test]
    async fn bibtex() {
        let actual_foldings = FeatureTester::new()
            .file("main.bib", "")
            .main("main.bib")
            .test_folding(LatexEnvironmentFoldingProvider)
            .await;

        assert!(actual_foldings.is_empty());
    }
}