release / build-and-publish (push) Successful in 5m22s
TODO §71.5. One setting picks a house layout — [format] style in lucisv.toml, --style, the lucisv.format.style drop-down, the server and MCP: lucisv (default), google, lowrisc, ansi, microsoft, nxp, and auto. Seven layout rules (indent, width, begin placement, else placement, else after a labelled end, one-per-line lists, list indent, alignment), one row per style in one table in crates/vlog_formatter/src/style.rs. google and lowrisc follow those published guides; ansi, microsoft and nxp are lucisv's guesses and say so. auto formats with each preset and keeps the result that changes the fewest lines (crates/vlog_formatter/src/auto.rs); the language server remembers each document's preset (lucisv_server/src/auto_style.rs) so selection and on-type formatting do not repeat the search. The style is part of the format cache key. Proof: default output byte-identical to 0.65.4 on 4 921 files (corpus + OpenTitan hw) against an independently rebuilt 0.65.4; every style formatted three times over the same files with no verification error, crash, token or comment difference (independent lexer) or second-run change. verify.rs is untouched. Dev (three rounds), QA (two rounds, pass with findings) and lead review are in docs/features/format-styles/README.md. Three one-line calls were added to lucisv_server/src/server/notification.rs for the memory's invalidation. TODO.md files the pre-existing bugs QA met on the way. Version bump (Cargo.toml, Cargo.lock, editors/vscode/package.json and both package-lock.json spots); notes in CHANGELOG.md (## 0.65.5) and editors/vscode/CHANGELOG.md. Gates: tools/gates.sh PASS on this tree; vlog_formatter 376/0, lucisv_server 1996/0, corpus 814/6. Co-Authored-By: Claude Mythos 5.2 <[email protected]>
837 lines
38 KiB
Rust
837 lines
38 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 a_selection_inside_a_nested_block_lays_out_only_that_block() {
|
|
// The owner's report (TODO §66.2): a `case` selected inside an `always`
|
|
// in a four-space file. Only the `case` moves, and it stays where it
|
|
// starts; the statement after it and the parent keep their bytes.
|
|
let tree = Tree::new("rangesnested");
|
|
let mut session = Session::new(vec![tree.path.clone()], false);
|
|
let text = "module m;\n always @* begin\n case (s)\n 0: y=a;\n endcase\n z = 1;\n end\nendmodule\n";
|
|
let uri = session.open(&tree.path.join("m.sv"), text);
|
|
|
|
let one = range_result(&mut session, &uri, json_range(2, 0, 5, 0));
|
|
assert_eq!(
|
|
one[0]["newText"],
|
|
"module m;\n always @* begin\n case (s)\n 0: y = a;\n endcase\n z = 1;\n end\nendmodule\n",
|
|
"{one:#?}"
|
|
);
|
|
// The multi-range method gives the same answer for the same selection.
|
|
assert_eq!(ranges_result(&mut session, &uri, vec![json_range(2, 0, 5, 0)]), one);
|
|
assert!(session.shown().is_empty());
|
|
}
|
|
|
|
#[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);
|
|
// The error sits on the keystroke's own line: since selection formatting
|
|
// (TODO §66.2) an error on *another* line no longer refuses the request.
|
|
let uri = session.open(&tree.path.join("m.sv"), "module m;\n wire a = ;\n");
|
|
|
|
assert_eq!(on_type(&mut session, &uri, ";", 1, 13), 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");
|
|
}
|
|
|
|
// ------------------------------------------------------------------
|
|
// `lucisv.format.style = "auto"`: the preset the server remembers
|
|
// ------------------------------------------------------------------
|
|
|
|
/// A google-formatted buffer: one connection per line at a 4-space indent.
|
|
const GOOGLE_BUFFER: &str = "\
|
|
module m (
|
|
input logic a,
|
|
output logic b
|
|
);
|
|
sub u_sub (
|
|
.a(a),
|
|
.b(b)
|
|
);
|
|
endmodule
|
|
";
|
|
|
|
fn push_auto(session: &mut Session) {
|
|
session.notify(
|
|
DidChangeConfiguration::METHOD,
|
|
serde_json::json!({ "settings": { "lucisv": { "format": { "style": "auto" } } } }),
|
|
);
|
|
}
|
|
|
|
fn format_whole(session: &mut Session, uri: &Uri) -> Value {
|
|
session.result(
|
|
Formatting::METHOD,
|
|
serde_json::json!({
|
|
"textDocument": { "uri": uri },
|
|
"options": { "tabSize": 2, "insertSpaces": true },
|
|
}),
|
|
)
|
|
}
|
|
|
|
/// After a whole-document format under `auto`, a range in the same buffer
|
|
/// keeps the buffer's preset even once a half-typed line elsewhere stops it
|
|
/// from parsing — which a fresh search could not do.
|
|
#[test]
|
|
fn auto_ranges_keep_the_remembered_preset_while_the_buffer_does_not_parse() {
|
|
let tree = Tree::new("autorange");
|
|
let mut session = Session::new(vec![tree.path.clone()], false);
|
|
push_auto(&mut session);
|
|
let uri = session.open(&tree.path.join("m.sv"), GOOGLE_BUFFER);
|
|
assert_eq!(format_whole(&mut session, &uri), serde_json::json!([]), "already google");
|
|
|
|
let edited = GOOGLE_BUFFER
|
|
.replace("endmodule", " sub u_two (.a(a), .b(b));\n assign c = ;\nendmodule");
|
|
change(&mut session, &uri, 2, serde_json::json!([{ "text": edited }]));
|
|
let ranged = range_result(&mut session, &uri, json_range(8, 0, 8, 0));
|
|
let text = ranged[0]["newText"].as_str().expect("an edit");
|
|
assert!(text.contains(" sub u_two (\n .a(a),\n .b(b)\n );\n"), "{text}");
|
|
// And the `;` path agrees with it, byte for byte.
|
|
let typed = on_type(&mut session, &uri, ";", 8, 27);
|
|
assert_eq!(typed, ranged);
|
|
}
|
|
|
|
/// With no memory and a buffer that does not parse there is nothing to
|
|
/// resolve from, so the default preset's layout answers — the documented
|
|
/// fallback — and nothing is remembered.
|
|
#[test]
|
|
fn auto_without_a_memory_and_without_a_parse_uses_the_default_layout() {
|
|
let tree = Tree::new("autofallback");
|
|
let mut session = Session::new(vec![tree.path.clone()], false);
|
|
push_auto(&mut session);
|
|
let text = GOOGLE_BUFFER
|
|
.replace("endmodule", " sub u_two (.a(a), .b(b));\n assign c = ;\nendmodule");
|
|
let uri = session.open(&tree.path.join("m.sv"), &text);
|
|
let ranged = range_result(&mut session, &uri, json_range(8, 0, 8, 0));
|
|
assert_eq!(ranged, serde_json::json!([]), "the default layout already holds that line");
|
|
assert!(session.server.auto_styles.is_empty(), "a fallback is not remembered");
|
|
}
|
|
|
|
/// With no memory and a buffer that parses, the first range request
|
|
/// resolves the preset once and remembers it for the next.
|
|
#[test]
|
|
fn auto_without_a_memory_resolves_from_a_buffer_that_parses() {
|
|
let tree = Tree::new("autoresolve");
|
|
let mut session = Session::new(vec![tree.path.clone()], false);
|
|
push_auto(&mut session);
|
|
let text = GOOGLE_BUFFER.replace("endmodule", " sub u_two (.a(a), .b(b));\nendmodule");
|
|
let uri = session.open(&tree.path.join("m.sv"), &text);
|
|
let ranged = range_result(&mut session, &uri, json_range(8, 0, 8, 0));
|
|
let out = ranged[0]["newText"].as_str().expect("an edit");
|
|
assert!(out.contains(" sub u_two (\n .a(a),\n .b(b)\n );\n"), "{out}");
|
|
assert!(!session.server.auto_styles.is_empty());
|
|
}
|
|
|
|
/// The google buffer re-laid out lucisv-style, with one unformatted
|
|
/// instance on line 3 that the two presets lay out differently.
|
|
const LUCISV_BUFFER: &str = "\
|
|
module m (input logic a, output logic b);
|
|
sub u_sub (.a(a), .b(b));
|
|
endmodule
|
|
";
|
|
|
|
fn lucisv_with_instance() -> String {
|
|
LUCISV_BUFFER.replace("endmodule", "sub u_two (.a(a), .b(b));\nendmodule")
|
|
}
|
|
|
|
/// The range answer for line 2 (0-based) of the lucisv buffer. A selection
|
|
/// keeps the indentation its line was written at, so lucisv's layout of the
|
|
/// one-line instance is the line itself — no edit — where google's would
|
|
/// break it into four (`auto_without_a_memory_resolves_from_a_buffer_that_parses`).
|
|
fn assert_lucisv_layout(session: &mut Session, uri: &Uri) {
|
|
let ranged = range_result(session, uri, json_range(2, 0, 2, 0));
|
|
assert_eq!(ranged, serde_json::json!([]), "laid out lucisv-style, not google-style");
|
|
}
|
|
|
|
/// R2-1 (a): the buffer replaced wholesale by text in another preset — a
|
|
/// reload from disk — is resolved again, not served the old preset.
|
|
#[test]
|
|
fn auto_a_replaced_buffer_is_resolved_again() {
|
|
let tree = Tree::new("autoreplaced");
|
|
let mut session = Session::new(vec![tree.path.clone()], false);
|
|
push_auto(&mut session);
|
|
let uri = session.open(&tree.path.join("m.sv"), GOOGLE_BUFFER);
|
|
format_whole(&mut session, &uri);
|
|
assert!(!session.server.auto_styles.is_empty(), "google remembered");
|
|
change(&mut session, &uri, 2, serde_json::json!([{ "text": lucisv_with_instance() }]));
|
|
assert_lucisv_layout(&mut session, &uri);
|
|
}
|
|
|
|
/// R2-1 (b): auto → a whole-document format under a named style → auto.
|
|
#[test]
|
|
fn auto_after_a_named_style_format_does_not_reuse_the_old_preset() {
|
|
let tree = Tree::new("autonamed");
|
|
let mut session = Session::new(vec![tree.path.clone()], false);
|
|
push_auto(&mut session);
|
|
let uri = session.open(&tree.path.join("m.sv"), GOOGLE_BUFFER);
|
|
format_whole(&mut session, &uri);
|
|
session.notify(
|
|
DidChangeConfiguration::METHOD,
|
|
serde_json::json!({ "settings": { "lucisv": { "format": { "style": "lucisv" } } } }),
|
|
);
|
|
assert!(session.server.auto_styles.is_empty(), "a settings change forgets everything");
|
|
format_whole(&mut session, &uri);
|
|
push_auto(&mut session);
|
|
// An incremental edit, so only the settings change and the named format
|
|
// can be what made the memory forget.
|
|
let text = lucisv_with_instance();
|
|
change(
|
|
&mut session,
|
|
&uri,
|
|
2,
|
|
serde_json::json!([{ "range": { "start": { "line": 0, "character": 0 },
|
|
"end": { "line": 9, "character": 0 } },
|
|
"text": text }]),
|
|
);
|
|
assert_lucisv_layout(&mut session, &uri);
|
|
}
|
|
|
|
/// R2-1 (c): close, then reopen the same URI with other content.
|
|
#[test]
|
|
fn auto_a_reopened_document_starts_with_no_memory() {
|
|
let tree = Tree::new("autoreopen");
|
|
let mut session = Session::new(vec![tree.path.clone()], false);
|
|
push_auto(&mut session);
|
|
let path = tree.path.join("m.sv");
|
|
let uri = session.open(&path, GOOGLE_BUFFER);
|
|
format_whole(&mut session, &uri);
|
|
session.notify(
|
|
DidCloseTextDocument::METHOD,
|
|
serde_json::json!({ "textDocument": { "uri": uri } }),
|
|
);
|
|
assert!(session.server.auto_styles.is_empty(), "closing forgets");
|
|
let uri = session.open(&path, &lucisv_with_instance());
|
|
assert_lucisv_layout(&mut session, &uri);
|
|
}
|
|
|
|
/// R2-1 (d): a renamed file's old name is forgotten with it.
|
|
#[test]
|
|
fn auto_a_rename_forgets_both_names() {
|
|
let tree = Tree::new("autorename");
|
|
let mut session = Session::new(vec![tree.path.clone()], false);
|
|
push_auto(&mut session);
|
|
let uri = session.open(&tree.path.join("m.sv"), GOOGLE_BUFFER);
|
|
format_whole(&mut session, &uri);
|
|
let new = crate::uri::path_to_uri(&tree.path.join("n.sv")).expect("uri");
|
|
session.notify(
|
|
DidRenameFiles::METHOD,
|
|
serde_json::json!({ "files": [{ "oldUri": uri.as_str(), "newUri": new.as_str() }] }),
|
|
);
|
|
assert!(session.server.auto_styles.is_empty());
|
|
}
|