summaryrefslogtreecommitdiff
path: root/support/texlab/crates/texlab/src/util/diagnostics.rs
blob: 0ef24d065bf16bb10177c1b3a329979f0ab61efa (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use base_db::{util::filter_regex_patterns, Document, Workspace};
use diagnostics::{
    types::{BibError, Diagnostic, DiagnosticData, TexError},
    DiagnosticBuilder, DiagnosticSource,
};
use rowan::TextRange;
use rustc_hash::FxHashMap;
use syntax::BuildErrorLevel;

use super::line_index_ext::LineIndexExt;

pub fn collect<'db>(
    workspace: &'db Workspace,
    source: &mut dyn DiagnosticSource,
) -> FxHashMap<&'db Document, Vec<lsp_types::Diagnostic>> {
    let mut builder = DiagnosticBuilder::default();
    source.publish(workspace, &mut builder);
    builder
        .iter()
        .filter_map(|(uri, diags)| workspace.lookup(uri).map(|document| (document, diags)))
        .map(|(document, diags)| {
            let diags = diags
                .into_iter()
                .filter_map(|diag| create_diagnostic(workspace, document, diag))
                .collect::<Vec<_>>();

            (document, diags)
        })
        .collect()
}

fn create_diagnostic(
    workspace: &Workspace,
    document: &Document,
    diagnostic: &Diagnostic,
) -> Option<lsp_types::Diagnostic> {
    let range = document.line_index.line_col_lsp_range(diagnostic.range)?;

    let severity = match &diagnostic.data {
        DiagnosticData::Tex(error) => match error {
            TexError::UnexpectedRCurly => lsp_types::DiagnosticSeverity::ERROR,
            TexError::ExpectingRCurly => lsp_types::DiagnosticSeverity::ERROR,
            TexError::MismatchedEnvironment => lsp_types::DiagnosticSeverity::ERROR,
            TexError::UnusedLabel => lsp_types::DiagnosticSeverity::HINT,
            TexError::UndefinedLabel => lsp_types::DiagnosticSeverity::ERROR,
            TexError::UndefinedCitation => lsp_types::DiagnosticSeverity::ERROR,
            TexError::DuplicateLabel(_) => lsp_types::DiagnosticSeverity::ERROR,
        },
        DiagnosticData::Bib(error) => match error {
            BibError::ExpectingLCurly => lsp_types::DiagnosticSeverity::ERROR,
            BibError::ExpectingKey => lsp_types::DiagnosticSeverity::ERROR,
            BibError::ExpectingRCurly => lsp_types::DiagnosticSeverity::ERROR,
            BibError::ExpectingEq => lsp_types::DiagnosticSeverity::ERROR,
            BibError::ExpectingFieldValue => lsp_types::DiagnosticSeverity::ERROR,
            BibError::UnusedEntry => lsp_types::DiagnosticSeverity::HINT,
            BibError::DuplicateEntry(_) => lsp_types::DiagnosticSeverity::ERROR,
        },
        DiagnosticData::Build(error) => match error.level {
            BuildErrorLevel::Error => lsp_types::DiagnosticSeverity::ERROR,
            BuildErrorLevel::Warning => lsp_types::DiagnosticSeverity::WARNING,
        },
    };

    let code = match &diagnostic.data {
        DiagnosticData::Tex(error) => match error {
            TexError::UnexpectedRCurly => Some(1),
            TexError::ExpectingRCurly => Some(2),
            TexError::MismatchedEnvironment => Some(3),
            TexError::UnusedLabel => Some(9),
            TexError::UndefinedLabel => Some(10),
            TexError::UndefinedCitation => Some(11),
            TexError::DuplicateLabel(_) => Some(14),
        },
        DiagnosticData::Bib(error) => match error {
            BibError::ExpectingLCurly => Some(4),
            BibError::ExpectingKey => Some(5),
            BibError::ExpectingRCurly => Some(6),
            BibError::ExpectingEq => Some(7),
            BibError::ExpectingFieldValue => Some(8),
            BibError::UnusedEntry => Some(12),
            BibError::DuplicateEntry(_) => Some(13),
        },
        DiagnosticData::Build(_) => None,
    };

    let source = match &diagnostic.data {
        DiagnosticData::Tex(_) | DiagnosticData::Bib(_) => "texlab",
        DiagnosticData::Build(_) => "latex",
    };

    let message = String::from(match &diagnostic.data {
        DiagnosticData::Tex(error) => match error {
            TexError::UnexpectedRCurly => "Unexpected \"}\"",
            TexError::ExpectingRCurly => "Expecting a curly bracket: \"}\"",
            TexError::MismatchedEnvironment => "Mismatched environment",
            TexError::UnusedLabel => "Unused label",
            TexError::UndefinedLabel => "Undefined reference",
            TexError::UndefinedCitation => "Undefined reference",
            TexError::DuplicateLabel(_) => "Duplicate label",
        },
        DiagnosticData::Bib(error) => match error {
            BibError::ExpectingLCurly => "Expecting a curly bracket: \"{\"",
            BibError::ExpectingKey => "Expecting a key",
            BibError::ExpectingRCurly => "Expecting a curly bracket: \"}\"",
            BibError::ExpectingEq => "Expecting an equality sign: \"=\"",
            BibError::ExpectingFieldValue => "Expecting a field value",
            BibError::UnusedEntry => "Unused entry",
            BibError::DuplicateEntry(_) => "Duplicate entry key",
        },
        DiagnosticData::Build(error) => &error.message,
    });

    let tags = match &diagnostic.data {
        DiagnosticData::Tex(error) => match error {
            TexError::UnexpectedRCurly => None,
            TexError::ExpectingRCurly => None,
            TexError::MismatchedEnvironment => None,
            TexError::UnusedLabel => Some(vec![lsp_types::DiagnosticTag::UNNECESSARY]),
            TexError::UndefinedLabel => None,
            TexError::UndefinedCitation => None,
            TexError::DuplicateLabel(_) => None,
        },
        DiagnosticData::Bib(error) => match error {
            BibError::ExpectingLCurly => None,
            BibError::ExpectingKey => None,
            BibError::ExpectingRCurly => None,
            BibError::ExpectingEq => None,
            BibError::ExpectingFieldValue => None,
            BibError::UnusedEntry => Some(vec![lsp_types::DiagnosticTag::UNNECESSARY]),
            BibError::DuplicateEntry(_) => None,
        },
        DiagnosticData::Build(_) => None,
    };

    let related_information = match &diagnostic.data {
        DiagnosticData::Tex(error) => match error {
            TexError::UnexpectedRCurly => None,
            TexError::ExpectingRCurly => None,
            TexError::MismatchedEnvironment => None,
            TexError::UnusedLabel => None,
            TexError::UndefinedLabel => None,
            TexError::UndefinedCitation => None,
            TexError::DuplicateLabel(others) => make_conflict_info(workspace, others, "label"),
        },
        DiagnosticData::Bib(error) => match error {
            BibError::ExpectingLCurly => None,
            BibError::ExpectingKey => None,
            BibError::ExpectingRCurly => None,
            BibError::ExpectingEq => None,
            BibError::ExpectingFieldValue => None,
            BibError::UnusedEntry => None,
            BibError::DuplicateEntry(others) => make_conflict_info(workspace, others, "entry"),
        },
        DiagnosticData::Build(_) => None,
    };

    Some(lsp_types::Diagnostic {
        severity: Some(severity),
        code: code.map(lsp_types::NumberOrString::Number),
        source: Some(String::from(source)),
        tags,
        related_information,
        ..lsp_types::Diagnostic::new_simple(range, message)
    })
}

fn make_conflict_info(
    workspace: &Workspace,
    locations: &Vec<(lsp_types::Url, TextRange)>,
    object: &str,
) -> Option<Vec<lsp_types::DiagnosticRelatedInformation>> {
    let mut items = Vec::new();
    for (uri, range) in locations {
        let range = workspace
            .lookup(uri)?
            .line_index
            .line_col_lsp_range(*range)?;

        let message = format!("conflicting {object} defined here");
        let location = lsp_types::Location::new(uri.clone(), range);
        items.push(lsp_types::DiagnosticRelatedInformation { location, message });
    }

    Some(items)
}

pub fn filter(
    all_diagnostics: &mut FxHashMap<&Document, Vec<lsp_types::Diagnostic>>,
    workspace: &Workspace,
) {
    let config = &workspace.config().diagnostics;
    for diagnostics in all_diagnostics.values_mut() {
        diagnostics.retain(|diagnostic| {
            filter_regex_patterns(
                &diagnostic.message,
                &config.allowed_patterns,
                &config.ignored_patterns,
            )
        });
    }
}