Skip to content

Commit 7cad6fa

Browse files
committed
Honor newlines before comments in formatter
Issue #156 Signed-off-by: Michal Čihař <michal@cihar.com>
1 parent f4ff382 commit 7cad6fa

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/Utils/Formatter.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,10 +362,19 @@ public function formatList($list)
362362
* @var Token
363363
*/
364364
$curr = $list->tokens[$list->idx];
365+
if ($list->idx + 1 < $list->count) {
366+
$next = $list->tokens[$list->idx + 1];
367+
} else {
368+
$next = null;
369+
}
365370

366371
if ($curr->type === Token::TYPE_WHITESPACE) {
367-
// Keep linebreaks after comments
368-
if (strpos($curr->token, "\n") !== false && $prev !== null && $prev->type === Token::TYPE_COMMENT) {
372+
// Keep linebreaks before and after comments
373+
if (strpos($curr->token, "\n") !== false && (
374+
($prev !== null && $prev->type === Token::TYPE_COMMENT) ||
375+
($next !== null && $next->type === Token::TYPE_COMMENT)
376+
)
377+
) {
369378
$lineEnded = true;
370379
}
371380
// Whitespaces are skipped because the formatter adds its own.

0 commit comments

Comments
 (0)