File tree Expand file tree Collapse file tree 2 files changed +26
-2
lines changed
Expand file tree Collapse file tree 2 files changed +26
-2
lines changed Original file line number Diff line number Diff 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) ;
@@ -581,6 +592,17 @@ impl<'a> Formatter<'a> {
581592 }
582593 }
583594
595+ fn next_non_whitespace_token ( & self , idx : usize ) -> Option < & Token < ' _ > > {
596+ let index = self . index . checked_add ( idx) ;
597+ if let Some ( index) = index {
598+ self . tokens [ index..]
599+ . iter ( )
600+ . find ( |t| t. kind != TokenKind :: Whitespace )
601+ } else {
602+ None
603+ }
604+ }
605+
584606 fn next_token ( & self , idx : usize ) -> Option < & Token < ' _ > > {
585607 let index = self . index . checked_add ( idx) ;
586608 if let Some ( index) = index {
Original file line number Diff line number Diff 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) ;
You can’t perform that action at this time.
0 commit comments