@@ -90,10 +90,12 @@ pub(crate) fn format(
90
90
TokenKind :: ReservedTopLevel => {
91
91
formatter. format_top_level_reserved_word ( token, & mut formatted_query) ;
92
92
formatter. previous_reserved_word = Some ( token) ;
93
+ formatter. previous_top_level_reserved_word = Some ( token) ;
93
94
}
94
95
TokenKind :: ReservedTopLevelNoIndent => {
95
96
formatter. format_top_level_reserved_word_no_indent ( token, & mut formatted_query) ;
96
97
formatter. previous_reserved_word = Some ( token) ;
98
+ formatter. previous_top_level_reserved_word = Some ( token) ;
97
99
}
98
100
TokenKind :: ReservedNewline => {
99
101
formatter. format_newline_reserved_word ( token, & mut formatted_query) ;
@@ -140,27 +142,34 @@ pub(crate) fn format(
140
142
struct Formatter < ' a > {
141
143
index : usize ,
142
144
previous_reserved_word : Option < & ' a Token < ' a > > ,
145
+ previous_top_level_reserved_word : Option < & ' a Token < ' a > > ,
143
146
tokens : & ' a [ Token < ' a > ] ,
144
147
params : Params < ' a > ,
145
148
options : & ' a FormatOptions < ' a > ,
146
149
indentation : Indentation < ' a > ,
147
150
inline_block : InlineBlock ,
151
+ line_start : usize ,
148
152
}
149
153
150
154
impl < ' a > Formatter < ' a > {
151
155
fn new ( tokens : & ' a [ Token < ' a > ] , params : & ' a QueryParams , options : & ' a FormatOptions ) -> Self {
152
156
Formatter {
153
157
index : 0 ,
154
158
previous_reserved_word : None ,
159
+ previous_top_level_reserved_word : None ,
155
160
tokens,
156
161
params : Params :: new ( params) ,
157
162
options,
158
163
indentation : Indentation :: new ( options) ,
159
- inline_block : InlineBlock :: new ( ) ,
164
+ inline_block : InlineBlock :: new (
165
+ options. max_inline_block ,
166
+ options. max_inline_arguments . is_none ( ) ,
167
+ ) ,
168
+ line_start : 0 ,
160
169
}
161
170
}
162
171
163
- fn format_line_comment ( & self , token : & Token < ' _ > , query : & mut String ) {
172
+ fn format_line_comment ( & mut self , token : & Token < ' _ > , query : & mut String ) {
164
173
let is_whitespace_followed_by_special_token =
165
174
self . next_token ( 1 ) . map_or ( false , |current_token| {
166
175
current_token. kind == TokenKind :: Whitespace
@@ -189,7 +198,7 @@ impl<'a> Formatter<'a> {
189
198
self . trim_all_spaces_end ( query) ;
190
199
query. push_str ( "::" ) ;
191
200
}
192
- fn format_block_comment ( & self , token : & Token < ' _ > , query : & mut String ) {
201
+ fn format_block_comment ( & mut self , token : & Token < ' _ > , query : & mut String ) {
193
202
self . add_new_line ( query) ;
194
203
query. push_str ( & self . indent_comment ( token. value ) ) ;
195
204
self . add_new_line ( query) ;
@@ -200,7 +209,16 @@ impl<'a> Formatter<'a> {
200
209
self . add_new_line ( query) ;
201
210
self . indentation . increase_top_level ( ) ;
202
211
query. push_str ( & self . equalize_whitespace ( & self . format_reserved_word ( token. value ) ) ) ;
203
- self . add_new_line ( query) ;
212
+ let len = self . top_level_tokens_span ( ) ;
213
+ if self
214
+ . options
215
+ . max_inline_top_level
216
+ . map_or ( true , |limit| limit < len)
217
+ {
218
+ self . add_new_line ( query) ;
219
+ } else {
220
+ query. push ( ' ' ) ;
221
+ }
204
222
}
205
223
206
224
fn format_top_level_reserved_word_no_indent ( & mut self , token : & Token < ' _ > , query : & mut String ) {
@@ -210,8 +228,17 @@ impl<'a> Formatter<'a> {
210
228
self . add_new_line ( query) ;
211
229
}
212
230
213
- fn format_newline_reserved_word ( & self , token : & Token < ' _ > , query : & mut String ) {
214
- self . add_new_line ( query) ;
231
+ fn format_newline_reserved_word ( & mut self , token : & Token < ' _ > , query : & mut String ) {
232
+ if self
233
+ . options
234
+ . max_inline_arguments
235
+ . map_or ( true , |limit| limit < self . line_len_next ( query) )
236
+ {
237
+ self . add_new_line ( query) ;
238
+ } else {
239
+ self . trim_spaces_end ( query) ;
240
+ query. push ( ' ' ) ;
241
+ }
215
242
query. push_str ( & self . equalize_whitespace ( & self . format_reserved_word ( token. value ) ) ) ;
216
243
query. push ( ' ' ) ;
217
244
}
@@ -303,7 +330,13 @@ impl<'a> Formatter<'a> {
303
330
304
331
if self . inline_block . is_active ( ) {
305
332
self . inline_block . end ( ) ;
306
- self . format_with_space_after ( & token, query) ;
333
+ if token. value . to_lowercase ( ) == "end" {
334
+ self . trim_spaces_end ( query) ;
335
+ query. push ( ' ' ) ;
336
+ self . format_with_spaces ( & token, query) ;
337
+ } else {
338
+ self . format_with_space_after ( & token, query) ;
339
+ }
307
340
} else {
308
341
self . indentation . decrease_block_level ( ) ;
309
342
self . add_new_line ( query) ;
@@ -317,7 +350,7 @@ impl<'a> Formatter<'a> {
317
350
}
318
351
319
352
// Commas start a new line (unless within inline parentheses or SQL "LIMIT" clause)
320
- fn format_comma ( & self , token : & Token < ' _ > , query : & mut String ) {
353
+ fn format_comma ( & mut self , token : & Token < ' _ > , query : & mut String ) {
321
354
self . trim_spaces_end ( query) ;
322
355
query. push_str ( token. value ) ;
323
356
query. push ( ' ' ) ;
@@ -332,6 +365,12 @@ impl<'a> Formatter<'a> {
332
365
{
333
366
return ;
334
367
}
368
+
369
+ if matches ! ( ( self . previous_top_level_reserved_word, self . options. max_inline_arguments) ,
370
+ ( Some ( word) , Some ( limit) ) if word. value. to_lowercase( ) == "select" && limit > self . line_len_next( query) )
371
+ {
372
+ return ;
373
+ }
335
374
self . add_new_line ( query) ;
336
375
}
337
376
@@ -355,11 +394,16 @@ impl<'a> Formatter<'a> {
355
394
}
356
395
}
357
396
358
- fn add_new_line ( & self , query : & mut String ) {
397
+ fn add_new_line ( & mut self , query : & mut String ) {
359
398
self . trim_spaces_end ( query) ;
399
+ if self . options . inline {
400
+ query. push ( ' ' ) ;
401
+ return ;
402
+ }
360
403
if !query. ends_with ( '\n' ) {
361
404
query. push ( '\n' ) ;
362
405
}
406
+ self . line_start = query. len ( ) ;
363
407
query. push_str ( & self . indentation . get_indent ( ) ) ;
364
408
}
365
409
@@ -446,6 +490,20 @@ impl<'a> Formatter<'a> {
446
490
}
447
491
}
448
492
493
+ fn line_len_next ( & self , query : & str ) -> usize {
494
+ query. len ( ) - self . line_start + self . next_token ( 1 ) . map_or ( 0 , |t| t. value . len ( ) )
495
+ }
496
+
497
+ fn top_level_tokens_span ( & self ) -> usize {
498
+ assert_eq ! ( self . tokens[ self . index] . kind, TokenKind :: ReservedTopLevel ) ;
499
+
500
+ self . tokens [ self . index ..]
501
+ . iter ( )
502
+ . skip ( 1 )
503
+ . map ( |token| token. value . len ( ) )
504
+ . sum ( )
505
+ }
506
+
449
507
fn format_no_change ( & self , token : & Token < ' _ > , query : & mut String ) {
450
508
query. push_str ( token. value ) ;
451
509
}
0 commit comments