summaryrefslogtreecommitdiff
path: root/support/texlab/src/features/definition/entry.rs
blob: b04490dc32a041c6e07ead05f527cf5bcb54d1ca (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
use cancellation::CancellationToken;
use lsp_types::{GotoDefinitionParams, LocationLink};

use crate::{
    features::cursor::CursorContext,
    syntax::{bibtex, latex, CstNode},
    LineIndexExt,
};

pub fn goto_entry_definition(
    context: &CursorContext<GotoDefinitionParams>,
    cancellation_token: &CancellationToken,
) -> Option<Vec<LocationLink>> {
    let main_document = context.request.main_document();

    let word = context
        .cursor
        .as_latex()
        .filter(|token| token.kind() == latex::WORD)?;

    let key = latex::Key::cast(word.parent())?;

    latex::Citation::cast(key.syntax().parent()?.parent()?)?;

    let origin_selection_range = main_document
        .line_index
        .line_col_lsp_range(key.small_range());

    for document in &context.request.subset.documents {
        if let Some(data) = document.data.as_bibtex() {
            for entry in data.root.children().filter_map(bibtex::Entry::cast) {
                cancellation_token.result().ok()?;

                if let Some(key) = entry.key().filter(|k| k.to_string() == key.to_string()) {
                    return Some(vec![LocationLink {
                        origin_selection_range: Some(origin_selection_range),
                        target_uri: document.uri.as_ref().clone().into(),
                        target_selection_range: document
                            .line_index
                            .line_col_lsp_range(key.small_range()),
                        target_range: document.line_index.line_col_lsp_range(entry.small_range()),
                    }]);
                }
            }
        }
    }

    None
}

#[cfg(test)]
mod tests {
    use indoc::indoc;
    use lsp_types::Range;

    use crate::{features::testing::FeatureTester, RangeExt};

    use super::*;

    #[test]
    fn test_empty_latex_document() {
        let request = FeatureTester::builder()
            .files(vec![("main.tex", "")])
            .main("main.tex")
            .line(0)
            .character(0)
            .build()
            .definition();

        let context = CursorContext::new(request);
        let actual_links = goto_entry_definition(&context, CancellationToken::none());

        assert!(actual_links.is_none());
    }

    #[test]
    fn test_empty_bibtex_document() {
        let request = FeatureTester::builder()
            .files(vec![("main.bib", "")])
            .main("main.bib")
            .line(0)
            .character(0)
            .build()
            .definition();

        let context = CursorContext::new(request);
        let actual_links = goto_entry_definition(&context, CancellationToken::none());

        assert!(actual_links.is_none());
    }

    #[test]
    fn test_simple() {
        let tester = FeatureTester::builder()
            .files(vec![
                (
                    "foo.tex",
                    indoc!(
                        r#"
                            \addbibresource{baz.bib}
                            \cite{foo}
                        "#
                    ),
                ),
                ("bar.bib", r#"@article{foo, bar = {baz}}"#),
                ("baz.bib", r#"@article{foo, bar = {baz}}"#),
            ])
            .main("foo.tex")
            .line(1)
            .character(6)
            .build();
        let target_uri = tester.uri("baz.bib").as_ref().clone().into();

        let request = tester.definition();
        let context = CursorContext::new(request);
        let actual_links = goto_entry_definition(&context, CancellationToken::none()).unwrap();

        let expected_links = vec![LocationLink {
            origin_selection_range: Some(Range::new_simple(1, 6, 1, 9)),
            target_uri,
            target_range: Range::new_simple(0, 0, 0, 26),
            target_selection_range: Range::new_simple(0, 9, 0, 12),
        }];

        assert_eq!(actual_links, expected_links);
    }
}