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;
// Tokens for highlighting PHP
public array $tokens = [];
public static function new(): Editor
{
return new self();
@ -129,7 +132,7 @@ class Editor {
// Pre-tokenize the file
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
@ -192,12 +195,6 @@ class Editor {
$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)
{
$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->dirty++;
@ -285,7 +290,7 @@ class Editor {
$row->chars = $newChars;
// 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++;
@ -332,11 +337,7 @@ class Editor {
protected function rowsToString(): string
{
$lines = [];
foreach ($this->rows as $row)
{
$lines[] = (string)$row;
}
$lines = array_map(fn (Row $row) => (string)$row, $this->rows);
return implode('', $lines);
}
@ -930,10 +931,10 @@ class Editor {
private function refreshPHPSyntax(): void
{
$this->syntax->tokens = PHP::getTokens($this->rowsToString());
$this->tokens = PHP::getTokens($this->rowsToString());
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 bool $hlOpenComment = FALSE;
private const T_RAW = -1;
private array $phpTokenHighlightMap = [
// Delimiters
T_ARRAY => Highlight::DELIMITER,
@ -229,6 +231,7 @@ class Row {
'rsize' => $this->rsize,
'chars' => $this->chars,
'render' => $this->render,
'hl' => $this->hl,
'hlOpenComment' => $this->hlOpenComment,
];
}
@ -446,19 +449,17 @@ class Row {
{
$rowNum = $this->idx + 1;
$hasTokens = isset($this->parent->syntax->tokens);
$hasRow = $hasTokens && array_key_exists($rowNum, $this->parent->syntax->tokens);
$hasRowTokens = array_key_exists($rowNum, $this->parent->tokens);
if ( ! (
$hasTokens &&
$hasRow &&
$hasRowTokens &&
$this->idx < $this->parent->numRows
))
{
return;
}
$tokens = $this->parent->syntax->tokens[$rowNum];
$tokens = $this->parent->tokens[$rowNum];
$inComment = ($this->idx > 0 && $this->parent->rows[$this->idx - 1]->hlOpenComment);
@ -567,7 +568,7 @@ class Row {
}
// 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'])];
array_replace_range($this->hl, $charStart, $charLen, $hl);

View File

@ -23,7 +23,7 @@ class PHP {
$this->code = $code;
$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>
</body>
</html>
<?php exit(); ?>