TODO section 28, lane C (the file-size rule in CLAUDE.md).
server.rs (2 559 production + 6 293 test lines) becomes server/{mod,
dispatch, client, index, notification, progress, diagnostics, batch,
formatting, large_file, semantic_tokens}.rs with the tests in
server/tests/ (16 files, one per former banner section, 15 shared
helpers hoisted into the harness); completion.rs (1 716 + 2 463)
becomes completion/{mod, member, module, tiers, word, site,
harness}.rs with each provider's tests beside it. answer_request (393)
and handle_notification (197) stay whole as the dispatch tables they
are; perf.rs stays whole because its one sentence has no and. Every
import elsewhere in the crate resolves through pub(crate) re-exports,
so main.rs, inline.rs, instantiate.rs, hover.rs, nav.rs and state.rs
are byte-identical. Proof: per-binary counts identical (1 435 / 85 / 3;
workspace 3 215 / 48), e2e and the vsix smoke green, a live LSP session
against opentitan answering byte-identically from this build and the
base, and a line-multiset + signature census (360/360, 188/188) done
by the dev and redone from scratch by QA.
docs/features/decomposition-server/README.md.
Co-Authored-By: Claude Fable 5 <[email protected]>
261 lines
11 KiB
Rust
261 lines
11 KiB
Rust
//! Incremental text sync and the change debounce.
|
|
|
|
use super::*;
|
|
|
|
// ------------------------------------------------------------------
|
|
// incremental text sync
|
|
// ------------------------------------------------------------------
|
|
|
|
#[test]
|
|
fn incremental_changes_land_the_same_buffer_as_one_full_change() {
|
|
let tree = Tree::new("incrementalsame");
|
|
let mut session = Session::new(vec![tree.path.clone()], false);
|
|
let uri = session.open(&tree.path.join("m.sv"), "module m;\nendmodule\n");
|
|
|
|
// Three ranges, each positioned against what the last one left.
|
|
change(
|
|
&mut session,
|
|
&uri,
|
|
2,
|
|
serde_json::json!([
|
|
{ "range": { "start": { "line": 1, "character": 0 },
|
|
"end": { "line": 1, "character": 0 } }, "text": "\n" },
|
|
{ "range": { "start": { "line": 1, "character": 0 },
|
|
"end": { "line": 1, "character": 0 } }, "text": " wire " },
|
|
{ "range": { "start": { "line": 1, "character": 7 },
|
|
"end": { "line": 1, "character": 7 } }, "text": "w;" },
|
|
]),
|
|
);
|
|
let composed = "module m;\n wire w;\nendmodule\n";
|
|
assert_eq!(buffer(&session, &uri), composed);
|
|
|
|
// The same buffer reached by a full-sync client, for comparison.
|
|
let mut full = Session::new(vec![tree.path.clone()], false);
|
|
let other = full.open(&tree.path.join("m.sv"), "module m;\nendmodule\n");
|
|
change(&mut full, &other, 2, serde_json::json!([{ "text": composed }]));
|
|
assert_eq!(buffer(&full, &other), composed);
|
|
|
|
// And the answers computed from it agree, not just the text.
|
|
assert_eq!(
|
|
session.result(
|
|
Formatting::METHOD,
|
|
serde_json::json!({
|
|
"textDocument": { "uri": uri },
|
|
"options": { "tabSize": 2, "insertSpaces": true },
|
|
}),
|
|
),
|
|
full.result(
|
|
Formatting::METHOD,
|
|
serde_json::json!({
|
|
"textDocument": { "uri": other },
|
|
"options": { "tabSize": 2, "insertSpaces": true },
|
|
}),
|
|
),
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn a_burst_of_changes_coalesces_into_one_reparse() {
|
|
// The change debounce's whole payoff: a burst of `didChange`
|
|
// notifications costs one reparse when the next thing that needs the
|
|
// analysis flushes them, not one per keystroke.
|
|
let tree = Tree::new("debounce");
|
|
let mut session = Session::new(vec![tree.path.clone()], false);
|
|
let uri = session.open(&tree.path.join("m.sv"), "module m;\nendmodule\n");
|
|
|
|
let before = session.server.generation;
|
|
session.batch(vec![
|
|
changed(&uri, 2, "module m;\n wire a;\nendmodule\n"),
|
|
changed(&uri, 3, "module m;\n wire a;\n wire b;\nendmodule\n"),
|
|
]);
|
|
// Text-current, analysis-stale: nothing has reparsed yet, and the
|
|
// newest text is already what the server holds.
|
|
assert_eq!(session.server.generation, before, "no keystroke reparses on its own");
|
|
assert_eq!(buffer(&session, &uri), "module m;\n wire a;\n wire b;\nendmodule\n");
|
|
|
|
// The first request after the burst is answered from one flush.
|
|
session.result(
|
|
DocumentSymbolRequest::METHOD,
|
|
serde_json::json!({ "textDocument": { "uri": uri } }),
|
|
);
|
|
assert_eq!(session.server.generation, before + 1, "one reparse for the whole burst");
|
|
assert!(session.server.dirty.is_empty(), "and nothing is left pending");
|
|
}
|
|
|
|
#[test]
|
|
fn closing_a_dirty_buffer_discards_its_change_and_leaves_the_others_alone() {
|
|
// The debounce's other half: a buffer closed mid-edit is never
|
|
// analyzed. Its pending text is dropped ahead of the flush at the top
|
|
// of `notification`, so the client gets the one clearing publish and
|
|
// nothing else — while every *other* dirty buffer still flushes,
|
|
// because closing one file says nothing about the rest (QA, CD-1).
|
|
let tree = Tree::new("closedirty");
|
|
let mut session = Session::new(vec![tree.path.clone()], false);
|
|
let closing = session.open(&tree.path.join("closing.sv"), "module a;\nendmodule\n");
|
|
let staying = session.open(&tree.path.join("staying.sv"), "module b;\nendmodule\n");
|
|
let _ = session.published();
|
|
|
|
// Both buffers go dirty with text that would diagnose if analyzed.
|
|
let before = session.server.generation;
|
|
for (uri, text) in [
|
|
(&closing, "module a;\n wire ;\nendmodule\n"),
|
|
(&staying, "module b;\n wire ;\nendmodule\n"),
|
|
] {
|
|
session.notify(
|
|
DidChangeTextDocument::METHOD,
|
|
serde_json::json!({
|
|
"textDocument": { "uri": uri, "version": 2 },
|
|
"contentChanges": [{ "text": text }],
|
|
}),
|
|
);
|
|
}
|
|
assert_eq!(session.server.generation, before, "a keystroke reparses nothing on its own");
|
|
|
|
session.notify(
|
|
DidCloseTextDocument::METHOD,
|
|
serde_json::json!({ "textDocument": { "uri": closing } }),
|
|
);
|
|
|
|
assert_eq!(
|
|
session.server.generation,
|
|
before + 1,
|
|
"one reparse — the buffer still open; the closing one's text went with it"
|
|
);
|
|
assert!(session.server.dirty.is_empty(), "nothing left pending");
|
|
|
|
let published = session.published();
|
|
let closed: Vec<_> = published.iter().filter(|p| p.uri == closing).collect();
|
|
assert_eq!(closed.len(), 1, "exactly one publish for the closed buffer: {published:?}");
|
|
assert!(closed[0].diagnostics.is_empty(), "and it is the clearing one: {published:?}");
|
|
let open: Vec<_> = published.iter().filter(|p| p.uri == staying).collect();
|
|
assert_eq!(open.len(), 1, "the other buffer publishes once: {published:?}");
|
|
assert!(
|
|
!open[0].diagnostics.is_empty(),
|
|
"the other buffer's pending edit was flushed as usual: {published:?}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn one_notification_is_one_generation_bump_however_many_ranges() {
|
|
// The pull-diagnostics `resultId` is this counter. One reparse and one
|
|
// stamp per notification: a bump per range would make every keystroke
|
|
// look like several to a pulling client, and none would make a
|
|
// changed buffer answer "unchanged".
|
|
let tree = Tree::new("incrementalgeneration");
|
|
let mut session = Session::with(vec![tree.path.clone()], pulling());
|
|
let uri = session.open(&tree.path.join("m.sv"), "module m;\nendmodule\n");
|
|
|
|
let before = session.server.generation;
|
|
change(
|
|
&mut session,
|
|
&uri,
|
|
2,
|
|
serde_json::json!([
|
|
{ "range": { "start": { "line": 0, "character": 9 },
|
|
"end": { "line": 0, "character": 9 } }, "text": "\n wire a;" },
|
|
{ "range": { "start": { "line": 1, "character": 9 },
|
|
"end": { "line": 1, "character": 9 } }, "text": "\n wire b;" },
|
|
{ "range": { "start": { "line": 2, "character": 7 },
|
|
"end": { "line": 2, "character": 8 } }, "text": "c" },
|
|
]),
|
|
);
|
|
assert_eq!(session.server.generation, before + 1, "one stamp per notification");
|
|
assert_eq!(buffer(&session, &uri), "module m;\n wire a;\n wire c;\nendmodule\n");
|
|
|
|
// An empty batch is not a change: nothing to reparse, nothing to stamp.
|
|
let stamp = session.server.generation;
|
|
change(&mut session, &uri, 3, serde_json::json!([]));
|
|
assert_eq!(session.server.generation, stamp, "an empty batch moves nothing");
|
|
}
|
|
|
|
#[test]
|
|
fn a_full_sync_clients_batch_still_lets_the_last_change_win() {
|
|
// A client that ignores the new capability and keeps sending whole
|
|
// buffers must behave exactly as it did before this feature.
|
|
let tree = Tree::new("incrementalfullsync");
|
|
let mut session = Session::new(vec![tree.path.clone()], false);
|
|
let uri = session.open(&tree.path.join("m.sv"), "module m;\nendmodule\n");
|
|
change(
|
|
&mut session,
|
|
&uri,
|
|
2,
|
|
serde_json::json!([
|
|
{ "text": "module a;\nendmodule\n" },
|
|
{ "text": "module b;\nendmodule\n" },
|
|
{ "text": "module c;\nendmodule\n" },
|
|
]),
|
|
);
|
|
assert_eq!(buffer(&session, &uri), "module c;\nendmodule\n");
|
|
}
|
|
|
|
#[test]
|
|
fn a_change_naming_a_buffer_that_was_never_opened_is_taken_as_written() {
|
|
// Out of protocol, but a full-text change used to be adopted whatever
|
|
// the state was, and still is: there is no base text to be wrong
|
|
// about. A *ranged* change against nothing lands its text at the
|
|
// start, which is the clamping rule, not a special case.
|
|
let tree = Tree::new("incrementalunopened");
|
|
let mut session = Session::new(vec![tree.path.clone()], false);
|
|
let uri = path_to_uri(&tree.path.join("never-opened.sv")).expect("uri");
|
|
change(&mut session, &uri, 1, serde_json::json!([{ "text": "module m;\nendmodule\n" }]));
|
|
assert_eq!(buffer(&session, &uri), "module m;\nendmodule\n");
|
|
}
|
|
|
|
#[test]
|
|
fn a_ranged_change_over_multi_byte_text_edits_the_right_columns() {
|
|
// The whole reason positions are converted rather than counted: the
|
|
// comment above the edited line is four characters and nine bytes.
|
|
let tree = Tree::new("incrementalunicode");
|
|
let mut session = Session::new(vec![tree.path.clone()], false);
|
|
let uri = session.open(&tree.path.join("m.sv"), "// é ✓ 🎉\nmodule m;\n wire a;\nendmodule\n");
|
|
change(
|
|
&mut session,
|
|
&uri,
|
|
2,
|
|
serde_json::json!([{
|
|
"range": { "start": { "line": 2, "character": 7 },
|
|
"end": { "line": 2, "character": 8 } },
|
|
"text": "renamed",
|
|
}]),
|
|
);
|
|
assert_eq!(buffer(&session, &uri), "// é ✓ 🎉\nmodule m;\n wire renamed;\nendmodule\n");
|
|
}
|
|
|
|
#[test]
|
|
fn a_ranged_change_republishes_diagnostics_for_what_it_produced() {
|
|
let tree = Tree::new("incrementalpublish");
|
|
let mut session = Session::new(vec![tree.path.clone()], false);
|
|
let uri = session.open(&tree.path.join("m.sv"), "module m;\n wire a;\nendmodule\n");
|
|
let _ = session.published();
|
|
|
|
// Delete the `a`, leaving `wire ;` — a parse error.
|
|
change(
|
|
&mut session,
|
|
&uri,
|
|
2,
|
|
serde_json::json!([{
|
|
"range": { "start": { "line": 1, "character": 7 },
|
|
"end": { "line": 1, "character": 8 } },
|
|
"text": "",
|
|
}]),
|
|
);
|
|
let published = session.published();
|
|
assert_eq!(published.len(), 1, "one publish per notification: {published:#?}");
|
|
assert!(!published[0].diagnostics.is_empty(), "the broken buffer is reported");
|
|
|
|
// Put it back: the diagnostics clear.
|
|
change(
|
|
&mut session,
|
|
&uri,
|
|
3,
|
|
serde_json::json!([{
|
|
"range": { "start": { "line": 1, "character": 7 },
|
|
"end": { "line": 1, "character": 7 } },
|
|
"text": "a",
|
|
}]),
|
|
);
|
|
let published = session.published();
|
|
assert_eq!(published.len(), 1);
|
|
assert!(published[0].diagnostics.is_empty(), "{published:#?}");
|
|
}
|