summaryrefslogtreecommitdiff
path: root/support/texlab/src/features/inlay_hint/label.rs
blob: ca52433e7bcf84af513d6ffa27aeb6003f661dd0 (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
use rowan::TextRange;

use crate::{
    db::{analysis::label, Document},
    util::{self, label::LabeledObject},
    Db,
};

use super::InlayHintBuilder;

pub(super) fn find_hints(
    db: &dyn Db,
    document: Document,
    range: TextRange,
    builder: &mut InlayHintBuilder,
) -> Option<()> {
    let data = document.parse(db).as_tex()?;
    for label in data
        .analyze(db)
        .labels(db)
        .iter()
        .copied()
        .filter(|label| matches!(label.origin(db), label::Origin::Definition(_)))
        .filter(|label| label.range(db).intersect(range).is_some())
    {
        if let Some(rendered) = util::label::render(db, document, label) {
            if let Some(number) = &rendered.number {
                let text = match &rendered.object {
                    LabeledObject::Section { prefix, .. } => {
                        format!("{} {}", prefix, number.text(db))
                    }
                    LabeledObject::Float { kind, .. } => {
                        format!("{} {}", kind.as_str(), number.text(db))
                    }
                    LabeledObject::Theorem { kind, .. } => {
                        format!("{} {}", kind.text(db), number.text(db))
                    }
                    LabeledObject::Equation => format!("Equation ({})", number.text(db)),
                    LabeledObject::EnumItem => format!("Item {}", number.text(db)),
                };

                builder.push(label.range(db).end(), text);
            }
        }
    }

    Some(())
}