Files
lucisv/crates/lucisv_server/src/diagnostics.rs
T
lucioandClaude Fable 5 bc606dd6bd
ci / checks (push) Successful in 45s
ci / corpus (push) Successful in 21s
Large-file fast path: 100 MB netlists open in 40 ms
Owner-requested round, the last of the large-workarea wave. Above
lucisv.largeFile.threshold (4 MiB default, 0 disables) a buffer opens
as a line index and nothing else — no lex, no parse, no semantics, and
its path removed from the index sources so nothing describes it by
stale disk bytes. Viewport highlighting lexes only the requested
window (snap to a line boundary, budget-bounded backward comment scan,
imperfection documented); semanticTokens/full answers the spec's
cancellation errors because the shipped VS Code binary marks tokens
complete on ANY non-error answer and never asks for ranges again —
settled by reading the bundle, verified independently by QA, pinned
with the client-library citations. Same-file definition/references
shipped on measurement (worst 37 ms vs the 150 ms budget); every
other handler answers as the empty file by construction; formatting
alone says why aloud. Two client facts recorded: VS Code never syncs
files over 50 MB, and above 20 MB our viewport tokens are the only
color.

QA rejected the first cut on the round's true shape — thousands of
tokens on one long line went quadratic through per-call UTF-16
conversion (7.86 s to color one 269 KB line). The fix adds a monotonic
PositionWalker to convert.rs (restart-on-backwards proven load-bearing
by the out-of-order tail-comment shape), adopted at three sites for
546x/759x on the probes — and it fixed ordinary files' semantic
tokens too (2.06 s to 232 ms on a one-line buffer), a cost the normal
path had always paid. Probes are now normally-running gates with 4.5x
margins, walker-vs-per-call equivalence swept over gnarly fixtures
and adversarial ping-pong.

51 tests added across both rounds (dev 43+5, QA 6+3, minus overlaps),
2 ignored probes. Completes the owner's large-workarea requests.

docs/features/large-file-mode/README.md

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-08-27 18:58:17 -07:00

157 lines
6.9 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`).
pub fn all(doc: &Document) -> 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(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<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 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");
}
}