LSP §9b: willSaveWaitUntil — protocol-native format-on-save

textDocument/willSaveWaitUntil is advertised and served, gated on a new
lucisv.formatOnSave setting that defaults to off (a server that formats
uninvited fights the editor's own format-on-save and doubles up); the
decline is one comparison, before any buffer lookup. When on, the hook
is textDocument/formatting with a different trigger: the same engine,
the same all-or-nothing verified-output contract, settings resolved by
the new format::settings_without_editor_options — the ladder minus the
tabSize rung the protocol simply does not carry here, with the honest
consequence (save-format can differ from manual format without a
lucisv.toml) documented in the README, the VS Code setting description
and the nvim README. Every save reason formats; refusals are shown per
save, on purpose.

18 tests added across settings/format/server/e2e, including QA's
executable proofs of the tab-size gap and its lucisv.toml resolution, a
no-reindex/no-generation-bump guard for the new setting, and an
idempotency round-trip through a real didChange. QA also fixed the
stale 'not advertised' feature list in editors/nvim/README.md and
sharpened the config-warning dedup claim to what the code does (once
per buffer state) — reworded in the README, pinned by test.

docs/features/will-save-wait-until/README.md

Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
lucioandClaude Fable 5 committed 2026-08-26 12:21:00 -07:00
1 parent c2ea1a11f6
commit 80b9afdfe3
11 files changed
+939 -11

No files matched your search

+67
View File
@@ -721,6 +721,73 @@ fn pushed_settings_reach_the_formatter_and_can_change_mid_session() {
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn the_save_hook_is_advertised_and_formats_only_when_it_is_asked_to() {
// The one thing a unit test cannot check: that the capability really
// reaches the wire, and that a client's `formatOnSave` push really turns
// the hook on for the requests that follow it.
let dir = temp_dir("willsave");
let file = dir.join("m.sv");
std::fs::write(&file, UNINDENTED).expect("write");
let root = format!("file://{}", dir.display());
let uri = format!("file://{}", file.display());
let will_save = |id: u32| {
frame(&format!(
r#"{{"jsonrpc":"2.0","id":{id},"method":"textDocument/willSaveWaitUntil","params":{{"textDocument":{{"uri":"{uri}"}},"reason":1}}}}"#
))
};
let formatted = "module settings_me;\n wire a;\nendmodule\n";
let session = [
frame(&format!(
r#"{{"jsonrpc":"2.0","id":1,"method":"initialize","params":{{"processId":null,"rootUri":"{root}","capabilities":{{}}}}}}"#
)),
frame(r#"{"jsonrpc":"2.0","method":"initialized","params":{}}"#),
frame(&format!(
r#"{{"jsonrpc":"2.0","method":"textDocument/didOpen","params":{{"textDocument":{{"uri":"{uri}","languageId":"systemverilog","version":1,"text":{}}}}}}}"#,
serde_json::to_string(UNINDENTED).expect("json")
)),
// Nothing pushed: the hook declines, so a client that never opted in
// saves exactly what it was going to save.
will_save(2),
frame(r#"{"jsonrpc":"2.0","method":"workspace/didChangeConfiguration","params":{"settings":{"lucisv":{"formatOnSave":true}}}}"#),
will_save(3),
// The client applies the edit and writes; the buffer it now holds is
// what the next save asks about.
frame(&format!(
r#"{{"jsonrpc":"2.0","method":"textDocument/didChange","params":{{"textDocument":{{"uri":"{uri}","version":2}},"contentChanges":[{{"text":{}}}]}}}}"#,
serde_json::to_string(formatted).expect("json")
)),
will_save(4),
frame(r#"{"jsonrpc":"2.0","id":9,"method":"shutdown","params":null}"#),
frame(r#"{"jsonrpc":"2.0","method":"exit","params":null}"#),
]
.concat();
let messages = talk(&session);
let initialize = messages.iter().find(|m| m["id"] == 1).expect("initialize response");
let sync = &initialize["result"]["capabilities"]["textDocumentSync"];
assert_eq!(sync["willSaveWaitUntil"], true, "the hook is advertised: {sync:#?}");
assert!(sync.get("willSave").is_none(), "the notification is not: {sync:#?}");
let declined = messages.iter().find(|m| m["id"] == 2).expect("a response to the first save");
assert!(declined["result"].is_null(), "off by default: {declined:#?}");
assert_eq!(new_text(&messages, 3), formatted, "the push turns it on for the next save");
let again = messages.iter().find(|m| m["id"] == 4).expect("a response to the third save");
assert_eq!(
again["result"].as_array().expect("an array of edits").len(),
0,
"a formatted buffer saves with no edits at all: {again:#?}"
);
assert!(
!messages.iter().any(|m| m["method"] == "window/showMessage"),
"nothing went wrong, so nothing was said: {messages:#?}"
);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn a_pushed_exclude_rebuilds_the_index_mid_session() {
// The one link a unit test cannot check: that the notification handler