summaryrefslogtreecommitdiff
path: root/support/texlab/crates/base-db/src/semantics.rs
blob: a2f0de291fb08ac3e73225701272232d2093fb36 (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
use rowan::{TextLen, TextRange};

pub mod auxiliary;
pub mod bib;
pub mod tex;

#[derive(PartialEq, Eq, Clone, Hash)]
pub struct Span {
    pub text: String,
    pub range: rowan::TextRange,
}

impl Span {
    pub fn new(text: String, range: rowan::TextRange) -> Self {
        Self { text, range }
    }

    pub fn empty(offset: rowan::TextSize) -> Self {
        Self {
            text: String::new(),
            range: rowan::TextRange::empty(offset),
        }
    }

    pub fn command<L: rowan::Language>(token: &rowan::SyntaxToken<L>) -> Self {
        let range = token.text_range();
        let range = TextRange::new(range.start() + "\\".text_len(), range.end());
        let text = String::from(&token.text()[1..]);
        Self::new(text, range)
    }
}

impl std::fmt::Debug for Span {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("Span")
            .field(&self.text)
            .field(&self.range)
            .finish()
    }
}

impl From<&syntax::latex::Key> for Span {
    fn from(key: &syntax::latex::Key) -> Self {
        Self {
            text: key.to_string(),
            range: syntax::latex::small_range(key),
        }
    }
}

impl<L: rowan::Language> From<&rowan::SyntaxToken<L>> for Span {
    fn from(token: &rowan::SyntaxToken<L>) -> Self {
        Self {
            text: token.text().into(),
            range: token.text_range(),
        }
    }
}