-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix bracket placement idempotence issues
- Move bracket mirroring from `StandardIndentation` to standalone rule `PlaceBrackets` and prioritise it to run earlier - Refactor and/or prioritise other rules so changes to vertical whitespace near brackets are applied before `PlaceBrackets`, incl.: - Move suppression of whitespace in empty `for` expressions from `VerticalSpacing` to `StandardSpacing` - Place `for` expressions on their own lines in `StrictExpressions` and `SemiStrictExpressions` - Run `StrictLists` after `*StrictExpressions` to ensure `for` expressions that break over multiple lines are not collapsed - Add `FOR_PARTS` to token data and parse `for` expressions in `Parser` instead of duplicating code in `VerticalSpacing` - Add `TokenCollection::setWhitespace()` and adopt in scenarios where suppression of whitespace should not be removed - Similarly, replace `TokenCollection::applyInnerWhitespace()` with `setInnerWhitespace()`
- Loading branch information
Showing
18 changed files
with
330 additions
and
202 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Lkrms\PrettyPHP\Rule; | ||
|
||
use Lkrms\PrettyPHP\Catalog\TokenFlag as Flag; | ||
use Lkrms\PrettyPHP\Catalog\WhitespaceFlag as Space; | ||
use Lkrms\PrettyPHP\Concern\TokenRuleTrait; | ||
use Lkrms\PrettyPHP\Contract\TokenRule; | ||
use Lkrms\PrettyPHP\AbstractTokenIndex; | ||
use Lkrms\PrettyPHP\Token; | ||
|
||
/** | ||
* Make brackets symmetrical | ||
* | ||
* @api | ||
*/ | ||
final class PlaceBrackets implements TokenRule | ||
{ | ||
use TokenRuleTrait; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public static function getPriority(string $method): ?int | ||
{ | ||
return [ | ||
self::PROCESS_TOKENS => 240, | ||
][$method] ?? null; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public static function getTokens(AbstractTokenIndex $idx): array | ||
{ | ||
return $idx->OpenBracket; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public static function needsSortedTokens(): bool | ||
{ | ||
return false; | ||
} | ||
|
||
/** | ||
* Apply the rule to the given tokens | ||
* | ||
* Inner whitespace is copied from open brackets to close brackets. | ||
* | ||
* Structural and `match` expression braces are ignored. | ||
*/ | ||
public function processTokens(array $tokens): void | ||
{ | ||
foreach ($tokens as $token) { | ||
if ( | ||
$token->Flags & Flag::STRUCTURAL_BRACE | ||
|| ($token->id === \T_OPEN_BRACE && $token->isMatchOpenBrace()) | ||
) { | ||
continue; | ||
} | ||
|
||
/** @var Token */ | ||
$close = $token->CloseBracket; | ||
if (!$token->hasNewlineBeforeNextCode()) { | ||
$close->Whitespace |= Space::NO_BLANK_BEFORE | Space::NO_LINE_BEFORE; | ||
} else { | ||
$close->Whitespace |= Space::LINE_BEFORE; | ||
if (!$close->hasNewlineBefore()) { | ||
$close->removeWhitespace(Space::NO_LINE_BEFORE); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.