From a3e902967ab48aac25d6fdadbcb43a46f4e1b766 Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Tue, 19 Nov 2019 15:57:51 -0500 Subject: [PATCH] A little cleanup refactoring --- src/Editor.php | 30 ++++++++------------ src/Enum/C.php | 4 ++- src/Enum/Color.php | 4 ++- src/Enum/Highlight.php | 4 ++- src/Enum/Key.php | 4 ++- src/Row.php | 2 +- src/Termios.php | 36 ++++++++++++++++-------- src/TermiosException.php | 12 ++++++++ src/Tokens/PHP.php | 2 +- src/{Enum => Traits}/ConstList.php | 2 +- src/{ => Traits}/MagicProperties.php | 2 +- tests/Tokens/PHPTest.php | 20 +++++++++---- tests/{Enum => Traits}/ConstListTest.php | 4 +-- 13 files changed, 81 insertions(+), 45 deletions(-) create mode 100644 src/TermiosException.php rename src/{Enum => Traits}/ConstList.php (86%) rename src/{ => Traits}/MagicProperties.php (91%) rename tests/{Enum => Traits}/ConstListTest.php (85%) diff --git a/src/Editor.php b/src/Editor.php index 9c970a5..bfa78e9 100644 --- a/src/Editor.php +++ b/src/Editor.php @@ -13,7 +13,7 @@ use Aviat\Kilo\Tokens\PHP; * @property-read int numRows */ class Editor { - use MagicProperties; + use Traits\MagicProperties; private string $ab = ''; @@ -51,6 +51,7 @@ class Editor { [$this->screenRows, $this->screenCols] = get_window_size(); + // Remove a row for the status bar, and one for the message bar $this->screenRows -= 2; } @@ -221,7 +222,7 @@ class Editor { $this->dirty++; // Re-tokenize the file - if ($updateSyntax && $this->syntax->filetype === 'PHP') + if ($updateSyntax) { $this->refreshPHPSyntax(); } @@ -245,10 +246,7 @@ class Editor { } // Re-tokenize the file - if ($this->syntax->filetype === 'PHP') - { - $this->refreshPHPSyntax(); - } + $this->refreshPHPSyntax(); $this->dirty++; } @@ -266,10 +264,7 @@ class Editor { $this->rows[$this->cursorY]->insertChar($this->cursorX, $c); // Re-tokenize the file - if ($this->syntax->filetype === 'PHP') - { - $this->refreshPHPSyntax(); - } + $this->refreshPHPSyntax(); $this->cursorX++; } @@ -297,10 +292,7 @@ class Editor { $this->cursorX = 0; // Re-tokenize the file - if ($this->syntax->filetype === 'PHP') - { - $this->refreshPHPSyntax(); - } + $this->refreshPHPSyntax(); } protected function deleteChar(): void @@ -325,10 +317,7 @@ class Editor { } // Re-tokenize the file - if ($this->syntax->filetype === 'PHP') - { - $this->refreshPHPSyntax(); - } + $this->refreshPHPSyntax(); } // ------------------------------------------------------------------------ @@ -931,6 +920,11 @@ class Editor { private function refreshPHPSyntax(): void { + if ($this->syntax->filetype !== 'PHP') + { + return; + } + $this->tokens = PHP::getTokens($this->rowsToString()); for ($i = 0; $i < $this->numRows; $i++) { diff --git a/src/Enum/C.php b/src/Enum/C.php index c415488..cedf72c 100644 --- a/src/Enum/C.php +++ b/src/Enum/C.php @@ -2,11 +2,13 @@ namespace Aviat\Kilo\Enum; +use Aviat\Kilo\Traits; + /** * Just a namespace for C language constants */ class C { - use ConstList; + use Traits\ConstList; // ------------------------------------------------------------------------ // ! Misc I/O constants diff --git a/src/Enum/Color.php b/src/Enum/Color.php index 74e937e..1fe6085 100644 --- a/src/Enum/Color.php +++ b/src/Enum/Color.php @@ -2,11 +2,13 @@ namespace Aviat\Kilo\Enum; +use Aviat\Kilo\Traits; + /** * ANSI Color escape sequences */ class Color { - use ConstList; + use Traits\ConstList; // Foreground colors public const FG_BLACK = 30; diff --git a/src/Enum/Highlight.php b/src/Enum/Highlight.php index ffa64cc..c9c4b09 100644 --- a/src/Enum/Highlight.php +++ b/src/Enum/Highlight.php @@ -2,8 +2,10 @@ namespace Aviat\Kilo\Enum; +use Aviat\Kilo\Traits; + class Highlight { - use ConstList; + use Traits\ConstList; public const NORMAL = 0; public const COMMENT = 1; diff --git a/src/Enum/Key.php b/src/Enum/Key.php index bccf5c0..a9ab9f1 100644 --- a/src/Enum/Key.php +++ b/src/Enum/Key.php @@ -2,8 +2,10 @@ namespace Aviat\Kilo\Enum; +use Aviat\Kilo\Traits; + class Key { - use ConstList; + use Traits\ConstList; public const ARROW_DOWN = 'ARROW_DOWN'; public const ARROW_LEFT = 'ARROW_LEFT'; diff --git a/src/Row.php b/src/Row.php index 194e5e1..97a6c81 100644 --- a/src/Row.php +++ b/src/Row.php @@ -9,7 +9,7 @@ use Aviat\Kilo\Enum\Highlight; * @property-read int rsize */ class Row { - use MagicProperties; + use Traits\MagicProperties; private string $chars = ''; public string $render = ''; diff --git a/src/Termios.php b/src/Termios.php index 3fa885b..cf692dd 100644 --- a/src/Termios.php +++ b/src/Termios.php @@ -18,22 +18,31 @@ class Termios { $ffi = get_ffi(); $termios = $ffi->new('struct termios'); $res = $ffi->tcgetattr(C::STDIN_FILENO, FFI::addr($termios)); + if ($res === -1) { - die('Failed to get terminal settings'); + throw new TermiosException('Failed to get existing terminal settings'); } $this->originalTermios = $termios; } - public static function enableRawMode(): void + /** + * Put the current terminal into raw input mode + * + * Returns TRUE if successful. Will return NULL if run more than once, as + * raw mode is pretty binary...there's no point in reapplying raw mode! + * + * @return bool|null + */ + public static function enableRawMode(): ?bool { static $run = FALSE; // Don't run this more than once! if ($run === TRUE) { - return; + return NULL; } $run = TRUE; @@ -55,13 +64,19 @@ class Termios { // Turn on raw mode $res = get_ffi()->tcsetattr(C::STDIN_FILENO, C::TCSAFLUSH, FFI::addr($termios)); - if ($res === -1) - { - die('Failed to set raw mode'); - } + + return $res !== -1; } - public static function disableRawMode(): void + /** + * Restores terminal settings that were changed when going into raw mode. + * + * Returns TRUE if settings are applied successfully. If raw mode was not + * enabled, this will output a line of escape codes and a new line. + * + * @return bool + */ + public static function disableRawMode(): bool { $instance = self::getInstance(); @@ -71,10 +86,7 @@ class Termios { $res = get_ffi()->tcsetattr(C::STDIN_FILENO, C::TCSAFLUSH, FFI::addr($instance->originalTermios)); - if ($res === -1) - { - die('Failed to restore terminal settings'); - } + return $res !== -1; } private static function getInstance(): self diff --git a/src/TermiosException.php b/src/TermiosException.php new file mode 100644 index 0000000..18fdb15 --- /dev/null +++ b/src/TermiosException.php @@ -0,0 +1,12 @@ +assertNotEmpty($file); + $filename = realpath(__DIR__ . '/../../test.php'); + $file = file_get_contents($filename); + $tokens = PHP::getFileTokens($filename); $lines = explode("\n", $file); array_unshift($lines, ''); + $this->verifyTokens($tokens, $lines); + } + + public function testGetFileTokensEmptyFile(): void + { + $filename = __DIR__ . '/../../foobarbaz.php'; + $this->assertEmpty(PHP::getFileTokens($filename)); + } + + private function verifyTokens(array $tokens, array $lines): void + { $misplacedTokens = []; foreach ($tokens as $index => $lineTokens) diff --git a/tests/Enum/ConstListTest.php b/tests/Traits/ConstListTest.php similarity index 85% rename from tests/Enum/ConstListTest.php rename to tests/Traits/ConstListTest.php index a6e0a9d..fb61ee0 100644 --- a/tests/Enum/ConstListTest.php +++ b/tests/Traits/ConstListTest.php @@ -1,8 +1,8 @@