diff --git a/src/Row.php b/src/Row.php index d02507d..4e05e85 100644 --- a/src/Row.php +++ b/src/Row.php @@ -23,10 +23,21 @@ class Row { */ public array $hl = []; + /** + * Are we in the middle of highlighting a multi-line comment? + */ private bool $hlOpenComment = FALSE; private const T_RAW = -1; + /** + * Create a row in the current document + * + * @param Document $parent + * @param string $chars + * @param int $idx + * @return self + */ public static function new(Document $parent, string $chars, int $idx): self { return new self( @@ -36,6 +47,11 @@ class Row { ); } + /** + * Create an empty Row + * + * @return self + */ public static function default(): self { return new self( @@ -82,11 +98,21 @@ class Row { } } + /** + * Convert the row contents to a string for saving + * + * @return string + */ public function __toString(): string { return $this->chars . "\n"; } + /** + * Set the properties to display for var_dump + * + * @return array + */ public function __debugInfo(): array { return [ @@ -109,6 +135,12 @@ class Row { return ! $this->parent->isEmpty(); } + /** + * Insert the string or character $c at index $at + * + * @param int $at + * @param string $c + */ public function insert(int $at, string $c): void { if ($at < 0 || $at > $this->size) @@ -119,19 +151,29 @@ class Row { // Safely insert into arbitrary position in the existing string $this->chars = substr($this->chars, 0, $at) . $c . substr($this->chars, $at); - $this->highlight(); + $this->update(); $this->parent->dirty = true; } + /** + * Append $s to the current row + * + * @param string $s + */ public function append(string $s): void { $this->chars .= $s; - $this->highlight(); + $this->update(); $this->parent->dirty = true; } + /** + * Delete the character at the specified index + * + * @param int $at + */ public function delete(int $at): void { if ($at < 0 || $at >= $this->size) @@ -140,18 +182,29 @@ class Row { } $this->chars = substr_replace($this->chars, '', $at, 1); - $this->highlight(); + $this->update(); $this->parent->dirty = true; } + /** + * Convert tabs to spaces for display, and update syntax highlighting + */ + public function update(): void + { + $this->render = tabs_to_spaces($this->chars); + $this->highlight(); + } + // ------------------------------------------------------------------------ // ! Syntax Highlighting // ------------------------------------------------------------------------ + /** + * Parse the current file to apply syntax highlighting + */ public function highlight(): void { - $this->render = tabs_to_spaces($this->chars); $this->hl = array_fill(0, $this->rsize, Highlight::NORMAL); if ($this->parent->fileType->name === 'PHP') @@ -214,6 +267,7 @@ class Row { || $this->highlightString($i, $syntax) || $this->highlightNumber($i, $syntax) ) { + $i++; continue; } @@ -395,7 +449,7 @@ class Row { protected function highlightMultilineComments(int &$i, Syntax $opts): bool { - if ( ! $opts->comments()) + if ( ! $opts->mlComments()) { return false; } @@ -569,7 +623,7 @@ class Row { $tokenHighlight = Highlight::fromPHPToken($token['type']); $charHighlight = Highlight::fromPHPChar(trim($token['char'])); - $hl = match(true) { + $highlight = match(true) { // Matches a predefined PHP token $token['type'] !== self::T_RAW && $tokenHighlight !== Highlight::NORMAL => $tokenHighlight, @@ -585,9 +639,9 @@ class Row { default => Highlight::NORMAL, }; - if ($hl !== Highlight::NORMAL) + if ($highlight !== Highlight::NORMAL) { - array_replace_range($this->hl, $charStart, $charLen, $hl); + array_replace_range($this->hl, $charStart, $charLen, $highlight); $offset = $charEnd; } } diff --git a/src/Syntax.php b/src/Syntax.php index 98c8aed..3d6cb0d 100644 --- a/src/Syntax.php +++ b/src/Syntax.php @@ -89,8 +89,15 @@ class Syntax { return $this->hasCharType && $this->highlightCharacters; } + public function mlComments(): bool + { + return $this->highlightComments + && strlen($this->multiLineCommentStart) !== 0 + && strlen($this->multiLineCommentStart) !== 0; + } + public function comments(): bool { - return $this->highlightComments; + return $this->highlightComments && strlen($this->singleLineCommentStart) !== 0; } } \ No newline at end of file