Skip to content

Commit 5127520

Browse files
committed
Fix comment parsing
1 parent 4bef088 commit 5127520

File tree

3 files changed

+46
-64
lines changed

3 files changed

+46
-64
lines changed

src/Utils/Formatter.php

Lines changed: 21 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -326,14 +326,6 @@ public function formatList($list)
326326
*/
327327
$prev = null;
328328

329-
/**
330-
* Comments are being formatted separately to maintain the whitespaces
331-
* before and after them.
332-
*
333-
* @var string
334-
*/
335-
$comment = '';
336-
337329
// In order to be able to format the queries correctly, the next token
338330
// must be taken into consideration. The loop below uses two pointers,
339331
// `$prev` and `$curr` which store two consecutive tokens.
@@ -342,39 +334,26 @@ public function formatList($list)
342334
/**
343335
* Token parsed at this moment.
344336
*
345-
* @var Token
337+
* @var Token $curr
346338
*/
347339
$curr = $list->tokens[$list->idx];
348340

349341
if ($curr->type === Token::TYPE_WHITESPACE) {
350342
// Whitespaces are skipped because the formatter adds its own.
351343
continue;
352-
} elseif ($curr->type === Token::TYPE_COMMENT) {
353-
// Whether the comments should be parsed.
354-
if (!empty($this->options['remove_comments'])) {
355-
continue;
356-
}
357-
358-
if ($list->tokens[$list->idx - 1]->type === Token::TYPE_WHITESPACE) {
359-
// The whitespaces before and after are preserved for
360-
// formatting reasons.
361-
$comment .= $list->tokens[$list->idx - 1]->token;
362-
}
363-
$comment .= $this->toString($curr);
364-
if (($list->tokens[$list->idx + 1]->type === Token::TYPE_WHITESPACE)
365-
&& ($list->tokens[$list->idx + 2]->type !== Token::TYPE_COMMENT)
366-
) {
367-
// Adding the next whitespace only there is no comment that
368-
// follows it immediately which may cause adding a
369-
// whitespace twice.
370-
$comment .= $list->tokens[$list->idx + 1]->token;
371-
}
344+
}
372345

373-
// Everything was handled here, no need to continue.
346+
if ($curr->type === Token::TYPE_COMMENT && $this->options['remove_comments']) {
347+
// Skip Comments if option `remove_comments` is enabled
374348
continue;
375349
}
376350

377351
// Checking if pointers were initialized.
352+
/**
353+
* Previous Token.
354+
*
355+
* @var Token $prev
356+
*/
378357
if ($prev !== null) {
379358
// Checking if a new clause started.
380359
if (static::isClause($prev) !== false) {
@@ -451,12 +430,6 @@ public function formatList($list)
451430
$shortGroup = false;
452431
}
453432

454-
// Delimiter must be placed on the same line with the last
455-
// clause.
456-
if ($curr->type === Token::TYPE_DELIMITER) {
457-
$lineEnded = false;
458-
}
459-
460433
// Adding the token.
461434
$ret .= $this->toString($prev);
462435

@@ -467,32 +440,29 @@ public function formatList($list)
467440
$indent = 0;
468441
}
469442

470-
if ($curr->type !== Token::TYPE_COMMENT) {
471-
$ret .= $this->options['line_ending']
472-
. str_repeat($this->options['indentation'], $indent);
473-
}
443+
$ret .= $this->options['line_ending']
444+
. str_repeat($this->options['indentation'], $indent);
445+
474446
$lineEnded = false;
475447
} else {
476448
// If the line ended there is no point in adding whitespaces.
477449
// Also, some tokens do not have spaces before or after them.
478-
if (!(($prev->type === Token::TYPE_OPERATOR && ($prev->value === '.' || $prev->value === '('))
479-
// No space after . (
480-
|| ($curr->type === Token::TYPE_OPERATOR && ($curr->value === '.' || $curr->value === ',' || $curr->value === '(' || $curr->value === ')'))
481-
// No space before . , ( )
482-
|| $curr->type === Token::TYPE_DELIMITER && mb_strlen($curr->value, 'UTF-8') < 2)
450+
if (
483451
// A space after delimiters that are longer than 2 characters.
484-
|| $prev->value === 'DELIMITER'
452+
$prev->value === 'DELIMITER'
453+
|| !(
454+
($prev->type === Token::TYPE_OPERATOR && ($prev->value === '.' || $prev->value === '('))
455+
// No space after . (
456+
|| ($curr->type === Token::TYPE_OPERATOR && ($curr->value === '.' || $curr->value === ',' || $curr->value === '(' || $curr->value === ')'))
457+
// No space before . , ( )
458+
|| $curr->type === Token::TYPE_DELIMITER && mb_strlen($curr->value, 'UTF-8') < 2
459+
)
485460
) {
486461
$ret .= ' ';
487462
}
488463
}
489464
}
490465

