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
5,134 changes: 2,908 additions & 2,226 deletions crates/squawk_fmt/src/fmt.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion crates/squawk_fmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ mod comment;
mod fmt;
pub mod token_compare;

pub use fmt::{fmt, fmt_str};
pub use fmt::{FormatOptions, fmt, fmt_str};
7 changes: 5 additions & 2 deletions crates/squawk_fmt/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ use std::process::ExitCode;
use annotate_snippets::{AnnotationKind, Level, Renderer, Snippet, renderer::DecorStyle};
use anyhow::Result;
use clap::Parser;
use squawk_fmt::token_compare::assert_no_dropped_tokens;
use squawk_fmt::{FormatOptions, token_compare::assert_no_dropped_tokens};
use squawk_syntax::SourceFile;

#[derive(Parser)]
#[command(name = "squawk-fmt")]
struct Cli {
/// Number of spaces to indent
#[arg(long, default_value_t = FormatOptions::default().indent)]
indent: usize,
/// File to format; reads from stdin if omitted
file: Option<PathBuf>,
}
Expand Down Expand Up @@ -52,7 +55,7 @@ fn main() -> Result<ExitCode> {
return Ok(ExitCode::FAILURE);
}

let formatted = squawk_fmt::fmt_str(&input)?;
let formatted = squawk_fmt::fmt_str(&input, FormatOptions { indent: cli.indent })?;
assert_no_dropped_tokens(&input, &formatted);

