Skip to content

Commit b251c7c

Browse files
lu-zeroshssoichiro
authored andcommitted
fix: Place a whitespace between the array type specifier and a reserved word
1 parent 7d3ae8c commit b251c7c

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/formatter.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,19 @@ impl<'a> Formatter<'a> {
200200
}
201201

202202
fn format_type_specifier(&self, token: &Token<'_>, query: &mut String) {
203+
const WHITESPACE_BEFORE: &[TokenKind] = &[
204+
TokenKind::Reserved,
205+
TokenKind::ReservedNewline,
206+
TokenKind::ReservedNewlineAfter,
207+
];
203208
self.trim_all_spaces_end(query);
204209
query.push_str(token.value);
210+
if self
211+
.next_non_whitespace_token(1)
212+
.is_some_and(|t| WHITESPACE_BEFORE.contains(&t.kind))
213+
{
214+
query.push(' ')
215+
}
205216
}
206217
fn format_block_comment(&mut self, token: &Token<'_>, query: &mut String) {
207218
self.add_new_line(query);
@@ -573,6 +584,17 @@ impl<'a> Formatter<'a> {
573584
}
574585
}
575586

587+
fn next_non_whitespace_token(&self, idx: usize) -> Option<&Token<'_>> {
588+
let index = self.index.checked_add(idx);
589+
if let Some(index) = index {
590+
self.tokens[index..]
591+
.iter()
592+
.find(|t| t.kind != TokenKind::Whitespace)
593+
} else {
594+
None
595+
}
596+
}
597+
576598
fn next_token(&self, idx: usize) -> Option<&Token<'_>> {
577599
let index = self.index.checked_add(idx);
578600
if let Some(index) = index {

src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -471,15 +471,17 @@ mod tests {
471471

472472
#[test]
473473
fn it_formats_type_specifiers() {
474-
let input = "SELECT id, ARRAY [] :: UUID [] FROM UNNEST($1 :: UUID []);";
474+
let input = "SELECT id, ARRAY [] :: UUID [] FROM UNNEST($1 :: UUID []) WHERE $1::UUID[] IS NOT NULL;";
475475
let options = FormatOptions::default();
476476
let expected = indoc!(
477477
"
478478
SELECT
479479
id,
480480
ARRAY[]::UUID[]
481481
FROM
482-
UNNEST($1::UUID[]);"
482+
UNNEST($1::UUID[])
483+
WHERE
484+
$1::UUID[] IS NOT NULL;"
483485
);
484486

485487
assert_eq!(format(input, &QueryParams::None, &options), expected);

0 commit comments

Comments
 (0)