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;