Skip to content

Commit da0183b

Browse files
committed
Deal with some corner-case situation about inlining top level
1 parent f5e9010 commit da0183b

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

src/formatter.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -344,14 +344,15 @@ impl<'a> Formatter<'a> {
344344
let previous_non_whitespace_token = self.previous_non_whitespace_token(1);
345345
let fold_in_top_level = !inlined
346346
&& self.options.max_inline_top_level.is_some()
347-
&& self
348-
.previous_non_whitespace_token(1)
349-
.is_some_and(|t| t.kind == TokenKind::ReservedTopLevel)
347+
&& previous_non_whitespace_token.is_some_and(|t| t.kind == TokenKind::ReservedTopLevel)
350348
&& self
351349
.indentation
352350
.previous_top_level_reserved()
353351
.is_some_and(|(_, span)| {
354-
span.blocks == 1 && span.newline_after && span.arguments == 1
352+
// We can have the two following situations
353+
// top level ( block )
354+
// top level ( block ) AS word (args, ...)
355+
span.blocks <= 2 && span.newline_after && span.arguments == 1
355356
});
356357

357358
// Take out the preceding space unless there was whitespace there in the original query
@@ -474,7 +475,7 @@ impl<'a> Formatter<'a> {
474475

475476
if let Some((_, span)) = self.indentation.previous_top_level_reserved() {
476477
let limit = self.options.max_inline_arguments.unwrap_or(0);
477-
if limit > span.full_span {
478+
if limit >= span.full_span {
478479
return;
479480
}
480481
}
@@ -671,18 +672,19 @@ impl<'a> Formatter<'a> {
671672
full_span += token.value.len();
672673
}
673674

675+
let limit = self.options.max_inline_top_level.unwrap_or(0);
674676
// if we are inside an inline block we decide our behaviour as if were inline
675677
let block_len = self.inline_block.cur_len();
676-
let (newline_before, newline_after) = if block_len > 0 {
677-
let limit = self.options.max_inline_top_level.unwrap_or(0);
678-
(limit < block_len, limit < full_span)
678+
679+
let newline_before = block_len == 0 || limit < block_len;
680+
681+
// if we are going to format a list of arguments take in account also the limit for
682+
// arguments
683+
let arguments_limit = self.options.max_inline_arguments.unwrap_or(0);
684+
let newline_after = if arguments > 1 && arguments_limit != 0 {
685+
arguments_limit.min(limit) < full_span
679686
} else {
680-
(
681-
true,
682-
self.options
683-
.max_inline_top_level
684-
.is_none_or(|limit| limit < full_span),
685-
)
687+
limit < full_span
686688
};
687689

688690
SpanInfo {

src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2353,8 +2353,7 @@ mod tests {
23532353
FROM table
23542354
)
23552355
SELECT
2356-
b,
2357-
field
2356+
b, field
23582357
FROM a, aa;"
23592358
};
23602359
assert_eq!(format(input, &QueryParams::None, &options), expected);

0 commit comments

Comments
 (0)