let reparse = SourceFile::parse(&formatted);
Expand Down
36 changes: 29 additions & 7 deletions crates/squawk_fmt/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn fmt(fixture: Fixture<&str>) {
.and_then(|x| x.strip_suffix(".sql"))
.unwrap();

let formatted = squawk_fmt::fmt_str(content).unwrap();
let formatted = squawk_fmt::fmt_str(content, Default::default()).unwrap();

assert_no_dropped_tokens(content, &formatted);
assert_parses(&formatted);
Expand Down Expand Up @@ -43,7 +43,7 @@ fn fmt_with_line_ending(line_ending: &str) -> String {
]
.join(line_ending);

match squawk_fmt::fmt_str(&sql) {
match squawk_fmt::fmt_str(&sql, Default::default()) {
Ok(formatted) => {
assert_no_dropped_tokens(&sql, &formatted);
assert_parses(&formatted);
Expand Down Expand Up @@ -88,34 +88,56 @@ fn fmt_cr_line_endings() {
assert_snapshot!(fmt_with_line_ending("\r"), @"-- a comment<CR>select 1;<CR><CR>/* a comment<CR> * spanning lines<CR> */<CR>select<CR> 'a',<CR> 'really long string ';<CR>");
}

#[test]
fn configurable_indent() {
let sql =
"select 'a', 'really long string ';\n";
let options = squawk_fmt::FormatOptions { indent: 4 };

assert_snapshot!(squawk_fmt::fmt_str(sql, options).unwrap(), @"
select
'a',
'really long string ';
");
}

#[test]
fn normalizes_line_endings_inside_block_comments() {
let sql = "select 1;\r\n/* a\n * comment\n */\nselect 2;\n";
let expected = "select 1;\r\n/* a\r\n * comment\r\n */\r\nselect 2;\r\n";

let formatted = squawk_fmt::fmt_str(sql).unwrap();
let formatted = squawk_fmt::fmt_str(sql, Default::default()).unwrap();
assert_eq!(formatted, expected);
assert_eq!(squawk_fmt::fmt_str(&formatted).unwrap(), expected);
assert_eq!(
squawk_fmt::fmt_str(&formatted, Default::default()).unwrap(),
expected
);
}

#[test]
fn removes_trailing_whitespace_from_comments() {
let sql = "select 1; -- ok \n/* a \n * comment\t\n */\nselect 2;\n";
let expected = "select 1; -- ok\n/* a\n * comment\n */\nselect 2;\n";

assert_eq!(squawk_fmt::fmt_str(sql).unwrap(), expected);
assert_eq!(
squawk_fmt::fmt_str(sql, Default::default()).unwrap(),
expected
);
}

#[test]
fn preserves_a_leading_bom() {
let sql = "\u{feff}select 1;\n";
let expected = "\u{feff}select 1;\n";

let formatted = squawk_fmt::fmt_str(sql).unwrap();
let formatted = squawk_fmt::fmt_str(sql, Default::default()).unwrap();
assert_no_dropped_tokens(sql, &formatted);
assert_parses(&formatted);
assert_eq!(formatted, expected);
assert_eq!(squawk_fmt::fmt_str(&formatted).unwrap(), expected);
assert_eq!(
squawk_fmt::fmt_str(&formatted, Default::default()).unwrap(),
expected
);
}

fn assert_parses(formatted: &str) {
Expand Down
2 changes: 1 addition & 1 deletion crates/squawk_server/src/handlers/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub(crate) fn handle_formatting(
if !parse.errors().is_empty() {
return Ok(Some(Vec::new()));
}
let formatted = squawk_fmt::fmt(&parse.tree(), line_ending)?;
let formatted = squawk_fmt::fmt(&parse.tree(), line_ending, Default::default())?;

if formatted == content.as_ref() {
return Ok(Some(Vec::new()));
Expand Down
6 changes: 3 additions & 3 deletions crates/squawk_syntax/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ fn plpgsql_suite_score() {

assert_snapshot!(table, @"
file bodies tokens unparsed err
plpgsql.sql 254 9746 784 65
plpgsql.sql 254 9716 782 60
plpgsql_array.sql 26 913 0 0
plpgsql_cache.sql 2 59 10 1
plpgsql_call.sql 45 1591 7 1
Expand All @@ -282,8 +282,8 @@ fn plpgsql_suite_score() {
plpgsql_transaction.sql 37 1191 0 0
plpgsql_trap.sql 7 340 21 1
plpgsql_trigger.sql 1 55 0 0
plpgsql_varprops.sql 33 699 3 1
total 549 18673 877 75
plpgsql_varprops.sql 33 683 0 0
total 549 18627 872 69
");
}

Expand Down
2 changes: 1 addition & 1 deletion crates/squawk_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ impl SquawkDatabase {
if !parse.errors().is_empty() {
return Err(Error::new("Cannot format SQL with syntax errors."));
}
squawk_fmt::fmt(&parse.tree(), line_ending).map_err(into_error)
squawk_fmt::fmt(&parse.tree(), line_ending, Default::default()).map_err(into_error)
}

pub fn lint(&self) -> Result<JsValue, Error> {
Expand Down
12 changes: 12 additions & 0 deletions crates/xtask/src/sync_pg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ const START_END_MARKERS: &[(&str, &str, &str)] = &[
"-- => is disallowed as an operator name now",
");",
),
(
"plpgsql.sql",
" for r in select I fought the law, the law won LOOP",
" end loop;",
),
(
"plpgsql_varprops.sql",
" for x[1], y in select 1, 2 loop -- fail (currently, unsupported syntax)",
" end loop;",
),
];

const AFTER_START_END_MARKERS: &[(&str, &str, &str)] = &[(
Expand Down Expand Up @@ -179,6 +189,8 @@ const IGNORED_LINES: &[&str] = &[
// the psql variable this reads is set by a \set we comment out, so the body
// would otherwise parse as the literal string `dobody`
"DO LANGUAGE plpgsql :'dobody';",
" Johnny Yuma;",
" return query select 10 into no_such_table;",
];

const VARIABLE_REPLACEMENTS: &[(&str, &str)] = &[
Expand Down
4 changes: 2 additions & 2 deletions postgres/plpgsql/plpgsql_varprops.sql
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ end$$;
do $$
declare x constant int[]; y int;
begin
for x[1], y in select 1, 2 loop -- fail (currently, unsupported syntax)
end loop;
-- for x[1], y in select 1, 2 loop -- fail (currently, unsupported syntax)
-- end loop;
end$$;

do $$
Expand Down
10 changes: 5 additions & 5 deletions postgres/regression_suite/plpgsql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2138,17 +2138,17 @@ create function bad_sql1() returns int as $$
declare a int;
begin
a := 5;
Johnny Yuma;
-- Johnny Yuma;
a := 10;
return a;
end$$ language plpgsql;

create function bad_sql2() returns int as $$
declare r record;
begin
for r in select I fought the law, the law won LOOP
raise notice 'in loop';
end loop;
-- for r in select I fought the law, the law won LOOP
-- raise notice 'in loop';
-- end loop;
return 5;
end;$$ language plpgsql;

Expand Down Expand Up @@ -3584,7 +3584,7 @@ select * from rttest();
create or replace function rttest()
returns setof int as $$
begin
return query select 10 into no_such_table;
-- return query select 10 into no_such_table;
end;
$$ language plpgsql;

Expand Down
Loading