summaryrefslogtreecommitdiff
path: root/support/texlab/src/syntax/bibtex/finder.rs
blob: acc46c6f45eca25337c33274234b671eddb0ca79 (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
use super::ast::*;
use crate::range::RangeExt;
use crate::syntax::text::SyntaxNode;
use lsp_types::Position;

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum BibtexNode<'a> {
    Root(&'a BibtexRoot),
    Preamble(&'a BibtexPreamble),
    String(&'a BibtexString),
    Entry(&'a BibtexEntry),
    Comment(&'a BibtexComment),
    Field(&'a BibtexField),
    Word(&'a BibtexWord),
    Command(&'a BibtexCommand),
    QuotedContent(&'a BibtexQuotedContent),
    BracedContent(&'a BibtexBracedContent),
    Concat(&'a BibtexConcat),
}

#[derive(Debug)]
pub struct BibtexFinder<'a> {
    pub position: Position,
    pub results: Vec<BibtexNode<'a>>,
}

impl<'a> BibtexFinder<'a> {
    pub fn new(position: Position) -> Self {
        BibtexFinder {
            position,
            results: Vec::new(),
        }
    }
}

impl<'a> BibtexVisitor<'a> for BibtexFinder<'a> {
    fn visit_root(&mut self, root: &'a BibtexRoot) {
        if root.range().contains(self.position) {
            self.results.push(BibtexNode::Root(root));
            BibtexWalker::walk_root(self, root);
        }
    }

    fn visit_comment(&mut self, comment: &'a BibtexComment) {
        if comment.range.contains(self.position) {
            self.results.push(BibtexNode::Comment(comment));
        }
    }

    fn visit_preamble(&mut self, preamble: &'a BibtexPreamble) {
        if preamble.range.contains(self.position) {
            self.results.push(BibtexNode::Preamble(preamble));
            BibtexWalker::walk_preamble(self, preamble);
        }
    }

    fn visit_string(&mut self, string: &'a BibtexString) {
        if string.range.contains(self.position) {
            self.results.push(BibtexNode::String(string));
            BibtexWalker::walk_string(self, string);
        }
    }

    fn visit_entry(&mut self, entry: &'a BibtexEntry) {
        if entry.range.contains(self.position) {
            self.results.push(BibtexNode::Entry(entry));
            BibtexWalker::walk_entry(self, entry);
        }
    }

    fn visit_field(&mut self, field: &'a BibtexField) {
        if field.range.contains(self.position) {
            self.results.push(BibtexNode::Field(field));
            BibtexWalker::walk_field(self, field);
        }
    }

    fn visit_word(&mut self, word: &'a BibtexWord) {
        if word.range.contains(self.position) {
            self.results.push(BibtexNode::Word(word));
        }
    }

    fn visit_command(&mut self, command: &'a BibtexCommand) {
        if command.range.contains(self.position) {
            self.results.push(BibtexNode::Command(command));
        }
    }

    fn visit_quoted_content(&mut self, content: &'a BibtexQuotedContent) {
        if content.range.contains(self.position) {
            self.results.push(BibtexNode::QuotedContent(content));
            BibtexWalker::walk_quoted_content(self, content);
        }
    }

    fn visit_braced_content(&mut self, content: &'a BibtexBracedContent) {
        if content.range.contains(self.position) {
            self.results.push(BibtexNode::BracedContent(content));
            BibtexWalker::walk_braced_content(self, content);
        }
    }

    fn visit_concat(&mut self, concat: &'a BibtexConcat) {
        if concat.range.contains(self.position) {
            self.results.push(BibtexNode::Concat(concat));
            BibtexWalker::walk_concat(self, concat);
        }
    }
}