diff --git a/composer.json b/composer.json index a8f85ce..faa2957 100644 --- a/composer.json +++ b/composer.json @@ -34,7 +34,8 @@ "phpstan/phpstan": "^2.0", "phpstan/extension-installer": "^1.4.3", "illuminate/support": "^11.30", - "symfony/process": "^7.2" + "symfony/process": "^7.2", + "rogervila/array-diff-multidimensional": "^2.1" }, "config": { "allow-plugins": { diff --git a/meta/sample.php b/meta/sample.php index a9014b9..7e5bacd 100644 --- a/meta/sample.php +++ b/meta/sample.php @@ -3,10 +3,13 @@ set_time_limit(2); use Phiki\Environment\Environment; +use Phiki\Grammar\DefaultGrammars; use Phiki\Phiki; use Phiki\Theme\Theme; +use Phiki\Token\Token; +use Symfony\Component\Process\Process; -require_once __DIR__.'/../vendor/autoload.php'; +require_once __DIR__ . '/../vendor/autoload.php'; set_error_handler(function ($severity, $message, $file, $line) { throw new ErrorException($message, 0, $severity, $file, $line); @@ -20,10 +23,43 @@ $grammars = $repository->getAllGrammarNames(); natsort($grammars); -$sample = file_get_contents(__DIR__.'/../resources/samples/'.$grammar.'.sample'); +$sample = file_get_contents($samplePath = __DIR__ . '/../resources/samples/' . $grammar . '.sample'); $tokens = (new Phiki($environment))->codeToTokens($sample, $grammar); $html = (new Phiki($environment))->codeToHtml($sample, $grammar, ['light' => Theme::GithubLight, 'dark' => Theme::GithubDark], $withGutter); +$process = new Process( + [ + 'node', + __DIR__ . '/../tests/Fixtures/vscode-textmate-compliance.js', + $samplePath, + array_flip(DefaultGrammars::SCOPES_TO_NAMES)[$grammar], + json_encode(collect(DefaultGrammars::SCOPES_TO_NAMES) + ->mapWithKeys(fn(string $name, string $scope) => [$scope => DefaultGrammars::NAMES_TO_PATHS[$name]]) + ->all()), + ], +); + +$process->run(); + +if (! $process->isSuccessful()) { + throw new RuntimeException($process->getErrorOutput() . ':' . PHP_EOL . $process->getOutput()); +} + +$vscodeTextmateOutput = array_map( + fn(array $lineTokens) => array_map( + fn(array $token) => new Token( + scopes: $token['scopes'], + text: $token['text'], + start: $token['start'], + end: $token['end'], + ), + $lineTokens + ), + json_decode($process->getOutput(), true), +); + +$tokenDiff = array_diff_multidimensional($tokens, $vscodeTextmateOutput, false); + ?> @@ -102,7 +138,22 @@ class="flex items-center gap-x-4"> = $html ?> - +
Phiki tokens:
+ +vscode-textmate tokens:
+ +Differences:
+ +