202 lines
9.3 KiB
Rust
202 lines
9.3 KiB
Rust
//! 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`).
|
|
///
|
|
/// `#[cfg(test)]` for the same reason `lint::diagnostics` is: the server
|
|
/// always has a workspace and always calls [`all_in`], so this is the door the
|
|
/// tests that predate the project-aware pass come in by.
|
|
#[cfg(test)]
|
|
pub fn all(doc: &Document) -> Vec<Diagnostic> {
|
|
all_in(doc, &crate::lint::LintRun::default())
|
|
}
|
|
|
|
/// [`all`] for a lint pass that knows what the project says — the workspace's
|
|
/// rule selection and its identifier census
|
|
/// (`docs/features/semantic-lints-on/README.md`). The parse and missing-closer
|
|
/// halves are unaffected by either; only the lint pass reads `run`.
|
|
pub fn all_in(doc: &Document, run: &crate::lint::LintRun) -> Vec<Diagnostic> {
|
|
// 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_in(doc, run));
|
|
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<Diagnostic> {
|
|
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 at its rule's own severity
|
|
// — `no-tabs` is a style rule, so INFORMATION (§33.0).
|
|
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::INFORMATION));
|
|
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::INFORMATION)), "{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));
|
|
}
|
|
|
|
/// The user-visible contract of header recovery (TODO §32), pinned where
|
|
/// the user meets it: a typo in a port list costs **one** diagnostic, and
|
|
/// the module around it survives whole — its name, the ports typed before
|
|
/// and after the typo, and its body all still in the outline.
|
|
///
|
|
/// Before this round the same buffer published two errors (the header, and
|
|
/// the orphaned `endmodule`), the body re-parsed as file-scope items, and
|
|
/// the outline had no module in it at all.
|
|
#[test]
|
|
fn a_broken_port_list_publishes_one_error_and_keeps_its_module() {
|
|
let (ws, uri) = doc_of("module m (input a, , input b);\n wire ok;\nendmodule\n");
|
|
let doc = ws.document(&uri).expect("open");
|
|
let diags = diagnostics(doc);
|
|
assert_eq!(diags.len(), 1, "{diags:#?}");
|
|
assert_eq!(diags[0].severity, Some(DiagnosticSeverity::ERROR));
|
|
assert_eq!(diags[0].range.start.line, 0, "the header's own line");
|
|
|
|
// The missing-closer pass explains a *parse* failure; there is no
|
|
// unclosed block here, so it must add nothing.
|
|
assert!(crate::pairs::missing_end_diagnostics(doc).is_empty());
|
|
|
|
let symbols = crate::symbols::document_symbols(doc);
|
|
assert_eq!(symbols.len(), 1, "the module is still in the outline");
|
|
assert_eq!(symbols[0].name, "m");
|
|
let children = symbols[0].children.as_ref().expect("module contents");
|
|
let names: Vec<&str> = children.iter().map(|c| c.name.as_str()).collect();
|
|
assert!(names.contains(&"a"), "the port before the typo: {names:?}");
|
|
assert!(names.contains(&"b"), "the port after it: {names:?}");
|
|
assert!(names.contains(&"ok"), "the body: {names:?}");
|
|
}
|
|
|
|
#[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");
|
|
}
|
|
}
|