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]>
171 lines
8.2 KiB
Rust
171 lines
8.2 KiB
Rust
//! The five formatting entries — whole document, one range, several ranges,
|
|
//! on-type, and the save hook — and what each says when the engine refuses.
|
|
//!
|
|
//! All five climb the same settings ladder and run the same engine
|
|
//! (`crate::format`); what differs is the trigger, and the trigger decides
|
|
//! the manners: a command the user pressed refuses out loud, a keystroke and
|
|
//! a save decline in silence. The once-per-change `lucisv.toml` warning and
|
|
//! the edit-or-nothing contract are the two pieces of server state this
|
|
//! needs, which is why the entries live here and not in `crate::format`.
|
|
|
|
use lsp_types::{
|
|
DocumentFormattingParams, DocumentOnTypeFormattingParams, DocumentRangeFormattingParams,
|
|
MessageType, Uri, WillSaveTextDocumentParams,
|
|
};
|
|
use serde_json::Value;
|
|
|
|
use super::dispatch::{to_value, Failure};
|
|
use super::Server;
|
|
use crate::{format, on_type};
|
|
|
|
impl Server {
|
|
pub(super) fn formatting(
|
|
&mut self,
|
|
params: DocumentFormattingParams,
|
|
) -> Result<Value, Failure> {
|
|
let uri = params.text_document.uri;
|
|
let Some(doc) = self.workspace.document(&uri) else { return Ok(Value::Null) };
|
|
let settings = format::settings_for(doc, self.workspace.client_settings(), ¶ms.options);
|
|
let result = format::formatting(doc, &settings.config);
|
|
// A broken `lucisv.toml` is a warning, not a failure: the CLI exits 2 on
|
|
// one, but an editor that refuses to format because of a typo in a
|
|
// config file is an editor the user turns off. The buffer is formatted
|
|
// with the built-in defaults, and the reason is said once per change of
|
|
// the file — a format-on-save loop would otherwise repeat it forever.
|
|
self.warn_about_config(&uri, settings.problem);
|
|
self.edits(result)
|
|
}
|
|
|
|
/// `textDocument/rangeFormatting`, on the same engine and the same
|
|
/// all-or-nothing contract as whole-document formatting.
|
|
pub(super) fn range_formatting(
|
|
&mut self,
|
|
params: DocumentRangeFormattingParams,
|
|
) -> Result<Value, Failure> {
|
|
let uri = params.text_document.uri;
|
|
let Some(doc) = self.workspace.document(&uri) else { return Ok(Value::Null) };
|
|
let settings = format::settings_for(doc, self.workspace.client_settings(), ¶ms.options);
|
|
let result = format::range_formatting(doc, &settings.config, params.range);
|
|
self.warn_about_config(&uri, settings.problem);
|
|
self.edits(result)
|
|
}
|
|
|
|
/// `textDocument/rangesFormatting` — LSP 3.18's multi-range form.
|
|
///
|
|
/// Line for line the handler above, with `ranges` where it has `range`:
|
|
/// the same settings ladder, the same engine, the same `edits` and the
|
|
/// same config warning. That is the point of the feature — a client asking
|
|
/// for five ranges gets one verified answer instead of five requests whose
|
|
/// edits it would have to reconcile itself. See
|
|
/// `docs/features/lsp-318-extras/README.md`.
|
|
pub(super) fn ranges_formatting(
|
|
&mut self,
|
|
params: format::RangesFormattingParams,
|
|
) -> Result<Value, Failure> {
|
|
let uri = params.text_document.uri;
|
|
let Some(doc) = self.workspace.document(&uri) else { return Ok(Value::Null) };
|
|
let settings = format::settings_for(doc, self.workspace.client_settings(), ¶ms.options);
|
|
let result = format::ranges_formatting(doc, &settings.config, ¶ms.ranges);
|
|
self.warn_about_config(&uri, settings.problem);
|
|
self.edits(result)
|
|
}
|
|
|
|
/// `textDocument/onTypeFormatting` — the newline and the semicolon.
|
|
///
|
|
/// Two things about it are unlike every other formatting entry, and both
|
|
/// follow from its trigger being a keystroke rather than a command:
|
|
///
|
|
/// * **It never speaks.** A formatter refusal is `[]` and silence, where
|
|
/// `textDocument/formatting` shows an ERROR and `willSaveWaitUntil`
|
|
/// shows one per save. Mid-edit code the engine declines is the *normal*
|
|
/// case here, not a failure worth a popup on every semicolon.
|
|
/// * **It never warns about a broken `lucisv.toml` either.** That warning
|
|
/// is deduped per buffer *state*, and `set_document` re-arms it — so a
|
|
/// request that by definition follows the `didChange` the user's own
|
|
/// keystroke produced would re-arm and re-show it on every character.
|
|
/// The user is told on their next explicit format or save, which are the
|
|
/// two moments they asked lucisv for something.
|
|
///
|
|
/// The settings ladder is the one `textDocument/formatting` climbs,
|
|
/// `tabSize` rung included: unlike `willSaveWaitUntil`, this request does
|
|
/// carry `FormattingOptions`.
|
|
pub(super) fn on_type_formatting(
|
|
&mut self,
|
|
params: DocumentOnTypeFormattingParams,
|
|
) -> Result<Value, Failure> {
|
|
let at = params.text_document_position;
|
|
let Some(doc) = self.workspace.document(&at.text_document.uri) else {
|
|
return to_value(Vec::<lsp_types::TextEdit>::new());
|
|
};
|
|
let settings = format::settings_for(doc, self.workspace.client_settings(), ¶ms.options);
|
|
let edits = on_type::on_type_formatting(doc, at.position, ¶ms.ch, &settings.config);
|
|
to_value(edits)
|
|
}
|
|
|
|
/// `textDocument/willSaveWaitUntil` — format the buffer the client is
|
|
/// about to write, for the editors that have no format-on-save of their
|
|
/// own.
|
|
///
|
|
/// `null` unless `lucisv.formatOnSave` is on. A capable client sends this
|
|
/// before *every* save, so declining has to be the cheap path — and an
|
|
/// editor that already formats on save (VS Code's `editor.formatOnSave`,
|
|
/// which runs `textDocument/formatting`) would otherwise format twice.
|
|
///
|
|
/// When it is on, this is `textDocument/formatting` with a different
|
|
/// trigger: the same engine, the same settings ladder, the same
|
|
/// all-or-nothing contract — one whole-document edit built from verified
|
|
/// output, or no edit at all. The save's `reason` is not read: manual,
|
|
/// after-delay and focus-out are all saves, and the user asked for their
|
|
/// saves to be formatted. The text is the buffer the server holds, because
|
|
/// the request carries none and the client has not written yet.
|
|
pub(super) fn will_save_wait_until(
|
|
&mut self,
|
|
params: WillSaveTextDocumentParams,
|
|
) -> Result<Value, Failure> {
|
|
if !self.workspace.client_settings().format_on_save {
|
|
return Ok(Value::Null);
|
|
}
|
|
let uri = params.text_document.uri;
|
|
let Some(doc) = self.workspace.document(&uri) else { return Ok(Value::Null) };
|
|
// No `FormattingOptions` on this request — the protocol carries none —
|
|
// so the ladder runs without its `tabSize` rung.
|
|
let settings =
|
|
format::settings_without_editor_options(doc, self.workspace.client_settings());
|
|
let result = format::formatting(doc, &settings.config);
|
|
self.warn_about_config(&uri, settings.problem);
|
|
self.edits(result)
|
|
}
|
|
|
|
/// Say once per change of a file that its `lucisv.toml` could not be read.
|
|
fn warn_about_config(&mut self, uri: &Uri, problem: Option<String>) {
|
|
if let Some(problem) = problem {
|
|
if self.config_warnings.insert(uri.clone()) {
|
|
self.show_message(
|
|
MessageType::WARNING,
|
|
format!("lucisv: {problem} — formatting with default settings"),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// One edit, none, or — when the formatter refused — none plus the reason.
|
|
///
|
|
/// It never emits partial output (CLAUDE.md invariant #1), so neither do
|
|
/// we. Diagnostics are not used for the failure: they belong to the parse
|
|
/// pass and are cleared on the next keystroke, which would erase an
|
|
/// explicit format failure the user asked for.
|
|
pub(super) fn edits(
|
|
&mut self,
|
|
result: Result<Option<lsp_types::TextEdit>, String>,
|
|
) -> Result<Value, Failure> {
|
|
match result {
|
|
Ok(Some(edit)) => to_value(vec![edit]),
|
|
Ok(None) => to_value(Vec::<lsp_types::TextEdit>::new()),
|
|
Err(message) => {
|
|
self.show_message(MessageType::ERROR, format!("lucisv: cannot format — {message}"));
|
|
to_value(Vec::<lsp_types::TextEdit>::new())
|
|
}
|
|
}
|
|
}
|
|
}
|