Skip to main content

vimdoc_language_server/
diagnostics.rs

1use std::collections::HashMap;
2
3use lsp_types::{Diagnostic, DiagnosticSeverity, NumberOrString, Range};
4
5use crate::parser::Document;
6
7#[must_use]
8pub fn compute(doc: &Document) -> Vec<Diagnostic> {
9    let mut diags = Vec::new();
10    let mut defined: HashMap<&str, Vec<Range>> = HashMap::new();
11
12    for span in doc.tag_defs() {
13        defined
14            .entry(span.name.as_str())
15            .or_default()
16            .push(span.range);
17    }
18
19    for (name, ranges) in &defined {
20        if ranges.len() > 1 {
21            for &range in ranges {
22                diags.push(Diagnostic {
23                    range,
24                    severity: Some(DiagnosticSeverity::WARNING),
25                    code: Some(NumberOrString::String("duplicate-tag".into())),
26                    message: format!("duplicate tag definition: *{name}*"),
27                    source: Some("vimdoc".into()),
28                    ..Default::default()
29                });
30            }
31        }
32    }
33
34    diags
35}