//! Diagnostics, published on every buffer change. use lsp_types::{Diagnostic, DiagnosticSeverity}; use crate::state::Document; /// The source string every diagnostic this server publishes carries, so a /// user can tell ours from another server's. pub const SOURCE: &str = "lucisv"; /// Everything the server has to say about a buffer: the parse errors first, /// then the lint warnings. /// /// **One list, one publish.** `textDocument/publishDiagnostics` *replaces* a /// document's diagnostics; it never merges. Publishing the parse pass and the /// lint pass as two notifications would leave whichever arrived second as the /// only one on screen, and make the squiggles flicker as each keystroke /// overwrote the other set. So the two sources are concatenated here and sent /// once. Each source is already in position order; they are not interleaved, /// because an error the user must fix reads better above an opinion about /// style. /// /// The missing-closer warnings sit between the two: they explain a parse /// error rather than add an opinion, and they exist at all only when the parse /// already failed (`pairs::missing_end_diagnostics`). pub fn all(doc: &Document) -> Vec { // A buffer on the large-file fast path was never parsed, so there is // nothing to say about it — and both passes below would read its *text* // (the lint context builds one, the fence filter scans for them), which is // the whole-file work the fast path exists to skip. The empty list is not // a silent no-op either: it is what clears the squiggles a buffer that has // just grown past the threshold left behind. if doc.large { return Vec::new(); } let mut all = diagnostics(doc); all.extend(crate::pairs::missing_end_diagnostics(doc)); all.extend(crate::lint::diagnostics(doc)); all } /// One LSP diagnostic per diagnostic the tolerant parser recorded. /// /// The list is always published, empty included: an empty publish is how a /// server clears the squiggles it drew for the previous keystroke. pub fn diagnostics(doc: &Document) -> Vec { let positions = doc.positions(); doc.diagnostics .iter() .map(|d| Diagnostic { // A lex error's span is empty; widen it so the editor draws // something. range: positions.diagnostic_range(d.span), severity: Some(DiagnosticSeverity::ERROR), source: Some(SOURCE.to_string()), message: d.message.clone(), ..Default::default() }) .collect() } #[cfg(test)] mod tests { use super::*; use crate::state::Workspace; use lsp_types::Uri; use std::str::FromStr; fn doc_of(text: &str) -> (Workspace, Uri) { let uri = Uri::from_str("file:///rtl/m.sv").expect("uri"); let mut ws = Workspace::default(); ws.set_document(uri.clone(), text.to_string()); (ws, uri) } #[test] fn a_clean_file_publishes_nothing() { let (ws, uri) = doc_of("module m;\n wire a;\nendmodule\n"); assert!(diagnostics(ws.document(&uri).expect("open")).is_empty()); } /// Guard for the one-parse QA finding: `Document.tree` is a cached /// derivation of `Document.text`, and nothing else asserts they agree. /// If `set_document` ever stops rebuilding the tree, the stale /// `module-filename` warning below survives the rename and this fails. #[test] fn set_document_rebuilds_the_tree_so_diagnostics_track_the_text() { let (mut ws, uri) = doc_of("module wrong;\nendmodule\n"); let before = all(ws.document(&uri).expect("open")); assert!( before.iter().any(|d| matches!(&d.code, Some(lsp_types::NumberOrString::String(s)) if s == "module-filename")), "premise: the mismatched name must warn, got {before:?}" ); ws.set_document(uri.clone(), "module m;\nendmodule\n".to_string()); let after = all(ws.document(&uri).expect("open")); assert!(after.is_empty(), "a stale tree survived the rename: {after:?}"); } #[test] fn a_broken_file_reports_at_the_right_line() { let (ws, uri) = doc_of("module m;\n wire ;\nendmodule\n"); let diags = diagnostics(ws.document(&uri).expect("open")); assert_eq!(diags.len(), 1); assert_eq!(diags[0].range.start.line, 1, "the broken line, 0-based"); assert_eq!(diags[0].severity, Some(DiagnosticSeverity::ERROR)); assert_eq!(diags[0].source.as_deref(), Some("lucisv")); assert!(diags[0].range.start < diags[0].range.end, "never zero-width"); } #[test] fn multi_byte_content_before_the_error_shifts_the_character() { // The `é` is 2 bytes but 1 UTF-16 unit: a byte-based server reports // character 8 here, and the squiggle lands one column late. let (ws, uri) = doc_of("module m;\n // é\n wire ;\nendmodule\n"); let diags = diagnostics(ws.document(&uri).expect("open")); assert_eq!(diags.len(), 1); assert_eq!(diags[0].range.start.line, 2); assert_eq!(diags[0].range.start.character, 7, "the `;`, in UTF-16 units"); } #[test] fn one_list_carries_both_sources() { // A file that parses cleanly and violates a lint rule: the publish is // one list, and the lint finding is in it as a warning. let (ws, uri) = doc_of("module m;\n\twire a;\nendmodule\n"); let published = all(ws.document(&uri).expect("open")); assert_eq!(published.len(), 1); assert_eq!(published[0].severity, Some(DiagnosticSeverity::WARNING)); assert_eq!(published[0].code, Some(lsp_types::NumberOrString::String("no-tabs".into()))); } #[test] fn a_parse_error_and_a_lint_warning_arrive_together() { let (ws, uri) = doc_of("module m;\n\twire ;\nendmodule\n"); let published = all(ws.document(&uri).expect("open")); let severities: Vec<_> = published.iter().map(|d| d.severity).collect(); assert!(severities.contains(&Some(DiagnosticSeverity::ERROR)), "{published:#?}"); assert!(severities.contains(&Some(DiagnosticSeverity::WARNING)), "{published:#?}"); assert!(published.iter().all(|d| d.source.as_deref() == Some("lucisv"))); // Errors first: a parse error is what the user must fix. assert_eq!(published[0].severity, Some(DiagnosticSeverity::ERROR)); } #[test] fn a_lex_error_gets_a_visible_range() { // The lexer gives up at end of file, which a trailing newline makes // an empty line; the range falls back to the line that has the text. let (ws, uri) = doc_of("module m;\n wire a; /* unterminated\n"); let diags = diagnostics(ws.document(&uri).expect("open")); assert_eq!(diags.len(), 1); assert_eq!(diags[0].message, "unterminated block comment"); assert_eq!(diags[0].range.start.line, 1); assert!(diags[0].range.start < diags[0].range.end, "must be drawable"); } }