From c16b9aa6f0429be22354fe88d23b8e08afc82477 Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Tue, 5 Nov 2019 12:28:10 -0500 Subject: [PATCH] Fix some editing issues --- kilo | 2 +- src/Editor.php | 36 +++++++++++++++--------------------- src/Row.php | 41 ++++++++++++++++++++++++++++++++++++++--- 3 files changed, 54 insertions(+), 25 deletions(-) diff --git a/kilo b/kilo index f3efaf3..a498f89 100755 --- a/kilo +++ b/kilo @@ -17,7 +17,7 @@ set_error_handler(static function (int $no, $str, $file, $line, $context) { 'message' => $str, 'file' => $file, 'line' => $line, - 'context' => $context, + // 'context' => $context, ], TRUE); file_put_contents('kilo.log', $msg, FILE_APPEND); diff --git a/src/Editor.php b/src/Editor.php index aa5f769..0e5a942 100644 --- a/src/Editor.php +++ b/src/Editor.php @@ -188,7 +188,6 @@ class Editor { if ($at === $this->numRows) { - $row->idx = $this->numRows; $this->rows[] = $row; } else @@ -206,17 +205,15 @@ class Editor { } } + $this->rows[$at]->update(); + + $this->dirty++; + // Re-tokenize the file if ($updateSyntax && $this->syntax->filetype === 'PHP') { $this->refreshPHPSyntax(); } - else - { - $this->rows[$at]->update(); - } - - $this->dirty++; } protected function deleteRow(int $at): void @@ -270,33 +267,30 @@ class Editor { { if ($this->cursorX === 0) { - $this->insertRow($this->cursorY, '', FALSE); + $this->insertRow($this->cursorY, ''); } else { $row = $this->rows[$this->cursorY]; $chars = $row->chars; - $newRowChars = substr($chars, $this->cursorX); - $row->chars = substr($chars, 0, $this->cursorX); + $newChars = substr($chars, 0, $this->cursorX); + + // Truncate the previous row + $row->chars = $newChars; + $row->update(); // Add a new row, with the contents from the cursor to the end of the line - $this->insertRow($this->cursorY + 1, $newRowChars, FALSE); - - // Update the (now previous) row - if ($this->syntax->filetype !== 'PHP') - { - $row->update(); - } + $this->insertRow($this->cursorY + 1, substr($chars, $this->cursorX), FALSE); } + $this->cursorY++; + $this->cursorX = 0; + // Re-tokenize the file if ($this->syntax->filetype === 'PHP') { $this->refreshPHPSyntax(); } - - $this->cursorY++; - $this->cursorX = 0; } protected function deleteChar(): void @@ -323,7 +317,7 @@ class Editor { // Re-tokenize the file if ($this->syntax->filetype === 'PHP') { - $this->syntax->tokens = get_php_tokens($this->rowsToString()); + $this->refreshPHPSyntax(); } } diff --git a/src/Row.php b/src/Row.php index 30a9cc2..389caf0 100644 --- a/src/Row.php +++ b/src/Row.php @@ -9,7 +9,7 @@ namespace Kilo; class Row { use MagicProperties; - public string $chars = ''; + private string $chars = ''; public string $render = ''; public array $hl = []; @@ -183,11 +183,23 @@ class Row { case 'rsize': return strlen($this->render); + case 'chars': + return $this->chars; + default: return NULL; } } + public function __set(string $key, $value): void + { + if ($key === 'chars') + { + $this->chars = $value; + $this->update(); + } + } + public function __toString(): string { return $this->chars . "\n"; @@ -232,6 +244,8 @@ class Row { { $idx = 0; + $this->render = ''; + for ($i = 0; $i < $this->size; $i++) { if ($this->chars[$i] === "\t") @@ -379,7 +393,28 @@ class Row { // Keywords if ($prevSep) { - foreach ($keywords1 as $k) + $findKeywords = function (array $keywords, int $syntaxType) use (&$i): void + { + foreach ($keywords as $k) + { + $klen = strlen($k); + $nextCharOffset = $i + $klen; + $isEndOfLine = $nextCharOffset >= $this->rsize; + $nextChar = ($isEndOfLine) ? "\0" : $this->render[$nextCharOffset]; + + if (substr($this->render, $i, $klen) === $k && is_separator($nextChar)) + { + array_replace_range($this->hl, $i, $klen, $syntaxType); + $i += $klen - 1; + break; + } + } + }; + + $findKeywords($keywords1, Highlight::KEYWORD1); + $findKeywords($keywords2, Highlight::KEYWORD2); + + /* foreach ($keywords1 as $k) { $klen = strlen($k); $nextCharOffset = $i + $klen; @@ -407,7 +442,7 @@ class Row { $i += $klen - 1; break; } - } + } */ } $prevSep = is_separator($char);