Skip to content

Commit b747e36

Browse files
✨ Group multiple Tab or multiple Whitespace in one token
1 parent 4b3a887 commit b747e36

File tree

2 files changed

+34
-18
lines changed

2 files changed

+34
-18
lines changed

TwigCS/Sniff/Standard/DelimiterSpacingSniff.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,17 @@ public function process(Token $token, $tokenPosition, $tokens)
4848
*/
4949
public function processStart(Token $token, $tokenPosition, $tokens)
5050
{
51-
$offset = 1;
52-
while ($this->isTokenMatching($tokens[$tokenPosition + $offset], Token::WHITESPACE_TYPE)) {
53-
++$offset;
54-
}
55-
5651
// Ignore new line
57-
if ($this->isTokenMatching($tokens[$tokenPosition + $offset], Token::EOL_TYPE)) {
52+
if ($this->isTokenMatching($tokens[$tokenPosition + 1], Token::EOL_TYPE)) {
5853
return;
5954
}
6055

61-
$count = $offset - 1;
56+
if ($this->isTokenMatching($tokens[$tokenPosition + 1], Token::WHITESPACE_TYPE)) {
57+
$count = strlen($tokens[$tokenPosition + 1]->getValue());
58+
} else {
59+
$count = 0;
60+
}
61+
6262
if (1 !== $count) {
6363
$this->addMessage(
6464
$this::MESSAGE_TYPE_ERROR,
@@ -77,17 +77,17 @@ public function processStart(Token $token, $tokenPosition, $tokens)
7777
*/
7878
public function processEnd(Token $token, $tokenPosition, $tokens)
7979
{
80-
$offset = 1;
81-
while ($this->isTokenMatching($tokens[$tokenPosition - $offset], Token::WHITESPACE_TYPE)) {
82-
++$offset;
83-
}
84-
8580
// Ignore new line
86-
if ($this->isTokenMatching($tokens[$tokenPosition - $offset], Token::EOL_TYPE)) {
81+
if ($this->isTokenMatching($tokens[$tokenPosition - 1], Token::EOL_TYPE)) {
8782
return;
8883
}
8984

90-
$count = $offset - 1;
85+
if ($this->isTokenMatching($tokens[$tokenPosition - 1], Token::WHITESPACE_TYPE)) {
86+
$count = strlen($tokens[$tokenPosition - 1]->getValue());
87+
} else {
88+
$count = 0;
89+
}
90+
9191
if (1 !== $count) {
9292
$this->addMessage(
9393
$this::MESSAGE_TYPE_ERROR,

TwigCS/Token/Tokenizer.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -435,14 +435,30 @@ protected function lexStart()
435435

436436
protected function lexTab()
437437
{
438-
$this->pushToken(Token::TAB_TYPE);
439-
$this->moveCursor($this->code[$this->cursor]);
438+
$currentToken = $this->code[$this->cursor];
439+
$whitespace = '';
440+
441+
while (preg_match('/\t/', $currentToken)) {
442+
$whitespace .= $currentToken;
443+
$this->moveCursor($currentToken);
444+
$currentToken = $this->code[$this->cursor];
445+
}
446+
447+
$this->pushToken(Token::TAB_TYPE, $whitespace);
440448
}
441449

442450
protected function lexWhitespace()
443451
{
444-
$this->pushToken(Token::WHITESPACE_TYPE, $this->code[$this->cursor]);
445-
$this->moveCursor($this->code[$this->cursor]);
452+
$currentToken = $this->code[$this->cursor];
453+
$whitespace = '';
454+
455+
while (' ' === $currentToken) {
456+
$whitespace .= $currentToken;
457+
$this->moveCursor($currentToken);
458+
$currentToken = $this->code[$this->cursor];
459+
}
460+
461+
$this->pushToken(Token::WHITESPACE_TYPE, $whitespace);
446462
}
447463

448464
protected function lexEOL()

0 commit comments

Comments
 (0)