summaryrefslogtreecommitdiff
path: root/support/texlab/crates/symbols/src/document.rs
blob: 611a7e0370bdd385f19c721bb8caea3b0c0c9c43 (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
mod bib;
mod tex;

use base_db::{deps::Project, util, Document, DocumentData, SymbolConfig, Workspace};

use crate::Symbol;

pub fn document_symbols(workspace: &Workspace, document: &Document) -> Vec<Symbol> {
    let project = Project::from_child(workspace, document);
    let mut symbols = match &document.data {
        DocumentData::Tex(data) => {
            let builder = tex::SymbolBuilder::new(&project, workspace.config());
            builder.visit(&data.root_node())
        }
        DocumentData::Bib(data) => {
            let builder = bib::SymbolBuilder;
            data.root_node()
                .children()
                .filter_map(|node| builder.visit(&node))
                .collect()
        }
        DocumentData::Aux(_)
        | DocumentData::Log(_)
        | DocumentData::Root
        | DocumentData::Latexmkrc(_)
        | DocumentData::FileList(_)
        | DocumentData::Tectonic => Vec::new(),
    };

    filter_symbols(&mut symbols, &workspace.config().symbols);
    symbols
}

fn filter_symbols(container: &mut Vec<Symbol>, config: &SymbolConfig) {
    let allowed = &config.allowed_patterns;
    let ignored = &config.ignored_patterns;

    let mut i = 0;
    while i < container.len() {
        let symbol = &mut container[i];
        if symbol.name.is_empty() || !util::filter_regex_patterns(&symbol.name, allowed, ignored) {
            let mut symbol = container.remove(i);
            container.append(&mut symbol.children);
        } else {
            filter_symbols(&mut symbol.children, config);
            i += 1;
        }
    }
}

#[cfg(test)]
mod tests;