Files
lucisv/crates/vfmt_server/src/format.rs
T
lucioandClaude Fable 5 315bbf23ab Refactoring M1: EDA filelist (.f) defines the index scope
Rope-port umbrella plan (docs/features/refactor) + TODO.md §8; M1 ships
vfmt_core::filelist (-f nesting with cycle + 256-depth guard, +incdir+/
+define+ recorded, env vars, warnings not errors), the vfmt.toml filelist
key, the server scope ladder (filelist > workspace walk > open file's
directory when rootless) and the vfmt.filelist VS Code setting.

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-08-24 04:11:45 -07:00

598 lines
27 KiB
Rust

//! `textDocument/formatting` and `textDocument/rangeFormatting`.
//!
//! The formatter's contract is all-or-nothing (CLAUDE.md invariant #1), and
//! these handlers keep it: either one whole-document edit built from verified
//! output, or no edit at all and an error for the user to read. That holds for
//! the range request too — its output is verified over the whole buffer, and
//! what makes it a *range* format is which constructs it laid out, not which
//! bytes it was allowed to replace.
use std::path::Path;
use lsp_types::{FormattingOptions, Range, TextEdit};
use vfmt_core::config::{Discovery, FileConfig};
use vlog_formatter::{format_range, format_source, Config, LineRanges};
use crate::settings::ClientSettings;
use crate::state::Document;
/// Indent widths a client may ask for. The same 1..8 range `vfmt.toml`'s own
/// `indent` key allows, so the two agree.
const INDENT_RANGE: std::ops::RangeInclusive<u32> = 1..=8;
/// What to format a buffer with, and the config problem to tell the user
/// about. The two are separate because a broken `vfmt.toml` must not stop the
/// buffer from being formatted.
pub struct Settings {
/// The settings to format with — the fallback defaults when `problem` is
/// set.
pub config: Config,
/// The `vfmt.toml` error governing this buffer, if there is one.
pub problem: Option<String>,
}
/// Resolve the settings governing a buffer.
///
/// Precedence, highest first:
///
/// 1. **`vfmt.toml`** — a checked-in config is the project speaking, and it
/// must stay authoritative or format-on-save would disagree with CI's
/// `vfmt --check`. That divergence is the exact thing lifting the config
/// into `vfmt_core` exists to remove.
/// 2. **The client's `vfmt.*` settings** (`settings.rs`) — typed on purpose
/// into an editor's settings UI, so more deliberate than a tab size, but
/// it is one user's editor and the repository outranks it.
/// 3. **`FormattingOptions::tab_size`** — sent on every request whether or
/// not the user ever chose it; it is the editor's own default, or its
/// guess from the file, so it is the weakest thing that is still a signal.
/// This is *not* the CLI's flag-beats-config order, and deliberately so.
/// 4. **The built-in default.**
///
/// Each rung applies only where every rung above it is silent, so a project
/// with no `vfmt.toml` and no `vfmt.*` settings behaves exactly as it did
/// before either existed.
pub fn settings_for(
doc: &Document,
client: &ClientSettings,
options: &FormattingOptions,
) -> Settings {
let (file, problem) = discover(doc.path.as_deref());
Settings { config: config_from(&file, client, options), problem }
}
/// Format a buffer.
///
/// `Ok(None)` means "nothing to do" — the buffer is already formatted, and an
/// edit that changes nothing still moves the cursor in some editors. `Err`
/// carries the formatter's own message; the caller surfaces it, and must not
/// invent an edit from it.
pub fn formatting(doc: &Document, config: &Config) -> Result<Option<TextEdit>, String> {
let formatted = format_source(&doc.text, config).map_err(|e| e.to_string())?;
if formatted == doc.text {
return Ok(None);
}
Ok(Some(TextEdit { range: doc.positions().whole_document(), new_text: formatted }))
}
/// Format the constructs a selection reaches, and nothing else.
///
/// The protocol's `Range` is a half-open span of UTF-16 positions; the engine
/// wants 1-based inclusive *lines*, so only the line numbers survive the
/// conversion. An end position at character 0 still names its line — an
/// editor's "select these three lines" ends at the start of the fourth — so
/// a range that stops at a line's very beginning does not drag that line in
/// unless it is also where the range started.
///
/// The reply is still one whole-document edit: the engine's contract is
/// all-or-nothing over the file, and a partial run's output differs from the
/// buffer only where it decided to differ.
pub fn range_formatting(
doc: &Document,
config: &Config,
range: Range,
) -> Result<Option<TextEdit>, String> {
let first = range.start.line.saturating_add(1);
// An end at character 0 of a later line is the start of that line, not
// any of its content.
let ends_before_its_line = range.end.character == 0 && range.end.line > range.start.line;
let last = if ends_before_its_line { range.end.line } else { range.end.line + 1 };
let lines = LineRanges::new([(first, last.max(first))])?;
let formatted = format_range(&doc.text, config, &lines).map_err(|e| e.to_string())?;
if formatted == doc.text {
return Ok(None);
}
Ok(Some(TextEdit { range: doc.positions().whole_document(), new_text: formatted }))
}
/// The indent width governing a buffer, for a lint fix that has to write
/// spaces where a tab was.
///
/// A `textDocument/codeAction` request carries no `FormattingOptions`, so there
/// is no client `tabSize` to consider here — the precedence is the same ladder
/// `settings_for` documents with rung 3 missing: `vfmt.toml` > `vfmt.indent` >
/// the built-in default. A broken config is silent on this path: the
/// formatting path is where the user is told about it, and warning from a
/// lightbulb the editor requests on every cursor move would be noise.
pub fn indent_width(path: Option<&Path>, client: &ClientSettings) -> usize {
let (file, _problem) = discover(path);
file.indent.or(client.indent).unwrap_or(Config::default().indent_width)
}
/// The config file governing `path`, by the CLI's own discovery rule, or the
/// error that stopped it from being read. A buffer with no path (`untitled:`)
/// has no directory to search from, so it gets no config.
fn discover(path: Option<&Path>) -> (FileConfig, Option<String>) {
let Some(path) = path else { return (FileConfig::default(), None) };
// A fresh `Discovery` per request, so a `vfmt.toml` edited while the
// server runs takes effect at the next format. One file read on an
// explicit, user-initiated request is cheaper than any cache invalidation
// scheme that could be wrong.
match Discovery::new(true).for_file(path) {
Ok(file) => (file, None),
Err(problem) => (FileConfig::default(), Some(problem)),
}
}
/// Fold the config file, the pushed `vfmt.*` settings and the client's tab
/// options into formatter settings, in the precedence `settings_for`
/// documents. Written weakest-first, so the last writer wins.
///
/// The client's `tab_size` is taken only when the request is sane: a client
/// asking for hard tabs, or for a 40-space indent, is asking for something
/// vfmt does not do, and silently honoring half of it is worse than ignoring
/// it. `vfmt.lineLength` has no `FormattingOptions` analog at all — the
/// protocol carries no line-length option — so before it existed there was no
/// way to set a width from an editor without a `vfmt.toml`.
fn config_from(file: &FileConfig, client: &ClientSettings, options: &FormattingOptions) -> Config {
let mut config = Config::default();
if options.insert_spaces && INDENT_RANGE.contains(&options.tab_size) {
config.indent_width = options.tab_size as usize;
}
if let Some(indent) = client.indent {
config.indent_width = indent;
}
if let Some(line_length) = client.line_length {
config.max_width = line_length;
}
if let Some(indent) = file.indent {
config.indent_width = indent;
}
if let Some(line_length) = file.line_length {
config.max_width = line_length;
}
config
}
#[cfg(test)]
mod tests {
use super::*;
use crate::settings::ClientSettings;
use crate::state::Workspace;
use lsp_types::{Position, Uri};
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::atomic::{AtomicUsize, Ordering};
fn document(text: &str) -> (Workspace, Uri) {
let uri = Uri::from_str("file:///rtl/m.sv").expect("uri");
let mut ws = Workspace::default();
ws.set_document(uri.clone(), text.to_string());
(ws, uri)
}
fn options(tab_size: u32, insert_spaces: bool) -> FormattingOptions {
FormattingOptions { tab_size, insert_spaces, ..Default::default() }
}
/// No `vfmt.*` settings pushed — what every test predating them assumes.
fn none() -> ClientSettings {
ClientSettings::default()
}
/// The settings a buffer resolves to, with nothing pushed by the client.
fn settings(ws: &Workspace, uri: &Uri, options: &FormattingOptions) -> Settings {
settings_with(ws, uri, &none(), options)
}
/// The settings a buffer resolves to against a given pushed configuration.
fn settings_with(
ws: &Workspace,
uri: &Uri,
client: &ClientSettings,
options: &FormattingOptions,
) -> Settings {
settings_for(ws.document(uri).expect("open"), client, options)
}
fn edit_of(
ws: &Workspace,
uri: &Uri,
options: &FormattingOptions,
) -> Result<Option<TextEdit>, String> {
edit_with(ws, uri, &none(), options)
}
fn edit_with(
ws: &Workspace,
uri: &Uri,
client: &ClientSettings,
options: &FormattingOptions,
) -> Result<Option<TextEdit>, String> {
let doc = ws.document(uri).expect("open");
formatting(doc, &settings_for(doc, client, options).config)
}
/// A scratch directory holding a `vfmt.toml` and one source file, so
/// discovery runs against a real tree. Removed on drop.
struct Tree {
path: PathBuf,
}
impl Tree {
fn new(config: &str) -> Tree {
static COUNTER: AtomicUsize = AtomicUsize::new(0);
let n = COUNTER.fetch_add(1, Ordering::Relaxed);
let path =
std::env::temp_dir().join(format!("vfmt_server_fmt_{}_{n}", std::process::id()));
let _ = std::fs::remove_dir_all(&path);
std::fs::create_dir_all(&path).expect("temp dir");
// A `.git` marker stops discovery here, so a `vfmt.toml` in a real
// ancestor of the temp dir can never leak into these tests.
std::fs::write(path.join(".git"), "gitdir: nowhere\n").expect("write .git");
std::fs::write(path.join("vfmt.toml"), config).expect("write config");
Tree { path }
}
/// An open buffer for `m.sv` inside this tree.
fn document(&self, text: &str) -> (Workspace, Uri) {
let file = self.path.join("m.sv");
std::fs::write(&file, text).expect("write source");
let uri = crate::uri::path_to_uri(&file).expect("uri");
let mut ws = Workspace::default();
ws.set_document(uri.clone(), text.to_string());
(ws, uri)
}
}
impl Drop for Tree {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.path);
}
}
#[test]
fn an_unformatted_buffer_gets_one_whole_document_edit() {
let (ws, uri) = document("module m;\nwire a;\nendmodule\n");
let edit = edit_of(&ws, &uri, &options(2, true)).expect("formats").expect("changed");
assert_eq!(edit.range.start.line, 0);
assert_eq!(edit.range.start.character, 0);
assert_eq!(edit.range.end.line, 3, "one past the last line");
assert_eq!(edit.new_text, "module m;\n wire a;\nendmodule\n");
}
#[test]
fn an_already_formatted_buffer_gets_no_edit() {
let (ws, uri) = document("module m;\n wire a;\nendmodule\n");
assert_eq!(edit_of(&ws, &uri, &options(2, true)).expect("formats"), None);
}
#[test]
fn a_sane_tab_size_becomes_the_indent_width() {
let (ws, uri) = document("module m;\nwire a;\nendmodule\n");
let edit = edit_of(&ws, &uri, &options(4, true)).expect("formats").expect("changed");
assert_eq!(edit.new_text, "module m;\n wire a;\nendmodule\n");
}
#[test]
fn hard_tabs_and_absurd_widths_fall_back_to_the_default() {
let nothing = FileConfig::default();
assert_eq!(
config_from(&nothing, &none(), &options(4, false)).indent_width,
2,
"hard tabs ignored"
);
assert_eq!(
config_from(&nothing, &none(), &options(40, true)).indent_width,
2,
"out of range ignored"
);
assert_eq!(
config_from(&nothing, &none(), &options(0, true)).indent_width,
2,
"zero ignored"
);
assert_eq!(
config_from(&nothing, &none(), &options(8, true)).indent_width,
8,
"the top of the range"
);
}
#[test]
fn a_line_length_is_never_taken_from_the_client() {
let config = config_from(&FileConfig::default(), &none(), &options(4, true));
assert_eq!(config.max_width, Config::default().max_width);
}
#[test]
fn a_config_file_beats_the_editors_tab_size() {
// The precedence decision, pinned: `tab_size` is the editor's default
// unless the user changed it, and a checked-in `vfmt.toml` is always
// deliberate. See `settings_for`.
let file = FileConfig { line_length: Some(120), indent: Some(4), ..Default::default() };
let config = config_from(&file, &none(), &options(2, true));
assert_eq!(config.indent_width, 4, "vfmt.toml wins over tab_size");
assert_eq!(config.max_width, 120, "line-length has no client analog at all");
// Where the config is silent the tab size still beats the default.
let only_width = FileConfig { line_length: Some(120), ..Default::default() };
assert_eq!(config_from(&only_width, &none(), &options(3, true)).indent_width, 3);
}
#[test]
fn a_vfmt_toml_governs_the_buffer_it_sits_above() {
let tree = Tree::new("indent = 4\n");
let (ws, uri) = tree.document("module m;\nwire a;\nendmodule\n");
// The client asks for 2, the config says 4: the config wins.
let settings = settings(&ws, &uri, &options(2, true));
assert_eq!(settings.config.indent_width, 4);
assert_eq!(settings.problem, None);
let edit = edit_of(&ws, &uri, &options(2, true)).expect("formats").expect("changed");
assert_eq!(edit.new_text, "module m;\n wire a;\nendmodule\n");
}
#[test]
fn a_broken_vfmt_toml_reports_a_problem_and_formats_with_defaults() {
let tree = Tree::new("indent = 4\nbogus = 1\n");
let (ws, uri) = tree.document("module m;\nwire a;\nendmodule\n");
let settings = settings(&ws, &uri, &options(2, true));
let problem = settings.problem.expect("a broken config must be reported");
assert!(problem.contains("unknown key `bogus`"), "{problem}");
assert!(problem.contains("vfmt.toml:2"), "the message names file and line: {problem}");
// Nothing from the broken file is used, not even the key that parsed.
assert_eq!(settings.config.indent_width, Config::default().indent_width);
// And the buffer is still formatted: an editor must not hard-fail on a
// config typo the way the CLI's exit 2 does.
let edit = edit_of(&ws, &uri, &options(2, true)).expect("formats").expect("changed");
assert_eq!(edit.new_text, "module m;\n wire a;\nendmodule\n");
}
#[test]
fn a_lint_fixs_indent_comes_from_the_config_or_the_built_in_default() {
// A code action has no `tabSize` to consider, so a quick fix that
// writes spaces where a tab was gets the buffer's own `vfmt.toml`.
let tree = Tree::new("indent = 4\n");
let (ws, uri) = tree.document("module m;\n\twire a;\nendmodule\n");
let doc = ws.document(&uri).expect("open");
assert_eq!(indent_width(doc.path.as_deref(), &none()), 4);
assert_eq!(
indent_width(None, &none()),
Config::default().indent_width,
"an untitled buffer"
);
}
#[test]
fn a_broken_config_leaves_the_lint_fix_on_the_default_indent_and_says_nothing() {
let tree = Tree::new("indent = 4\nbogus = 1\n");
let (ws, uri) = tree.document("module m;\n\twire a;\nendmodule\n");
let doc = ws.document(&uri).expect("open");
assert_eq!(indent_width(doc.path.as_deref(), &none()), Config::default().indent_width);
}
#[test]
fn an_untitled_buffer_has_no_directory_to_discover_from() {
let uri = Uri::from_str("untitled:Untitled-1").expect("uri");
let mut ws = Workspace::default();
ws.set_document(uri.clone(), "module m;\nwire a;\nendmodule\n".to_string());
let settings = settings(&ws, &uri, &options(4, true));
assert_eq!(settings.problem, None);
assert_eq!(settings.config.indent_width, 4, "the tab size is all there is");
}
// ------------------------------------------------------------------
// the precedence ladder, one rung at a time
// ------------------------------------------------------------------
/// `vfmt.*` settings as a client would push them.
fn pushed(line_length: Option<usize>, indent: Option<usize>) -> ClientSettings {
ClientSettings { line_length, indent, ..Default::default() }
}
#[test]
fn rung_1_a_config_file_beats_a_pushed_setting() {
// The whole reason the ladder is in this order: a checked-in
// `vfmt.toml` is the project speaking, and format-on-save must agree
// with what `vfmt --check` does in CI.
let tree = Tree::new("indent = 4\nline-length = 120\n");
let (ws, uri) = tree.document("module m;\nwire a;\nendmodule\n");
let client = pushed(Some(80), Some(8));
let settings = settings_with(&ws, &uri, &client, &options(2, true));
assert_eq!(settings.config.indent_width, 4, "vfmt.toml wins");
assert_eq!(settings.config.max_width, 120, "for line-length too");
let edit = edit_with(&ws, &uri, &client, &options(2, true)).expect("formats");
assert_eq!(edit.expect("changed").new_text, "module m;\n wire a;\nendmodule\n");
}
#[test]
fn rung_2_a_pushed_setting_beats_the_editors_tab_size() {
// `tabSize` arrives on every request whether the user chose it or
// not; `vfmt.indent` was typed on purpose.
let (ws, uri) = document("module m;\nwire a;\nendmodule\n");
let client = pushed(None, Some(4));
let settings = settings_with(&ws, &uri, &client, &options(2, true));
assert_eq!(settings.config.indent_width, 4);
let edit = edit_with(&ws, &uri, &client, &options(2, true)).expect("formats");
assert_eq!(edit.expect("changed").new_text, "module m;\n wire a;\nendmodule\n");
}
#[test]
fn rung_3_the_tab_size_still_beats_the_default_where_nothing_was_pushed() {
let (ws, uri) = document("module m;\nwire a;\nendmodule\n");
// Pushed settings that say nothing about indent leave rung 3 in charge.
let client = pushed(Some(120), None);
let settings = settings_with(&ws, &uri, &client, &options(3, true));
assert_eq!(settings.config.indent_width, 3, "the tab size");
assert_eq!(settings.config.max_width, 120, "and the pushed width");
}
#[test]
fn rung_4_the_default_applies_when_every_rung_above_is_silent() {
let (ws, uri) = document("module m;\nwire a;\nendmodule\n");
// Hard tabs, so even the tab size is not a usable signal.
let settings = settings_with(&ws, &uri, &none(), &options(4, false));
assert_eq!(settings.config.indent_width, Config::default().indent_width);
assert_eq!(settings.config.max_width, Config::default().max_width);
}
#[test]
fn each_key_climbs_the_ladder_independently() {
// A `vfmt.toml` naming only `indent` must not also claim the width:
// the pushed `lineLength` is still the best answer for that key.
let tree = Tree::new("indent = 4\n");
let (ws, uri) = tree.document("module m;\nwire a;\nendmodule\n");
let settings = settings_with(&ws, &uri, &pushed(Some(60), Some(8)), &options(2, true));
assert_eq!(settings.config.indent_width, 4, "config wins the key it names");
assert_eq!(settings.config.max_width, 60, "and is silent about the other");
}
#[test]
fn a_pushed_line_length_is_the_only_way_to_set_a_width_without_a_config() {
// The protocol has no line-length option at all, so before
// `vfmt.lineLength` a config-less project could not narrow its lines
// from the editor. This is the new capability, at the level that
// actually reflows text.
let long = "module m;\n assign a = bbbbbbbbbbbb + cccccccccccc + dddddddddddd + eeeeeeeeeeee;\nendmodule\n";
let (ws, uri) = document(long);
assert_eq!(edit_of(&ws, &uri, &options(2, true)).expect("formats"), None, "fits at 100");
let edit = edit_with(&ws, &uri, &pushed(Some(40), None), &options(2, true))
.expect("formats")
.expect("40 columns must reflow it");
assert!(edit.new_text.lines().count() > long.lines().count(), "{}", edit.new_text);
}
#[test]
fn an_untitled_buffer_honours_pushed_settings() {
// It has no directory to discover a `vfmt.toml` from, so rung 2 is the
// highest rung it has — and before this feature it had only `tabSize`.
let uri = Uri::from_str("untitled:Untitled-1").expect("uri");
let mut ws = Workspace::default();
ws.set_document(uri.clone(), "module m;\nwire a;\nendmodule\n".to_string());
let settings = settings_with(&ws, &uri, &pushed(Some(60), Some(4)), &options(2, true));
assert_eq!(settings.problem, None);
assert_eq!(settings.config.indent_width, 4, "pushed indent beats the tab size");
assert_eq!(settings.config.max_width, 60);
}
#[test]
fn a_lint_fix_climbs_the_same_ladder_without_rung_3() {
// A code action has no `tabSize`, so `vfmt.indent` is the only thing
// under a `vfmt.toml` here.
let tree = Tree::new("indent = 4\n");
let (ws, uri) = tree.document("module m;\n\twire a;\nendmodule\n");
let doc = ws.document(&uri).expect("open");
assert_eq!(indent_width(doc.path.as_deref(), &pushed(None, Some(8))), 4, "config wins");
assert_eq!(indent_width(None, &pushed(None, Some(8))), 8, "untitled: the pushed value");
assert_eq!(indent_width(None, &none()), Config::default().indent_width, "then default");
}
#[test]
fn a_broken_config_leaves_the_pushed_settings_in_charge() {
// Nothing from a broken `vfmt.toml` is used, so the next rung down is
// the one that answers — which is now the pushed settings, not the
// built-in default.
let tree = Tree::new("indent = 4\nbogus = 1\n");
let (ws, uri) = tree.document("module m;\nwire a;\nendmodule\n");
let settings = settings_with(&ws, &uri, &pushed(Some(60), Some(8)), &options(2, true));
assert!(settings.problem.is_some(), "the user is still told");
assert_eq!(settings.config.indent_width, 8);
assert_eq!(settings.config.max_width, 60);
}
/// Three items, one per line, so a range's reach is visible line by line.
const THREE: &str = "module m;\n wire a;\n wire b;\nendmodule\n";
fn at(start_line: u32, start_char: u32, end_line: u32, end_char: u32) -> Range {
Range {
start: Position { line: start_line, character: start_char },
end: Position { line: end_line, character: end_char },
}
}
fn ranged(text: &str, range: Range) -> Result<Option<TextEdit>, String> {
let (ws, uri) = document(text);
let doc = ws.document(&uri).expect("open");
range_formatting(doc, &settings_for(doc, &none(), &options(2, true)).config, range)
}
#[test]
fn a_range_formats_only_the_lines_it_covers() {
// A cursor sitting on line 2 (0-based line 1) with nothing selected.
let edit = ranged(THREE, at(1, 4, 1, 4)).expect("formats").expect("changed");
assert_eq!(edit.new_text, "module m;\n wire a;\n wire b;\nendmodule\n");
// And the reply is still one whole-document edit.
assert_eq!(edit.range.start, Position { line: 0, character: 0 });
assert_eq!(edit.range.end.line, 4);
}
#[test]
fn a_selection_ending_at_the_start_of_a_line_stops_before_it() {
// "Select these two lines" in an editor ends at line 3, character 0.
let edit = ranged(THREE, at(1, 0, 3, 0)).expect("formats").expect("changed");
assert_eq!(edit.new_text, "module m;\n wire a;\n wire b;\nendmodule\n");
// One line only, ending where it began.
let edit = ranged(THREE, at(2, 0, 3, 0)).expect("formats").expect("changed");
assert_eq!(edit.new_text, "module m;\n wire a;\n wire b;\nendmodule\n");
}
#[test]
fn a_range_over_nothing_formattable_produces_no_edit() {
// Line 4 is `endmodule`, already written the way the formatter writes
// it, so there is nothing to send.
assert_eq!(ranged(THREE, at(3, 0, 3, 9)).expect("formats"), None);
}
#[test]
fn a_range_whose_end_is_past_the_document_still_answers() {
let edit = ranged(THREE, at(0, 0, 99, 0)).expect("formats").expect("changed");
assert_eq!(edit.new_text, "module m;\n wire a;\n wire b;\nendmodule\n");
}
#[test]
fn a_range_over_multi_byte_content_lands_on_the_right_lines() {
// The character columns are UTF-16 units; only the *lines* reach the
// engine, so the emoji line must not shift which item is formatted.
let text = "module m;\n // é ✓ 🎉 note\n wire a;\n wire b;\nendmodule\n";
let edit = ranged(text, at(2, 0, 2, 11)).expect("formats").expect("changed");
assert_eq!(
edit.new_text,
"module m;\n // é ✓ 🎉 note\n wire a;\n wire b;\nendmodule\n"
);
assert!(edit.new_text.contains("é ✓ 🎉"), "comments are never dropped");
}
#[test]
fn an_unparsable_buffer_produces_no_range_edit_either() {
// Range formatting cannot rescue broken input; the strict parse still
// has to succeed.
let message =
ranged("module m;\n wire ;\nendmodule\n", at(0, 0, 0, 9)).expect_err("must refuse");
assert!(!message.is_empty(), "the user needs something to read");
}
#[test]
fn an_unparsable_buffer_produces_no_edit() {
let (ws, uri) = document("module m;\n wire ;\nendmodule\n");
let message = edit_of(&ws, &uri, &options(2, true)).expect_err("must refuse");
assert!(!message.is_empty(), "the user needs something to read");
}
#[test]
fn multi_byte_content_survives_formatting() {
let (ws, uri) = document("module m;\n// é ✓ 🎉\nwire a;\nendmodule\n");
let edit = edit_of(&ws, &uri, &options(2, true)).expect("formats").expect("changed");
assert!(edit.new_text.contains("é ✓ 🎉"), "comments are never dropped");
// The replaced range must cover the whole document in UTF-16 terms.
assert_eq!(edit.range.end.line, 4);
}
}