summaryrefslogtreecommitdiff
path: root/support/texlab/src/definition/bibtex_string.rs
blob: 0e4f6431530a56a796c5c073a6ecbc9657d010d9 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use crate::syntax::*;
use crate::workspace::*;
use futures_boxed::boxed;
use lsp_types::{LocationLink, Position, TextDocumentPositionParams};

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub struct BibtexStringDefinitionProvider;

impl FeatureProvider for BibtexStringDefinitionProvider {
    type Params = TextDocumentPositionParams;
    type Output = Vec<LocationLink>;

    #[boxed]
    async fn execute<'a>(&'a self, request: &'a FeatureRequest<Self::Params>) -> Self::Output {
        if let SyntaxTree::Bibtex(tree) = &request.document().tree {
            if let Some(reference) = Self::find_reference(tree, request.params.position) {
                return Self::find_definitions(&request.view.document.uri, tree, reference);
            }
        }
        Vec::new()
    }
}

impl BibtexStringDefinitionProvider {
    fn find_reference(tree: &BibtexSyntaxTree, position: Position) -> Option<&BibtexToken> {
        let mut nodes = tree.find(position);
        nodes.reverse();
        match (&nodes[0], &nodes.get(1)) {
            (BibtexNode::Word(word), Some(BibtexNode::Field(_)))
            | (BibtexNode::Word(word), Some(BibtexNode::Concat(_))) => Some(&word.token),
            _ => None,
        }
    }

    fn find_definitions(
        uri: &Uri,
        tree: &BibtexSyntaxTree,
        reference: &BibtexToken,
    ) -> Vec<LocationLink> {
        let mut links = Vec::new();
        for string in tree.strings() {
            if let Some(name) = &string.name {
                if name.text() == reference.text() {
                    links.push(LocationLink {
                        origin_selection_range: Some(reference.range()),
                        target_uri: uri.clone().into(),
                        target_range: string.range(),
                        target_selection_range: name.range(),
                    });
                }
            }
        }
        links
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::range::RangeExt;
    use lsp_types::{Position, Range};

    #[test]
    fn test_simple() {
        let links = test_feature(
            BibtexStringDefinitionProvider,
            FeatureSpec {
                files: vec![FeatureSpec::file(
                    "foo.bib",
                    "@string{foo = {bar}}\n@article{bar, author = foo}",
                )],
                main_file: "foo.bib",
                position: Position::new(1, 24),
                ..FeatureSpec::default()
            },
        );

        assert_eq!(
            links,
            vec![LocationLink {
                origin_selection_range: Some(Range::new_simple(1, 23, 1, 26)),
                target_uri: FeatureSpec::uri("foo.bib"),
                target_range: Range::new_simple(0, 0, 0, 20),
                target_selection_range: Range::new_simple(0, 8, 0, 11)
            }]
        );
    }

    #[test]
    fn test_concat() {
        let links = test_feature(
            BibtexStringDefinitionProvider,
            FeatureSpec {
                files: vec![FeatureSpec::file(
                    "foo.bib",
                    "@string{foo = {bar}}\n@article{bar, author = foo # \"bar\"}",
                )],
                main_file: "foo.bib",
                position: Position::new(1, 24),
                ..FeatureSpec::default()
            },
        );

        assert_eq!(
            links,
            vec![LocationLink {
                origin_selection_range: Some(Range::new_simple(1, 23, 1, 26)),
                target_uri: FeatureSpec::uri("foo.bib"),
                target_range: Range::new_simple(0, 0, 0, 20),
                target_selection_range: Range::new_simple(0, 8, 0, 11)
            }]
        );
    }

    #[test]
    fn test_field() {
        let links = test_feature(
            BibtexStringDefinitionProvider,
            FeatureSpec {
                files: vec![FeatureSpec::file(
                    "foo.bib",
                    "@string{foo = {bar}}\n@article{bar, author = foo}",
                )],
                main_file: "foo.bib",
                position: Position::new(1, 18),
                ..FeatureSpec::default()
            },
        );

        assert!(links.is_empty());
    }
}