summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/Utils/Formatter.php72
-rw-r--r--tests/Utils/CLITest.php2
-rw-r--r--tests/Utils/FormatterTest.php36
3 files changed, 46 insertions, 64 deletions
diff --git a/src/Utils/Formatter.php b/src/Utils/Formatter.php
index 5b6da32..402d051 100644
--- a/src/Utils/Formatter.php
+++ b/src/Utils/Formatter.php
@@ -326,14 +326,6 @@ class Formatter
*/
$prev = null;
- /**
- * Comments are being formatted separately to maintain the whitespaces
- * before and after them.
- *
- * @var string
- */
- $comment = '';
-
// In order to be able to format the queries correctly, the next token
// must be taken into consideration. The loop below uses two pointers,
// `$prev` and `$curr` which store two consecutive tokens.
@@ -342,39 +334,26 @@ class Formatter
/**
* Token parsed at this moment.
*
- * @var Token
+ * @var Token $curr
*/
$curr = $list->tokens[$list->idx];
if ($curr->type === Token::TYPE_WHITESPACE) {
// Whitespaces are skipped because the formatter adds its own.
continue;
- } elseif ($curr->type === Token::TYPE_COMMENT) {
- // Whether the comments should be parsed.
- if (!empty($this->options['remove_comments'])) {
- continue;
- }
-
- if ($list->tokens[$list->idx - 1]->type === Token::TYPE_WHITESPACE) {
- // The whitespaces before and after are preserved for
- // formatting reasons.
- $comment .= $list->tokens[$list->idx - 1]->token;
- }
- $comment .= $this->toString($curr);
- if (($list->tokens[$list->idx + 1]->type === Token::TYPE_WHITESPACE)
- && ($list->tokens[$list->idx + 2]->type !== Token::TYPE_COMMENT)
- ) {
- // Adding the next whitespace only there is no comment that
- // follows it immediately which may cause adding a
- // whitespace twice.
- $comment .= $list->tokens[$list->idx + 1]->token;
- }
+ }
- // Everything was handled here, no need to continue.
+ if ($curr->type === Token::TYPE_COMMENT && $this->options['remove_comments']) {
+ // Skip Comments if option `remove_comments` is enabled
continue;
}
// Checking if pointers were initialized.
+ /**
+ * Previous Token.
+ *
+ * @var Token $prev
+ */
if ($prev !== null) {
// Checking if a new clause started.
if (static::isClause($prev) !== false) {
@@ -451,12 +430,6 @@ class Formatter
$shortGroup = false;
}
- // Delimiter must be placed on the same line with the last
- // clause.
- if ($curr->type === Token::TYPE_DELIMITER) {
- $lineEnded = false;
- }
-
// Adding the token.
$ret .= $this->toString($prev);
@@ -467,32 +440,29 @@ class Formatter
$indent = 0;
}
- if ($curr->type !== Token::TYPE_COMMENT) {
- $ret .= $this->options['line_ending']
- . str_repeat($this->options['indentation'], $indent);
- }
+ $ret .= $this->options['line_ending']
+ . str_repeat($this->options['indentation'], $indent);
+
$lineEnded = false;
} else {
// If the line ended there is no point in adding whitespaces.
// Also, some tokens do not have spaces before or after them.
- if (!(($prev->type === Token::TYPE_OPERATOR && ($prev->value === '.' || $prev->value === '('))
- // No space after . (
- || ($curr->type === Token::TYPE_OPERATOR && ($curr->value === '.' || $curr->value === ',' || $curr->value === '(' || $curr->value === ')'))
- // No space before . , ( )
- || $curr->type === Token::TYPE_DELIMITER && mb_strlen($curr->value, 'UTF-8') < 2)
+ if (
// A space after delimiters that are longer than 2 characters.
- || $prev->value === 'DELIMITER'
+ $prev->value === 'DELIMITER'
+ || !(
+ ($prev->type === Token::TYPE_OPERATOR && ($prev->value === '.' || $prev->value === '('))
+ // No space after . (
+ || ($curr->type === Token::TYPE_OPERATOR && ($curr->value === '.' || $curr->value === ',' || $curr->value === '(' || $curr->value === ')'))
+ // No space before . , ( )
+ || $curr->type === Token::TYPE_DELIMITER && mb_strlen($curr->value, 'UTF-8') < 2
+ )
) {
$ret .= ' ';
}
}
}
- if (!empty($comment)) {
- $ret .= $comment;
- $comment = '';
- }
-
// Iteration finished, consider current token as previous.
$prev = $curr;
}
diff --git a/tests/Utils/CLITest.php b/tests/Utils/CLITest.php
index a97d98f..41d3965 100644
--- a/tests/Utils/CLITest.php
+++ b/tests/Utils/CLITest.php
@@ -39,7 +39,7 @@ class CLITest extends TestCase
),
array(
array('q' => 'SELECT /* comment */ 1 /* other */', 'f' => 'text'),
- "SELECT\n /* comment */ 1 /* other */\n",
+ "SELECT\n /* comment */ 1 /* other */\n",
0,
),
array(
diff --git a/tests/Utils/FormatterTest.php b/tests/Utils/FormatterTest.php
index 60db8ca..55246e0 100644
--- a/tests/Utils/FormatterTest.php
+++ b/tests/Utils/FormatterTest.php
@@ -250,12 +250,36 @@ class FormatTest extends TestCase
array('type' => 'html'),
),
array(
+ 'SELECT /* Comment */ 1' . "\n" .
+ 'FROM tbl # Comment' . "\n" .
+ 'WHERE 1 -- Comment',
+ 'SELECT' . "\n" .
+ ' /* Comment */ 1' . "\n" .
+ 'FROM' . "\n" .
+ ' tbl # Comment' . "\n" .
+ 'WHERE' . "\n" .
+ ' 1 -- Comment',
+ array('type' => 'text'),
+ ),
+ array(
'SELECT 1 # Comment',
'<span class="sql-reserved">SELECT</span>' . '<br/>' .
'&nbsp;&nbsp;&nbsp;&nbsp;<span class="sql-number">1</span> <span class="sql-comment"># Comment</span>',
array('type' => 'html'),
),
array(
+ 'SELECT 1 -- comment',
+ '<span class="sql-reserved">SELECT</span>' . '<br/>' .
+ '&nbsp;&nbsp;&nbsp;&nbsp;<span class="sql-number">1</span> <span class="sql-comment">-- comment</span>',
+ array('type' => 'html'),
+ ),
+ array(
+ 'SELECT 1 -- comment',
+ '<span class="sql-reserved">SELECT</span>' . '<br/>' .
+ '&nbsp;&nbsp;&nbsp;&nbsp;<span class="sql-number">1</span>',
+ array('type' => 'html', 'remove_comments' => true),
+ ),
+ array(
'SELECT HEX("1")',
'<span class="sql-reserved">SELECT</span>' . '<br/>' .
'&nbsp;&nbsp;&nbsp;&nbsp;<span class="sql-keyword">HEX</span>(<span class="sql-string">"1"</span>)',
@@ -317,18 +341,6 @@ class FormatTest extends TestCase
array('type' => 'html'),
),
array(
- 'SELECT 1 -- comment',
- '<span class="sql-reserved">SELECT</span>' . '<br/>' .
- '&nbsp;&nbsp;&nbsp;&nbsp;<span class="sql-number">1</span> <span class="sql-comment">-- comment</span>',
- array('type' => 'html'),
- ),
- array(
- 'SELECT 1 -- comment',
- '<span class="sql-reserved">SELECT</span>' . '<br/>' .
- '&nbsp;&nbsp;&nbsp;&nbsp;<span class="sql-number">1</span>',
- array('type' => 'html', 'remove_comments' => true),
- ),
- array(
'CREATE TABLE IF NOT EXISTS `pma__bookmark` (' . "\n" .
' `id` int(11) NOT NULL auto_increment,' . "\n" .
' `dbase` varchar(255) NOT NULL default "",' . "\n" .