Skip to content

Commit cdc0732

Browse files
authored
More inline fixes (#109)
This PR addresses several edge cases in the inline formatting logic, specifically: - Handling of line comments forcing newlines in reserved words - Improving fold-in detection for top-level blocks with more complex patterns - Fixing comparison operator for inline argument limits (>= vs >) - Refining newline logic for top-level keywords based on argument counts
1 parent e47d31f commit cdc0732

File tree

3 files changed

+43
-22
lines changed

3 files changed

+43
-22
lines changed

src/formatter.rs

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -332,12 +332,30 @@ impl<'a> Formatter<'a> {
332332
}
333333

334334
fn format_newline_reserved_word(&mut self, token: &Token<'_>, query: &mut String) {
335-
if !self.inline_block.is_active()
336-
&& self
337-
.options
338-
.max_inline_arguments
339-
.is_none_or(|limit| limit < self.indentation.span())
335+
// line comments force a newline
336+
let after_line_comment = self
337+
.previous_non_whitespace_token(1)
338+
.is_some_and(|t| t.kind == TokenKind::LineComment);
339+
340+
if after_line_comment
341+
|| !self.inline_block.is_active()
342+
&& self
343+
.options
344+
.max_inline_arguments
345+
.is_none_or(|limit| limit < self.indentation.span())
340346
{
347+
// We inlined something to the top level let's increase the indentation now
348+
if let Some((_, s)) = self.indentation.previous_top_level_reserved() {
349+
if !s.newline_after {
350+
self.indentation.increase_top_level(s.clone());
351+
}
352+
}
353+
354+
// let's undo the line comment indentation
355+
if after_line_comment {
356+
self.trim_spaces_end(query);
357+
}
358+
341359
self.add_new_line(query);
342360
} else {
343361
self.trim_spaces_end(query);
@@ -373,14 +391,15 @@ impl<'a> Formatter<'a> {
373391
let previous_non_whitespace_token = self.previous_non_whitespace_token(1);
374392
let fold_in_top_level = !inlined
375393
&& self.options.max_inline_top_level.is_some()
376-
&& self
377-
.previous_non_whitespace_token(1)
378-
.is_some_and(|t| t.kind == TokenKind::ReservedTopLevel)
394+
&& previous_non_whitespace_token.is_some_and(|t| t.kind == TokenKind::ReservedTopLevel)
379395
&& self
380396
.indentation
381397
.previous_top_level_reserved()
382398
.is_some_and(|(_, span)| {
383-
span.blocks == 1 && span.newline_after && span.arguments == 1
399+
// We can have the two following situations
400+
// top level ( block )
401+
// top level ( block ) AS word (args, ...)
402+
span.blocks <= 2 && span.newline_after && span.arguments == 1
384403
});
385404

386405
// Take out the preceding space unless there was whitespace there in the original query
@@ -503,7 +522,7 @@ impl<'a> Formatter<'a> {
503522

504523
if let Some((_, span)) = self.indentation.previous_top_level_reserved() {
505524
let limit = self.options.max_inline_arguments.unwrap_or(0);
506-
if limit > span.full_span {
525+
if limit >= span.full_span {
507526
return;
508527
}
509528
}
@@ -700,18 +719,19 @@ impl<'a> Formatter<'a> {
700719
full_span += token.value.len();
701720
}
702721

722+
let limit = self.options.max_inline_top_level.unwrap_or(0);
703723
// if we are inside an inline block we decide our behaviour as if were inline
704724
let block_len = self.inline_block.cur_len();
705-
let (newline_before, newline_after) = if block_len > 0 {
706-
let limit = self.options.max_inline_top_level.unwrap_or(0);
707-
(limit < block_len, limit < full_span)
725+
726+
let newline_before = block_len == 0 || limit < block_len;
727+
728+
// if we are going to format a list of arguments take in account also the limit for
729+
// arguments
730+
let arguments_limit = self.options.max_inline_arguments.unwrap_or(0);
731+
let newline_after = if arguments > 1 && arguments_limit != 0 {
732+
arguments_limit.min(limit) < full_span
708733
} else {
709-
(
710-
true,
711-
self.options
712-
.max_inline_top_level
713-
.is_none_or(|limit| limit < full_span),
714-
)
734+
limit < full_span
715735
};
716736

717737
SpanInfo {

src/indentation.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ impl<'a> Indentation<'a> {
7676
folded = kind == Some(IndentType::FoldedBlock);
7777
if kind != Some(IndentType::Top) {
7878
break;
79+
} else {
80+
self.top_level_span.pop();
7981
}
8082
}
8183
folded

src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2277,8 +2277,7 @@ mod tests {
22772277
FROM table
22782278
)
22792279
SELECT
2280-
b,
2281-
field
2280+
b, field
22822281
FROM a, aa;"
22832282
};
22842283
assert_eq!(format(input, &QueryParams::None, &options), expected);
@@ -2501,7 +2500,7 @@ from
25012500
SELECT true
25022501
FROM bar
25032502
WHERE bar.foo = $99
2504-
AND bar.foo > $100
2503+
AND bar.foo > $100
25052504
),
25062505
c = CASE WHEN $6 THEN NULL ELSE COALESCE($7, c) END,
25072506
d = CASE

0 commit comments

Comments
 (0)