diff --git a/src/Editor.php b/src/Editor.php index 5e7f980..9c970a5 100644 --- a/src/Editor.php +++ b/src/Editor.php @@ -37,6 +37,9 @@ class Editor { public ?Syntax $syntax; + // Tokens for highlighting PHP + public array $tokens = []; + public static function new(): Editor { return new self(); @@ -129,7 +132,7 @@ class Editor { // Pre-tokenize the file if ($this->syntax->filetype === 'PHP') { - $this->syntax->tokens = PHP::getFileTokens($this->filename); + $this->tokens = PHP::getFileTokens($this->filename); } // Update the syntax highlighting for all the rows of the file @@ -192,12 +195,6 @@ class Editor { $row = Row::new($this, $s, $at); - // Update row indicies - for ($i = $at + 1; $i < $this->numRows; $i++) - { - $this->rows[$i]->idx++; - } - if ($at === $this->numRows) { $this->rows[] = $row; @@ -211,6 +208,14 @@ class Editor { ]; } + ksort($this->rows); + + // Update row indexes + for ($i = 0; $i < $this->numRows; $i++) + { + $this->rows[$i]->idx = $i; + } + $this->rows[$at]->update(); $this->dirty++; @@ -285,7 +290,7 @@ class Editor { $row->chars = $newChars; // Add a new row, with the contents from the cursor to the end of the line - $this->insertRow($this->cursorY + 1, substr($chars, $this->cursorX), FALSE); + $this->insertRow($this->cursorY + 1, substr($chars, $this->cursorX)); } $this->cursorY++; @@ -332,11 +337,7 @@ class Editor { protected function rowsToString(): string { - $lines = []; - foreach ($this->rows as $row) - { - $lines[] = (string)$row; - } + $lines = array_map(fn (Row $row) => (string)$row, $this->rows); return implode('', $lines); } @@ -930,10 +931,10 @@ class Editor { private function refreshPHPSyntax(): void { - $this->syntax->tokens = PHP::getTokens($this->rowsToString()); + $this->tokens = PHP::getTokens($this->rowsToString()); for ($i = 0; $i < $this->numRows; $i++) { - $this->rows[$i]->updateSyntax(); + $this->rows[$i]->update(); } } } diff --git a/src/Row.php b/src/Row.php index d276a17..194e5e1 100644 --- a/src/Row.php +++ b/src/Row.php @@ -22,6 +22,8 @@ class Row { private Editor $parent; private bool $hlOpenComment = FALSE; + private const T_RAW = -1; + private array $phpTokenHighlightMap = [ // Delimiters T_ARRAY => Highlight::DELIMITER, @@ -229,6 +231,7 @@ class Row { 'rsize' => $this->rsize, 'chars' => $this->chars, 'render' => $this->render, + 'hl' => $this->hl, 'hlOpenComment' => $this->hlOpenComment, ]; } @@ -446,19 +449,17 @@ class Row { { $rowNum = $this->idx + 1; - $hasTokens = isset($this->parent->syntax->tokens); - $hasRow = $hasTokens && array_key_exists($rowNum, $this->parent->syntax->tokens); + $hasRowTokens = array_key_exists($rowNum, $this->parent->tokens); if ( ! ( - $hasTokens && - $hasRow && + $hasRowTokens && $this->idx < $this->parent->numRows )) { return; } - $tokens = $this->parent->syntax->tokens[$rowNum]; + $tokens = $this->parent->tokens[$rowNum]; $inComment = ($this->idx > 0 && $this->parent->rows[$this->idx - 1]->hlOpenComment); @@ -567,7 +568,7 @@ class Row { } // Highlight raw characters - if (($token['typeName'] === 'RAW') && array_key_exists(trim($token['char']), $this->phpCharacterHighlightMap)) + if (($token['type'] === self::T_RAW) && array_key_exists(trim($token['char']), $this->phpCharacterHighlightMap)) { $hl = $this->phpCharacterHighlightMap[trim($token['char'])]; array_replace_range($this->hl, $charStart, $charLen, $hl); diff --git a/src/Tokens/PHP.php b/src/Tokens/PHP.php index a1a682e..fe985de 100644 --- a/src/Tokens/PHP.php +++ b/src/Tokens/PHP.php @@ -23,7 +23,7 @@ class PHP { $this->code = $code; $this->rawLines = $lines; - $this->tokens = array_fill(1, count($lines) - 1, []); + $this->tokens = array_fill(1, count($lines), []); } /** diff --git a/test.php b/test.php index 119be7b..d45a5cc 100644 --- a/test.php +++ b/test.php @@ -81,3 +81,4 @@ TEMPLATE;

+ \ No newline at end of file