summaryrefslogtreecommitdiff
path: root/support/texlab/src/hover/latex/label.rs
blob: 66724d442bd491fe025bd3bb481c74b47b5b07ba (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
use crate::{
    feature::{DocumentView, FeatureProvider, FeatureRequest},
    outline::{Outline, OutlineContext},
    protocol::{Hover, HoverContents, Position, RangeExt, TextDocumentPositionParams},
    syntax::{latex, LatexLabelKind, SyntaxNode},
    workspace::{Document, DocumentContent},
};
use async_trait::async_trait;
use std::sync::Arc;

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

#[async_trait]
impl FeatureProvider for LatexLabelHoverProvider {
    type Params = TextDocumentPositionParams;
    type Output = Option<Hover>;

    async fn execute<'a>(&'a self, req: &'a FeatureRequest<Self::Params>) -> Self::Output {
        let table = req.current().content.as_latex()?;
        let reference = Self::find_reference(table, req.params.position)?;
        let (doc, def) = Self::find_definition(&req.view, reference)?;

        let snapshot = Arc::clone(&req.view.snapshot);
        let view = DocumentView::analyze(snapshot, doc, &req.options, &req.current_dir);
        let outline = Outline::analyze(&view, &req.options, &req.current_dir);
        let outline_ctx = OutlineContext::parse(&view, &outline, def)?;
        let markup = outline_ctx.documentation();
        Some(Hover {
            contents: HoverContents::Markup(markup),
            range: Some(reference.range()),
        })
    }
}

impl LatexLabelHoverProvider {
    fn find_reference(table: &latex::SymbolTable, pos: Position) -> Option<&latex::Token> {
        for label in &table.labels {
            let names = label.names(&table);
            if names.len() == 1 && table[label.parent].range().contains(pos) {
                return Some(&label.names(&table)[0]);
            }

            for name in &names {
                if name.range().contains(pos) {
                    return Some(name);
                }
            }
        }
        None
    }

    fn find_definition(
        view: &DocumentView,
        reference: &latex::Token,
    ) -> Option<(Arc<Document>, latex::Label)> {
        for doc in &view.related {
            if let DocumentContent::Latex(table) = &doc.content {
                for label in &table.labels {
                    if label.kind == LatexLabelKind::Definition {
                        for name in label.names(&table) {
                            if name.text() == reference.text() {
                                return Some((Arc::clone(&doc), *label));
                            }
                        }
                    }
                }
            }
        }
        None
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        feature::FeatureTester,
        protocol::{Range, RangeExt},
    };

    #[tokio::test]
    async fn empty_latex_document() {
        let actual_hover = FeatureTester::new()
            .file("main.tex", "")
            .main("main.tex")
            .position(0, 0)
            .test_position(LatexLabelHoverProvider)
            .await;

        assert_eq!(actual_hover, None);
    }

    #[tokio::test]
    async fn empty_bibtex_document() {
        let actual_hover = FeatureTester::new()
            .file("main.bib", "")
            .main("main.bib")
            .position(0, 0)
            .test_position(LatexLabelHoverProvider)
            .await;

        assert_eq!(actual_hover, None);
    }

    #[tokio::test]
    async fn section() {
        let actual_hover = FeatureTester::new()
            .file("main.tex", r#"\section{Foo}\label{sec:foo}"#)
            .main("main.tex")
            .position(0, 23)
            .test_position(LatexLabelHoverProvider)
            .await
            .unwrap();

        assert_eq!(actual_hover.range.unwrap(), Range::new_simple(0, 20, 0, 27));
    }
}