Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions src/SqlFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public function format(string $string, string $indentString = ' '): string
$inlineCount = 0;
$inlineIndented = false;
$clauseLimit = false;
$inIfCondition = false;

$appendNewLineIfNotAddedFx = static function () use (&$addedNewline, &$return, $tab, &$indentLevel): void {
// Add a newline if not already added
Expand Down Expand Up @@ -301,8 +302,8 @@ public function format(string $string, string $indentString = ' '): string
$newline = true;
$increaseBlockIndent = true;
}
} elseif (in_array($tokenValueUpper, ['WHEN', 'THEN', 'ELSE', 'END'], true)) {
if ($tokenValueUpper !== 'THEN') {
} elseif (in_array($tokenValueUpper, ['IF', 'WHEN', 'THEN', 'ELSE', 'ELSEIF', 'ELSIF', 'END'], true)) {
if ($tokenValueUpper !== 'THEN' && $tokenValueUpper !== 'IF') {
$decreaseIndentationLevelFx();

if ($prevNotWhitespaceToken !== null && strtoupper($prevNotWhitespaceToken->value()) !== 'CASE') {
Expand All @@ -314,6 +315,20 @@ public function format(string $string, string $indentString = ' '): string
$newline = true;
$increaseBlockIndent = true;
}

// Track IF condition context only for IF/ELSEIF that are part of conditional blocks
// (not for "IF()" function calls)
if (in_array($tokenValueUpper, ['IF', 'ELSEIF', 'ELSIF'], true)) {
// Check if this IF is part of a conditional block by looking at the next token
$nextToken = $cursor->subCursor()->next(Token::TOKEN_TYPE_WHITESPACE);
if (
$nextToken !== null && $nextToken->value() !== '('
) {
$inIfCondition = true;
}
} elseif ($tokenValueUpper === 'THEN' || $tokenValueUpper === 'ELSE' || $tokenValueUpper === 'END') {
$inIfCondition = false;
}
} elseif (
$clauseLimit &&
$token->value() !== ',' &&
Expand All @@ -332,9 +347,12 @@ public function format(string $string, string $indentString = ' '): string
$newline = true;
}
} elseif ($token->isOfType(Token::TOKEN_TYPE_RESERVED_NEWLINE)) {
// Newline reserved words start a new line
// Newline reserved words start a new line, except for AND/OR within IF conditions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are more contexts where boolean expressions can be used the same way, at least CASE WHEN. Not sure if this optimization is welcomed in this PR.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

The behavior is now:

  • IF/ELSEIF/ELSIF conditions: AND/OR stay on the same line
  • CASE WHEN conditions: AND/OR stay on the same line
  • WHERE/HAVING clauses: AND/OR still get newlines (unchanged - this is probably desired for readability)
  • JOIN ON conditions: AND/OR still get newlines (unchanged)


$appendNewLineIfNotAddedFx();
// Skip newlines for AND/OR when inside IF condition
if (! $inIfCondition || ! in_array($tokenValueUpper, ['AND', 'OR'], true)) {
$appendNewLineIfNotAddedFx();
}

if ($token->hasExtraWhitespace()) {
$highlighted = preg_replace('/\s+/', ' ', $highlighted);
Expand Down
2 changes: 2 additions & 0 deletions src/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ final class Tokenizer
'DUPLICATE',
'DYNAMIC',
'ELSE',
'ELSEIF',
'ELSIF',
'ENCLOSED',
'END',
'ENGINE',
Expand Down
12 changes: 12 additions & 0 deletions tests/clihighlight.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1205,3 +1205,15 @@ MY_NON_TOP_LEVEL_KEYWORD_FX_3();
CREATE TABLE t (
c VARCHAR(20)
) DEFAULT CHARACTER SET utf8mb4 ENGINE = InnoDB
---
BEGIN
IF first_name != '' AND last_name != '' THEN
RETURN CONCAT(first_name, ' ', last_name);
ELSEIF first_name != '' THEN
RETURN first_name;
ELSIF last_name != '' THEN
RETURN last_name;
ELSE
RETURN id;
END IF;
END;
2 changes: 2 additions & 0 deletions tests/compress.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,5 @@ SELECT a FROM test STRAIGHT_JOIN test2 ON test.id = test2.id
SELECT t.id, t.start, t.end, t.end AS e2, t.limit, t.begin, t.case, t.when, t.then, t.else FROM t WHERE t.start = t.end
---
CREATE TABLE t (c VARCHAR(20)) DEFAULT CHARACTER SET utf8mb4 ENGINE = InnoDB
---
BEGIN IF first_name != '' AND last_name != '' THEN RETURN CONCAT(first_name, ' ', last_name); ELSEIF first_name != '' THEN RETURN first_name; ELSIF last_name != '' THEN RETURN last_name; ELSE RETURN id; END IF; END;
12 changes: 12 additions & 0 deletions tests/format-highlight.html
Original file line number Diff line number Diff line change
Expand Up @@ -1205,3 +1205,15 @@
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">CREATE</span> <span style="font-weight:bold;">TABLE</span> <span style="color: #333;">t</span> (
<span style="color: #333;">c</span> <span style="font-weight:bold;">VARCHAR</span>(<span style="color: green;">20</span>)
) <span style="font-weight:bold;">DEFAULT</span> <span style="font-weight:bold;">CHARACTER</span> <span style="font-weight:bold;">SET</span> <span style="color: #333;">utf8mb4</span> <span style="font-weight:bold;">ENGINE</span> <span >=</span> <span style="color: #333;">InnoDB</span></pre>
---
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">BEGIN</span>
<span style="font-weight:bold;">IF</span> <span style="color: #333;">first_name</span> <span >!</span><span >=</span> <span style="color: blue;">''</span> <span style="font-weight:bold;">AND</span> <span style="color: #333;">last_name</span> <span >!</span><span >=</span> <span style="color: blue;">''</span> <span style="font-weight:bold;">THEN</span>
<span style="font-weight:bold;">RETURN</span> <span style="font-weight:bold;">CONCAT</span>(<span style="color: #333;">first_name</span><span >,</span> <span style="color: blue;">' '</span><span >,</span> <span style="color: #333;">last_name</span>)<span >;</span>
<span style="font-weight:bold;">ELSEIF</span> <span style="color: #333;">first_name</span> <span >!</span><span >=</span> <span style="color: blue;">''</span> <span style="font-weight:bold;">THEN</span>
<span style="font-weight:bold;">RETURN</span> <span style="color: #333;">first_name</span><span >;</span>
<span style="font-weight:bold;">ELSIF</span> <span style="color: #333;">last_name</span> <span >!</span><span >=</span> <span style="color: blue;">''</span> <span style="font-weight:bold;">THEN</span>
<span style="font-weight:bold;">RETURN</span> <span style="color: #333;">last_name</span><span >;</span>
<span style="font-weight:bold;">ELSE</span>
<span style="font-weight:bold;">RETURN</span> <span style="color: #333;">id</span><span >;</span>
<span style="font-weight:bold;">END</span> <span style="font-weight:bold;">IF</span><span >;</span>
<span style="font-weight:bold;">END</span><span >;</span></pre>
12 changes: 12 additions & 0 deletions tests/format.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1203,3 +1203,15 @@ WHERE
CREATE TABLE t (
c VARCHAR(20)
) DEFAULT CHARACTER SET utf8mb4 ENGINE = InnoDB
---
BEGIN
IF first_name != '' AND last_name != '' THEN
RETURN CONCAT(first_name, ' ', last_name);
ELSEIF first_name != '' THEN
RETURN first_name;
ELSIF last_name != '' THEN
RETURN last_name;
ELSE
RETURN id;
END IF;
END;
2 changes: 2 additions & 0 deletions tests/highlight.html
Original file line number Diff line number Diff line change
Expand Up @@ -425,3 +425,5 @@
<span style="font-weight:bold;">WHERE</span> <span style="color: #333;">t</span><span >.</span><span style="color: #333;">start</span> <span >=</span> <span style="color: #333;">t</span><span >.</span><span style="color: #333;">end</span></pre>
---
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">CREATE</span> <span style="font-weight:bold;">TABLE</span> <span style="color: #333;">t</span> (<span style="color: #333;">c</span> <span style="font-weight:bold;">VARCHAR</span>(<span style="color: green;">20</span>)) <span style="font-weight:bold;">DEFAULT</span> <span style="font-weight:bold;">CHARACTER</span> <span style="font-weight:bold;">SET</span> <span style="color: #333;">utf8mb4</span> <span style="font-weight:bold;">ENGINE</span> <span >=</span> <span style="color: #333;">InnoDB</span></pre>
---
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">BEGIN</span> <span style="font-weight:bold;">IF</span> <span style="color: #333;">first_name</span> <span >!</span><span >=</span> <span style="color: blue;">''</span> <span style="font-weight:bold;">AND</span> <span style="color: #333;">last_name</span> <span >!</span><span >=</span> <span style="color: blue;">''</span> <span style="font-weight:bold;">THEN</span> <span style="font-weight:bold;">RETURN</span> <span style="font-weight:bold;">CONCAT</span>(<span style="color: #333;">first_name</span><span >,</span> <span style="color: blue;">' '</span><span >,</span> <span style="color: #333;">last_name</span>)<span >;</span> <span style="font-weight:bold;">ELSEIF</span> <span style="color: #333;">first_name</span> <span >!</span><span >=</span> <span style="color: blue;">''</span> <span style="font-weight:bold;">THEN</span> <span style="font-weight:bold;">RETURN</span> <span style="color: #333;">first_name</span><span >;</span> <span style="font-weight:bold;">ELSIF</span> <span style="color: #333;">last_name</span> <span >!</span><span >=</span> <span style="color: blue;">''</span> <span style="font-weight:bold;">THEN</span> <span style="font-weight:bold;">RETURN</span> <span style="color: #333;">last_name</span><span >;</span> <span style="font-weight:bold;">ELSE</span> <span style="font-weight:bold;">RETURN</span> <span style="color: #333;">id</span><span >;</span> <span style="font-weight:bold;">END</span> <span style="font-weight:bold;">IF</span><span >;</span> <span style="font-weight:bold;">END</span><span >;</span></pre>
2 changes: 2 additions & 0 deletions tests/sql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -425,3 +425,5 @@ FROM t
WHERE t.start = t.end
---
CREATE TABLE t (c VARCHAR(20)) DEFAULT CHARACTER SET utf8mb4 ENGINE = InnoDB
---
BEGIN IF first_name != '' AND last_name != '' THEN RETURN CONCAT(first_name, ' ', last_name); ELSEIF first_name != '' THEN RETURN first_name; ELSIF last_name != '' THEN RETURN last_name; ELSE RETURN id; END IF; END;