Files
lucisv/crates/vfmt_server/src/diagnostics.rs
T
lucioandClaude Fable 5 c8b936a741 Add vfmt-server and linter-core (jointly: round 11)
vfmt-server (new crate, binary vfmt-server): LSP over stdio on
rust-analyzer's minimal stack (lsp-server/lsp-types/serde/serde_json,
exact-pinned, 18 packages audited incl. all build scripts). Ships
formatting (byte-identical to the CLI), tolerant-parse diagnostics
with UTF-16-correct positions (CRLF collapse pinned; EOF spans
widened to drawable lines), semanticTokens/full, hover with `define
expansion (file table -> workspace fallback), definition through
both resolution layers (Ambiguous -> all locations), references,
documentSymbol and capped workspace/symbol. Deferred capabilities
are not advertised. 860-file corpus session: 0.79s, 29.5MB. QA drove
a real stdio session: diagnostics clear on fix, staleness model
honored, malformed frames survived.

linter-core (new crate vlog_linter + vfmt check): ruff-style rules
with Safe/Unsafe fix applicability, inline waivers with unused-
waiver detection (Verible-spelling waivers for unimplemented rules
deliberately tolerated -- corpus-driven), seven seed rules, --fix /
--select / --ignore / --output-format json, frozen exit codes, bare
vfmt byte-identical to before (worktree-proven). Corpus baseline:
7806 violations in 93 files, deterministic. Lead-directed fix:
--fix never edits inside // vfmt: off fences (reported, not
auto-fixed), with a scan backstop for tolerant-parse trees where
fence::collect sees no regions.

Both dev agents were interrupted by a session limit mid-round and
resumed from transcripts; the workspace survived intact.

Dev: 2 Opus agents; QA: 2 Sonnet agents; reviews in
docs/features/{vfmt-server,linter-core}/README.md.

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-08-19 09:15:54 -07:00

85 lines
3.2 KiB
Rust

//! Parse 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.
const SOURCE: &str = "vfmt";
/// 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());
}
#[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("vfmt"));
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 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");
}
}