From b980a6feb0530c6316fef31bb10261c1cd61d307 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Fri, 9 Apr 2021 21:19:46 -0400 Subject: [PATCH] Improve highlighting for non-PHP languages --- src/FileType.php | 18 +++++++++++--- src/Row.php | 63 ++++++++++++++++++++++++++++++++++++++++-------- src/Syntax.php | 4 +++ 3 files changed, 72 insertions(+), 13 deletions(-) diff --git a/src/FileType.php b/src/FileType.php index 337656f..d428e0e 100644 --- a/src/FileType.php +++ b/src/FileType.php @@ -22,12 +22,20 @@ class FileType { '.c', '.h', '.cpp', '.cxx', '.cc', '.hpp' => Syntax::new( 'C', [ - 'continue', 'typedef', 'switch', 'return', 'static', 'while', 'break', 'struct', - 'union', 'class', 'else', 'enum', 'for', 'case', 'if', + 'auto', 'break', 'case', 'const', 'continue', 'default', 'do', 'typedef', 'switch', 'return', + 'static', 'while', 'break', 'struct', 'extern', 'union', 'class', 'else', 'enum', 'for', 'case', + 'if', 'inline', 'register', 'restrict', 'return', 'sizeof', 'switch', 'typedef', 'union', 'volatile' ], [ '#include', 'unsigned', '#define', '#ifndef', 'double', 'signed', '#endif', - '#ifdef', 'float', '#error', '#undef', 'long', 'char', 'int', 'void', '#if', + '#ifdef', 'float', '#error', '#undef', '#elif', 'long', 'char', 'int', 'void', '#if', + 'uint32_t', 'wchar_t', 'int32_t', 'int64_t', 'uint64_t', 'int16_t', 'uint16_t', + 'uint8_t', 'int8_t', + ], + [ + '<=>', '<<=', '>>=', + '++', '--', '==', '!=', '>=', '<=', '&&', '||', '<<', '>>', + '+=', '-=', '*=', '/=', '%=', '&=', '|=', '^=', '->', '::', ], hasCharType: true, highlightCharacters: true, @@ -47,6 +55,10 @@ class FileType { 'uint8', 'uint16', 'uint32', 'uint64', 'int8', 'int16', 'int32', 'int64', 'float32', 'float64', 'uint', 'int', 'uintptr', 'complex64', 'complex128', 'byte', 'rune', '[]', ], + [ + '&^=', '...', '>>=', '<<=', '--', '%=', '>>', ':=', '++', '/=', '<<', '>=', + '<-', '^=', '*=', '<=', '||', '|=', '-=', '!=', '==', '&&', '&=', '+=', + ], hasCharType: true, highlightCharacters: true, ), diff --git a/src/Row.php b/src/Row.php index 6e27748..c54e7ec 100644 --- a/src/Row.php +++ b/src/Row.php @@ -224,6 +224,21 @@ class Row { return false; } + protected function highlightChar(int &$i, array $chars, int $syntaxType): bool + { + $char = $this->render[$i]; + + if (in_array($char, $chars, TRUE)) + { + $this->hl[$i] = $syntaxType; + $i += 1; + + return true; + } + + return false; + } + protected function highlightPrimaryKeywords(int &$i, Syntax $opts): bool { return $this->highlightWord($i, $opts->keywords1, Highlight::KEYWORD1); @@ -234,20 +249,48 @@ class Row { return $this->highlightWord($i, $opts->keywords2, Highlight::KEYWORD2); } - protected function highlightChar(int &$i, Syntax $opts): bool + protected function highlightOperators(int &$i, Syntax $opts): bool + { + return $this->highlightWord($i, $opts->operators, Highlight::OPERATOR); + } + + protected function highlightCommonOperators(int &$i): bool + { + return $this->highlightChar( + $i, + ['+', '-', '*', '/', '<', '^', '>', '%', '=', ':', ',', ';', '&', '~'], + Highlight::OPERATOR + ); + } + + protected function highlightCommonDelimeters(int &$i): bool + { + return $this->highlightChar( + $i, + ['{', '}', '[', ']', '(', ')'], + Highlight::DELIMITER + ); + } + + protected function highlightCharacter(int &$i, Syntax $opts): bool { $char = $this->render[$i]; + $nextChar = $this->render[$i + 1]; if ($opts->characters() && $char === "'") { - $nextChar = $this->render[$i + 1]; - $closingIndex = ($nextChar === '\\') ? $i + 3 : $i + 2; + $offset = ($nextChar === '\\') ? $i + 2 : $i + 1; + $closingIndex = strpos($this->render, "'", $i + 1); + if ($closingIndex === false) + { + return false; + } $closingChar = $this->render[$closingIndex]; if ($closingChar === "'") { - array_replace_range($this->hl, $i, $closingIndex, Highlight::CHARACTER); - $i = $closingIndex; + array_replace_range($this->hl, $i, $closingIndex - $i + 1, Highlight::CHARACTER); + $i = $closingIndex + 1; return true; } @@ -376,9 +419,6 @@ class Row { while ($i < $this->rsize) { - $char = $this->render[$i]; - $prevHl = ($i > 0) ? $this->hl[$i - 1] : Highlight::NORMAL; - // Multi-line comments if ($mcsLen > 0 && $mceLen > 0 && $inString === '') { @@ -407,11 +447,14 @@ class Row { } if ( - $this->highlightChar($i, $syntax) - || $this->highlightComment($i, $syntax) + $this->highlightComment($i, $syntax) || $this->highlightMultilineComments($i, $syntax) || $this->highlightPrimaryKeywords($i, $syntax) || $this->highlightSecondaryKeywords($i, $syntax) + || $this->highlightOperators($i, $syntax) + || $this->highlightCommonOperators($i) + || $this->highlightCommonDelimeters($i) + || $this->highlightCharacter($i, $syntax) || $this->highlightString($i, $syntax) || $this->highlightNumber($i, $syntax) ) { diff --git a/src/Syntax.php b/src/Syntax.php index 2c01aff..98c8aed 100644 --- a/src/Syntax.php +++ b/src/Syntax.php @@ -10,6 +10,7 @@ class Syntax { string $name, array $keywords1 = [], array $keywords2 = [], + array $operators = [], string $slcs = '//', string $mcs = '/*', string $mce = '*/', @@ -24,6 +25,7 @@ class Syntax { $name, $keywords1, $keywords2, + $operators, $slcs, $mcs, $mce, @@ -47,6 +49,8 @@ class Syntax { public array $keywords1, /** Secondary set of language keywords */ public array $keywords2, + /** Operators for the current language */ + public array $operators, /** Syntax to start a single line comment */ public string $singleLineCommentStart, /** Syntax to start a multi-line comment */