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
433 changes: 346 additions & 87 deletions crates/squawk_fmt/src/fmt.rs

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions crates/squawk_fmt/tests/after/create_table.snap
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,27 @@ create table t(U&"c!006fl" uescape /* c */ '!' int);
create table foo /*a*/ /*b*/.bar(id int);
create table foo -- a line comment
.bar(id int);

-- comments between table args
create table t(
a int /*x*/,
b int
);
create table t(
/*a*/ a int,
/*b*/ b int /*c*/
);
create table t(
a int -- one
,
b int
);
create table t(
a int,
b int -- two

);

-- line comment before the semicolon
create table t(a int)-- one
;
16 changes: 16 additions & 0 deletions crates/squawk_fmt/tests/after/select_comments.snap
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,19 @@ select
1 -- a line comment
,
2;

-- line comments before the semicolon
select
1-- a
;
select
1-- b
-- c
;
select
1,
2-- d
;
select
1/*e*/-- f
;
137 changes: 137 additions & 0 deletions crates/squawk_fmt/tests/after/types.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
---
source: crates/squawk_fmt/tests/tests.rs
input_file: crates/squawk_fmt/tests/before/types.sql
---
-- keywords in types are lowercased
create table t(
a int,
b numeric(10, 2),
c pg_catalog.varchar(10),
d "MyType"
);

-- character types
create table t(
a varchar(10),
b character varying,
c national char varying(2),
d nchar(3),
e character(4),
f national character
);

-- bit & double types
create table t(
a bit,
b bit(4),
c bit varying,
d bit varying(3),
e double precision
);

-- date & time types
create table t(
a time,
b time(3) with time zone,
c timestamp without time zone,
d timestamp(6) with time zone
);

-- interval types
create table t(
a interval,
b interval(6),
c interval year,
d interval year to month,
e interval day to hour,
f interval hour to minute,
g interval second(2),
h interval day to second(3)
);

-- array types
create table t(
a int[],
b text array,
c text array[4],
d int[][3],
e int[10][10]
);

-- types in casts
select
1::int8,
cast(1 as int8),
treat(2 as bigint),
pg_catalog.varchar(10) 'foo';
select
'1'::interval day to second(3),
'a'::character varying(2),
now()::timestamp(3) with time zone;
select 1::setof int;

-- comments inside types
create table t(
a national /*a*/ char /*b*/ varying /*c*/(2),
b int /*d*/[],
c interval /*e*/ day to /*f*/ second /*g*/(3),
d numeric /*h*/(/*i*/ 10 /*j*/, /*k*/ 2 /*l*/),
e time /*m*/(/*n*/ 3 /*o*/) /*p*/ with /*q*/ time /*r*/ zone,
f int[ /*s*/][/*t*/ 3 /*u*/],
g timestamp /*v*/ without /*w*/ time /*x*/ zone,
h timestamp with /*y*/ time /*z*/ zone,
i double /*aa*/ precision,
j character /*bb*/ varying,
k national /*cc*/ character,
l bit /*dd*/ varying /*ee*/(/*ff*/ 3 /*gg*/),
m interval year /*hh*/ to /*ii*/ month
);
select 1::setof /*a*/ int, 2::pg_catalog /*b*/./*c*/ int4;

-- line comments inside types
create table t(
a int -- one
[],
b numeric -- two
(10),
c text -- three
array[2],
d time -- four
(3) with time zone,
e interval day to -- five
second(3)
);

-- interval literals keep their qualifier
select
interval '1' day to second(3),
interval '2' year to month,
interval(4) '3';

-- comments around casts
select 1 /*a*/::/*b*/ int8;
select cast /*c*/(/*d*/ 1 /*e*/ as /*f*/ int8 /*g*/);
select treat /*h*/(2 as /*i*/ bigint);
select pg_catalog.varchar(10) /*j*/ 'foo';
select interval '4' /*k*/ year to month;

-- line comments before a type's trailing keywords
create table t(
a double -- one
precision,
b bit -- two
varying(3),
c national -- three
character varying(2),
d varchar -- four
(10),
e timestamp without -- five
time zone,
f time(3) with time -- six
zone,
g interval year -- seven
to month
);
select
1::double -- eight
precision;
12 changes: 12 additions & 0 deletions crates/squawk_fmt/tests/before/create_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,15 @@ create table t (U&"c!006fl" uescape /* c */ '!' int);
create table foo/*a*//*b*/.bar (id int);
create table foo -- a line comment
. bar (id int);

-- comments between table args
create table t (a int /*x*/, b int);
create table t (/*a*/ a int, /*b*/ b int /*c*/);
create table t (a int -- one
, b int);
create table t (a int, b int -- two
);

