Create php8 fork
All checks were successful
timw4mail/php-kilo/pipeline/head This commit looks good

This commit is contained in:
Timothy Warren 2020-12-04 11:18:21 -05:00
parent 1e4735ee38
commit 7cd7d1baa5
11 changed files with 2718 additions and 1184 deletions

View File

@ -15,8 +15,8 @@
},
"require-dev": {
"ext-json": "*",
"phpunit/phpunit": "^8",
"spatie/phpunit-snapshot-assertions": "^2.2.0"
"phpunit/phpunit": "^9.5.0",
"spatie/phpunit-snapshot-assertions": "^4.2.0"
},
"scripts": {
"coverage": "phpdbg -qrr -- vendor/bin/phpunit -c phpunit.xml tests",

2070
composer.lock generated

File diff suppressed because it is too large Load Diff

59
kilo
View File

@ -6,46 +6,7 @@ namespace Aviat\Kilo;
require_once __DIR__ . '/vendor/autoload.php';
// Log notices/errors/warnings to file
set_error_handler(static function (int $no, $str, $file, $line) {
$msg = print_r([
'errno' => $no,
'message' => $str,
'file' => $file,
'line' => $line,
], TRUE);
file_put_contents('kilo.log', $msg, FILE_APPEND);
}, -1);
// ! Init with an IIFE
return (static function (int $argc, array $argv): int {
Termios::enableRawMode();
register_shutdown_function([Termios::class, 'disableRawMode']);
$editor = Editor::new();
if ($argc >= 2)
{
$editor->open($argv[1]);
}
$editor->setStatusMessage('HELP: Ctrl-S = save | Ctrl-Q = quit | Ctrl-F = find');
try
{
// Input Loop
while (true)
{
$editor->refreshScreen();
$char = $editor->processKeypress();
if ($char === NULL)
{
break;
}
}
}
catch (\Throwable $e)
{
set_exception_handler(static function (\Throwable $e) {
$msg = print_r([
'code' => $e->getCode(),
'message' => $e->getMessage(),
@ -53,9 +14,25 @@ return (static function (int $argc, array $argv): int {
'line' => $e->getLine(),
'trace' => $e->getTraceAsString(),
], TRUE);
file_put_contents('kilo.exception.log', $msg, FILE_APPEND);
file_put_contents('kilo.log', $msg, FILE_APPEND);
});
// ! Init with an IIFE
return (static function (int $argc, array $argv): int {
Termios::enableRawMode();
register_shutdown_function([Termios::class, 'disableRawMode']);
$editor = Editor::new();
$editor->setStatusMessage('HELP: Ctrl-S = save | Ctrl-Q = quit | Ctrl-F = find');
if ($argc >= 2)
{
$editor->open($argv[1]);
}
// Input Loop
do { $editor->refreshScreen();} while ($editor->processKeypress() !== NULL);
return 0;
})($argc, $argv);

View File

@ -1,24 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
colors="true"
stopOnFailure="false"
beStrictAboutTestsThatDoNotTestAnything="true"
>
<filter>
<whitelist>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" colors="true" stopOnFailure="false" beStrictAboutTestsThatDoNotTestAnything="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
<exclude>
<file>src/constants.php</file>
</exclude>
</whitelist>
</filter>
<report>
<html outputDirectory="coverage"/>
<text outputFile="php://stdout" showUncoveredFiles="true"/>
</report>
</coverage>
<testsuites>
<testsuite name="PHPKilo">
<directory phpVersion="7.4.0" phpVersionOperator=">=">tests</directory>
<directory phpVersion="7.4.0" phpVersionOperator="&gt;=">tests</directory>
</testsuite>
</testsuites>
<logging>
<log type="coverage-html" target="coverage"/>
<log type="coverage-text" target="php://stdout" showUncoveredFiles="true" />
</logging>
<logging/>
</phpunit>

View File

@ -52,7 +52,7 @@ class Editor {
$this->screenRows -= 2;
}
public function __get(string $name)
public function __get(string $name): ?int
{
if ($name === 'numRows')
{
@ -96,7 +96,6 @@ class Editor {
KeyCode::ARROW_UP => KeyType::ARROW_UP,
KeyCode::DEL_KEY => KeyType::DEL_KEY,
KeyCode::ENTER => KeyType::ENTER,
KeyCode::ESCAPE => KeyType::ESCAPE,
KeyCode::PAGE_DOWN => KeyType::PAGE_DOWN,
KeyCode::PAGE_UP => KeyType::PAGE_UP,
@ -104,6 +103,10 @@ class Editor {
KeyCode::CTRL('h') => KeyType::BACKSPACE,
KeyCode::BACKSPACE => KeyType::BACKSPACE,
// Escape
KeyCode::CTRL('l') => KeyType::ESCAPE,
KeyCode::ESCAPE => KeyType::ESCAPE,
// Home Key
"\eOH" => KeyType::HOME_KEY,
"\e[1~" => KeyType::HOME_KEY,
@ -271,6 +274,8 @@ class Editor {
protected function insertNewline(): void
{
// @TODO attempt smart indentation on newline?
if ($this->cursorX === 0)
{
$this->insertRow($this->cursorY, '');
@ -432,6 +437,11 @@ class Editor {
$current = $lastMatch;
if (empty($query))
{
return;
}
for ($i = 0; $i < $this->numRows; $i++)
{
$current += $direction;
@ -717,7 +727,7 @@ class Editor {
{
$callback($buffer, $c);
}
return '';
return ($c === KeyType::ENTER) ? $buffer : '';
}
if ($c === KeyType::DEL_KEY || $c === KeyType::BACKSPACE)
@ -786,7 +796,7 @@ class Editor {
$row = ($this->cursorY >= $this->numRows)
? NULL
: $this->rows[$this->cursorY];
$rowlen = $row ? $row->size : 0;
$rowlen = $row->size ?? 0;
if ($this->cursorX > $rowlen)
{
$this->cursorX = $rowlen;
@ -903,7 +913,7 @@ class Editor {
protected function refreshSyntax(): void
{
// Update the syntax highlighting for all the rows of the file
array_walk($this->rows, fn (Row $row) => $row->updateSyntax());
array_walk($this->rows, static fn (Row $row) => $row->updateSyntax());
}
private function refreshPHPSyntax(): void

View File

@ -495,7 +495,7 @@ class Row {
break;
}
$char = $token['char'];
$char = $token['char'] ?? '';
$charLen = strlen($char);
if ($charLen === 0 || $offset >= $this->rsize)
{

View File

@ -35,6 +35,11 @@ class PHP {
*/
public static function getTokens(string $code): array
{
if (class_exists('PhpToken'))
{
return \PhpToken::tokenize($code);
}
return (new self($code))->organizeTokens();
}
@ -77,6 +82,11 @@ class PHP {
return $this->tokens;
}
protected function processObjectToken(\PhpToken $token)
{
}
protected function processArrayToken(array $token): void
{
[$type, $rawChar, $currentLine] = $token;

View File

@ -283,7 +283,7 @@ function syntax_to_color(int $hl): int
* Replace tabs with the specified number of spaces.
*
* @param string $str
* @param int? $number
* @param int|null $number
* @return string
*/
function tabs_to_spaces(string $str, ?int $number = KILO_TAB_STOP): string

View File

@ -8,9 +8,10 @@ abstract class Foo implements Ifoo {
* @param float $b
* @param array $c
* @param callable $d
* @param string $e
* @return string
*/
abstract public function bar(int $a, float $b, array $c, callable $d): string;
abstract public function bar(int $a, float $b, array $c, callable $d, string $e): string;
protected function doNothing(): void {}
}
@ -41,6 +42,8 @@ class FooBar extends Foo implements Ifoo {
}
}
$square = fn (int $x) => $x ** 2;
/*
* Multi-line comment
*/
@ -76,7 +79,11 @@ $template = <<<'TEMPLATE'
TEMPLATE;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>HTML</title>
</head>
<body>
<h1><?= $_SERVER['HTTP_HOST'] ?></h1>
</body>

View File

@ -18,7 +18,7 @@ class KeyCodeTest extends TestCase {
$actual = KeyCode::CTRL($char);
$this->assertEquals(ctrl_key($char), $ord, "chr(ctrl_key) !== CTRL");
$this->assertEquals(ctrl_key($char), $ord, 'chr(ctrl_key) !== CTRL');
$this->assertEquals($expected, $actual, "CTRL+'{$char}' should return chr($ord)");
}
}

File diff suppressed because it is too large Load Diff