Fix syntax highlighting dissapearing when code pushed to new line (for PHP)

This commit is contained in:
Timothy Warren 2019-11-15 16:19:34 -05:00
parent 345239d248
commit cf75f92cd5
4 changed files with 25 additions and 22 deletions

View File

@ -37,6 +37,9 @@ class Editor {
public ?Syntax $syntax; public ?Syntax $syntax;
// Tokens for highlighting PHP
public array $tokens = [];
public static function new(): Editor public static function new(): Editor
{ {
return new self(); return new self();
@ -129,7 +132,7 @@ class Editor {
// Pre-tokenize the file // Pre-tokenize the file
if ($this->syntax->filetype === 'PHP') 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 // Update the syntax highlighting for all the rows of the file
@ -192,12 +195,6 @@ class Editor {
$row = Row::new($this, $s, $at); $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) if ($at === $this->numRows)
{ {
$this->rows[] = $row; $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->rows[$at]->update();
$this->dirty++; $this->dirty++;
@ -285,7 +290,7 @@ class Editor {
$row->chars = $newChars; $row->chars = $newChars;
// 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($chars, $this->cursorX), FALSE); $this->insertRow($this->cursorY + 1, substr($chars, $this->cursorX));
} }
$this->cursorY++; $this->cursorY++;
@ -332,11 +337,7 @@ class Editor {
protected function rowsToString(): string protected function rowsToString(): string
{ {
$lines = []; $lines = array_map(fn (Row $row) => (string)$row, $this->rows);
foreach ($this->rows as $row)
{
$lines[] = (string)$row;
}
return implode('', $lines); return implode('', $lines);
} }
@ -930,10 +931,10 @@ class Editor {
private function refreshPHPSyntax(): void private function refreshPHPSyntax(): void
{ {
$this->syntax->tokens = PHP::getTokens($this->rowsToString()); $this->tokens = PHP::getTokens($this->rowsToString());
for ($i = 0; $i < $this->numRows; $i++) for ($i = 0; $i < $this->numRows; $i++)
{ {
$this->rows[$i]->updateSyntax(); $this->rows[$i]->update();
} }
} }
} }

View File

@ -22,6 +22,8 @@ class Row {
private Editor $parent; private Editor $parent;
private bool $hlOpenComment = FALSE; private bool $hlOpenComment = FALSE;
private const T_RAW = -1;
private array $phpTokenHighlightMap = [ private array $phpTokenHighlightMap = [
// Delimiters // Delimiters
T_ARRAY => Highlight::DELIMITER, T_ARRAY => Highlight::DELIMITER,
@ -229,6 +231,7 @@ class Row {
'rsize' => $this->rsize, 'rsize' => $this->rsize,
'chars' => $this->chars, 'chars' => $this->chars,
'render' => $this->render, 'render' => $this->render,
'hl' => $this->hl,
'hlOpenComment' => $this->hlOpenComment, 'hlOpenComment' => $this->hlOpenComment,
]; ];
} }
@ -446,19 +449,17 @@ class Row {
{ {
$rowNum = $this->idx + 1; $rowNum = $this->idx + 1;
$hasTokens = isset($this->parent->syntax->tokens); $hasRowTokens = array_key_exists($rowNum, $this->parent->tokens);
$hasRow = $hasTokens && array_key_exists($rowNum, $this->parent->syntax->tokens);
if ( ! ( if ( ! (
$hasTokens && $hasRowTokens &&
$hasRow &&
$this->idx < $this->parent->numRows $this->idx < $this->parent->numRows
)) ))
{ {
return; return;
} }
$tokens = $this->parent->syntax->tokens[$rowNum]; $tokens = $this->parent->tokens[$rowNum];
$inComment = ($this->idx > 0 && $this->parent->rows[$this->idx - 1]->hlOpenComment); $inComment = ($this->idx > 0 && $this->parent->rows[$this->idx - 1]->hlOpenComment);
@ -567,7 +568,7 @@ class Row {
} }
// Highlight raw characters // 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'])]; $hl = $this->phpCharacterHighlightMap[trim($token['char'])];
array_replace_range($this->hl, $charStart, $charLen, $hl); array_replace_range($this->hl, $charStart, $charLen, $hl);

View File

@ -23,7 +23,7 @@ class PHP {
$this->code = $code; $this->code = $code;
$this->rawLines = $lines; $this->rawLines = $lines;
$this->tokens = array_fill(1, count($lines) - 1, []); $this->tokens = array_fill(1, count($lines), []);
} }
/** /**

View File

@ -81,3 +81,4 @@ TEMPLATE;
<h1><?= $_SERVER['HTTP_HOST'] ?></h1> <h1><?= $_SERVER['HTTP_HOST'] ?></h1>
</body> </body>
</html> </html>
<?php exit(); ?>