From 027ca226b9408b08c9f7c79b16c0368ef65e6d01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Tue, 4 Jun 2024 13:23:28 +0200 Subject: [PATCH 1/2] Add extra new line between queries --- src/SqlFormatter.php | 10 +++++++++- tests/clihighlight.txt | 7 +++++++ tests/format-highlight.html | 7 +++++++ tests/format.txt | 7 +++++++ 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/SqlFormatter.php b/src/SqlFormatter.php index b5d5b27..b22882d 100644 --- a/src/SqlFormatter.php +++ b/src/SqlFormatter.php @@ -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; diff --git a/tests/clihighlight.txt b/tests/clihighlight.txt index 8dec919..8df9a49 100644 --- a/tests/clihighlight.txt +++ b/tests/clihighlight.txt @@ -798,6 +798,7 @@ * LIMIT 1; + SELECT a, b, @@ -807,6 +808,7 @@ e LIMIT 1, 2; + SELECT 1, 2, @@ -1014,7 +1016,9 @@ --- -- semicolon must decrease special indentation level MY_NON_TOP_LEVEL_KEYWORD_FX_1(); + MY_NON_TOP_LEVEL_KEYWORD_FX_2(); + SELECT x FROM @@ -1022,11 +1026,14 @@ MY_NON_TOP_LEVEL_KEYWORD_FX_2(); 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 diff --git a/tests/format-highlight.html b/tests/format-highlight.html index d3bf857..d63a56a 100644 --- a/tests/format-highlight.html +++ b/tests/format-highlight.html @@ -798,6 +798,7 @@ * LIMIT 1; + SELECT a, b, @@ -807,6 +808,7 @@ e LIMIT 1, 2; + SELECT 1, 2, @@ -1014,7 +1016,9 @@ ---
-- semicolon must decrease special indentation level
 MY_NON_TOP_LEVEL_KEYWORD_FX_1();
+
 MY_NON_TOP_LEVEL_KEYWORD_FX_2();
+
 SELECT
   x
 FROM
@@ -1022,11 +1026,14 @@
     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
diff --git a/tests/format.txt b/tests/format.txt
index 55cb5c9..e2f5085 100644
--- a/tests/format.txt
+++ b/tests/format.txt
@@ -796,6 +796,7 @@ SELECT
   *
 LIMIT
   1;
+
 SELECT
   a,
   b,
@@ -805,6 +806,7 @@ FROM
   e
 LIMIT
   1, 2;
+
 SELECT
   1,
   2,
@@ -1012,7 +1014,9 @@ SELECT
 ---
 -- semicolon must decrease special indentation level
 MY_NON_TOP_LEVEL_KEYWORD_FX_1();
+
 MY_NON_TOP_LEVEL_KEYWORD_FX_2();
+
 SELECT
   x
 FROM
@@ -1020,11 +1024,14 @@ 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

From c0254f2a2a8c3a0d64113d11a42a6a3f99f0c908 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= 
Date: Tue, 4 Jun 2024 14:00:07 +0200
Subject: [PATCH 2/2] Also within query statement block

---
 src/SqlFormatter.php        | 15 +++++++++------
 tests/clihighlight.txt      |  5 +++++
 tests/format-highlight.html |  5 +++++
 tests/format.txt            |  5 +++++
 4 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/src/SqlFormatter.php b/src/SqlFormatter.php
index b22882d..00b23cb 100644
--- a/src/SqlFormatter.php
+++ b/src/SqlFormatter.php
@@ -86,7 +86,12 @@ public function format(string $string, string $indentString = '  '): string
                 return;
             }
 
-            $return = substr($return, 0, -($indentLevel + 1)) . str_repeat($tab, $indentLevel);
+            $rtrimLength = $indentLevel + 1;
+            while (substr($return, -($rtrimLength + 2), 1) === "\n") {
+                $rtrimLength++;
+            }
+
+            $return = substr($return, 0, -$rtrimLength) . str_repeat($tab, $indentLevel);
         };
 
         // Tokenize String
