diff --git a/src/SqlFormatter.php b/src/SqlFormatter.php index 064fd58..0f6a0d2 100644 --- a/src/SqlFormatter.php +++ b/src/SqlFormatter.php @@ -256,6 +256,15 @@ public function format(string $string, string $indentString = ' '): string if ($token->value() === 'LIMIT' && ! $inlineParentheses) { $clauseLimit = true; } + } elseif ($token->value() === ';') { + // If the last indent type was 'special', decrease the special indent for this round + reset($indentTypes); + if (current($indentTypes) === 'special') { + $indentLevel--; + array_shift($indentTypes); + } + + $newline = true; } elseif ( $clauseLimit && $token->value() !== ',' && diff --git a/tests/clihighlight.txt b/tests/clihighlight.txt index 49032f2..ab3bb04 100644 --- a/tests/clihighlight.txt +++ b/tests/clihighlight.txt @@ -1009,3 +1009,28 @@ --- SELECT 1 :: text; +--- +-- 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 +FROM + ( + SELECT + 1 as x + ); +MY_NON_TOP_LEVEL_KEYWORD_FX_6(); +END; diff --git a/tests/compress.txt b/tests/compress.txt index 70266fa..5805fce 100644 --- a/tests/compress.txt +++ b/tests/compress.txt @@ -93,3 +93,5 @@ WITH cte1 AS (SELECT a, b FROM table1), cte2 AS (SELECT c, d FROM table2) SELECT SELECT a, GROUP_CONCAT(b, '.') OVER (ORDER BY c GROUPS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW EXCLUDE NO OTHERS) AS no_others, GROUP_CONCAT(b, '.') OVER (ORDER BY c GROUPS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW EXCLUDE CURRENT ROW) AS current_row, GROUP_CONCAT(b, '.') OVER (ORDER BY c GROUPS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW EXCLUDE GROUP) AS grp, GROUP_CONCAT(b, '.') OVER (ORDER BY c GROUPS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW EXCLUDE TIES) AS tie, GROUP_CONCAT(b, '.') FILTER (WHERE c != 'two') OVER (ORDER BY a) AS filtered, CONVERT(VARCHAR(20), AVG(SalesYTD) OVER (PARTITION BY TerritoryID ORDER BY DATEPART(yy, ModifiedDate)), 1) AS MovingAvg, AVG(starting_salary) OVER w2 AVG, MIN(starting_salary) OVER w2 MIN_STARTING_SALARY, MAX(starting_salary) OVER (w1 ORDER BY hire_date), LISTAGG(arg, ',') OVER (PARTITION BY part ORDER BY ord ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS LISTAGG_ROWS, LISTAGG(arg, ',') OVER (PARTITION BY part ORDER BY ord RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS LISTAGG_RANGE, MIN(Revenue) OVER (PARTITION BY DepartmentID ORDER BY RevenueYear ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) AS MinRevenueBeyond FROM t1 WINDOW w1 AS (PARTITION BY department, division), w2 AS (w1 ORDER BY hire_date); --- SELECT 1::text; +--- +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 FROM (SELECT 1 as x); MY_NON_TOP_LEVEL_KEYWORD_FX_6(); END; diff --git a/tests/format-highlight.html b/tests/format-highlight.html index af708fa..106d5a2 100644 --- a/tests/format-highlight.html +++ b/tests/format-highlight.html @@ -1009,3 +1009,28 @@ ---
SELECT
   1 :: text;
+--- +
-- 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
+FROM
+  (
+    SELECT
+      1 as x
+  );
+MY_NON_TOP_LEVEL_KEYWORD_FX_6();
+END;
diff --git a/tests/format.txt b/tests/format.txt index fbf9f18..a41ffd3 100644 --- a/tests/format.txt +++ b/tests/format.txt @@ -1007,3 +1007,28 @@ WINDOW --- SELECT 1 :: text; +--- +-- 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 +FROM + ( + SELECT + 1 as x + ); +MY_NON_TOP_LEVEL_KEYWORD_FX_6(); +END; diff --git a/tests/highlight.html b/tests/highlight.html index b722fdd..fe4fcb3 100644 --- a/tests/highlight.html +++ b/tests/highlight.html @@ -304,3 +304,21 @@ WINDOW w1 AS (PARTITION BY department, division), w2 AS (w1 ORDER BY hire_date); ---
SELECT 1::text;
+--- +
-- 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
+  FROM
+    (SELECT 1 as x);
+  MY_NON_TOP_LEVEL_KEYWORD_FX_6();
+END;
diff --git a/tests/sql.sql b/tests/sql.sql index 8e4a025..267a504 100644 --- a/tests/sql.sql +++ b/tests/sql.sql @@ -304,3 +304,21 @@ FROM t1 WINDOW w1 AS (PARTITION BY department, division), w2 AS (w1 ORDER BY hire_date); --- SELECT 1::text; +--- +-- 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 + FROM + (SELECT 1 as x); + MY_NON_TOP_LEVEL_KEYWORD_FX_6(); +END;