From 5746d15117967568a420f4aa30186bf9dd9ce65e Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Wed, 14 Apr 2021 12:25:41 -0400 Subject: [PATCH] Remove redundant method --- src/FileType.php | 6 ++ src/Row.php | 155 +++++++++++++++++++++++------------------------ 2 files changed, 81 insertions(+), 80 deletions(-) diff --git a/src/FileType.php b/src/FileType.php index d428e0e..27cd61c 100644 --- a/src/FileType.php +++ b/src/FileType.php @@ -16,6 +16,12 @@ class FileType { $ext = strstr(basename($filename), '.'); $ext = ($ext !== FALSE) ? $ext : ''; return match ($ext) { + '.sh', '.bash' => Syntax::new( + 'Shell', + slcs: '#', + mcs: '', + mce: '', + ), '.php', 'kilo' => Syntax::new( 'PHP', ), diff --git a/src/Row.php b/src/Row.php index a2d7d73..d02507d 100644 --- a/src/Row.php +++ b/src/Row.php @@ -152,7 +152,80 @@ class Row { public function highlight(): void { $this->render = tabs_to_spaces($this->chars); - $this->highlightGeneral(); + $this->hl = array_fill(0, $this->rsize, Highlight::NORMAL); + + if ($this->parent->fileType->name === 'PHP') + { + $this->highlightPHP(); + return; + } + + $syntax = $this->parent->fileType->syntax; + + $mcs = $syntax->multiLineCommentStart; + $mce = $syntax->multiLineCommentEnd; + + $mcsLen = strlen($mcs); + $mceLen = strlen($mce); + + $inString = ''; + $inComment = ($this->idx > 0 && $this->parent->rows[$this->idx - 1]->hlOpenComment); + + $i = 0; + + while ($i < $this->rsize) + { + // Multi-line comments + if ($mcsLen > 0 && $mceLen > 0 && $inString === '') + { + if ($inComment) + { + $this->hl[$i] = Highlight::ML_COMMENT; + if (substr($this->render, $i, $mceLen) === $mce) + { + array_replace_range($this->hl, $i, $mceLen, Highlight::ML_COMMENT); + $i += $mceLen; + $inComment = FALSE; + continue; + } + + $i++; + continue; + } + + if (substr($this->render, $i, $mcsLen) === $mcs) + { + array_replace_range($this->hl, $i, $mcsLen, Highlight::ML_COMMENT); + $i += $mcsLen; + $inComment = TRUE; + continue; + } + } + + if ( + $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) + ) { + continue; + } + + $i++; + } + + $changed = $this->hlOpenComment !== $inComment; + $this->hlOpenComment = $inComment; + if ($changed && $this->idx + 1 < $this->parent->numRows) + { + $this->parent->rows[$this->idx + 1]->highlight(); + } } protected function highlightNumber(int &$i, Syntax $opts): bool @@ -280,7 +353,7 @@ class Row { if ($opts->characters() && $char === "'") { $offset = ($nextChar === '\\') ? $i + 2 : $i + 1; - $closingIndex = strpos($this->render, "'", $i + 1); + $closingIndex = strpos($this->render, "'", $offset); if ($closingIndex === false) { return false; @@ -394,84 +467,6 @@ class Row { return false; } - protected function highlightGeneral(): void - { - $this->hl = array_fill(0, $this->rsize, Highlight::NORMAL); - - if ($this->parent->fileType->name === 'PHP') - { - $this->highlightPHP(); - return; - } - - $syntax = $this->parent->fileType->syntax; - - $mcs = $syntax->multiLineCommentStart; - $mce = $syntax->multiLineCommentEnd; - - $mcsLen = strlen($mcs); - $mceLen = strlen($mce); - - $inString = ''; - $inComment = ($this->idx > 0 && $this->parent->rows[$this->idx - 1]->hlOpenComment); - - $i = 0; - - while ($i < $this->rsize) - { - // Multi-line comments - if ($mcsLen > 0 && $mceLen > 0 && $inString === '') - { - if ($inComment) - { - $this->hl[$i] = Highlight::ML_COMMENT; - if (substr($this->render, $i, $mceLen) === $mce) - { - array_replace_range($this->hl, $i, $mceLen, Highlight::ML_COMMENT); - $i += $mceLen; - $inComment = FALSE; - continue; - } - - $i++; - continue; - } - - if (substr($this->render, $i, $mcsLen) === $mcs) - { - array_replace_range($this->hl, $i, $mcsLen, Highlight::ML_COMMENT); - $i += $mcsLen; - $inComment = TRUE; - continue; - } - } - - if ( - $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) - ) { - continue; - } - - $i++; - } - - $changed = $this->hlOpenComment !== $inComment; - $this->hlOpenComment = $inComment; - if ($changed && $this->idx + 1 < $this->parent->numRows) - { - $this->parent->rows[$this->idx + 1]->highlight(); - } - } - protected function highlightPHP(): void { $rowNum = $this->idx + 1;