From d30788ec441f2e404552ed732ae92b5ab7655df6 Mon Sep 17 00:00:00 2001 From: Steve Dignam Date: Sun, 27 Sep 2026 18:50:59 -0400 Subject: [PATCH] fmt: add width param --- crates/squawk_fmt/src/fmt.rs | 4 ++++ crates/squawk_fmt/src/main.rs | 11 ++++++++++- crates/squawk_fmt/tests/tests.rs | 26 ++++++++++++++++++++++++-- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/crates/squawk_fmt/src/fmt.rs b/crates/squawk_fmt/src/fmt.rs index 20f672a3..81c28ad6 100644 --- a/crates/squawk_fmt/src/fmt.rs +++ b/crates/squawk_fmt/src/fmt.rs @@ -19,16 +19,19 @@ use tiny_pretty::Doc; use tiny_pretty::{LineBreak, PrintOptions, print}; const DEFAULT_INDENT: usize = 2; +const DEFAULT_WIDTH: usize = 80; #[derive(Debug, Clone, Copy)] pub struct FormatOptions { pub indent: usize, + pub width: usize, } impl Default for FormatOptions { fn default() -> Self { Self { indent: DEFAULT_INDENT, + width: DEFAULT_WIDTH, } } } @@ -20302,6 +20305,7 @@ pub fn fmt( &doc, &PrintOptions { line_break, + width: options.width, ..Default::default() }, )) diff --git a/crates/squawk_fmt/src/main.rs b/crates/squawk_fmt/src/main.rs index e252bfae..7597794b 100644 --- a/crates/squawk_fmt/src/main.rs +++ b/crates/squawk_fmt/src/main.rs @@ -14,6 +14,9 @@ struct Cli { /// Number of spaces to indent #[arg(long, default_value_t = FormatOptions::default().indent)] indent: usize, + /// The line length where Squawk tries wrapping + #[arg(long, default_value_t = FormatOptions::default().width)] + width: usize, /// File to format; reads from stdin if omitted file: Option, } @@ -55,7 +58,13 @@ fn main() -> Result { return Ok(ExitCode::FAILURE); } - let formatted = squawk_fmt::fmt_str(&input, FormatOptions { indent: cli.indent })?; + let formatted = squawk_fmt::fmt_str( + &input, + FormatOptions { + indent: cli.indent, + width: cli.width, + }, + )?; assert_no_dropped_tokens(&input, &formatted); let reparse = SourceFile::parse(&formatted); diff --git a/crates/squawk_fmt/tests/tests.rs b/crates/squawk_fmt/tests/tests.rs index 6735a58c..3923ef9e 100644 --- a/crates/squawk_fmt/tests/tests.rs +++ b/crates/squawk_fmt/tests/tests.rs @@ -19,7 +19,11 @@ fn fmt(fixture: Fixture<&str>) { assert_no_dropped_tokens(content, &formatted); assert_parses(&formatted); - + assert_eq!( + squawk_fmt::fmt_str(&formatted, Default::default()).unwrap(), + formatted, + "formatting isn't idempotent" + ); with_settings!({ omit_expression => true, input_file => absolute_fixture_path, @@ -92,7 +96,10 @@ fn fmt_cr_line_endings() { fn configurable_indent() { let sql = "select 'a', 'really long string ';\n"; - let options = squawk_fmt::FormatOptions { indent: 4 }; + let options = squawk_fmt::FormatOptions { + indent: 4, + ..Default::default() + }; assert_snapshot!(squawk_fmt::fmt_str(sql, options).unwrap(), @" select @@ -101,6 +108,21 @@ fn configurable_indent() { "); } +#[test] +fn configurable_width() { + let sql = "select first_column, second_column;\n"; + let options = squawk_fmt::FormatOptions { + width: 20, + ..Default::default() + }; + + assert_snapshot!(squawk_fmt::fmt_str(sql, options).unwrap(), @" + select + first_column, + second_column; + "); +} + #[test] fn normalizes_line_endings_inside_block_comments() { let sql = "select 1;\r\n/* a\n * comment\n */\nselect 2;\n";