From 3d2bc7ef48f2f143e96ff21307d3325a4911902a Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Thu, 31 Oct 2019 19:05:39 -0400 Subject: [PATCH] Partially fix inserting new lines in the PHP version --- src/Editor.php | 37 +++++++++++++++++++++++-------------- src/functions.php | 5 ++++- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/Editor.php b/src/Editor.php index ce07883..aa5f769 100644 --- a/src/Editor.php +++ b/src/Editor.php @@ -127,9 +127,9 @@ class Editor { } // Update the syntax highlighting for all the rows of the file - for ($i = 0; $i < $this->numRows; $i++) + foreach ($this->rows as $row) { - $this->rows[$i]->updateSyntax(); + $row->updateSyntax(); } return; @@ -193,19 +193,17 @@ class Editor { } else { - // Update other row indices - $numRows = $this->numRows; - $i = $at + 1; - for (; $i <= $numRows; $i++) - { - $this->rows[$i]->idx++; - } - $this->rows = [ ...array_slice($this->rows, 0, $at), $row, ...array_slice($this->rows, $at), ]; + + // Update row indicies + foreach ($this->rows as $i => $row) + { + $row->idx = $i; + } } // Re-tokenize the file @@ -272,18 +270,29 @@ class Editor { { if ($this->cursorX === 0) { - $this->insertRow($this->cursorY, ''); + $this->insertRow($this->cursorY, '', FALSE); } else { $row = $this->rows[$this->cursorY]; + $chars = $row->chars; + $newRowChars = substr($chars, $this->cursorX); + $row->chars = substr($chars, 0, $this->cursorX); // Add a new row, with the contents from the cursor to the end of the line - $this->insertRow($this->cursorY + 1, substr($row->chars, $this->cursorX)); + $this->insertRow($this->cursorY + 1, $newRowChars, FALSE); // Update the (now previous) row - $row->chars = substr($row->chars, 0, $this->cursorX); - $row->update(); + if ($this->syntax->filetype !== 'PHP') + { + $row->update(); + } + } + + // Re-tokenize the file + if ($this->syntax->filetype === 'PHP') + { + $this->refreshPHPSyntax(); } $this->cursorY++; diff --git a/src/functions.php b/src/functions.php index 704f0c1..517fefc 100644 --- a/src/functions.php +++ b/src/functions.php @@ -355,7 +355,10 @@ function get_php_tokens(string $code): array // So the array of tokens isn't sparse for ($i = $lineNum; $i < $currentLine; $i++) { - $tokens[$i] = []; + if ( ! array_key_exists($i, $tokens)) + { + $tokens[$i] = []; + } } $lineNum = $currentLine;