summaryrefslogtreecommitdiff
path: root/support/texlab/src/diagnostics.rs
blob: bdd8faf106f14646bb1246770112e70f1a756478 (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
mod bibtex;
mod build;
mod chktex;
mod latex;

use std::sync::Arc;

use dashmap::DashMap;
use lsp_types::{DiagnosticSeverity, NumberOrString, Range, Url};
use regex::Regex;

use crate::Workspace;

use self::{
    bibtex::collect_bibtex_diagnostics, build::collect_build_diagnostics,
    chktex::collect_chktex_diagnostics, latex::collect_latex_diagnostics,
};

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Diagnostic {
    pub severity: DiagnosticSeverity,
    pub range: Range,
    pub code: DiagnosticCode,
    pub message: String,
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Hash)]
pub enum DiagnosticCode {
    Latex(LatexCode),
    Bibtex(BibtexCode),
    Chktex(String),
    Build(Arc<Url>),
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
pub enum LatexCode {
    UnexpectedRCurly,
    RCurlyInserted,
    MismatchedEnvironment,
}

impl From<LatexCode> for String {
    fn from(code: LatexCode) -> Self {
        match code {
            LatexCode::UnexpectedRCurly => "Unexpected \"}\"".to_string(),
            LatexCode::RCurlyInserted => "Missing \"}\" inserted".to_string(),
            LatexCode::MismatchedEnvironment => "Mismatched environment".to_string(),
        }
    }
}

impl From<LatexCode> for NumberOrString {
    fn from(code: LatexCode) -> Self {
        match code {
            LatexCode::UnexpectedRCurly => NumberOrString::Number(1),
            LatexCode::RCurlyInserted => NumberOrString::Number(2),
            LatexCode::MismatchedEnvironment => NumberOrString::Number(3),
        }
    }
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash)]
#[allow(clippy::enum_variant_names)]
pub enum BibtexCode {
    ExpectingLCurly,
    ExpectingKey,
    ExpectingRCurly,
    ExpectingEq,
    ExpectingFieldValue,
}

impl From<BibtexCode> for String {
    fn from(code: BibtexCode) -> Self {
        match code {
            BibtexCode::ExpectingLCurly => "Expecting a curly bracket: \"{\"".to_string(),
            BibtexCode::ExpectingKey => "Expecting a key".to_string(),
            BibtexCode::ExpectingRCurly => "Expecting a curly bracket: \"}\"".to_string(),
            BibtexCode::ExpectingEq => "Expecting an equality sign: \"=\"".to_string(),
            BibtexCode::ExpectingFieldValue => "Expecting a field value".to_string(),
        }
    }
}

impl From<BibtexCode> for NumberOrString {
    fn from(code: BibtexCode) -> Self {
        match code {
            BibtexCode::ExpectingLCurly => NumberOrString::Number(4),
            BibtexCode::ExpectingKey => NumberOrString::Number(5),
            BibtexCode::ExpectingRCurly => NumberOrString::Number(6),
            BibtexCode::ExpectingEq => NumberOrString::Number(7),
            BibtexCode::ExpectingFieldValue => NumberOrString::Number(8),
        }
    }
}

#[derive(Default, Clone)]
pub struct DiagnosticManager {
    all_diagnostics: Arc<DashMap<Arc<Url>, Vec<Diagnostic>>>,
}

impl DiagnosticManager {
    pub fn push_syntax(&self, workspace: &Workspace, uri: &Url) {
        collect_bibtex_diagnostics(&self.all_diagnostics, workspace, uri)
            .or_else(|| collect_latex_diagnostics(&self.all_diagnostics, workspace, uri))
            .or_else(|| collect_build_diagnostics(&self.all_diagnostics, workspace, uri));
    }

    pub fn push_chktex(&self, workspace: &Workspace, uri: &Url) {
        collect_chktex_diagnostics(&self.all_diagnostics, workspace, uri);
    }

    pub fn publish(&self, workspace: &Workspace, uri: &Url) -> Vec<lsp_types::Diagnostic> {
        let options = &workspace.environment.options.diagnostics;

        let mut results = Vec::new();
        if let Some(diagnostics) = self.all_diagnostics.get(uri) {
            for diagnostic in diagnostics.iter() {
                if !options.allowed_patterns.is_empty()
                    && !options
                        .allowed_patterns
                        .iter()
                        .any(|pattern| pattern.0.is_match(&diagnostic.message))
                {
                    continue;
                }

                if options
                    .ignored_patterns
                    .iter()
                    .any(|pattern| pattern.0.is_match(&diagnostic.message))
                {
                    continue;
                }

                let source = match diagnostic.code {
                    DiagnosticCode::Latex(_) | DiagnosticCode::Bibtex(_) => "texlab",
                    DiagnosticCode::Chktex(_) => "chktex",
                    DiagnosticCode::Build(_) => "latex-build",
                };

                let code = match diagnostic.code.clone() {
                    DiagnosticCode::Latex(code) => Some(code.into()),
                    DiagnosticCode::Bibtex(code) => Some(code.into()),
                    DiagnosticCode::Chktex(code) => Some(NumberOrString::String(code)),
                    DiagnosticCode::Build(_) => None,
                };

                results.push(lsp_types::Diagnostic {
                    range: diagnostic.range,
                    code,
                    severity: Some(diagnostic.severity),
                    message: diagnostic.message.clone(),
                    source: Some(source.to_string()),
                    ..Default::default()
                });
            }
        }

        results
    }
}

#[derive(Debug, Default)]
pub struct DiagnosticFilter {
    pub allowed_patterns: Vec<Regex>,
    pub ignored_patterns: Vec<Regex>,
}