Skip to content

Commit d5da159

Browse files
committed
fix - invalid formatting - kokororin/vscode-phpfmt#176
1 parent a8deafa commit d5da159

7 files changed

Lines changed: 86 additions & 29 deletions

fmt.stub.php

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4826,8 +4826,18 @@ public function candidate($source, $foundTokens) {
48264826
$tkns = token_get_all($source);
48274827

48284828
$this->tkns = [];
4829+
$inShortEcho = false;
48294830
foreach ($tkns as $i => $token) {
4830-
if (T_WHITESPACE === $token[0] && !$this->hasLn($token[1])) {
4831+
if (isset($token[0]) && T_OPEN_TAG_WITH_ECHO === $token[0]) {
4832+
$inShortEcho = true;
4833+
} elseif (isset($token[0]) && T_CLOSE_TAG === $token[0]) {
4834+
$inShortEcho = false;
4835+
}
4836+
if (T_WHITESPACE === $token[0] && !$this->hasLn($token[1])) {
4837+
if ($inShortEcho) {
4838+
$this->tkns[] = $token;
4839+
continue;
4840+
}
48314841
$c = count($this->tkns);
48324842
if (PHP_VERSION_ID >= 80000 && $c) {
48334843
if (isset($tkns[$i + 1][1]) && $tkns[$i + 1][1] === '&'
@@ -5418,7 +5428,7 @@ public function format($source) {
54185428
break;
54195429

54205430
case T_CLOSE_TAG:
5421-
$space = $this->getSpace(!$hasEchoAfterOpenTag && !$this->hasLnBefore());
5431+
$space = $this->getSpace(!$hasEchoAfterOpenTag && !$this->hasLnBefore());
54225432
if ($space === '') {
54235433
if ($this->leftTokenIs([ST_SEMI_COLON]) && !$this->hasLnBefore() && !$hasOpenTagWithEcho) {
54245434
$space = $this->getSpace();
@@ -5433,8 +5443,11 @@ public function format($source) {
54335443
case T_OPEN_TAG_WITH_ECHO:
54345444
$hasEchoAfterOpenTag = true;
54355445
$hasOpenTagWithEcho = true;
5436-
$this->appendCode($text);
5437-
break;
5446+
$this->appendCode($text);
5447+
$this->printUntil(T_CLOSE_TAG);
5448+
$hasEchoAfterOpenTag = false;
5449+
$hasOpenTagWithEcho = false;
5450+
continue 2;
54385451

54395452
case T_OPEN_TAG:
54405453
$hasEchoAfterOpenTag = true;
@@ -7883,26 +7896,30 @@ public function format($source) {
78837896
switch ($id) {
78847897
case T_OPEN_TAG:
78857898

7886-
$prevText = null;
7887-
if ($this->ptr > 0) {
7888-
list($id2, $prevText) = $this->getToken($this->leftToken());
7889-
}
7899+
$prevText = null;
7900+
if ($this->ptr > 0) {
7901+
list($id2, $prevText) = $this->getToken($this->leftToken());
7902+
}
78907903

7891-
$prevSpace = '';
7892-
if ($prevText !== null) {
7893-
$lastNewlineText = strrchr($prevText, $this->newLine);
7894-
if ($lastNewlineText !== false) {
7895-
$prevSpace = substr($lastNewlineText, 1);
7896-
} elseif (preg_match('/^\s+$/', $prevText)) {
7897-
// If prevText contains only whitespace, use it as prevSpace
7898-
$prevSpace = $prevText;
7899-
}
7900-
}
7901-
$skipPadLeft = false;
7902-
if (rtrim($prevSpace) == $prevSpace) {
7903-
$skipPadLeft = true;
7904+
$linePrefix = '';
7905+
if ($prevText !== null) {
7906+
$lastNewlineText = strrchr($prevText, $this->newLine);
7907+
if ($lastNewlineText !== false) {
7908+
$linePrefix = substr($lastNewlineText, 1);
7909+
} else {
7910+
$linePrefix = $prevText;
7911+
}
79047912
}
7905-
$prevSpace = preg_replace('/[^\s\t]/', ' ', $prevSpace);
7913+
7914+
// For the first line, only restore trailing whitespace before the open tag.
7915+
// For subsequent lines, align using a same-width prefix (non-whitespace becomes spaces).
7916+
$padFirstLine = '';
7917+
if ($linePrefix !== '') {
7918+
if (preg_match('/[ \t]*$/', $linePrefix, $m)) {
7919+
$padFirstLine = $m[0];
7920+
}
7921+
}
7922+
$padOtherLines = ($linePrefix === '') ? '' : preg_replace('/[^\s\t]/', ' ', $linePrefix);
79067923

79077924
$placeholders = [];
79087925
$strings = [];
@@ -7950,18 +7967,14 @@ public function format($source) {
79507967
$tmp = explode($this->newLine, $stack);
79517968
$lastLine = sizeof($tmp) - 2;
79527969
foreach ($tmp as $idx => $line) {
7953-
$before = $prevSpace;
7970+
$before = (0 === $idx) ? $padFirstLine : $padOtherLines;
79547971
if ('' === trim($line)) {
79557972
continue;
79567973
}
79577974
$indent = '';
79587975
if (0 != $idx && $idx < $lastLine) {
79597976
$indent = $this->indentChar;
79607977
}
7961-
if ($skipPadLeft) {
7962-
$before = '';
7963-
$skipPadLeft = false;
7964-
}
79657978
$tmp[$idx] = $before . $indent . $line;
79667979
}
79677980

tests/Original/267-wrong-html.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</h1>
77
</td>
88
<td>
9-
<?='another'?>
9+
<?= 'another' ?>
1010
</td>
1111
</tr>
1212
</table>

tests/Original/319-align-php-code.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121

2222
<div>
23-
a <?php
23+
a <?php
2424
//passes:AlignPHPCode
2525
echo 'a';
2626
if ($a) {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
// Repro for https://github.com/kokororin/vscode-phpfmt/issues/176
3+
// Desired: keep spaces in short echo tags.
4+
//passes:AlignDoubleArrow,SpaceAroundParentheses
5+
//excludes:AllmanStyleBraces
6+
?>
7+
<div>
8+
<?= $variableName; ?>
9+
</div>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
// Repro for https://github.com/kokororin/vscode-phpfmt/issues/176
3+
// Desired: keep spaces in short echo tags.
4+
//passes:AlignDoubleArrow,SpaceAroundParentheses
5+
//excludes:AllmanStyleBraces
6+
?>
7+
<div>
8+
<?= $variableName; ?>
9+
</div>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<body>
2+
<div class="this-is-a-template">
3+
<?php echo exampleFunction( '<div class="test">Label</div>' ); ?>
4+
<?php echo exampleFunction( '<div class="test">Label</div>' ); ?>
5+
<?php echo exampleFunction( '<div class="test">Label</div>' ); ?>
6+
</div>
7+
8+
<div class="my-container <?php echo $myVariable; ?>">
9+
<!-- Write something here -->
10+
</div>
11+
</body>
12+
13+
<?php //passes:AlignPHPCode ?>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<body>
2+
<div class="this-is-a-template">
3+
<?php echo exampleFunction('<div class="test">Label</div>'); ?>
4+
<?php echo exampleFunction('<div class="test">Label</div>'); ?>
5+
<?php echo exampleFunction('<div class="test">Label</div>'); ?>
6+
</div>
7+
8+
<div class="my-container <?php echo $myVariable; ?>">
9+
<!-- Write something here -->
10+
</div>
11+
</body>
12+
13+
<?php //passes:AlignPHPCode ?>

0 commit comments

Comments
 (0)