Compose source slicing and semantic highlighting into immutable annotated code snippets.
CodeKit connects CodeSlicer, CodeHighlight, and CodeSnippet. It selects source code, derives semantic syntax annotations, and returns a renderer-independent snippet while preserving source context.
use Alto\Code\Kit\CodeKit;
use Alto\Code\Slicer\CodeSource;
$slice = CodeSource::fromFile('src/Command/BuildCommand.php')
->slice()
->method('execute');
$snippet = (new CodeKit())
->snippet($slice)
->dedent();Install ALTO CodeKit with Composer:
composer require alto/code-kitCodeKit requires PHP 8.4 or later. It installs CodeSlicer, CodeHighlight, and CodeSnippet.
CodeKit parses the complete source before projecting the selected range. Context-sensitive syntax therefore remains correct when a slice starts inside a docblock, string, heredoc, or embedded language.
$snippet = (new CodeKit())->snippet(
CodeSource::fromFile('src/Example.php')->lines(24, 32),
);The resulting CodeSnippet keeps its source name, original line numbers, language, and syntax
annotations.
A single line needs no surrounding output context:
$line = (new CodeKit())->snippet(
CodeSource::fromString($code, 'php')->lines(24, 24),
);Use annotate() when a snippet already exists:
use Alto\Code\Snippet\CodeSnippet;
$snippet = CodeSnippet::fromCode('$total = array_sum($prices);', 'php');
$annotated = (new CodeKit())->annotate($snippet);Existing metadata, selections, and annotations remain available. Each added syntax annotation
contains the semantic scope and tokenType supplied by CodeHighlight. Whitespace is left
unannotated.
CodeKit owns orchestration only. CodeSlicer locates source ranges, CodeHighlight parses syntax, and CodeSnippet carries the immutable result. Rendering to HTML, SVG, PNG, terminals, or slides belongs to consumers.
See the documentation for installation, a guided example, and the public API.
Contributions of all kinds are welcome. Visit the project on GitHub to report a bug, suggest a feature, or open a pull request. Before submitting code, run:
# Runs PHP CS Fixer, PHPStan, and PHPUnit
composer qaChanges to public behavior should include tests and documentation.
ALTO CodeKit is open source. You can support its continued development through GitHub Sponsors.
Sharing this package with others or starring it on GitHub is also much appreciated.
ALTO CodeKit is released by ALTO PHP under the MIT License.