Skip to content

Commit 2cfcbc5

Browse files
committed
feat: Extend the span statistics
And use it for deciding when to inline single/simple blocks
1 parent b91ff52 commit 2cfcbc5

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

src/formatter.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,15 @@ impl<'a> Formatter<'a> {
233233
self.add_new_line(query);
234234
}
235235
query.push_str(&self.equalize_whitespace(&self.format_reserved_word(token.value)));
236-
if newline_after && token.alias != "CREATE" {
236+
237+
if newline_after
238+
&& token.alias != "CREATE"
239+
&& (self.options.max_inline_top_level.is_none()
240+
|| self
241+
.next_non_whitespace_token(1)
242+
.is_some_and(|t| t.kind != TokenKind::OpenParen)
243+
|| span_info.arguments != 0)
244+
{
237245
self.indentation.increase_top_level(span_info);
238246
self.add_new_line(query);
239247
} else {
@@ -553,6 +561,17 @@ impl<'a> Formatter<'a> {
553561
}
554562
}
555563

564+
fn next_non_whitespace_token(&self, idx: usize) -> Option<&Token<'_>> {
565+
let index = self.index.checked_add(idx);
566+
if let Some(index) = index {
567+
self.tokens[index..]
568+
.iter()
569+
.find(|t| t.kind != TokenKind::Whitespace)
570+
} else {
571+
None
572+
}
573+
}
574+
556575
fn top_level_tokens_info(&self) -> SpanInfo {
557576
let mut block_level = self.block_level;
558577
let mut full_span = 0;

src/lib.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -688,12 +688,11 @@ mod tests {
688688
"
689689
INSERT INTO t(id, a, min, max)
690690
SELECT input.id, input.a, input.min, input.max
691-
FROM
692-
(
693-
SELECT id, a, min, max
694-
FROM foo
695-
WHERE a IN ('a', 'b')
696-
) AS input
691+
FROM (
692+
SELECT id, a, min, max
693+
FROM foo
694+
WHERE a IN ('a', 'b')
695+
) AS input
697696
WHERE (SELECT true FROM condition)
698697
ON CONFLICT ON CONSTRAINT a_id_key DO UPDATE SET
699698
id = EXCLUDED.id,
@@ -2283,8 +2282,7 @@ mod tests {
22832282
cc AS (
22842283
INSERT INTO
22852284
C (a, b, c, d)
2286-
VALUES
2287-
(1, 2, 3, 4)
2285+
VALUES (1, 2, 3, 4)
22882286
)
22892287
SELECT b, field
22902288
FROM a, aa;"

0 commit comments

Comments
 (0)