Skip to content

Commit b43b07d

Browse files
committed
fix - new line removed - kokororin/vscode-phpfmt#175
1 parent 05d3bff commit b43b07d

5 files changed

Lines changed: 96 additions & 5 deletions

File tree

fmt.stub.php

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11317,15 +11317,49 @@ public function format($source) {
1131711317
$this->appendCode($text);
1131811318
$this->printUntil(ST_PARENTHESES_OPEN);
1131911319
$this->printBlock(ST_PARENTHESES_OPEN, ST_PARENTHESES_CLOSE);
11320-
$this->printAndStopAt(ST_CURLY_OPEN);
11321-
if ($this->rightTokenIs(ST_CURLY_CLOSE)) {
11320+
11321+
// Look ahead to check if function is empty before deciding how to format
11322+
$lookAheadPtr = $this->ptr;
11323+
$isEmptyFunction = false;
11324+
11325+
// Walk forward to find ST_CURLY_OPEN
11326+
while ($lookAheadPtr < count($this->tkns)) {
11327+
$lookAheadPtr++;
11328+
if (!isset($this->tkns[$lookAheadPtr])) break;
11329+
11330+
list($lookId, $lookText) = $this->getToken($this->tkns[$lookAheadPtr]);
11331+
if ($lookId == ST_CURLY_OPEN) {
11332+
// Found opening brace, now check if next non-whitespace token is closing brace
11333+
$nextPtr = $lookAheadPtr;
11334+
while ($nextPtr < count($this->tkns)) {
11335+
$nextPtr++;
11336+
if (!isset($this->tkns[$nextPtr])) break;
11337+
11338+
list($nextId, $nextText) = $this->getToken($this->tkns[$nextPtr]);
11339+
if ($nextId == T_WHITESPACE) continue; // Skip whitespace
11340+
11341+
if ($nextId == ST_CURLY_CLOSE) {
11342+
$isEmptyFunction = true;
11343+
}
11344+
break;
11345+
}
11346+
break;
11347+
}
11348+
}
11349+
11350+
if ($isEmptyFunction) {
11351+
// Format as single line for empty functions
11352+
$this->printAndStopAt(ST_CURLY_OPEN);
1132211353
$this->rtrimAndAppendCode($this->getSpace() . ST_CURLY_OPEN);
11323-
$this->printAndStopAt(ST_CURLY_CLOSE);
11354+
$this->walkUntil(ST_CURLY_CLOSE);
1132411355
$this->rtrimAndAppendCode(ST_CURLY_CLOSE);
1132511356
break;
11357+
} else {
11358+
// For non-empty functions, preserve original formatting
11359+
$this->printAndStopAt(ST_CURLY_OPEN);
11360+
prev($this->tkns);
11361+
break;
1132611362
}
11327-
prev($this->tkns);
11328-
break;
1132911363
default:
1133011364
$this->appendCode($text);
1133111365
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
// Issue #175: PSR2EmptyFunction removes lines between functions
3+
function foo() {
4+
}
5+
6+
function bar(int $baz, string $qux) {
7+
noop();
8+
}
9+
10+
function empty1() {
11+
}
12+
13+
function empty2() {
14+
15+
16+
}
17+
18+
function withComment() {
19+
// This should not be collapsed
20+
}
21+
22+
function anotherEmpty() {
23+
}
24+
//passes:PSR2EmptyFunction
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
// Issue #175: PSR2EmptyFunction removes lines between functions
3+
function foo() {}
4+
5+
function bar(int $baz, string $qux) {
6+
noop();
7+
}
8+
9+
function empty1() {}
10+
11+
function empty2() {}
12+
13+
function withComment() {
14+
// This should not be collapsed
15+
}
16+
17+
function anotherEmpty() {}
18+
//passes:PSR2EmptyFunction
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
function foo() {
3+
}
4+
5+
function bar(int $baz, string $qux) {
6+
noop();
7+
}
8+
//passes:PSR2EmptyFunction
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
function foo() {}
3+
4+
function bar(int $baz, string $qux) {
5+
noop();
6+
}
7+
//passes:PSR2EmptyFunction

0 commit comments

Comments
 (0)