Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/squawk_fmt/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
}
Expand Down Expand Up @@ -20302,6 +20305,7 @@ pub fn fmt(
&doc,
&PrintOptions {
line_break,
width: options.width,
..Default::default()
},
))
Expand Down
11 changes: 10 additions & 1 deletion crates/squawk_fmt/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>,
}
Expand Down Expand Up @@ -55,7 +58,13 @@ fn main() -> Result<ExitCode> {
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);
Expand Down
26 changes: 24 additions & 2 deletions crates/squawk_fmt/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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";
Expand Down
Loading