summaryrefslogtreecommitdiff
path: root/support/texlab/src/diagnostics/latex.rs
blob: b835f8839839e37eeb1fcdfe3a495c3d4e3ab31c (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
use std::sync::Arc;

use cstree::TextRange;
use lsp_types::{Diagnostic, DiagnosticSeverity};
use multimap::MultiMap;

use crate::{
    syntax::{latex, CstNode},
    Document, LineIndexExt, Uri, Workspace,
};

pub fn analyze_latex_static(
    workspace: &dyn Workspace,
    diagnostics_by_uri: &mut MultiMap<Arc<Uri>, Diagnostic>,
    uri: &Uri,
) -> Option<()> {
    let document = workspace.get(uri)?;
    if !document.uri.as_str().ends_with(".tex") {
        return None;
    }

    let data = document.data.as_latex()?;

    for node in data.root.descendants() {
        analyze_environment(&document, diagnostics_by_uri, node)
            .or_else(|| analyze_curly_group(&document, diagnostics_by_uri, node))
            .or_else(|| {
                if node.kind() == latex::ERROR && node.first_token()?.text() == "}" {
                    diagnostics_by_uri.insert(
                        Arc::clone(&document.uri),
                        Diagnostic {
                            range: document.line_index.line_col_lsp_range(node.text_range()),
                            severity: Some(DiagnosticSeverity::Error),
                            code: None,
                            code_description: None,
                            source: Some("texlab".to_string()),
                            message: "Unexpected \"}\"".to_string(),
                            related_information: None,
                            tags: None,
                            data: None,
                        },
                    );
                    Some(())
                } else {
                    None
                }
            });
    }

    Some(())
}

fn analyze_environment(
    document: &Document,
    diagnostics_by_uri: &mut MultiMap<Arc<Uri>, Diagnostic>,
    node: &latex::SyntaxNode,
) -> Option<()> {
    let environment = latex::Environment::cast(node)?;
    let name1 = environment.begin()?.name()?.word()?;
    let name2 = environment.end()?.name()?.word()?;
    if name1.text() != name2.text() {
        diagnostics_by_uri.insert(
            Arc::clone(&document.uri),
            Diagnostic {
                range: document.line_index.line_col_lsp_range(name1.text_range()),
                severity: Some(DiagnosticSeverity::Error),
                code: None,
                code_description: None,
                source: Some("texlab".to_string()),
                message: "Mismatched environment".to_string(),
                related_information: None,
                tags: None,
                data: None,
            },
        );
    }
    Some(())
}

fn analyze_curly_group(
    document: &Document,
    diagnostics_by_uri: &mut MultiMap<Arc<Uri>, Diagnostic>,
    node: &latex::SyntaxNode,
) -> Option<()> {
    if !matches!(
        node.kind(),
        latex::CURLY_GROUP
            | latex::CURLY_GROUP_COMMAND
            | latex::CURLY_GROUP_KEY_VALUE
            | latex::CURLY_GROUP_WORD
            | latex::CURLY_GROUP_WORD_LIST
    ) {
        return None;
    }

    let is_inside_verbatim_environment = node
        .ancestors()
        .filter_map(latex::Environment::cast)
        .filter_map(|env| env.begin())
        .filter_map(|begin| begin.name())
        .filter_map(|name| name.word())
        .any(|name| ["asy", "lstlisting", "minted", "verbatim"].contains(&name.text()));

    if !is_inside_verbatim_environment
        && !node
            .children_with_tokens()
            .filter_map(|element| element.into_token())
            .any(|token| token.kind() == latex::R_CURLY)
    {
        diagnostics_by_uri.insert(
            Arc::clone(&document.uri),
            Diagnostic {
                range: document
                    .line_index
                    .line_col_lsp_range(TextRange::empty(node.text_range().end())),
                severity: Some(DiagnosticSeverity::Error),
                code: None,
                code_description: None,
                source: Some("texlab".to_string()),
                message: "Missing \"}\" inserted".to_string(),
                related_information: None,
                tags: None,
                data: None,
            },
        );
    }

    Some(())
}