Skip to content

Commit

Permalink
skip tokens in output
Browse files Browse the repository at this point in the history
  • Loading branch information
felixdorn committed Nov 26, 2021
1 parent 6df2810 commit cd3fba6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# tin

tin is a PHP code highlighter for the terminal.
tin is a PHP code highlighter for the terminal.

[![Tests](https://github.com/felixdorn/tin/actions/workflows/tests.yml/badge.svg?branch=main)](https://github.com/felixdorn/tin/actions/workflows/tests.yml)
[![Formats](https://github.com/felixdorn/tin/actions/workflows/formats.yml/badge.svg?branch=main)](https://github.com/felixdorn/tin/actions/workflows/formats.yml)
[![Version](https://poser.pugx.org/felixdorn/tin/version)](//packagist.org/packages/felixdorn/tin)
Expand Down Expand Up @@ -47,11 +47,21 @@ use Felix\Tin\Token;

$tin->process(
$code,
fn(Token $token, Token $lastToken) => $token->line . '| ' . $token->text
function(Token $token, Token $lastToken): ?string {
// won't print any of the ";" tokens
if ($token->is(";")) {
return null;
}

return $token->line . '| ' . $token->text;
}
)
```

> Last token represents the last token in the input code, it is very useful as you can then get the number of lines in the code with `$lastToken->line` to properly indent line numbers for example.
Last token represents the last token in the input code, it is very useful as you can then get the number of lines in the
code with `$lastToken->line` to properly indent line numbers for example.

If you return `null` from the callback, the token will be skipped.

## Themes

Expand Down Expand Up @@ -87,6 +97,7 @@ class OneDark extends Theme
* Various outputs (cli / web)
* grayscale theme
* better themes (will also boost performances a lot!!)

## Known Issues

Named parameters are simply ignored by the built-in PHP parser which means that if you're named parameter is also a
Expand Down
11 changes: 8 additions & 3 deletions src/Tin.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,14 @@ public function process(string $code, callable $transformer): string
};
}

$text = $token->text;
$token->text = "\e[38;2;" . $color . 'm' . $token->text . "\e[0m";
$highlighted .= $transformer($token, $lastToken);
$text = $token->text;
$token->text = "\e[38;2;" . $color . 'm' . $token->text . "\e[0m";
$highlightedToken = $transformer($token, $lastToken);

if ($highlightedToken !== null) {
$highlighted .= $highlightedToken;
}

$token->text = $text;
}

Expand Down
14 changes: 14 additions & 0 deletions tests/TinTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,17 @@

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

it('can skip tokens', function () {
$tin = Tin::from(new OneDark());

$output = $tin->process('<?php echo "Hello world";', function (Token $token, Token $lastToken) {
if ($token->id === T_ECHO) {
return null;
}

return $token->id;
});

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

0 comments on commit cd3fba6

Please sign in to comment.