Skip to main content

nifty_lsp/features/
diagnostics.rs

1use lsp_types::{Diagnostic, DiagnosticRelatedInformation, DiagnosticSeverity};
2use nifty_syntax::FileId;
3
4use crate::document::Encoding;
5use crate::project::Project;
6
7const SOURCE: &str = "nifty";
8
9/// The errors in `file`, with the other places each involves, which may be in other files.
10pub(crate) fn diagnostics(project: &Project, file: FileId, encoding: Encoding) -> Vec<Diagnostic> {
11    let document = project.document(file);
12    project
13        .diagnostics_in(file)
14        .map(|diagnostic| {
15            let related: Vec<_> = diagnostic
16                .notes
17                .iter()
18                .map(|note| DiagnosticRelatedInformation {
19                    location: project.location(note.span, encoding),
20                    message: note.message.clone(),
21                })
22                .collect();
23            Diagnostic {
24                range: document.range(diagnostic.span, encoding),
25                severity: Some(DiagnosticSeverity::ERROR),
26                source: Some(SOURCE.to_owned()),
27                message: diagnostic.message.clone(),
28                related_information: (!related.is_empty()).then_some(related),
29                ..Diagnostic::default()
30            }
31        })
32        .collect()
33}