summaryrefslogtreecommitdiff
path: root/support/texlab/src/db/analysis.rs
blob: 79e81c963c67f4e4da6c97081d7edab78b01bbd8 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
pub mod label;

use rowan::{ast::AstNode, TextRange};

use crate::{
    syntax::latex::{self, HasCurly},
    Db,
};

use super::Word;

#[salsa::tracked]
pub struct TexLink {
    pub kind: TexLinkKind,
    pub path: Word,
    pub range: TextRange,
    pub base_dir: Option<Word>,
}

impl TexLink {
    fn of_include(db: &dyn Db, node: latex::SyntaxNode, results: &mut Vec<Self>) -> Option<()> {
        let include = latex::Include::cast(node)?;
        let kind = match include.syntax().kind() {
            latex::LATEX_INCLUDE => TexLinkKind::Tex,
            latex::BIBLATEX_INCLUDE | latex::BIBTEX_INCLUDE => TexLinkKind::Bib,
            latex::PACKAGE_INCLUDE => TexLinkKind::Sty,
            latex::CLASS_INCLUDE => TexLinkKind::Cls,
            _ => return None,
        };

        for path in include.path_list()?.keys() {
            results.push(Self::new(
                db,
                kind,
                Word::new(db, path.to_string()),
                latex::small_range(&path),
                None,
            ));
        }

        Some(())
    }

    fn of_import(db: &dyn Db, node: latex::SyntaxNode, results: &mut Vec<Self>) -> Option<()> {
        let import = latex::Import::cast(node)?;

        let mut base_dir = import.directory()?.key()?.to_string();
        if !base_dir.ends_with("/") {
            base_dir.push('/');
        }

        let path = import.file()?.key()?;
        results.push(Self::new(
            db,
            TexLinkKind::Tex,
            Word::new(db, path.to_string()),
            latex::small_range(&path),
            Some(Word::new(db, base_dir)),
        ));

        Some(())
    }
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
pub enum TexLinkKind {
    Sty,
    Cls,
    Tex,
    Bib,
}

impl TexLinkKind {
    pub fn extensions(self) -> &'static [&'static str] {
        match self {
            Self::Sty => &["sty"],
            Self::Cls => &["cls"],
            Self::Tex => &["tex"],
            Self::Bib => &["bib"],
        }
    }
}

#[salsa::tracked]
pub struct TheoremEnvironment {
    pub name: Word,
    pub description: Word,
}

impl TheoremEnvironment {
    fn of_definition(db: &dyn Db, node: latex::SyntaxNode, results: &mut Vec<Self>) -> Option<()> {
        let theorem = latex::TheoremDefinition::cast(node)?;
        let name = theorem.name()?.key()?.to_string();
        let description = theorem.description()?;
        let description = description.content_text()?;

        results.push(Self::new(
            db,
            Word::new(db, name),
            Word::new(db, description),
        ));

        Some(())
    }
}

#[salsa::tracked]
pub struct GraphicsPath {
    #[return_ref]
    pub path: String,
}

impl GraphicsPath {
    pub fn of_command(db: &dyn Db, node: latex::SyntaxNode, results: &mut Vec<Self>) -> Option<()> {
        let definition = latex::GraphicsPath::cast(node)?;
        for path in definition
            .path_list()
            .filter_map(|group| group.key())
            .map(|path| path.to_string())
        {
            results.push(GraphicsPath::new(db, path));
        }

        Some(())
    }
}

#[salsa::tracked]
pub struct TexAnalysis {
    #[return_ref]
    pub links: Vec<TexLink>,

    #[return_ref]
    pub labels: Vec<label::Name>,

    #[return_ref]
    pub label_numbers: Vec<label::Number>,

    #[return_ref]
    pub theorem_environments: Vec<TheoremEnvironment>,

    #[return_ref]
    pub graphics_paths: Vec<GraphicsPath>,

    #[return_ref]
    pub command_name_ranges: Vec<TextRange>,

    #[return_ref]
    pub environment_names: Vec<String>,
}

#[salsa::tracked]
impl TexAnalysis {
    #[salsa::tracked]
    pub fn has_document_environment(self, db: &dyn Db) -> bool {
        self.environment_names(db)
            .iter()
            .any(|name| name == "document")
    }
}

impl TexAnalysis {
    pub(super) fn analyze(db: &dyn Db, root: &latex::SyntaxNode) -> Self {
        let mut links = Vec::new();
        let mut labels = Vec::new();
        let mut label_numbers = Vec::new();
        let mut theorem_environments = Vec::new();
        let mut graphics_paths = Vec::new();
        let mut command_name_ranges = Vec::new();
        let mut environment_names = Vec::new();

        for node in root.descendants() {
            TexLink::of_include(db, node.clone(), &mut links)
                .or_else(|| TexLink::of_import(db, node.clone(), &mut links))
                .or_else(|| label::Name::of_definition(db, node.clone(), &mut labels))
                .or_else(|| label::Name::of_reference(db, node.clone(), &mut labels))
                .or_else(|| label::Name::of_reference_range(db, node.clone(), &mut labels))
                .or_else(|| label::Number::of_number(db, node.clone(), &mut label_numbers))
                .or_else(|| {
                    TheoremEnvironment::of_definition(db, node.clone(), &mut theorem_environments)
                })
                .or_else(|| GraphicsPath::of_command(db, node.clone(), &mut graphics_paths))
                .or_else(|| {
                    let range = latex::GenericCommand::cast(node.clone())?
                        .name()?
                        .text_range();

                    command_name_ranges.push(range);
                    Some(())
                })
                .or_else(|| {
                    let begin = latex::Begin::cast(node.clone())?;
                    environment_names.push(begin.name()?.key()?.to_string());
                    Some(())
                });
        }

        Self::new(
            db,
            links,
            labels,
            label_numbers,
            theorem_environments,
            graphics_paths,
            command_name_ranges,
            environment_names,
        )
    }
}