Skip to content

Commit

Permalink
formats
Browse files Browse the repository at this point in the history
  • Loading branch information
felixdorn committed Oct 4, 2022
1 parent ae2b4e1 commit 175ea8f
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions src/Tin.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public function highlight(string $code): string
"\e[38;2;%sm%s | \e[0m",
$this->theme->comment,
str_pad(
(string)$line,
strlen((string)$lineCount),
(string) $line,
strlen((string) $lineCount),
' ',
STR_PAD_LEFT
));
Expand All @@ -35,17 +35,17 @@ public function highlight(string $code): string

public function process(string $code, callable $transformer): string
{
$code = rtrim(str_replace(["\r\n", "\r"], "\n", $code));
$code = rtrim(str_replace(["\r\n", "\r"], "\n", $code));
$highlighted = array_fill(1, substr_count($code, PHP_EOL) + 1, []);
$tokens = $this->tokenize($code);
$tokens = $this->tokenize($code);
$inAttribute = false;

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 Down Expand Up @@ -82,8 +82,8 @@ public function process(string $code, callable $transformer): string
array_keys($highlighted),
function (string $_, int|string $line) use ($transformer, $highlighted) {
$line = $transformer(
(int)$line,
$highlighted[(int)$line],
(int) $line,
$highlighted[(int) $line],
count($highlighted),
$this->theme
);
Expand All @@ -96,12 +96,12 @@ function (string $_, int|string $line) use ($transformer, $highlighted) {

private function tokenize(string $code): array
{
$tokens = Token::tokenize($code);
$tokens = Token::tokenize($code);
$indexed = [];
$line = 1;
$line = 1;

foreach ($tokens as $token) {
$splits = explode("\n", $token->text);
$splits = explode("\n", $token->text);
$newLines = count($splits) - 1;

foreach ($splits as $split) {
Expand Down Expand Up @@ -141,17 +141,17 @@ protected function idFromContext(array $tokens, int $index): int
return T_NAMED_PARAMETER;
}

$behind = $this->lookBehind($tokens, $index - 1);
$behind = $this->lookBehind($tokens, $index - 1);
$twoBehind = $this->lookBehind($tokens, $index - 2);

return match ($behind->id) {
T_NEW, T_USE, T_PRIVATE, T_PROTECTED, T_PUBLIC, T_NAMESPACE, T_CLASS, T_INTERFACE, T_TRAIT, T_EXTENDS, T_IMPLEMENTS, T_INSTEADOF, 58, 124, 63, 44 => T_CLASS_NAME,
T_FUNCTION => $twoBehind->is([T_PRIVATE, T_PROTECTED, T_PUBLIC, T_STATIC]) ? T_METHOD_NAME : T_FUNCTION_DECL,
T_FUNCTION => $twoBehind->is([T_PRIVATE, T_PROTECTED, T_PUBLIC, T_STATIC]) ? T_METHOD_NAME : T_FUNCTION_DECL,
T_OBJECT_OPERATOR => $ahead->text === '(' ? T_METHOD_NAME : T_VARIABLE,
T_DOUBLE_COLON => $ahead->text === '(' || $ahead->id === T_INSTEADOF || $ahead->id === T_AS ? T_METHOD_NAME : T_CONST_NAME,
T_AS => T_METHOD_NAME,
T_ATTRIBUTE => T_ATTRIBUTE_CLASS,
default => (function () use ($ahead, $twoBehind, $tokens, $index) {
T_DOUBLE_COLON => $ahead->text === '(' || $ahead->id === T_INSTEADOF || $ahead->id === T_AS ? T_METHOD_NAME : T_CONST_NAME,
T_AS => T_METHOD_NAME,
T_ATTRIBUTE => T_ATTRIBUTE_CLASS,
default => (function () use ($ahead, $twoBehind, $tokens, $index) {
if ($ahead->text === '(') {
return T_FUNCTION_NAME;
}
Expand Down

0 comments on commit 175ea8f

Please sign in to comment.