-- line comment before the semicolon
create table t (a int) -- one
;
11 changes: 11 additions & 0 deletions crates/squawk_fmt/tests/before/select_comments.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,14 @@ select 1 /*a*/ /*b*/ group by 1;

select 1 -- a line comment
, 2;

-- line comments before the semicolon
select 1 -- a
;
select 1 -- b
-- c
;
select 1, 2 -- d
;
select 1 /*e*/ -- f
;
99 changes: 99 additions & 0 deletions crates/squawk_fmt/tests/before/types.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
-- keywords in types are lowercased
create table t (a INT, b NUMERIC(10, 2), c PG_CATALOG.VARCHAR(10), d "MyType");

-- character types
create table t (
a VARCHAR(10),
b CHARACTER VARYING,
c NATIONAL CHAR VARYING (2),
d NCHAR(3),
e CHARACTER(4),
f NATIONAL CHARACTER
);

-- bit & double types
create table t (a BIT, b BIT(4), c BIT VARYING, d BIT VARYING(3), e DOUBLE PRECISION);

-- date & time types
create table t (
a TIME,
b TIME(3) WITH TIME ZONE,
c TIMESTAMP WITHOUT TIME ZONE,
d TIMESTAMP(6) WITH TIME ZONE
);

-- interval types
create table t (
a INTERVAL,
b INTERVAL(6),
c INTERVAL YEAR,
d INTERVAL YEAR TO MONTH,
e INTERVAL DAY TO HOUR,
f INTERVAL HOUR TO MINUTE,
g INTERVAL SECOND(2),
h INTERVAL DAY TO SECOND(3)
);

-- array types
create table t (a INT[], b TEXT ARRAY, c TEXT ARRAY[4], d INT[][3], e INT[10][10]);

-- types in casts
select 1::int8, cast(1 as INT8), treat(2 as BIGINT), pg_catalog.varchar(10) 'foo';
select '1'::INTERVAL DAY TO SECOND(3), 'a'::CHARACTER VARYING(2), now()::TIMESTAMP(3) WITH TIME ZONE;
select 1::SETOF INT;

-- comments inside types
create table t (
a NATIONAL /*a*/ CHAR /*b*/ VARYING /*c*/ (2),
b INT /*d*/ [],
c INTERVAL /*e*/ DAY TO /*f*/ SECOND /*g*/ (3),
d NUMERIC /*h*/ ( /*i*/ 10 /*j*/ , /*k*/ 2 /*l*/ ),
e TIME /*m*/ ( /*n*/ 3 /*o*/ ) /*p*/ WITH /*q*/ TIME /*r*/ ZONE,
f INT [ /*s*/ ] [ /*t*/ 3 /*u*/ ],
g TIMESTAMP /*v*/ WITHOUT /*w*/ TIME /*x*/ ZONE,
h TIMESTAMP WITH /*y*/ TIME /*z*/ ZONE,
i DOUBLE /*aa*/ PRECISION,
j CHARACTER /*bb*/ VARYING,
k NATIONAL /*cc*/ CHARACTER,
l BIT /*dd*/ VARYING /*ee*/ ( /*ff*/ 3 /*gg*/ ),
m INTERVAL YEAR /*hh*/ TO /*ii*/ MONTH
);
select 1::SETOF /*a*/ INT, 2::pg_catalog /*b*/ . /*c*/ int4;

-- line comments inside types
create table t (a INT -- one
[], b NUMERIC -- two
(10), c TEXT -- three
ARRAY[2], d TIME -- four
(3) WITH TIME ZONE, e INTERVAL DAY TO -- five
SECOND(3));

-- interval literals keep their qualifier
select interval '1' day to second(3), interval '2' year to month, interval(4) '3';

-- comments around casts
select 1 /*a*/ :: /*b*/ INT8;
select cast /*c*/ ( /*d*/ 1 /*e*/ as /*f*/ INT8 /*g*/ );
select treat /*h*/ ( 2 as /*i*/ BIGINT );
select pg_catalog.varchar(10) /*j*/ 'foo';
select interval '4' /*k*/ year to month;

-- line comments before a type's trailing keywords
create table t (
a DOUBLE -- one
PRECISION,
b BIT -- two
VARYING(3),
c NATIONAL -- three
CHARACTER VARYING(2),
d VARCHAR -- four
(10),
e TIMESTAMP WITHOUT -- five
TIME ZONE,
f TIME(3) WITH TIME -- six
ZONE,
g INTERVAL YEAR -- seven
TO MONTH
);
select 1::DOUBLE -- eight
PRECISION;
Loading
Loading