Fix some editing issues

This commit is contained in:
Timothy Warren 2019-11-05 12:28:10 -05:00
parent 3d2bc7ef48
commit c16b9aa6f0
3 changed files with 54 additions and 25 deletions

2
kilo
View File

@ -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);

View File

@ -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();
}
}

View File

@ -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);