491-
if (!empty($comment)) {
492-
$ret .= $comment;
493-
$comment = '';
494-
}
495-
496466
// Iteration finished, consider current token as previous.
497467
$prev = $curr;
498468
}

tests/Utils/CLITest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function highlightParams()
3939
),
4040
array(
4141
array('q' => 'SELECT /* comment */ 1 /* other */', 'f' => 'text'),
42-
"SELECT\n /* comment */ 1 /* other */\n",
42+
"SELECT\n /* comment */ 1 /* other */\n",
4343
0,
4444
),
4545
array(

tests/Utils/FormatterTest.php

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,36 @@ public function formatQueries()
249249
'&nbsp;&nbsp;&nbsp;&nbsp;<span class="sql-number">1</span>',
250250
array('type' => 'html'),
251251
),
252+
array(
253+
'SELECT /* Comment */ 1' . "\n" .
254+
'FROM tbl # Comment' . "\n" .
255+
'WHERE 1 -- Comment',
256+
'SELECT' . "\n" .
257+
' /* Comment */ 1' . "\n" .
258+
'FROM' . "\n" .
259+
' tbl # Comment' . "\n" .
260+
'WHERE' . "\n" .
261+
' 1 -- Comment',
262+
array('type' => 'text'),
263+
),
252264
array(
253265
'SELECT 1 # Comment',
254266
'<span class="sql-reserved">SELECT</span>' . '<br/>' .
255267
'&nbsp;&nbsp;&nbsp;&nbsp;<span class="sql-number">1</span> <span class="sql-comment"># Comment</span>',
256268
array('type' => 'html'),
257269
),
270+
array(
271+
'SELECT 1 -- comment',
272+
'<span class="sql-reserved">SELECT</span>' . '<br/>' .
273+
'&nbsp;&nbsp;&nbsp;&nbsp;<span class="sql-number">1</span> <span class="sql-comment">-- comment</span>',
274+
array('type' => 'html'),
275+
),
276+
array(
277+
'SELECT 1 -- comment',
278+
'<span class="sql-reserved">SELECT</span>' . '<br/>' .
279+
'&nbsp;&nbsp;&nbsp;&nbsp;<span class="sql-number">1</span>',
280+
array('type' => 'html', 'remove_comments' => true),
281+
),
258282
array(
259283
'SELECT HEX("1")',
260284
'<span class="sql-reserved">SELECT</span>' . '<br/>' .
@@ -316,18 +340,6 @@ public function formatQueries()
316340
'&nbsp;&nbsp;&nbsp;&nbsp;superado = <span class="sql-number">0</span>',
317341
array('type' => 'html'),
318342
),
319-
array(
320-
'SELECT 1 -- comment',
321-
'<span class="sql-reserved">SELECT</span>' . '<br/>' .
322-
'&nbsp;&nbsp;&nbsp;&nbsp;<span class="sql-number">1</span> <span class="sql-comment">-- comment</span>',
323-
array('type' => 'html'),
324-
),
325-
array(
326-
'SELECT 1 -- comment',
327-
'<span class="sql-reserved">SELECT</span>' . '<br/>' .
328-
'&nbsp;&nbsp;&nbsp;&nbsp;<span class="sql-number">1</span>',
329-
array('type' => 'html', 'remove_comments' => true),
330-
),
331343
array(
332344
'CREATE TABLE IF NOT EXISTS `pma__bookmark` (' . "\n" .
333345
' `id` int(11) NOT NULL auto_increment,' . "\n" .

0 commit comments

Comments
 (0)