Skip to content

Commit 762690d

Browse files
committed
Deal with some corner-case situation about inlining top level
1 parent 104a3ad commit 762690d

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

src/formatter.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::borrow::Cow;
2+
use std::usize;
23

34
use crate::indentation::Indentation;
45
use crate::inline_block::InlineBlock;
@@ -344,14 +345,15 @@ impl<'a> Formatter<'a> {
344345
let previous_non_whitespace_token = self.previous_non_whitespace_token(1);
345346
let fold_in_top_level = !inlined
346347
&& 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)
348+
&& previous_non_whitespace_token.is_some_and(|t| t.kind == TokenKind::ReservedTopLevel)
350349
&& self
351350
.indentation
352351
.previous_top_level_reserved()
353352
.is_some_and(|(_, span)| {
354-
span.blocks == 1 && span.newline_after && span.arguments == 1
353+
// We can have the two following situations
354+
// top level ( block )
355+
// top level ( block ) AS word (args, ...)
356+
span.blocks <= 2 && span.newline_after && span.arguments == 1
355357
});
356358

357359
// Take out the preceding space unless there was whitespace there in the original query
@@ -472,9 +474,9 @@ impl<'a> Formatter<'a> {
472474
return;
473475
}
474476

475-
if let Some((_, span)) = self.indentation.previous_top_level_reserved() {
477+
if let Some((t, span)) = self.indentation.previous_top_level_reserved() {
476478
let limit = self.options.max_inline_arguments.unwrap_or(0);
477-
if limit > span.full_span {
479+
if limit >= span.full_span {
478480
return;
479481
}
480482
}
@@ -671,18 +673,19 @@ impl<'a> Formatter<'a> {
671673
full_span += token.value.len();
672674
}
673675

676+
let limit = self.options.max_inline_top_level.unwrap_or(0);
674677
// if we are inside an inline block we decide our behaviour as if were inline
675678
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)
679+
680+
let newline_before = block_len == 0 || limit < block_len;
681+
682+
// if we are going to format a list of arguments take in account also the limit for
683+
// arguments
684+
let arguments_limit = self.options.max_inline_arguments.unwrap_or(0);
685+
let newline_after = if arguments > 1 && arguments_limit != 0 {
686+
arguments_limit.min(limit) < full_span
679687
} else {
680-
(
681-
true,
682-
self.options
683-
.max_inline_top_level
684-
.is_none_or(|limit| limit < full_span),
685-
)
688+
limit < full_span
686689
};
687690

688691
SpanInfo {

0 commit comments

Comments
 (0)