-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Ability to skip "meta" tokens (whitespace, comments, attributes) #7559
Open
Wirone
wants to merge
1
commit into
PHP-CS-Fixer:master
Choose a base branch
from
Wirone:codito/navigate-to-non-meta-tokens
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+135
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
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 | ||
---|---|---|---|---|
|
@@ -523,6 +523,18 @@ public function getCodeHash(): string | |||
return $this->codeHash; | ||||
} | ||||
|
||||
/** | ||||
* Get index for closest next token which is non meta (whitespace, comments, PHP8 attribute). | ||||
* | ||||
* This method is shorthand for getNonMetaSibling method. | ||||
* | ||||
* @param int $index token index | ||||
*/ | ||||
public function getNextNonMeta(int $index): ?int | ||||
{ | ||||
return $this->getNonMetaSibling($index, 1); | ||||
} | ||||
|
||||
/** | ||||
* Get index for closest next token which is non whitespace. | ||||
* | ||||
|
@@ -572,6 +584,43 @@ public function getNonWhitespaceSibling(int $index, int $direction, ?string $whi | |||
} | ||||
} | ||||
|
||||
/** | ||||
* Get index for closest sibling token which is non meta (whitespace, comment, PHP8 attribute). | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. curious - is |
||||
* | ||||
* @param int $index token index | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
* @param -1|1 $direction | ||||
*/ | ||||
public function getNonMetaSibling(int $index, int $direction): ?int | ||||
{ | ||||
if (!\defined('T_ATTRIBUTE')) { | ||||
return $this->getMeaningfulTokenSibling($index, $direction); | ||||
} | ||||
|
||||
do { | ||||
$index = $this->getMeaningfulTokenSibling($index, $direction); | ||||
|
||||
if (1 === $direction && $this[$index]->isGivenKind(T_ATTRIBUTE)) { | ||||
$index = $this->findBlockEnd(self::BLOCK_TYPE_ATTRIBUTE, $index); | ||||
} elseif (-1 === $direction && $this[$index]->isGivenKind(CT::T_ATTRIBUTE_CLOSE)) { | ||||
$index = $this->findBlockStart(self::BLOCK_TYPE_ATTRIBUTE, $index); | ||||
} | ||||
} while ($this[$index]->isGivenKind([T_ATTRIBUTE, CT::T_ATTRIBUTE_CLOSE])); | ||||
|
||||
return $index; | ||||
} | ||||
|
||||
/** | ||||
* Get index for closest previous token which is non meta (whitespace, comment, PHP8 attribute). | ||||
* | ||||
* This method is shorthand for getNonMetaSibling method. | ||||
* | ||||
* @param int $index token index | ||||
*/ | ||||
public function getPrevNonMeta(int $index): ?int | ||||
{ | ||||
return $this->getNonMetaSibling($index, -1); | ||||
} | ||||
|
||||
/** | ||||
* Get index for closest previous token which is non whitespace. | ||||
* | ||||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general, for any code:
I struggle to understand the need for code without that code being used. For now, it looks only as a dead code
( and I would love to have automation to prevent any deadcode being in repo, as side note ;) )
I also fail to understand how common this will get used - is it's needed only for very limited usecase (and thus encapsulated into method of class, where will be the usage) or spread across widely (thus should be exposed via "main" Tokens).
For that, I would love to see actual usage of code, eg in the PR that is actually using it.
We could also have a PR adding Tokens code (without usage), and other feature PR build on top, that shows how it will be used.
Or other way around, merge feature PR without having this logic externalised to Tokens, and then refactoring PR with code moving to Tokens and getting use in multiple rules.