Skip to content

Commit

Permalink
add a firstInLine property on tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
felixdorn committed Nov 28, 2021
1 parent 79396a3 commit 78e9db7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ $tin->process(
return null;
}

if ($token->firstInLine) {
return $token->line . '| ' . $token->text;
}

return $token->text;
}
)
Expand Down
23 changes: 14 additions & 9 deletions src/Tin.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ public function highlight(string $code): string

public function process(string $code, callable $transformer): string
{
$highlighted = '';
$tokens = Token::tokenize($code);
$highlighted = '';
$tokens = Token::tokenize($code);
$inAttribute = false;
$lastToken = $tokens[array_key_last($tokens)];
$lastToken = $tokens[array_key_last($tokens)];

$lastLine = -1;
foreach ($tokens as $index => $token) {
if ($token->id !== T_STRING) {
if ($token->text === ':' && $tokens[$index - 1]->id === T_NAMED_PARAMETER) {
$token->id = T_NAMED_PARAMETER;
} elseif ($inAttribute && $token->text === ']' && ($tokens[$index + 1]->id === T_WHITESPACE || $tokens[$index + 1]->id === T_ATTRIBUTE)) {
$token->id = T_ATTRIBUTE_END;
$token->id = T_ATTRIBUTE_END;
$inAttribute = false;
} elseif ($token->id === T_ATTRIBUTE) {
$inAttribute = true;
Expand All @@ -42,8 +44,8 @@ public function process(string $code, callable $transformer): string
$token->id = $this->idFromContext($tokens, $index);
}

// token with ids lower than 256 are equal to ord($token->text)
// and will never be colorized as they are things like ;{}()[]
// token with ids lower than 256 are equal to ord($token->text)
// and will never be colorized as they are things like ;{}()[]
if ($token->id < 256) {
$color = $this->theme->default;
} else {
Expand All @@ -61,8 +63,11 @@ public function process(string $code, callable $transformer): string
};
}

$text = $token->text;
$token->text = "\e[38;2;" . $color . 'm' . $token->text . "\e[0m";
$text = $token->text;
$token->text = "\e[38;2;" . $color . 'm' . $token->text . "\e[0m";
$token->firstInLine = $lastLine !== $token->line;
$lastLine = $token->line;

$highlightedToken = $transformer($token, $lastToken);

if ($highlightedToken !== null) {
Expand All @@ -76,7 +81,7 @@ public function process(string $code, callable $transformer): string
}

/**
* Find the real type of T_STRING token which is one of :
* Find the real type of T_STRING token which is one of :.
*
* - T_CLASS_NAME
* - T_FUNCTION_DECL
Expand Down
1 change: 1 addition & 0 deletions src/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@

class Token extends PhpToken
{
public bool $firstInLine = false;
}
10 changes: 10 additions & 0 deletions tests/TinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,13 @@

expect($output)->toBe(T_OPEN_TAG . T_WHITESPACE . T_CONSTANT_ENCAPSED_STRING . ord(';'));
});

it('marks the first tokens in a line as first', function () {
$tin = Tin::from(new OneDark());

$output = $tin->process('<?php echo "Hello world";', function (Token $token, Token $lastToken) {
return $token->firstInLine ? 'y' : 'n';
});

expect($output)->toBe('ynnnn');
});

0 comments on commit 78e9db7

Please sign in to comment.