summaryrefslogtreecommitdiff
path: root/support/texlab/src/syntax/latex/env.rs
blob: 163db02ca870aa9fa2a36d9ce1f2f886faf6564c (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
use super::ast::*;
use crate::syntax::language::*;
use crate::syntax::text::SyntaxNode;
use lsp_types::Range;
use std::sync::Arc;

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct LatexEnvironmentDelimiter {
    pub command: Arc<LatexCommand>,
}

impl LatexEnvironmentDelimiter {
    pub fn name(&self) -> Option<&LatexToken> {
        self.command.extract_word(0)
    }

    pub fn is_math(&self) -> bool {
        if let Some(name) = self.name() {
            LANGUAGE_DATA
                .math_environments
                .iter()
                .any(|env| env == name.text())
        } else {
            false
        }
    }

    pub fn is_enum(&self) -> bool {
        if let Some(name) = self.name() {
            LANGUAGE_DATA
                .enum_environments
                .iter()
                .any(|env| env == name.text())
        } else {
            false
        }
    }
}

impl SyntaxNode for LatexEnvironmentDelimiter {
    fn range(&self) -> Range {
        self.command.range()
    }
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct LatexEnvironment {
    pub left: LatexEnvironmentDelimiter,
    pub right: LatexEnvironmentDelimiter,
}

impl LatexEnvironment {
    pub fn is_root(&self) -> bool {
        self.left
            .name()
            .iter()
            .chain(self.right.name().iter())
            .any(|name| name.text() == "document")
    }

    fn parse(commands: &[Arc<LatexCommand>]) -> Vec<Self> {
        let mut stack = Vec::new();
        let mut environments = Vec::new();
        for command in commands {
            if let Some(delimiter) = Self::parse_delimiter(command) {
                if delimiter.command.name.text() == "\\begin" {
                    stack.push(delimiter);
                } else if let Some(begin) = stack.pop() {
                    environments.push(Self {
                        left: begin,
                        right: delimiter,
                    });
                }
            }
        }
        environments
    }

    fn parse_delimiter(command: &Arc<LatexCommand>) -> Option<LatexEnvironmentDelimiter> {
        if command.name.text() != "\\begin" && command.name.text() != "\\end" {
            return None;
        }

        if command.args.is_empty() {
            return None;
        }

        if command.has_word(0)
            || command.args[0].children.is_empty()
            || command.args[0].right.is_none()
        {
            let delimiter = LatexEnvironmentDelimiter {
                command: Arc::clone(&command),
            };
            Some(delimiter)
        } else {
            None
        }
    }
}

impl SyntaxNode for LatexEnvironment {
    fn range(&self) -> Range {
        Range::new(self.left.start(), self.right.end())
    }
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct LatexEnvironmentInfo {
    pub environments: Vec<LatexEnvironment>,
    pub is_standalone: bool,
}

impl LatexEnvironmentInfo {
    pub fn parse(commands: &[Arc<LatexCommand>]) -> Self {
        let environments = LatexEnvironment::parse(commands);
        let is_standalone = environments.iter().any(LatexEnvironment::is_root);
        Self {
            environments,
            is_standalone,
        }
    }
}