summaryrefslogtreecommitdiff
path: root/support/texlab/src/features/inlay_hint.rs
blob: 5d2f5d4e11b64db2f0bc3c85cb568b9758da4126 (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
mod label;

use lsp_types::{InlayHint, InlayHintLabel, Range, Url};
use rowan::TextSize;

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

pub fn find_all(db: &dyn Db, uri: &Url, range: Range) -> Option<Vec<InlayHint>> {
    let document = Workspace::get(db).lookup_uri(db, uri)?;
    let line_index = document.line_index(db);

    let mut builder = InlayHintBuilder {
        line_index,
        hints: Vec::new(),
    };

    let range = line_index.offset_lsp_range(range);
    label::find_hints(db, document, range, &mut builder);
    Some(builder.hints)
}

struct InlayHintBuilder<'db> {
    line_index: &'db LineIndex,
    hints: Vec<InlayHint>,
}

impl<'db> InlayHintBuilder<'db> {
    pub fn push(&mut self, offset: TextSize, text: String) {
        let position = self.line_index.line_col_lsp(offset);
        self.hints.push(InlayHint {
            position,
            label: InlayHintLabel::String(text),
            kind: None,
            text_edits: None,
            tooltip: None,
            padding_left: Some(true),
            padding_right: None,
            data: None,
        });
    }
}