summaryrefslogtreecommitdiff
path: root/support/texlab/crates/diagnostics/src/labels.rs
blob: 03df664a15fb477e631838a34cc39db0f444f32e (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
use std::borrow::Cow;

use base_db::{
    semantics::tex::{Label, LabelKind},
    util::queries,
    DocumentData, Workspace,
};
use itertools::Itertools;
use rustc_hash::FxHashSet;

use crate::{
    types::{DiagnosticData, TexError},
    Diagnostic, DiagnosticBuilder, DiagnosticSource,
};

#[derive(Default)]
pub struct LabelErrors;

impl DiagnosticSource for LabelErrors {
    fn publish<'db>(
        &'db mut self,
        workspace: &'db Workspace,
        builder: &mut DiagnosticBuilder<'db>,
    ) {
        detect_undefined_and_unused_labels(workspace, builder);
        detect_duplicate_labels(workspace, builder);
    }
}

fn detect_undefined_and_unused_labels<'db>(
    workspace: &'db Workspace,
    builder: &mut DiagnosticBuilder<'db>,
) {
    let graphs: Vec<_> = workspace
        .iter()
        .map(|start| base_db::graph::Graph::new(workspace, start))
        .collect();

    for document in workspace.iter() {
        let DocumentData::Tex(data) = &document.data else {
            continue;
        };

        let mut label_refs = FxHashSet::default();
        let mut label_defs = FxHashSet::default();
        let project = graphs
            .iter()
            .filter(|graph| graph.preorder().contains(&document))
            .flat_map(|graph| graph.preorder());

        for label in project
            .filter_map(|child| child.data.as_tex())
            .flat_map(|data| data.semantics.labels.iter())
        {
            if label.kind == LabelKind::Definition {
                label_defs.insert(&label.name.text);
            } else {
                label_refs.insert(&label.name.text);
            }
        }

        for label in &data.semantics.labels {
            if label.kind != LabelKind::Definition && !label_defs.contains(&label.name.text) {
                let diagnostic = Diagnostic {
                    range: label.name.range,
                    data: DiagnosticData::Tex(TexError::UndefinedLabel),
                };
                builder.push(&document.uri, Cow::Owned(diagnostic));
            }

            if label.kind == LabelKind::Definition && !label_refs.contains(&label.name.text) {
                let diagnostic = Diagnostic {
                    range: label.name.range,
                    data: DiagnosticData::Tex(TexError::UnusedLabel),
                };
                builder.push(&document.uri, Cow::Owned(diagnostic));
            }
        }
    }
}

fn detect_duplicate_labels<'db>(workspace: &'db Workspace, builder: &mut DiagnosticBuilder<'db>) {
    for conflict in queries::Conflict::find_all::<Label>(workspace) {
        let others = conflict
            .rest
            .iter()
            .map(|location| (location.document.uri.clone(), location.range))
            .collect();

        let diagnostic = Diagnostic {
            range: conflict.main.range,
            data: DiagnosticData::Tex(TexError::DuplicateLabel(others)),
        };

        builder.push(&conflict.main.document.uri, Cow::Owned(diagnostic));
    }
}