From e22bb0545decefdd081b5dc241431859f47d95f9 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Wed, 14 Apr 2021 17:19:01 -0400 Subject: [PATCH 1/3] Make operator highlighting optional --- src/FileType.php | 7 +++++++ src/Row.php | 9 +++++++-- src/Syntax.php | 11 ++++++++++- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/FileType.php b/src/FileType.php index 27cd61c..fa442e6 100644 --- a/src/FileType.php +++ b/src/FileType.php @@ -18,9 +18,15 @@ class FileType { return match ($ext) { '.sh', '.bash' => Syntax::new( 'Shell', + [ + 'case', 'do', 'done', 'elif', 'else', 'esac', 'fi', 'for', 'function', 'if', 'in', + 'select', 'then', 'time', 'until', 'while', 'declare', + ], + ['set'], slcs: '#', mcs: '', mce: '', + highlightNumbers: false, ), '.php', 'kilo' => Syntax::new( 'PHP', @@ -49,6 +55,7 @@ class FileType { '.css', '.less', '.sass', '.scss' => Syntax::new( 'CSS', slcs: '', + hasCommonOperators: false, ), '.go' => Syntax::new( 'Go', diff --git a/src/Row.php b/src/Row.php index 98219f9..8015c86 100644 --- a/src/Row.php +++ b/src/Row.php @@ -257,7 +257,7 @@ class Row { || $this->highlightString($i, $syntax) || $this->highlightOperators($i, $syntax) || $this->highlightCommonDelimeters($i) - || $this->highlightCommonOperators($i) + || $this->highlightCommonOperators($i, $syntax) || $this->highlightNumber($i, $syntax) ) { continue; @@ -376,8 +376,13 @@ class Row { return $this->highlightWord($i, $opts->operators, Highlight::OPERATOR); } - protected function highlightCommonOperators(int &$i): bool + protected function highlightCommonOperators(int &$i, Syntax $opts): bool { + if ( ! $opts->commonOperators()) + { + return false; + } + return $this->highlightChar( $i, ['+', '-', '*', '/', '<', '^', '>', '%', '=', ':', ',', ';', '&', '~'], diff --git a/src/Syntax.php b/src/Syntax.php index 3d6cb0d..d311b14 100644 --- a/src/Syntax.php +++ b/src/Syntax.php @@ -19,6 +19,7 @@ class Syntax { bool $highlightComments = true, bool $hasCharType = false, bool $highlightCharacters = false, + bool $hasCommonOperators = true, ): self { return new self( @@ -34,12 +35,13 @@ class Syntax { $highlightCharacters, $highlightStrings, $highlightComments, + $hasCommonOperators, ); } public static function default(): self { - return self::new('No filetype', slcs: '', mcs: '', mce: '', highlightNumbers: false, highlightStrings: false); + return self::new('No filetype', slcs: '', mcs: '', mce: '', highlightNumbers: false, highlightStrings: false, hasCommonOperators: false); } private function __construct( @@ -67,6 +69,8 @@ class Syntax { private bool $highlightStrings, /** should we highlight comments? */ private bool $highlightComments, + /** should we highlight common operators? */ + private bool $hasCommonOperators, ) {} public function numbers(): bool @@ -100,4 +104,9 @@ class Syntax { { return $this->highlightComments && strlen($this->singleLineCommentStart) !== 0; } + + public function commonOperators(): bool + { + return $this->hasCommonOperators; + } } \ No newline at end of file From 3ef4268263526ecdeb290569b6201f9289ea742a Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Wed, 14 Apr 2021 18:38:33 -0400 Subject: [PATCH 2/3] More docblocks --- src/FileType.php | 7 ++++ src/Row.php | 95 ++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 98 insertions(+), 4 deletions(-) diff --git a/src/FileType.php b/src/FileType.php index fa442e6..c800c9b 100644 --- a/src/FileType.php +++ b/src/FileType.php @@ -23,6 +23,7 @@ class FileType { 'select', 'then', 'time', 'until', 'while', 'declare', ], ['set'], + operators: ['[[', ']]'], slcs: '#', mcs: '', mce: '', @@ -103,6 +104,12 @@ class FileType { 'usize', '&str', 'Copy', 'Drop', 'From', 'Into', 'None', 'Self', 'Send', 'Some', 'Sync', 'bool', 'char', 'i128', 'u128', 'Box', 'Err', 'Ord', 'Vec', 'dyn', 'f32', 'f64', 'i16', 'i32', 'i64', 'str', 'u16', 'u32', 'u64', 'Eq', 'Fn', 'Ok', 'i8', 'u8', + '&mut self', '&mut', '&self', 'self', + ], + [ + '...', '=>', '..', '>>=', '<<=', '--', '%=', '>>', ':=', '++', '/=', '<<', '>=', + '<-', '^=', '*=', '<=', '||', '|=', '-=', '!=', '==', '&&', '&=', '+=', '..=', + '.', ], hasCharType: true, highlightCharacters: true, diff --git a/src/Row.php b/src/Row.php index 8015c86..9d7dab9 100644 --- a/src/Row.php +++ b/src/Row.php @@ -170,6 +170,11 @@ class Row { $this->update(); } + /** + * Set the contents of the Row + * + * @param string $chars + */ public function setChars(string $chars): void { $this->chars = $chars; @@ -274,10 +279,17 @@ class Row { } } + /** + * Highlight number literals + * + * @param int $i + * @param Syntax $opts + * @return bool + */ protected function highlightNumber(int &$i, Syntax $opts): bool { $char = $this->render[$i]; - if ($opts->numbers() && is_digit($char)) + if ($opts->numbers() && is_digit($char) && $this->hl[$i] === Highlight::NORMAL) { if ($i > 0) { @@ -311,9 +323,18 @@ class Row { return false; } - protected function highlightWord(int &$i, array $keywords, int $syntaxType): bool + /** + * Highlight keywords and/or operators + * + * @param int $i + * @param array $keywords + * @param int $syntaxType + * @param bool $requireSeparator + * @return bool + */ + protected function highlightWord(int &$i, array $keywords, int $syntaxType, bool $requireSeparator = true): bool { - if ($i > 0) + if ($i > 0 && $requireSeparator) { $prevChar = $this->render[$i - 1]; if ( ! is_separator($prevChar)) @@ -341,6 +362,14 @@ class Row { return false; } + /** + * Highlight a single-char character from a list of provided keywords + * + * @param int $i + * @param array $chars + * @param int $syntaxType + * @return bool + */ protected function highlightChar(int &$i, array $chars, int $syntaxType): bool { if ($this->hl[$i] !== Highlight::NORMAL) @@ -361,21 +390,49 @@ class Row { return false; } + /** + * Highlight primary keywords + * + * @param int $i + * @param Syntax $opts + * @return bool + */ protected function highlightPrimaryKeywords(int &$i, Syntax $opts): bool { return $this->highlightWord($i, $opts->keywords1, Highlight::KEYWORD1); } + /** + * Highlight secondary keywords + * + * @param int $i + * @param Syntax $opts + * @return bool + */ protected function highlightSecondaryKeywords(int &$i, Syntax $opts): bool { return $this->highlightWord($i, $opts->keywords2, Highlight::KEYWORD2); } + /** + * Highlight language-specific operators + * + * @param int $i + * @param Syntax $opts + * @return bool + */ protected function highlightOperators(int &$i, Syntax $opts): bool { - return $this->highlightWord($i, $opts->operators, Highlight::OPERATOR); + return $this->highlightWord($i, $opts->operators, Highlight::OPERATOR, false); } + /** + * Highlight common single-character operators + * + * @param int $i + * @param Syntax $opts + * @return bool + */ protected function highlightCommonOperators(int &$i, Syntax $opts): bool { if ( ! $opts->commonOperators()) @@ -390,6 +447,12 @@ class Row { ); } + /** + * Highlight brackets and braces + * + * @param int $i + * @return bool + */ protected function highlightCommonDelimeters(int &$i): bool { return $this->highlightChar( @@ -399,6 +462,13 @@ class Row { ); } + /** + * Highlight character literals + * + * @param int $i + * @param Syntax $opts + * @return bool + */ protected function highlightCharacter(int &$i, Syntax $opts): bool { if (($i + 1) >= $this->rsize) @@ -431,6 +501,13 @@ class Row { return false; } + /** + * Highlight single-line comments + * + * @param int $i + * @param Syntax $opts + * @return bool + */ protected function highlightComment(int &$i, Syntax $opts): bool { if ( ! $opts->comments()) @@ -452,6 +529,13 @@ class Row { return false; } + /** + * Highlight quote-delimited string literals + * + * @param int $i + * @param Syntax $opts + * @return bool + */ protected function highlightString(int &$i, Syntax $opts): bool { $char = $this->render[$i]; @@ -495,6 +579,9 @@ class Row { return false; } + /** + * Highlight PHP code based on pre-parsed tokens + */ protected function highlightPHP(): void { $rowNum = $this->idx + 1; From 457560137d64777779d3cd3838515a0e83463af3 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Wed, 14 Apr 2021 19:00:37 -0400 Subject: [PATCH 3/3] Comments and tests --- src/FileType.php | 13 ++++++++++++- src/Row.php | 2 -- src/Syntax.php | 12 ++++++++++-- tests/Type/PointTest.php | 24 ++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 tests/Type/PointTest.php diff --git a/src/FileType.php b/src/FileType.php index c800c9b..e8ed1b3 100644 --- a/src/FileType.php +++ b/src/FileType.php @@ -3,7 +3,12 @@ namespace Aviat\Kilo; class FileType { - + /** + * Create the FileType object from the filename + * + * @param string|null $filename + * @return self + */ public static function from(?string $filename): self { $syntax = self::getSyntaxFromFilename((string)$filename); @@ -11,6 +16,12 @@ class FileType { return new self($syntax->filetype, $syntax); } + /** + * Create the Syntax object from the filename + * + * @param string $filename + * @return Syntax + */ private static function getSyntaxFromFilename(string $filename): Syntax { $ext = strstr(basename($filename), '.'); diff --git a/src/Row.php b/src/Row.php index 9d7dab9..8874806 100644 --- a/src/Row.php +++ b/src/Row.php @@ -11,8 +11,6 @@ use Aviat\Kilo\Enum\RawKeyCode; * @property-read string $chars */ class Row { - // use Traits\MagicProperties; - /** * The version of the row to be displayed (where tabs are converted to display spaces) */ diff --git a/src/Syntax.php b/src/Syntax.php index d311b14..06ac164 100644 --- a/src/Syntax.php +++ b/src/Syntax.php @@ -41,7 +41,15 @@ class Syntax { public static function default(): self { - return self::new('No filetype', slcs: '', mcs: '', mce: '', highlightNumbers: false, highlightStrings: false, hasCommonOperators: false); + return self::new( + 'No filetype', slcs: + '', mcs: + '', mce: + '', + highlightNumbers: false, + highlightStrings: false, + hasCommonOperators: false, + ); } private function __construct( @@ -57,7 +65,7 @@ class Syntax { public string $singleLineCommentStart, /** Syntax to start a multi-line comment */ public string $multiLineCommentStart, - /** Syntax to end a multi-line commment */ + /** Syntax to end a multi-line comment */ public string $multiLineCommentEnd, /** Should we highlight numbers? */ private bool $highlightNumbers, diff --git a/tests/Type/PointTest.php b/tests/Type/PointTest.php new file mode 100644 index 0000000..5b4c4e2 --- /dev/null +++ b/tests/Type/PointTest.php @@ -0,0 +1,24 @@ +assertEquals(1, $p->x); + $this->assertEquals(2, $p->y); + } + + public function testPointFrom(): void + { + $p = Point::new(3, 7); + $p2 = Point::from($p); + + $this->assertEquals($p->x, $p2->x); + $this->assertEquals($p->y, $p2->y); + } +}