summaryrefslogtreecommitdiff
path: root/support/texlab/crates/texlab/src/features/definition/label.rs
blob: 84f17b570452fc602958678f30e248ef13667ab3 (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
use base_db::{semantics::tex::LabelKind, DocumentData};

use crate::util::{self, cursor::CursorContext};

use super::DefinitionResult;

pub(super) fn goto_definition<'a>(
    context: &CursorContext<'a>,
) -> Option<Vec<DefinitionResult<'a>>> {
    let (name_text, origin_selection_range) = context
        .find_label_name_key()
        .or_else(|| context.find_label_name_command())?;

    for document in &context.project.documents {
        let DocumentData::Tex(data) = &document.data else { continue };

        let Some(label) = data
            .semantics
            .labels
            .iter()
            .filter(|label| label.kind == LabelKind::Definition)
            .find(|label| label.name.text == name_text) else { continue };

        let target_selection_range = label.name.range;
        let target_range = util::label::render(context.workspace, &context.project, label)
            .map_or(target_selection_range, |label| label.range);

        return Some(vec![DefinitionResult {
            origin_selection_range,
            target: document,
            target_range,
            target_selection_range,
        }]);
    }

    None
}