summaryrefslogtreecommitdiff
path: root/support/texlab/src/features/link.rs
blob: 71bba58ba70badc4f3d03db12b34ad27dce1e2c0 (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
mod include;

use lsp_types::{DocumentLink, Url};
use rowan::TextRange;

use crate::{
    db::{Document, Workspace},
    util::{line_index::LineIndex, line_index_ext::LineIndexExt},
    Db,
};

pub fn find_all(db: &dyn Db, uri: &Url) -> Option<Vec<DocumentLink>> {
    let document = Workspace::get(db).lookup_uri(db, uri)?;
    let mut builder = LinkBuilder {
        db,
        line_index: document.contents(db).line_index(db),
        links: Vec::new(),
    };

    include::find_links(db, document, &mut builder);
    Some(builder.links)
}

struct LinkBuilder<'db> {
    db: &'db dyn Db,
    line_index: &'db LineIndex,
    links: Vec<DocumentLink>,
}

impl<'db> LinkBuilder<'db> {
    pub fn push(&mut self, range: TextRange, target: Document) {
        let range = self.line_index.line_col_lsp_range(range);
        let target = Some(target.location(self.db).uri(self.db).clone());
        self.links.push(DocumentLink {
            range,
            target,
            tooltip: None,
            data: None,
        });
    }
}