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

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum LatexNode {
    Root(Arc<LatexRoot>),
    Group(Arc<LatexGroup>),
    Command(Arc<LatexCommand>),
    Text(Arc<LatexText>),
    Comma(Arc<LatexComma>),
    Math(Arc<LatexMath>),
}

#[derive(Debug)]
pub struct LatexFinder {
    pub position: Position,
    pub results: Vec<LatexNode>,
}

impl LatexFinder {
    pub fn new(position: Position) -> Self {
        Self {
            position,
            results: Vec::new(),
        }
    }
}

impl LatexVisitor for LatexFinder {
    fn visit_root(&mut self, root: Arc<LatexRoot>) {
        if root.range().contains(self.position) {
            self.results.push(LatexNode::Root(Arc::clone(&root)));
            LatexWalker::walk_root(self, root);
        }
    }

    fn visit_group(&mut self, group: Arc<LatexGroup>) {
        if group.range.contains(self.position) {
            self.results.push(LatexNode::Group(Arc::clone(&group)));
            LatexWalker::walk_group(self, group);
        }
    }

    fn visit_command(&mut self, command: Arc<LatexCommand>) {
        if command.range.contains(self.position) {
            self.results.push(LatexNode::Command(Arc::clone(&command)));
            LatexWalker::walk_command(self, command);
        }
    }

    fn visit_text(&mut self, text: Arc<LatexText>) {
        if text.range.contains(self.position) {
            self.results.push(LatexNode::Text(Arc::clone(&text)));
            LatexWalker::walk_text(self, text);
        }
    }

    fn visit_comma(&mut self, comma: Arc<LatexComma>) {
        if comma.range().contains(self.position) {
            self.results.push(LatexNode::Comma(Arc::clone(&comma)));
            LatexWalker::walk_comma(self, comma);
        }
    }

    fn visit_math(&mut self, math: Arc<LatexMath>) {
        if math.range().contains(self.position) {
            self.results.push(LatexNode::Math(Arc::clone(&math)));
            LatexWalker::walk_math(self, math);
        }
    }
}