diff --git a/src/SqlFormatter.php b/src/SqlFormatter.php index b5d5b27..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 @@ -115,7 +120,13 @@ 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, ' '); + + $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..1d86750 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,15 @@ 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 @@ -1035,6 +1043,7 @@ MY_NON_TOP_LEVEL_KEYWORD_FX_3(); SELECT 1 as x ); + MY_NON_TOP_LEVEL_KEYWORD_FX_6(); END; --- @@ -1143,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 d3bf857..9c273f9 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,15 @@
     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
@@ -1035,6 +1043,7 @@
       SELECT
         1 as x
     );
+
   MY_NON_TOP_LEVEL_KEYWORD_FX_6();
 END;
--- @@ -1143,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 55cb5c9..bead82e 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,15 @@ 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 @@ -1033,6 +1041,7 @@ BEGIN SELECT 1 as x ); + MY_NON_TOP_LEVEL_KEYWORD_FX_6(); END; --- @@ -1141,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