summaryrefslogtreecommitdiff
path: root/support/texlab/src/features/hover/citation.rs
blob: c097a922f7eb8a1e5fcfab38ae9239e0384ea8ed (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
133
use cancellation::CancellationToken;
use lsp_types::{Hover, HoverContents, HoverParams};

use crate::{citation, features::cursor::CursorContext, LineIndexExt};

pub fn find_citation_hover(
    context: &CursorContext<HoverParams>,
    cancellation_token: &CancellationToken,
) -> Option<Hover> {
    cancellation_token.result().ok()?;
    let main_document = context.request.main_document();

    let (key_text, key_range) = context
        .find_citation_key_word()
        .or_else(|| context.find_citation_key_command())
        .or_else(|| context.find_entry_key())?;

    let contents = context
        .request
        .subset
        .documents
        .iter()
        .find_map(|document| {
            document
                .data
                .as_bibtex()
                .and_then(|data| citation::render_citation(&data.root, &key_text))
        })?;

    Some(Hover {
        range: Some(main_document.line_index.line_col_lsp_range(key_range)),
        contents: HoverContents::Markup(contents),
    })
}

#[cfg(test)]
mod tests {
    use lsp_types::{MarkupContent, MarkupKind, 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()
            .hover();

        let context = CursorContext::new(request);
        let actual_hover = find_citation_hover(&context, CancellationToken::none());

        assert_eq!(actual_hover, None);
    }

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

        let context = CursorContext::new(request);
        let actual_hover = find_citation_hover(&context, CancellationToken::none());

        assert_eq!(actual_hover, None);
    }

    #[test]
    fn test_inside_cite() {
        let request = FeatureTester::builder()
            .files(vec![
                (
                    "main.bib",
                    "@article{foo, author = {Foo Bar}, title = {Baz Qux}, year = 1337}",
                ),
                ("main.tex", "\\addbibresource{main.bib}\n\\cite{foo}"),
            ])
            .main("main.tex")
            .line(1)
            .character(7)
            .build()
            .hover();

        let context = CursorContext::new(request);
        let actual_hover = find_citation_hover(&context, CancellationToken::none()).unwrap();

        let expected_hover = Hover {
            contents: HoverContents::Markup(MarkupContent {
                kind: MarkupKind::Markdown,
                value: "Bar, Foo. (1337). *Baz Qux*.".into(),
            }),
            range: Some(Range::new_simple(1, 6, 1, 9)),
        };
        assert_eq!(actual_hover, expected_hover);
    }

    #[test]
    fn test_inside_entry() {
        let request = FeatureTester::builder()
            .files(vec![
                (
                    "main.bib",
                    "@article{foo, author = {Foo Bar}, title = {Baz Qux}, year = 1337}",
                ),
                ("main.tex", "\\addbibresource{main.bib}\n\\cite{foo}"),
            ])
            .main("main.bib")
            .line(0)
            .character(11)
            .build()
            .hover();

        let context = CursorContext::new(request);
        let actual_hover = find_citation_hover(&context, CancellationToken::none()).unwrap();

        let expected_hover = Hover {
            contents: HoverContents::Markup(MarkupContent {
                kind: MarkupKind::Markdown,
                value: "Bar, Foo. (1337). *Baz Qux*.".into(),
            }),
            range: Some(Range::new_simple(0, 9, 0, 12)),
        };
        assert_eq!(actual_hover, expected_hover);
    }
}