summaryrefslogtreecommitdiff
path: root/support/texlab/crates/symbols/src/types.rs
blob: 5d31c981434900431d80b22045b670b40c621430 (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
use base_db::{data::BibtexEntryTypeCategory, semantics::Span, Document};
use rowan::TextRange;

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum SymbolKind {
    Section,
    Figure,
    Algorithm,
    Table,
    Listing,
    Enumeration,
    EnumerationItem,
    Theorem,
    Equation,
    Entry(BibtexEntryTypeCategory),
    Field,
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Symbol {
    pub name: String,
    pub kind: SymbolKind,
    pub label: Option<Span>,
    pub full_range: TextRange,
    pub selection_range: TextRange,
    pub children: Vec<Symbol>,
}

impl Symbol {
    pub fn new_simple(
        name: String,
        kind: SymbolKind,
        full_range: TextRange,
        selection_range: TextRange,
    ) -> Self {
        Self {
            name,
            kind,
            label: None,
            full_range,
            selection_range,
            children: Vec::new(),
        }
    }

    pub fn new_label(name: String, kind: SymbolKind, range: TextRange, label: Span) -> Self {
        Self {
            name,
            kind,
            full_range: range,
            selection_range: label.range,
            label: Some(label),
            children: Vec::new(),
        }
    }

    pub fn keywords(&self) -> Vec<&str> {
        match self.kind {
            SymbolKind::Section => vec![&self.name, "latex", "section"],
            SymbolKind::Figure => vec![&self.name, "latex", "float", "figure"],
            SymbolKind::Algorithm => vec![&self.name, "latex", "float", "algorithm"],
            SymbolKind::Table => vec![&self.name, "latex", "float", "table"],
            SymbolKind::Listing => vec![&self.name, "latex", "float", "listing"],
            SymbolKind::Enumeration => vec![&self.name, "latex", "enumeration"],
            SymbolKind::EnumerationItem => vec![&self.name, "latex", "enumeration", "item"],
            SymbolKind::Theorem => vec![&self.name, "latex", "math"],
            SymbolKind::Equation => vec![&self.name, "latex", "math", "equation"],
            SymbolKind::Entry(BibtexEntryTypeCategory::String) => {
                vec![&self.name, "bibtex", "string"]
            }
            SymbolKind::Entry(_) => vec![&self.name, "bibtex", "entry"],
            SymbolKind::Field => vec![&self.name, "bibtex", "field"],
        }
    }

    pub fn flatten(mut self, buffer: &mut Vec<Self>) {
        for symbol in self.children.drain(..) {
            symbol.flatten(buffer);
        }

        buffer.push(self);
    }
}

#[derive(Debug)]
pub struct SymbolLocation<'a> {
    pub document: &'a Document,
    pub symbol: Symbol,
}