@@ -117,11 +122,9 @@ public function format(string $string, string $indentString = '  '): string
             if ($newline) {
                 $return = rtrim($return, ' ');
 
-                if ($indentLevel === 0) {
-                    $prevNotWhitespaceToken = $cursor->subCursor()->previous(Token::TOKEN_TYPE_WHITESPACE);
-                    if ($prevNotWhitespaceToken !== null && $prevNotWhitespaceToken->value() === ';') {
-                        $return .= "\n";
-                    }
+                $prevNotWhitespaceToken = $cursor->subCursor()->previous(Token::TOKEN_TYPE_WHITESPACE);
+                if ($prevNotWhitespaceToken !== null && $prevNotWhitespaceToken->value() === ';') {
+                    $return .= "\n";
                 }
 
                 $return      .= "\n" . str_repeat($tab, $indentLevel);
diff --git a/tests/clihighlight.txt b/tests/clihighlight.txt
index 8df9a49..1d86750 100644
--- a/tests/clihighlight.txt
+++ b/tests/clihighlight.txt
@@ -1031,6 +1031,7 @@ MY_NON_TOP_LEVEL_KEYWORD_FX_3();
 
 BEGIN
   MY_NON_TOP_LEVEL_KEYWORD_FX_4();
+
   MY_NON_TOP_LEVEL_KEYWORD_FX_5();
 END;
 
@@ -1042,6 +1043,7 @@ MY_NON_TOP_LEVEL_KEYWORD_FX_3();
       SELECT
         1 as x
     );
+
   MY_NON_TOP_LEVEL_KEYWORD_FX_6();
 END;
 ---
@@ -1150,16 +1152,19 @@ MY_NON_TOP_LEVEL_KEYWORD_FX_3();
   catch if ERROR_NUMBER() = 544 begin
     set
       IDENTITY_INSERT [t] on;
+
     begin
       try insert into [t] ([name], [int], [float], [null])
       values
         (N 'Ewa', 1, 1.0, null);
+
       set
         IDENTITY_INSERT [t] off;
     end try begin
       catch
       set
         IDENTITY_INSERT [t] off;
+
       throw;
     end catch
   end
diff --git a/tests/format-highlight.html b/tests/format-highlight.html
index d63a56a..9c273f9 100644
--- a/tests/format-highlight.html
+++ b/tests/format-highlight.html
@@ -1031,6 +1031,7 @@
 
 BEGIN
   MY_NON_TOP_LEVEL_KEYWORD_FX_4();
+
   MY_NON_TOP_LEVEL_KEYWORD_FX_5();
 END;
 
@@ -1042,6 +1043,7 @@
       SELECT
         1 as x
     );
+
   MY_NON_TOP_LEVEL_KEYWORD_FX_6();
 END;
--- @@ -1150,16 +1152,19 @@ catch if ERROR_NUMBER() = 544 begin set IDENTITY_INSERT [t] on; + begin try insert into [t] ([name], [int], [float], [null]) values (N 'Ewa', 1, 1.0, null); + set IDENTITY_INSERT [t] off; end try begin catch set IDENTITY_INSERT [t] off; + throw; end catch end diff --git a/tests/format.txt b/tests/format.txt index e2f5085..bead82e 100644 --- a/tests/format.txt +++ b/tests/format.txt @@ -1029,6 +1029,7 @@ MY_NON_TOP_LEVEL_KEYWORD_FX_3(); BEGIN MY_NON_TOP_LEVEL_KEYWORD_FX_4(); + MY_NON_TOP_LEVEL_KEYWORD_FX_5(); END; @@ -1040,6 +1041,7 @@ BEGIN SELECT 1 as x ); + MY_NON_TOP_LEVEL_KEYWORD_FX_6(); END; --- @@ -1148,16 +1150,19 @@ end try begin catch if ERROR_NUMBER() = 544 begin set IDENTITY_INSERT [t] on; + begin try insert into [t] ([name], [int], [float], [null]) values (N 'Ewa', 1, 1.0, null); + set IDENTITY_INSERT [t] off; end try begin catch set IDENTITY_INSERT [t] off; + throw; end catch end