Skip to content

Commit

Permalink
formats; special type for doc blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
felixdorn committed Oct 12, 2022
1 parent faae829 commit 65ef649
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/Enums/TokenType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ enum TokenType: string
case Keyword = 'keyword';
case Variable = 'variable';
case Comment = 'comment';
case DocComment = 'docComment';
case String = 'string';
case Function = 'function';
case Number = 'number';
Expand All @@ -30,7 +31,8 @@ public static function fromId(int $id): self

return match ($id) {
T_METHOD_NAME, T_FUNCTION_DECL => TokenType::Function,
T_COMMENT, T_DOC_COMMENT => TokenType::Comment,
T_COMMENT => TokenType::Comment,
T_DOC_COMMENT => TokenType::DocComment,
T_CONSTANT_ENCAPSED_STRING, T_ENCAPSED_AND_WHITESPACE => TokenType::String,
T_INLINE_HTML => TokenType::Html,
T_LNUMBER, T_DNUMBER => TokenType::Number,
Expand Down
2 changes: 1 addition & 1 deletion src/Themes/JetbrainsDark.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function color(TokenType $type): string
return match ($type) {
TokenType::Keyword => '204;102;50',
TokenType::Variable => '152;118;170',
TokenType::LineNumber, TokenType::Comment => '128;128;128',
TokenType::DocComment, TokenType::LineNumber, TokenType::Comment => '128;128;128',
TokenType::String => '106;135;89',
TokenType::Function, TokenType::NamedParameter, TokenType::Attribute => '255;198;109',
TokenType::Number, TokenType::Html => '104;151;187',
Expand Down
2 changes: 1 addition & 1 deletion src/Themes/OneDark.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function color(TokenType $type): string
return match ($type) {
TokenType::Keyword => '199;120;221',
TokenType::Variable => '224;107;116',
TokenType::LineNumber, TokenType::Comment => '91;98;110',
TokenType::DocComment, TokenType::LineNumber, TokenType::Comment => '91;98;110',
TokenType::String => '152;195;121',
TokenType::Function, TokenType::NamedParameter, TokenType::Attribute => '98;174;239',
TokenType::Number, TokenType::Html => '229;192;122',
Expand Down
14 changes: 7 additions & 7 deletions src/Tin.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ public function highlight(string $code): string
*/
public function process(string $code, callable $transformer): string
{
$tokens = $this->groupTokensByLine(
$buffer = '';
$tokens = $this->groupTokensByLine(
Tokenizer::tokenize($code)
);
$totalLines = $tokens->count();
$buffer = '';

foreach ($tokens as $n => $lineTokens) {
if ($line = $transformer(new Line($n + 1, $lineTokens, $totalLines, $this->output))) {
Expand All @@ -95,22 +95,22 @@ private function groupTokensByLine(iterable $tokens): SplQueue
$grouped = new SplQueue();

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

// add empty queues for new lines
for ($i = $lineIndex; $i <= $lineIndex + $newLineCount; $i++) {
if (isset($grouped[$i])) {
for ($i = 0; $i <= $newLineCount; $i++) {
if (isset($grouped[$lineIndex + $i])) {
continue;
}

/** @var SplQueue<Token> $queue */
$queue = new SplQueue();
$grouped->add($i, $queue);
$grouped->add($lineIndex + $i, $queue);
}

foreach ($lines as $line) {
if ($token->id === T_INLINE_HTML) {
if ($token->id === T_INLINE_HTML || $token->id === T_DOC_COMMENT) {
$grouped[$lineIndex]?->push($token->withText($line));
$lineIndex++;
continue;
Expand Down
10 changes: 10 additions & 0 deletions src/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ protected function process(): Generator
$inAttribute = false;

foreach ($raw as $index => $token) {
// Remove a trailing new line if the T_WHITESPACE succeeds to a T_DOC_COMMENT
if ($token->is(T_WHITESPACE) && $index - 1 >= 0 && $raw[$index - 1]->is(T_DOC_COMMENT)) {
if (str_ends_with($token->text, PHP_EOL)) {
$token->text = substr($token->text, 0, -1);
}

yield Token::fromPhpToken(T_WHITESPACE, $token);
continue;
}

if ($token->is(['true', 'false', 'null', 'string', 'int', 'float', 'object', 'callable', 'array', 'iterable', 'bool', 'self'])) {
yield Token::fromPhpToken(T_BUILTIN_TYPE, $token);
continue;
Expand Down

0 comments on commit 65ef649

Please sign in to comment.