Skip to content

Commit

Permalink
Assume words after "." as identifiers only
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Oct 6, 2024
1 parent a9acb2c commit 20d7b7c
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/SqlFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ public function format(string $string, string $indentString = ' '): string

// Format token by token
while ($token = $cursor->next(Token::TOKEN_TYPE_WHITESPACE)) {
$prevNotWhitespaceToken = $cursor->subCursor()->previous(Token::TOKEN_TYPE_WHITESPACE);
$tokenValueUpper = strtoupper($token->value());
if ($prevNotWhitespaceToken !== null && $prevNotWhitespaceToken->value() === '.') {
$tokenValueUpper = false;
}

$highlighted = $this->highlighter->highlightToken(
$token->type(),
$token->value(),
Expand All @@ -122,7 +128,6 @@ public function format(string $string, string $indentString = ' '): string
if ($newline) {
$return = rtrim($return, ' ');

$prevNotWhitespaceToken = $cursor->subCursor()->previous(Token::TOKEN_TYPE_WHITESPACE);
if ($prevNotWhitespaceToken !== null && $prevNotWhitespaceToken->value() === ';') {
$return .= "\n";
}
Expand Down Expand Up @@ -274,7 +279,7 @@ public function format(string $string, string $indentString = ' '): string
}

// if SQL 'LIMIT' clause, start variable to reset newline
if (strtoupper($token->value()) === 'LIMIT' && ! $inlineParentheses) {
if ($tokenValueUpper === 'LIMIT' && ! $inlineParentheses) {
$clauseLimit = true;
}
} elseif ($token->value() === ';') {
Expand All @@ -284,30 +289,28 @@ public function format(string $string, string $indentString = ' '): string
}

$newline = true;
} elseif (strtoupper($token->value()) === 'CASE') {
} elseif ($tokenValueUpper === 'CASE') {
$increaseBlockIndent = true;
} elseif (strtoupper($token->value()) === 'BEGIN') {
} elseif ($tokenValueUpper === 'BEGIN') {
$newline = true;
$increaseBlockIndent = true;
} elseif (strtoupper($token->value()) === 'LOOP') {
} elseif ($tokenValueUpper === 'LOOP') {
// https://docs.oracle.com/en/database/oracle/oracle-database/19/lnpls/basic-LOOP-statement.html

$prevNotWhitespaceToken = $cursor->subCursor()->previous(Token::TOKEN_TYPE_WHITESPACE);
if ($prevNotWhitespaceToken !== null && strtoupper($prevNotWhitespaceToken->value()) !== 'END') {
$newline = true;
$increaseBlockIndent = true;
}
} elseif (in_array(strtoupper($token->value()), ['WHEN', 'THEN', 'ELSE', 'END'], true)) {
if (strtoupper($token->value()) !== 'THEN') {
} elseif (in_array($tokenValueUpper, ['WHEN', 'THEN', 'ELSE', 'END'], true)) {
if ($tokenValueUpper !== 'THEN') {
$decreaseIndentationLevelFx();

$prevNotWhitespaceToken = $cursor->subCursor()->previous(Token::TOKEN_TYPE_WHITESPACE);
if ($prevNotWhitespaceToken !== null && strtoupper($prevNotWhitespaceToken->value()) !== 'CASE') {
$appendNewLineIfNotAddedFx();
}
}

if (strtoupper($token->value()) === 'THEN' || strtoupper($token->value()) === 'ELSE') {
if ($tokenValueUpper === 'THEN' || $tokenValueUpper === 'ELSE') {
$newline = true;
$increaseBlockIndent = true;
}
Expand Down Expand Up @@ -338,7 +341,6 @@ public function format(string $string, string $indentString = ' '): string
}
} elseif ($token->isOfType(Token::TOKEN_TYPE_BOUNDARY)) {
// Multiple boundary characters in a row should not have spaces between them (not including parentheses)
$prevNotWhitespaceToken = $cursor->subCursor()->previous(Token::TOKEN_TYPE_WHITESPACE);
if ($prevNotWhitespaceToken !== null && $prevNotWhitespaceToken->isOfType(Token::TOKEN_TYPE_BOUNDARY)) {
$prevToken = $cursor->subCursor()->previous();
if ($prevToken !== null && ! $prevToken->isOfType(Token::TOKEN_TYPE_WHITESPACE)) {
Expand Down
16 changes: 16 additions & 0 deletions tests/clihighlight.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1185,3 +1185,19 @@ MY_NON_TOP_LEVEL_KEYWORD_FX_3();
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
2 changes: 2 additions & 0 deletions tests/compress.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,5 @@ begin try insert into [t] ([name], [int], [float], [null]) values (N'Ewa', 1, 1.
BEGIN FOR i IN 1..5 LOOP DBMS_OUTPUT.PUT_LINE(i); END LOOP; END;
---
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
16 changes: 16 additions & 0 deletions tests/format-highlight.html
Original file line number Diff line number Diff line change
Expand Up @@ -1185,3 +1185,19 @@
<span style="font-weight:bold;">FROM</span>
<span style="color: #333;">test</span>
<span style="font-weight:bold;">STRAIGHT_JOIN</span> <span style="color: #333;">test2</span> <span style="font-weight:bold;">ON</span> <span style="color: #333;">test</span><span >.</span><span style="color: #333;">id</span> <span >=</span> <span style="color: #333;">test2</span><span >.</span><span style="color: #333;">id</span></pre>
---
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span>
<span style="color: #333;">t</span><span >.</span><span style="color: #333;">id</span><span >,</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><span >,</span>
<span style="color: #333;">t</span><span >.</span><span style="color: #333;">end</span> <span style="font-weight:bold;">AS</span> <span style="color: #333;">e2</span><span >,</span>
<span style="color: #333;">t</span><span >.</span><span style="color: #333;">limit</span><span >,</span>
<span style="color: #333;">t</span><span >.</span><span style="color: #333;">begin</span><span >,</span>
<span style="color: #333;">t</span><span >.</span><span style="color: #333;">case</span><span >,</span>
<span style="color: #333;">t</span><span >.</span><span style="color: #333;">when</span><span >,</span>
<span style="color: #333;">t</span><span >.</span><span style="color: #333;">then</span><span >,</span>
<span style="color: #333;">t</span><span >.</span><span style="color: #333;">else</span>
<span style="font-weight:bold;">FROM</span>
<span style="color: #333;">t</span>
<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>
16 changes: 16 additions & 0 deletions tests/format.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1183,3 +1183,19 @@ SELECT
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
14 changes: 14 additions & 0 deletions tests/highlight.html
Original file line number Diff line number Diff line change
Expand Up @@ -409,3 +409,17 @@
<span style="font-weight:bold;">END</span><span >;</span></pre>
---
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span> <span style="color: #333;">a</span> <span style="font-weight:bold;">FROM</span> <span style="color: #333;">test</span> <span style="font-weight:bold;">STRAIGHT_JOIN</span> <span style="color: #333;">test2</span> <span style="font-weight:bold;">ON</span> <span style="color: #333;">test</span><span >.</span><span style="color: #333;">id</span> <span >=</span> <span style="color: #333;">test2</span><span >.</span><span style="color: #333;">id</span></pre>
---
<pre style="color: black; background-color: white;"><span style="font-weight:bold;">SELECT</span>
<span style="color: #333;">t</span><span >.</span><span style="color: #333;">id</span><span >,</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><span >,</span>
<span style="color: #333;">t</span><span >.</span><span style="color: #333;">end</span> <span style="font-weight:bold;">AS</span> <span style="color: #333;">e2</span><span >,</span>
<span style="color: #333;">t</span><span >.</span><span style="color: #333;">limit</span><span >,</span>
<span style="color: #333;">t</span><span >.</span><span style="color: #333;">begin</span><span >,</span>
<span style="color: #333;">t</span><span >.</span><span style="color: #333;">case</span><span >,</span>
<span style="color: #333;">t</span><span >.</span><span style="color: #333;">when</span><span >,</span>
<span style="color: #333;">t</span><span >.</span><span style="color: #333;">then</span><span >,</span>
<span style="color: #333;">t</span><span >.</span><span style="color: #333;">else</span>
<span style="font-weight:bold;">FROM</span> <span style="color: #333;">t</span>
<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>
14 changes: 14 additions & 0 deletions tests/sql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -409,3 +409,17 @@ BEGIN
END;
---
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

0 comments on commit 20d7b7c

Please sign in to comment.