Partially fix inserting new lines in the PHP version

This commit is contained in:
Timothy Warren 2019-10-31 19:05:39 -04:00
parent d617c1c009
commit 3d2bc7ef48
2 changed files with 27 additions and 15 deletions

View File

@ -127,9 +127,9 @@ class Editor {
} }
// Update the syntax highlighting for all the rows of the file // 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; return;
@ -193,19 +193,17 @@ class Editor {
} }
else else
{ {
// Update other row indices
$numRows = $this->numRows;
$i = $at + 1;
for (; $i <= $numRows; $i++)
{
$this->rows[$i]->idx++;
}
$this->rows = [ $this->rows = [
...array_slice($this->rows, 0, $at), ...array_slice($this->rows, 0, $at),
$row, $row,
...array_slice($this->rows, $at), ...array_slice($this->rows, $at),
]; ];
// Update row indicies
foreach ($this->rows as $i => $row)
{
$row->idx = $i;
}
} }
// Re-tokenize the file // Re-tokenize the file
@ -272,18 +270,29 @@ class Editor {
{ {
if ($this->cursorX === 0) if ($this->cursorX === 0)
{ {
$this->insertRow($this->cursorY, ''); $this->insertRow($this->cursorY, '', FALSE);
} }
else else
{ {
$row = $this->rows[$this->cursorY]; $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 // 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 // Update the (now previous) row
$row->chars = substr($row->chars, 0, $this->cursorX); if ($this->syntax->filetype !== 'PHP')
$row->update(); {
$row->update();
}
}
// Re-tokenize the file
if ($this->syntax->filetype === 'PHP')
{
$this->refreshPHPSyntax();
} }
$this->cursorY++; $this->cursorY++;

View File

@ -355,7 +355,10 @@ function get_php_tokens(string $code): array
// So the array of tokens isn't sparse // So the array of tokens isn't sparse
for ($i = $lineNum; $i < $currentLine; $i++) for ($i = $lineNum; $i < $currentLine; $i++)
{ {
$tokens[$i] = []; if ( ! array_key_exists($i, $tokens))
{
$tokens[$i] = [];
}
} }
$lineNum = $currentLine; $lineNum = $currentLine;