diff options
author | Michal Čihař <michal@cihar.com> | 2017-06-08 15:13:45 +0200 |
---|---|---|
committer | Michal Čihař <michal@cihar.com> | 2017-06-08 15:13:45 +0200 |
commit | 7cad6fa657e8dd70704b4163bb09751d98408f9b (patch) | |
tree | e4faffa75088a513dc288aa5cf1f0aaf16733bde /src | |
parent | f4ff382ca6ad1d24a26ca5c743d91cb18f569ec6 (diff) | |
download | sql-parser-7cad6fa657e8dd70704b4163bb09751d98408f9b.zip sql-parser-7cad6fa657e8dd70704b4163bb09751d98408f9b.tar.gz sql-parser-7cad6fa657e8dd70704b4163bb09751d98408f9b.tar.bz2 |
Honor newlines before comments in formatter
Issue #156
Signed-off-by: Michal Čihař <michal@cihar.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/Utils/Formatter.php | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/src/Utils/Formatter.php b/src/Utils/Formatter.php index ac3284c..393eaf3 100644 --- a/src/Utils/Formatter.php +++ b/src/Utils/Formatter.php @@ -362,10 +362,19 @@ class Formatter * @var Token */ $curr = $list->tokens[$list->idx]; + if ($list->idx + 1 < $list->count) { + $next = $list->tokens[$list->idx + 1]; + } else { + $next = null; + } if ($curr->type === Token::TYPE_WHITESPACE) { - // Keep linebreaks after comments - if (strpos($curr->token, "\n") !== false && $prev !== null && $prev->type === Token::TYPE_COMMENT) { + // Keep linebreaks before and after comments + if (strpos($curr->token, "\n") !== false && ( + ($prev !== null && $prev->type === Token::TYPE_COMMENT) || + ($next !== null && $next->type === Token::TYPE_COMMENT) + ) + ) { $lineEnded = true; } // Whitespaces are skipped because the formatter adds its own. |