//! 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 { 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 { 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 { 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 { let at = params.text_document_position; let Some(doc) = self.workspace.document(&at.text_document.uri) else { return to_value(Vec::::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 { 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) { 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, String>, ) -> Result { match result { Ok(Some(edit)) => to_value(vec![edit]), Ok(None) => to_value(Vec::::new()), Err(message) => { self.show_message(MessageType::ERROR, format!("lucisv: cannot format — {message}")); to_value(Vec::::new()) } } } }