Files
lucisv/crates/lucisv_server/src/server/tests/formatting.rs
T
lucioandClaude Fable 5 b96827c51d Decomposition, lane C: lucisv_server's server.rs and completion.rs become modules, a pure move
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]>
2026-09-02 21:33:48 -07:00

629 lines
29 KiB
Rust

//! `onTypeFormatting`, `rangesFormatting` and `willSaveWaitUntil`, through the real dispatch.
use super::*;
// ------------------------------------------------------------------
// `textDocument/onTypeFormatting`
// ------------------------------------------------------------------
/// One `onTypeFormatting` for a buffer: the character typed, and the
/// position the client reports for the cursor after it.
fn on_type(session: &mut Session, uri: &Uri, typed: &str, line: u32, character: u32) -> Value {
on_type_with(session, uri, typed, line, character, serde_json::json!({}))
}
fn on_type_with(
session: &mut Session,
uri: &Uri,
typed: &str,
line: u32,
character: u32,
options: Value,
) -> Value {
let mut all = serde_json::json!({ "tabSize": 2, "insertSpaces": true });
if let (Some(all), Some(extra)) = (all.as_object_mut(), options.as_object()) {
for (key, value) in extra {
all.insert(key.clone(), value.clone());
}
}
session.result(
OnTypeFormatting::METHOD,
serde_json::json!({
"textDocument": { "uri": uri },
"position": { "line": line, "character": character },
"ch": typed,
"options": all,
}),
)
}
#[test]
fn a_typed_newline_re_indents_the_line_it_opened() {
// The dispatch arm, end to end: a real request, decoded by the real
// decoder, answered by the real handler.
let tree = Tree::new("ontypenewline");
let mut session = Session::new(vec![tree.path.clone()], false);
let uri = session
.open(&tree.path.join("m.sv"), "module m;\n always_comb begin\n\n end\nendmodule\n");
let edits = on_type(&mut session, &uri, "\n", 2, 0);
assert_eq!(edits[0]["newText"], " ", "{edits:#?}");
assert_eq!(edits[0]["range"]["start"], serde_json::json!({ "line": 2, "character": 0 }));
assert_eq!(edits[0]["range"]["end"], serde_json::json!({ "line": 2, "character": 0 }));
assert!(session.shown().is_empty(), "a keystroke says nothing to the user");
}
#[test]
fn a_typed_semicolon_lays_out_the_statement_through_the_range_engine() {
let tree = Tree::new("ontypesemi");
let mut session = Session::new(vec![tree.path.clone()], false);
let text = "module m;\n wire a;\n wire b;\nendmodule\n";
let uri = session.open(&tree.path.join("m.sv"), text);
let typed = on_type(&mut session, &uri, ";", 1, 11);
assert_eq!(typed[0]["newText"], "module m;\n wire a;\n wire b;\nendmodule\n");
// The same answer `textDocument/rangeFormatting` gives for that line,
// byte for byte: this path is that request with a different trigger.
let ranged = session.result(
RangeFormatting::METHOD,
serde_json::json!({
"textDocument": { "uri": uri },
"range": { "start": { "line": 1, "character": 0 },
"end": { "line": 1, "character": 0 } },
"options": { "tabSize": 2, "insertSpaces": true },
}),
);
assert_eq!(typed, ranged);
assert!(session.shown().is_empty());
}
// ------------------------------------------------------------------
// `textDocument/rangesFormatting` (LSP 3.18), through the real dispatch
// ------------------------------------------------------------------
/// One LSP range, as a client writes it.
fn json_range(sl: u32, sc: u32, el: u32, ec: u32) -> Value {
serde_json::json!({
"start": { "line": sl, "character": sc },
"end": { "line": el, "character": ec },
})
}
fn ranges_result(session: &mut Session, uri: &Uri, ranges: Vec<Value>) -> Value {
session.result(
format::RANGES_FORMATTING,
serde_json::json!({
"textDocument": { "uri": uri },
"ranges": ranges,
"options": { "tabSize": 2, "insertSpaces": true },
}),
)
}
fn range_result(session: &mut Session, uri: &Uri, range: Value) -> Value {
session.result(
RangeFormatting::METHOD,
serde_json::json!({
"textDocument": { "uri": uri },
"range": range,
"options": { "tabSize": 2, "insertSpaces": true },
}),
)
}
/// Four items on four lines, so two ranges can straddle one they leave.
const FOUR_ITEMS: &str = "module m;\n wire a;\n wire b;\n wire c;\nendmodule\n";
#[test]
fn a_ranges_request_lays_out_every_range_it_names_in_one_answer() {
// The dispatch arm, end to end: a real request with a real `ranges`
// array, decoded by the hand-rolled params and answered by the real
// handler. Lines 2 and 4, leaving line 3 exactly as written.
let tree = Tree::new("rangesfmt");
let mut session = Session::new(vec![tree.path.clone()], false);
let uri = session.open(&tree.path.join("m.sv"), FOUR_ITEMS);
let edits =
ranges_result(&mut session, &uri, vec![json_range(1, 0, 2, 0), json_range(3, 0, 4, 0)]);
assert_eq!(
edits[0]["newText"], "module m;\n wire a;\n wire b;\n wire c;\nendmodule\n",
"{edits:#?}"
);
// Still one whole-document edit, the shape `rangeFormatting` answers.
assert_eq!(edits.as_array().map(Vec::len), Some(1));
assert_eq!(edits[0]["range"]["start"], serde_json::json!({ "line": 0, "character": 0 }));
assert!(session.shown().is_empty(), "a clean config says nothing");
}
#[test]
fn one_range_through_the_new_method_answers_what_the_old_method_answers() {
// The two requests are the same engine, and a client must never have
// to choose between them for correctness — only for round trips.
let tree = Tree::new("rangesone");
let mut session = Session::new(vec![tree.path.clone()], false);
let uri = session.open(&tree.path.join("m.sv"), FOUR_ITEMS);
for range in [
json_range(1, 0, 2, 0),
json_range(1, 4, 1, 4),
json_range(0, 0, 99, 0),
json_range(4, 0, 4, 9),
] {
let one = range_result(&mut session, &uri, range.clone());
let many = ranges_result(&mut session, &uri, vec![range.clone()]);
assert_eq!(one, many, "{range}");
}
}
#[test]
fn an_empty_ranges_array_is_answered_with_no_edit_and_no_word() {
let tree = Tree::new("rangesempty");
let mut session = Session::new(vec![tree.path.clone()], false);
let uri = session.open(&tree.path.join("m.sv"), FOUR_ITEMS);
assert_eq!(ranges_result(&mut session, &uri, vec![]), serde_json::json!([]));
assert!(session.shown().is_empty(), "an empty ask is not a problem");
}
#[test]
fn a_ranges_request_for_a_buffer_the_server_does_not_hold_is_null() {
// What every other formatting handler answers for an unknown document.
let tree = Tree::new("rangesunknown");
let mut session = Session::new(vec![tree.path.clone()], false);
let uri = path_to_uri(&tree.path.join("never-opened.sv")).expect("uri");
assert_eq!(ranges_result(&mut session, &uri, vec![json_range(0, 0, 1, 0)]), Value::Null);
}
#[test]
fn a_refusal_is_the_same_answer_and_the_same_popup_on_both_range_methods() {
// No partial output (CLAUDE.md invariant #1), and the multi-range path
// reaches the *same* `edits()` — so the answer and the message must be
// equal values, not merely similar shapes.
let tree = Tree::new("rangesrefuse");
let mut session = Session::new(vec![tree.path.clone()], false);
let uri = session.open(&tree.path.join("m.sv"), "module m;\n wire ;\nendmodule\n");
let _ = session.drain();
let one = range_result(&mut session, &uri, json_range(0, 0, 1, 0));
let said_once = session.shown();
let many =
ranges_result(&mut session, &uri, vec![json_range(0, 0, 1, 0), json_range(2, 0, 3, 0)]);
let said_again = session.shown();
assert_eq!(one, serde_json::json!([]), "no edit at all");
assert_eq!(one, many, "byte-identical answers");
assert_eq!(said_once.len(), 1, "{said_once:?}");
assert_eq!(said_once, said_again, "and byte-identical ERRORs: {said_again:?}");
}
#[test]
fn a_broken_config_warns_once_from_the_multi_range_path_too() {
// It shares `warn_about_config` with the other explicit formatting
// requests, dedup included — this is an explicit request the user made,
// not a keystroke, so it does speak.
let tree = Tree::new("rangesconfig");
std::fs::write(tree.path.join(".git"), "gitdir: nowhere\n").expect("write .git");
std::fs::write(tree.path.join("lucisv.toml"), "bogus = 1\n").expect("write config");
let mut session = Session::new(vec![tree.path.clone()], false);
let uri = session.open(&tree.path.join("m.sv"), FOUR_ITEMS);
let _ = session.drain();
let edits = ranges_result(&mut session, &uri, vec![json_range(1, 0, 2, 0)]);
assert_eq!(
edits[0]["newText"],
"module m;\n wire a;\n wire b;\n wire c;\nendmodule\n"
);
let warned = session.shown();
assert_eq!(warned.len(), 1, "{warned:?}");
assert_eq!(warned[0].0, MessageType::WARNING);
assert!(warned[0].1.contains("unknown key `bogus`"), "{warned:?}");
// And once per buffer state, exactly as the other paths.
let _ = ranges_result(&mut session, &uri, vec![json_range(1, 0, 2, 0)]);
assert!(session.shown().is_empty(), "said once, not once per request");
}
#[test]
fn params_that_do_not_carry_ranges_are_invalid_params_and_not_a_panic() {
// The params are hand-rolled, so their refusal is ours to get right:
// a missing `ranges` is a client bug and answers an error, never a
// guess about what the client meant.
let tree = Tree::new("rangesjunk");
let mut session = Session::new(vec![tree.path.clone()], false);
let uri = session.open(&tree.path.join("m.sv"), FOUR_ITEMS);
let response = session.request(
format::RANGES_FORMATTING,
serde_json::json!({
"textDocument": { "uri": uri },
"options": { "tabSize": 2, "insertSpaces": true },
}),
);
let error = response.response_result.expect_err("missing `ranges` is not answerable");
assert_eq!(error.code, lsp_server::ErrorCode::InvalidParams as i32, "{error:?}");
}
#[test]
fn a_keystroke_the_engine_refuses_is_answered_with_silence_and_not_a_popup() {
// The deliberate contrast with `textDocument/formatting` and the save
// hook, both of which show an ERROR on a refusal. Mid-edit code the
// engine declines is the *normal* case on a keystroke, and a modal the
// user cannot type past would arrive on every semicolon.
let tree = Tree::new("ontypequiet");
let mut session = Session::new(vec![tree.path.clone()], false);
let uri = session.open(&tree.path.join("m.sv"), "module m;\n wire a;\n wire ;\n");
assert_eq!(on_type(&mut session, &uri, ";", 1, 11), serde_json::json!([]));
assert!(session.shown().is_empty(), "not one word: {:?}", session.shown());
// ...and the whole-document request on the same buffer still does say
// something, so the silence is this path's decision and not a
// regression in `edits()`.
let _ = formatting_result(&mut session, &uri);
assert_eq!(session.shown().len(), 1, "the explicit request still reports");
}
#[test]
fn a_broken_config_is_never_reported_from_a_keystroke() {
// `warn_about_config`'s dedup is cleared by `set_document`, and an
// on-type request follows the `didChange` the user's own keystroke
// produced — so warning here would mean one popup per character. The
// user is told on their next explicit format instead.
let tree = Tree::new("ontypeconfig");
std::fs::write(tree.path.join(".git"), "gitdir: nowhere\n").expect("write .git");
std::fs::write(tree.path.join("lucisv.toml"), "bogus = 1\n").expect("write config");
let mut session = Session::new(vec![tree.path.clone()], false);
let uri = session.open(&tree.path.join("m.sv"), "module m;\n\n wire a;\nendmodule\n");
let _ = session.drain();
for _ in 0..3 {
let edits = on_type(&mut session, &uri, "\n", 1, 0);
assert_eq!(edits[0]["newText"], " ", "it still formats with the defaults");
assert!(session.shown().is_empty(), "and still says nothing");
}
// The explicit request is where the user hears about it.
let _ = formatting_result(&mut session, &uri);
let shown = session.shown();
assert_eq!(shown.len(), 1, "{shown:?}");
assert!(shown[0].1.contains("unknown key `bogus`"), "{shown:?}");
}
#[test]
fn a_keystroke_climbs_the_same_settings_ladder_a_manual_format_does() {
// Unlike `willSaveWaitUntil`, this request carries `FormattingOptions`,
// so the editor's own tab size is a rung — outranked by a pushed
// `lucisv.indent`, exactly as `format::settings_for` documents.
let tree = Tree::new("ontypesettings");
let mut session = Session::new(vec![tree.path.clone()], false);
let uri = session.open(&tree.path.join("m.sv"), "module m;\n\n wire a;\nendmodule\n");
let tabbed = serde_json::json!({ "tabSize": 4 });
let edits = on_type_with(&mut session, &uri, "\n", 1, 0, tabbed.clone());
assert_eq!(edits[0]["newText"], " ", "rung 3, the editor's tab size");
session.notify(
DidChangeConfiguration::METHOD,
serde_json::json!({ "settings": { "lucisv": { "indent": 8 } } }),
);
let edits = on_type_with(&mut session, &uri, "\n", 1, 0, tabbed);
assert_eq!(edits[0]["newText"], " ", "rung 2 outranks it");
// Hard tabs are not a width this formatter can honour, so rung 3 drops
// out entirely and the built-in default answers.
session.notify(
DidChangeConfiguration::METHOD,
serde_json::json!({ "settings": { "lucisv": {} } }),
);
let hard = serde_json::json!({ "tabSize": 4, "insertSpaces": false });
let edits = on_type_with(&mut session, &uri, "\n", 1, 0, hard);
assert_eq!(edits[0]["newText"], " ", "never a tab, and never a guess");
}
#[test]
fn a_keystroke_in_a_buffer_the_server_does_not_hold_answers_an_empty_array() {
// `[]` rather than `null`: the protocol's answer for "no edits", and
// what every other request on this path already returns.
let tree = Tree::new("ontypeunopened");
let mut session = Session::new(vec![tree.path.clone()], false);
let uri = path_to_uri(&tree.path.join("never-opened.sv")).expect("uri");
assert_eq!(on_type(&mut session, &uri, "\n", 1, 0), serde_json::json!([]));
assert_eq!(on_type(&mut session, &uri, ";", 1, 0), serde_json::json!([]));
assert!(session.shown().is_empty());
}
#[test]
fn a_trigger_character_this_server_never_advertised_answers_nothing() {
let tree = Tree::new("ontypeunknown");
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");
for typed in ["}", ")", "e"] {
assert_eq!(on_type(&mut session, &uri, typed, 1, 11), serde_json::json!([]), "{typed}");
}
assert!(session.shown().is_empty());
}
// ------------------------------------------------------------------
// `textDocument/willSaveWaitUntil`
// ------------------------------------------------------------------
/// The `lucisv.formatOnSave` push a client makes, on or off.
fn format_on_save(on: bool) -> Value {
serde_json::json!({ "settings": { "lucisv": { "formatOnSave": on } } })
}
/// One `willSaveWaitUntil` for a buffer, for the reason given (1 = manual,
/// 2 = after delay, 3 = focus out).
fn will_save(session: &mut Session, uri: &Uri, reason: u8) -> Value {
session.result(
WillSaveWaitUntil::METHOD,
serde_json::json!({ "textDocument": { "uri": uri }, "reason": reason }),
)
}
#[test]
fn the_save_hook_declines_until_format_on_save_is_pushed() {
let tree = Tree::new("willsaveoff");
let mut session = Session::new(vec![tree.path.clone()], false);
let uri = session.open(&tree.path.join("m.sv"), "module m;\nwire a;\nendmodule\n");
// The default: a client that never pushed the setting is answered
// `null`, and told nothing at all.
assert_eq!(will_save(&mut session, &uri, 1), Value::Null, "off by default");
assert!(session.shown().is_empty(), "a decline is silent");
// Pushed on, mid-session, with no restart.
session.notify(DidChangeConfiguration::METHOD, format_on_save(true));
let edits = will_save(&mut session, &uri, 1);
assert_eq!(edits[0]["newText"], "module m;\n wire a;\nendmodule\n", "{edits:#?}");
// And off again: the hook goes back to declining.
session.notify(DidChangeConfiguration::METHOD, format_on_save(false));
assert_eq!(will_save(&mut session, &uri, 1), Value::Null, "turned back off");
}
#[test]
fn toggling_format_on_save_neither_rescans_nor_bumps_the_generation() {
// QA (2026-08-26): `lucisv.formatOnSave` is read afresh by every save,
// like `indent`/`lineLength` already are by every format — it must not
// cost a re-walk of the workspace, and (since a rescan is what bumps
// it) must not move the generation counter a pull-diagnostics client's
// `resultId` is stamped with either. `only_an_exclude_change_asks_for_a_rescan`
// (state.rs) pins the same thing one layer down; this pins it through
// the real notification handler.
let tree = Tree::new("willsavenoreindex");
tree.package("kept.sv", "kept_pkg");
let mut session = Session::new(vec![tree.path.clone()], false);
let before = session.server.generation;
session.notify(DidChangeConfiguration::METHOD, format_on_save(true));
assert_eq!(session.server.generation, before, "turning it on moved nothing");
session.notify(DidChangeConfiguration::METHOD, format_on_save(false));
assert_eq!(session.server.generation, before, "turning it off moved nothing either");
}
#[test]
fn the_save_hook_answers_exactly_what_the_formatting_request_answers() {
// The whole contract: this is `textDocument/formatting` with a
// different trigger, so the bytes must be identical — not merely
// equivalent.
let tree = Tree::new("willsavesame");
let mut session = Session::new(vec![tree.path.clone()], false);
let uri = session.open(&tree.path.join("m.sv"), "module m;\nwire a;\n wire b;\nendmodule\n");
session.notify(DidChangeConfiguration::METHOD, format_on_save(true));
assert_eq!(will_save(&mut session, &uri, 1), formatting_result(&mut session, &uri));
}
#[test]
fn every_save_reason_is_a_save() {
// Manual, after-delay and focus-out all format: the user asked for
// their saves to be formatted, and all three write the file.
let tree = Tree::new("willsavereason");
let mut session = Session::new(vec![tree.path.clone()], false);
let uri = session.open(&tree.path.join("m.sv"), "module m;\nwire a;\nendmodule\n");
session.notify(DidChangeConfiguration::METHOD, format_on_save(true));
for reason in [1, 2, 3] {
let edits = will_save(&mut session, &uri, reason);
assert_eq!(
edits[0]["newText"], "module m;\n wire a;\nendmodule\n",
"reason {reason}: {edits:#?}"
);
}
}
#[test]
fn the_save_hook_answers_null_for_a_buffer_it_does_not_hold() {
// The request carries no text — the client has not written yet — so a
// buffer the server never saw is one it cannot format.
let tree = Tree::new("willsaveclosed");
let mut session = Session::new(vec![tree.path.clone()], false);
session.notify(DidChangeConfiguration::METHOD, format_on_save(true));
let uri = path_to_uri(&tree.path.join("never-opened.sv")).expect("uri");
assert_eq!(will_save(&mut session, &uri, 1), Value::Null);
assert!(session.shown().is_empty(), "and nothing is said about it");
}
#[test]
fn an_already_formatted_buffer_is_saved_with_no_edits() {
// The same `[]` the formatting request gives for `Ok(None)`: an edit
// that changes nothing still moves the cursor in some editors.
let tree = Tree::new("willsaveclean");
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");
session.notify(DidChangeConfiguration::METHOD, format_on_save(true));
let edits = will_save(&mut session, &uri, 1);
assert_eq!(edits, serde_json::json!([]), "{edits:#?}");
assert_eq!(edits, formatting_result(&mut session, &uri), "the same as the request's");
}
#[test]
fn a_buffer_the_formatter_refuses_is_saved_unedited_and_the_reason_is_shown() {
// CLAUDE.md invariant #1 on the save path: no partial output, ever.
// The message repeats per save on purpose — a save the user asked to
// be formatted, that was not, is worth saying every time it happens.
let tree = Tree::new("willsavebroken");
let mut session = Session::new(vec![tree.path.clone()], false);
let uri = session.open(&tree.path.join("m.sv"), "module m;\n wire ;\nendmodule\n");
session.notify(DidChangeConfiguration::METHOD, format_on_save(true));
let _ = session.drain();
for save in 1..=2 {
let edits = will_save(&mut session, &uri, 1);
assert_eq!(edits, serde_json::json!([]), "save {save}: no edit at all");
let shown = session.shown();
assert_eq!(shown.len(), 1, "save {save}: {shown:#?}");
assert_eq!(shown[0].0, MessageType::ERROR);
assert!(shown[0].1.starts_with("lucisv: cannot format — "), "{}", shown[0].1);
}
}
#[test]
fn applying_the_save_hooks_edit_leaves_the_next_save_nothing_to_do() {
// The formatter's own idempotency, asserted through this path: the
// buffer the client writes is a fixed point, so a save right after a
// save cannot keep rewriting the file.
let tree = Tree::new("willsaveidempotent");
let mut session = Session::new(vec![tree.path.clone()], false);
let uri = session
.open(&tree.path.join("m.sv"), "module m;\nwire a;\n assign a = 1;\nendmodule\n");
session.notify(DidChangeConfiguration::METHOD, format_on_save(true));
let edits = will_save(&mut session, &uri, 1);
let formatted = edits[0]["newText"].as_str().expect("newText").to_string();
// The client applies the edit and writes; the buffer it holds is what
// the next save asks about.
change(&mut session, &uri, 2, serde_json::json!([{ "text": formatted.clone() }]));
assert_eq!(buffer(&session, &uri), formatted);
assert_eq!(will_save(&mut session, &uri, 1), serde_json::json!([]), "a fixed point");
}
/// What `textDocument/formatting` answers for the given `tabSize`.
fn formatting_with_tab_size(session: &mut Session, uri: &Uri, tab_size: u32) -> Value {
session.result(
Formatting::METHOD,
serde_json::json!({
"textDocument": { "uri": uri },
"options": { "tabSize": tab_size, "insertSpaces": true },
}),
)
}
#[test]
fn the_save_hook_and_a_manual_format_can_legitimately_disagree_on_indent() {
// QA (2026-08-26): the documented gap in Assumptions/Handshakes,
// constructed for real. No `lucisv.toml`, no `lucisv.indent` pushed,
// and an editor whose tab size is 4: a manual format honors rung 3
// (the editor's `tabSize`) and indents 4; the save hook has no rung 3
// to consult at all and falls straight to the built-in default, 2.
let tree = Tree::new("willsavetabgap");
let mut session = Session::new(vec![tree.path.clone()], false);
let uri = session.open(&tree.path.join("m.sv"), "module m;\nwire a;\nendmodule\n");
session.notify(DidChangeConfiguration::METHOD, format_on_save(true));
let manual = formatting_with_tab_size(&mut session, &uri, 4);
assert_eq!(manual[0]["newText"], "module m;\n wire a;\nendmodule\n", "{manual:#?}");
let saved = will_save(&mut session, &uri, 1);
assert_eq!(saved[0]["newText"], "module m;\n wire a;\nendmodule\n", "{saved:#?}");
assert_ne!(manual, saved, "the documented gap is real, not just written down");
}
#[test]
fn a_lucisv_toml_erases_the_tab_size_gap() {
// QA (2026-08-26): with a checked-in config, rung 1 outranks both the
// editor's `tabSize` and the built-in default, so the two paths agree
// exactly again — no `lucisv.toml`, and this would be the previous
// test.
let tree = Tree::new("willsavetabgapclosed");
tree.write("lucisv.toml", "indent = 4\n");
let mut session = Session::new(vec![tree.path.clone()], false);
let uri = session.open(&tree.path.join("m.sv"), "module m;\nwire a;\nendmodule\n");
session.notify(DidChangeConfiguration::METHOD, format_on_save(true));
// A wildly different editor tab size changes nothing: the config wins.
let manual = formatting_with_tab_size(&mut session, &uri, 8);
let saved = will_save(&mut session, &uri, 1);
assert_eq!(manual[0]["newText"], "module m;\n wire a;\nendmodule\n", "{manual:#?}");
assert_eq!(manual, saved, "the config makes the two paths agree exactly");
}
#[test]
fn a_broken_config_still_saves_with_defaults_and_warns_once() {
// QA (2026-08-26): the interaction attack-surface 4 calls out —
// `formatOnSave` on, the buffer itself fine, but `lucisv.toml` broken.
// The save must not be sacrificed to a typo in a file the user is not
// even editing: it formats with the built-in defaults, and the
// warning follows the same per-file-change dedup a broken config
// already gets everywhere else, so a save loop cannot turn it into
// spam the way the formatter-refusal ERROR deliberately can.
//
// The buffer is already at the built-in default (2-space indent), so
// neither save produces an edit and — crucially — neither save is
// followed by the client-applies-the-edit `didChange` that would
// itself re-arm the warning (`set_document` clears
// `config_warnings`). That is what isolates the dedup this test is
// actually about from the unrelated re-arming a *changed* buffer gets.
let tree = Tree::new("willsavebrokenconfig");
tree.write("lucisv.toml", "indent = 4\nbogus = 1\n");
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");
session.notify(DidChangeConfiguration::METHOD, format_on_save(true));
let _ = session.drain();
let first = will_save(&mut session, &uri, 1);
assert_eq!(first, serde_json::json!([]), "the buffer needed no edit: {first:#?}");
let shown = session.shown();
assert_eq!(shown.len(), 1, "{shown:#?}");
assert_eq!(shown[0].0, MessageType::WARNING);
assert!(shown[0].1.contains("formatting with default settings"), "{}", shown[0].1);
// A second save of the same still-broken config, with nothing having
// changed the buffer in between, says nothing new.
let second = will_save(&mut session, &uri, 1);
assert_eq!(second, serde_json::json!([]), "{second:#?}");
assert!(session.shown().is_empty(), "the warning does not repeat per save");
}
#[test]
fn a_reformatting_save_re_arms_the_broken_config_warning_on_the_very_next_save() {
// QA (2026-08-26) finding, not a fix: `warn_about_config`'s dedup is
// keyed on `set_document`, and `set_document` is exactly what runs
// when the client applies a save hook's edit and the resulting
// `didChange` reaches the server (see
// `applying_the_save_hooks_edit_leaves_the_next_save_nothing_to_do`,
// which does this on purpose to model the real sequence). So "warns
// once" (Assumptions, work log) only holds once the buffer stops
// needing edits — a save that *itself* reformats the buffer re-arms
// the same warning for the very next save, because that save is
// preceded by a `didChange` the dedup treats as a fresh state. Two
// back-to-back saves of a buffer that needs reformatting under a
// broken config warn twice, not once; the third (once the buffer is a
// fixed point and no further `didChange` lands) is finally silent.
// This is consistent with the doc comment on `warn_about_config`
// ("once per change of a file") — it is the *save hook's own* prose
// ("warns once") that does not spell out this edge, and a future
// reader relying on literally one warning per broken config should
// know better.
let tree = Tree::new("willsavebrokenconfigrearm");
tree.write("lucisv.toml", "indent = 4\nbogus = 1\n");
let mut session = Session::new(vec![tree.path.clone()], false);
let uri = session.open(&tree.path.join("m.sv"), "module m;\nwire a;\nendmodule\n");
session.notify(DidChangeConfiguration::METHOD, format_on_save(true));
let _ = session.drain();
let first = will_save(&mut session, &uri, 1);
let formatted = first[0]["newText"].as_str().expect("newText").to_string();
assert_eq!(formatted, "module m;\n wire a;\nendmodule\n", "{first:#?}");
assert_eq!(session.shown().len(), 1, "the first save warns");
// The client applies the edit and writes, exactly as
// `applying_the_save_hooks_edit_leaves_the_next_save_nothing_to_do`
// does — this is what re-arms the dedup.
change(&mut session, &uri, 2, serde_json::json!([{ "text": formatted.clone() }]));
let second = will_save(&mut session, &uri, 1);
assert_eq!(second, serde_json::json!([]), "already formatted: {second:#?}");
assert_eq!(session.shown().len(), 1, "the second save warns again, not zero times");
// No further `didChange` follows a no-op save, so the third finally
// stays quiet — the dedup this test is not disputing.
let third = will_save(&mut session, &uri, 1);
assert_eq!(third, serde_json::json!([]));
assert!(session.shown().is_empty(), "only now does the warning stop repeating");
}