Skip to content

Commit

Permalink
Add extra new line between queries
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Jun 5, 2024
1 parent 15b7420 commit 027ca22
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/SqlFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,15 @@ public function format(string $string, string $indentString = ' '): string

// If we need a new line before the token
if ($newline) {
$return = rtrim($return, ' ');
$return = rtrim($return, ' ');

if ($indentLevel === 0) {
$prevNotWhitespaceToken = $cursor->subCursor()->previous(Token::TOKEN_TYPE_WHITESPACE);
if ($prevNotWhitespaceToken !== null && $prevNotWhitespaceToken->value() === ';') {
$return .= "\n";
}
}

$return .= "\n" . str_repeat($tab, $indentLevel);
$newline = false;
$addedNewline = true;
Expand Down
7 changes: 7 additions & 0 deletions tests/clihighlight.txt
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,7 @@
*
LIMIT
1;

SELECT
a,
b,
Expand All @@ -807,6 +808,7 @@
e
LIMIT
1, 2;

SELECT
1,
2,
Expand Down Expand Up @@ -1014,19 +1016,24 @@
---
-- semicolon must decrease special indentation level
MY_NON_TOP_LEVEL_KEYWORD_FX_1();

MY_NON_TOP_LEVEL_KEYWORD_FX_2();

SELECT
x
FROM
(
SELECT
1 as x
);

MY_NON_TOP_LEVEL_KEYWORD_FX_3();

BEGIN
MY_NON_TOP_LEVEL_KEYWORD_FX_4();
MY_NON_TOP_LEVEL_KEYWORD_FX_5();
END;

BEGIN
SELECT
x
Expand Down
7 changes: 7 additions & 0 deletions tests/format-highlight.html
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,7 @@
<span >*</span>
<span style="font-weight:bold;">LIMIT</span>
<span style="color: green;">1</span><span >;</span>

<span style="font-weight:bold;">SELECT</span>
<span style="color: #333;">a</span><span >,</span>
<span style="color: #333;">b</span><span >,</span>
Expand All @@ -807,6 +808,7 @@
<span style="color: #333;">e</span>
<span style="font-weight:bold;">LIMIT</span>
<span style="color: green;">1</span><span >,</span> <span style="color: green;">2</span><span >;</span>

<span style="font-weight:bold;">SELECT</span>
<span style="color: green;">1</span><span >,</span>
<span style="color: green;">2</span><span >,</span>
Expand Down Expand Up @@ -1014,19 +1016,24 @@
---
<pre style="color: black; background-color: white;"><span style="color: #aaa;">-- semicolon must decrease special indentation level</span>
<span style="color: #333;">MY_NON_TOP_LEVEL_KEYWORD_FX_1</span>()<span >;</span>

<span style="color: #333;">MY_NON_TOP_LEVEL_KEYWORD_FX_2</span>()<span >;</span>

<span style="font-weight:bold;">SELECT</span>
<span style="color: #333;">x</span>
<span style="font-weight:bold;">FROM</span>
(
<span style="font-weight:bold;">SELECT</span>
<span style="color: green;">1</span> <span style="font-weight:bold;">as</span> <span style="color: #333;">x</span>
)<span >;</span>

<span style="color: #333;">MY_NON_TOP_LEVEL_KEYWORD_FX_3</span>()<span >;</span>

<span style="font-weight:bold;">BEGIN</span>
<span style="color: #333;">MY_NON_TOP_LEVEL_KEYWORD_FX_4</span>()<span >;</span>
<span style="color: #333;">MY_NON_TOP_LEVEL_KEYWORD_FX_5</span>()<span >;</span>
<span style="font-weight:bold;">END</span><span >;</span>

<span style="font-weight:bold;">BEGIN</span>
<span style="font-weight:bold;">SELECT</span>
<span style="color: #333;">x</span>
Expand Down
7 changes: 7 additions & 0 deletions tests/format.txt
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,7 @@ SELECT
*
LIMIT
1;

SELECT
a,
b,
Expand All @@ -805,6 +806,7 @@ FROM
e
LIMIT
1, 2;

SELECT
1,
2,
Expand Down Expand Up @@ -1012,19 +1014,24 @@ SELECT
---
-- semicolon must decrease special indentation level
MY_NON_TOP_LEVEL_KEYWORD_FX_1();

MY_NON_TOP_LEVEL_KEYWORD_FX_2();

SELECT
x
FROM
(
SELECT
1 as x
);

MY_NON_TOP_LEVEL_KEYWORD_FX_3();

BEGIN
MY_NON_TOP_LEVEL_KEYWORD_FX_4();
MY_NON_TOP_LEVEL_KEYWORD_FX_5();
END;

BEGIN
SELECT
x
Expand Down

0 comments on commit 027ca22

Please sign in to comment.