summaryrefslogtreecommitdiff
path: root/support/texlab/crates/commands/src/find_envs.rs
blob: 1ea88657aba8adf938328f8a80f5a6194343606c (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use base_db::{semantics::Span, Document, DocumentData};
use rowan::{ast::AstNode, TextRange, TextSize};
use syntax::latex;

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct EnvironmentMatch {
    pub name: Span,
    pub full_range: TextRange,
}

pub fn find_environments(document: &Document, offset: TextSize) -> Vec<EnvironmentMatch> {
    let DocumentData::Tex(data) = &document.data else {
        return Vec::new();
    };

    let root = latex::SyntaxNode::new_root(data.green.clone());

    let Some(token) = root.token_at_offset(offset).right_biased() else {
        return Vec::new();
    };

    let mut results = Vec::new();
    for environment in token
        .parent_ancestors()
        .filter_map(latex::Environment::cast)
    {
        let Some(name) = environment
            .begin()
            .and_then(|begin| begin.name())
            .and_then(|group| group.key())
            .map(|name| Span::from(&name))
        else {
            continue;
        };

        let full_range = latex::small_range(&environment);
        results.push(EnvironmentMatch { name, full_range });
    }

    results.reverse();
    results
}

#[cfg(test)]
mod tests {
    use expect_test::{expect, Expect};
    use test_utils::fixture::Fixture;

    use crate::find_environments;

    fn check(fixture: &str, expect: Expect) {
        let fixture = Fixture::parse(fixture);
        let workspace = fixture.workspace;
        let document = workspace.iter().next().unwrap();
        let offset = fixture.documents[0].cursor.unwrap();
        let results = find_environments(document, offset);
        expect.assert_debug_eq(&results);
    }

    #[test]
    fn test_simple() {
        check(
            r#"
%! main.tex
\begin{a}
    |
\end{a}
"#,
            expect![[r#"
                [
                    EnvironmentMatch {
                        name: Span(
                            "a",
                            7..8,
                        ),
                        full_range: 0..17,
                    },
                ]
            "#]],
        );
    }

    #[test]
    fn test_nested() {
        check(
            r#"
%! main.tex
\begin{a}
    \begin{b}
        \begin{c}
            |
        \end{c}
    \end{b}
    \begin{d}
    \end{d}
\end{a}"#,
            expect![[r#"
                [
                    EnvironmentMatch {
                        name: Span(
                            "a",
                            7..8,
                        ),
                        full_range: 0..103,
                    },
                    EnvironmentMatch {
                        name: Span(
                            "b",
                            21..22,
                        ),
                        full_range: 14..69,
                    },
                    EnvironmentMatch {
                        name: Span(
                            "c",
                            39..40,
                        ),
                        full_range: 32..57,
                    },
                ]
            "#]],
        );
    }
}