php-kilo/src/Row.php

711 lines
14 KiB
PHP

<?php declare(strict_types=1);
namespace Aviat\Kilo;
use Aviat\Kilo\Enum\Highlight;
use Aviat\Kilo\Enum\RawKeyCode;
/**
* @property-read int $size
* @property-read int $rsize
* @property-read string $chars
*/
class Row {
/**
* The version of the row to be displayed (where tabs are converted to display spaces)
*/
public string $render = '';
/**
* The mapping of characters to their highlighting type
*/
public array $hl = [];
/**
* Are we in the middle of highlighting a multi-line comment?
*/
private bool $hlOpenComment = FALSE;
/**
* Create a row in the current document
*
* @param Document $parent
* @param string $chars
* @param int $idx
* @return self
*/
public static function new(Document $parent, string $chars, int $idx): self
{
return new self(
$parent,
$chars,
$idx,
);
}
/**
* Create an empty Row
*
* @return self
*/
public static function default(): self
{
return new self(
Document::new(),
'',
0,
);
}
private function __construct(
/**
* The document that this row belongs to
*/
private Document $parent,
/**
* @var string The raw characters in the row
*/
private string $chars,
/**
* @var int The line number of the current row
*/
public int $idx,
) {}
public function __get(string $name): mixed
{
return match ($name)
{
'size' => strlen($this->chars),
'rsize' => strlen($this->render),
'chars' => $this->chars,
default => NULL,
};
}
/**
* Convert the row contents to a string for saving
*
* @return string
*/
public function __toString(): string
{
return $this->chars . "\n";
}
/**
* Set the properties to display for var_dump
*
* @return array
*/
public function __debugInfo(): array
{
return [
'size' => $this->size,
'rsize' => $this->rsize,
'chars' => $this->chars,
'render' => $this->render,
'hl' => $this->hl,
'hlOpenComment' => $this->hlOpenComment,
];
}
/**
* Is this row a valid part of a document?
*
* @return bool
*/
public function isValid(): bool
{
return ! $this->parent->isEmpty();
}
/**
* Insert the string or character $c at index $at
*
* @param int $at
* @param string $c
*/
public function insert(int $at, string $c): void
{
if ($at < 0 || $at > $this->size)
{
$this->append($c);
return;
}
// Safely insert into arbitrary position in the existing string
$this->chars = substr($this->chars, 0, $at) . $c . substr($this->chars, $at);
$this->update();
}
/**
* Append $s to the current row
*
* @param string $s
*/
public function append(string $s): void
{
$this->chars .= $s;
$this->update();
}
/**
* Delete the character at the specified index
*
* @param int $at
*/
public function delete(int $at): void
{
if ($at < 0 || $at >= $this->size)
{
return;
}
$this->chars = substr_replace($this->chars, '', $at, 1);
$this->update();
}
/**
* Set the contents of the Row
*
* @param string $chars
*/
public function setChars(string $chars): void
{
$this->chars = $chars;
$this->update();
}
/**
* Convert tabs to spaces for display, and update syntax highlighting
*/
public function update(): void
{
$this->render = tabs_to_spaces($this->chars);
$this->highlight();
}
// ------------------------------------------------------------------------
// ! Syntax Highlighting
// ------------------------------------------------------------------------
/**
* Parse the current file to apply syntax highlighting
*/
public function highlight(): void
{
$this->hl = array_fill(0, $this->rsize, Highlight::NORMAL);
if ($this->parent->fileType->name === 'PHP')
{
$this->highlightPHP();
return;
}
if ($this->parent->fileType->name === 'No filetype')
{
return;
}
$syntax = $this->parent->fileType->syntax;
$mcs = $syntax->multiLineCommentStart;
$mce = $syntax->multiLineCommentEnd;
$mcsLen = strlen($mcs);
$mceLen = strlen($mce);
$inString = '';
$inComment = ($this->idx > 0 && $this->parent->rows[$this->idx - 1]->hlOpenComment);
$i = 0;
while ($i < $this->rsize)
{
// Multi-line comments
if ($syntax->mlComments() && $inString === '')
{
if ($inComment)
{
$this->hl[$i] = Highlight::ML_COMMENT;
if (substr($this->render, $i, $mceLen) === $mce)
{
array_replace_range($this->hl, $i, $mceLen, Highlight::ML_COMMENT);
$i += $mceLen;
$inComment = FALSE;
continue;
}
$i++;
continue;
}
if (substr($this->render, $i, $mcsLen) === $mcs)
{
array_replace_range($this->hl, $i, $mcsLen, Highlight::ML_COMMENT);
$i += $mcsLen;
$inComment = TRUE;
continue;
}
}
if (
$this->highlightCharacter($i, $syntax)
|| $this->highlightComment($i, $syntax)
|| $this->highlightPrimaryKeywords($i, $syntax)
|| $this->highlightSecondaryKeywords($i, $syntax)
|| $this->highlightString($i, $syntax)
|| $this->highlightOperators($i, $syntax)
|| $this->highlightCommonDelimeters($i)
|| $this->highlightCommonOperators($i, $syntax)
|| $this->highlightNumber($i, $syntax)
) {
continue;
}
$i++;
}
$changed = $this->hlOpenComment !== $inComment;
$this->hlOpenComment = $inComment;
if ($changed && $this->idx + 1 < $this->parent->numRows)
{
$this->parent->rows[$this->idx + 1]->update();
}
}
/**
* Highlight number literals
*
* @param int $i
* @param Syntax $opts
* @return bool
*/
protected function highlightNumber(int &$i, Syntax $opts): bool
{
$char = $this->render[$i];
if ($opts->numbers() && is_digit($char) && $this->hl[$i] === Highlight::NORMAL)
{
if ($i > 0)
{
$prevChar = $this->render[$i - 1];
if ( ! is_separator($prevChar))
{
return false;
}
}
while (true)
{
$this->hl[$i] = Highlight::NUMBER;
$i++;
if ($i < strlen($this->render))
{
$nextChar = $this->render[$i];
if (stripos('abcdefx.1234567890', $nextChar) === FALSE)
{
return true;
}
continue;
}
break;
}
}
return false;
}
/**
* Highlight keywords and/or operators
*
* @param int $i
* @param array $keywords
* @param int $syntaxType
* @param bool $requireSeparator
* @return bool
*/
protected function highlightWord(int &$i, array $keywords, int $syntaxType, bool $requireSeparator = true): bool
{
if ($i > 0 && $requireSeparator)
{
$prevChar = $this->render[$i - 1];
if ( ! is_separator($prevChar))
{
return false;
}
}
foreach ($keywords as $k)
{
$klen = strlen($k);
$nextCharOffset = $i + $klen;
$isEndOfLine = $nextCharOffset >= $this->rsize;
$nextChar = ($isEndOfLine) ? RawKeyCode::NULL : $this->render[$nextCharOffset];
if (substr($this->render, $i, $klen) === $k && is_separator($nextChar))
{
array_replace_range($this->hl, $i, $klen, $syntaxType);
$i += $klen;
return true;
}
}
return false;
}
/**
* Highlight a single-char character from a list of provided keywords
*
* @param int $i
* @param array $chars
* @param int $syntaxType
* @return bool
*/
protected function highlightChar(int &$i, array $chars, int $syntaxType): bool
{
if ($this->hl[$i] !== Highlight::NORMAL)
{
return false;
}
$char = $this->render[$i];
if (in_array($char, $chars, TRUE))
{
$this->hl[$i] = $syntaxType;
$i++;
return true;
}
return false;
}
/**
* Highlight primary keywords
*
* @param int $i
* @param Syntax $opts
* @return bool
*/
protected function highlightPrimaryKeywords(int &$i, Syntax $opts): bool
{
return $this->highlightWord($i, $opts->keywords1, Highlight::KEYWORD1);
}
/**
* Highlight secondary keywords
*
* @param int $i
* @param Syntax $opts
* @return bool
*/
protected function highlightSecondaryKeywords(int &$i, Syntax $opts): bool
{
return $this->highlightWord($i, $opts->keywords2, Highlight::KEYWORD2);
}
/**
* Highlight language-specific operators
*
* @param int $i
* @param Syntax $opts
* @return bool
*/
protected function highlightOperators(int &$i, Syntax $opts): bool
{
return $this->highlightWord($i, $opts->operators, Highlight::OPERATOR, false);
}
/**
* Highlight common single-character operators
*
* @param int $i
* @param Syntax $opts
* @return bool
*/
protected function highlightCommonOperators(int &$i, Syntax $opts): bool
{
if ( ! $opts->commonOperators())
{
return false;
}
return $this->highlightChar(
$i,
['+', '-', '*', '/', '<', '^', '>', '%', '=', ':', ',', ';', '&', '~', '!', '|', '.'],
Highlight::OPERATOR
);
}
/**
* Highlight brackets and braces
*
* @param int $i
* @return bool
*/
protected function highlightCommonDelimeters(int &$i): bool
{
return $this->highlightChar(
$i,
['{', '}', '[', ']', '(', ')'],
Highlight::DELIMITER
);
}
/**
* Highlight character literals
*
* @param int $i
* @param Syntax $opts
* @return bool
*/
protected function highlightCharacter(int &$i, Syntax $opts): bool
{
$char = $this->render[$i];
if (($i + 1) >= $this->rsize || $char !== "'")
{
return false;
}
$nextChar = $this->render[$i + 1];
if ($opts->characters())
{
$offset = ($nextChar === '\\') ? $i + 3 : $i + 1;
$closingIndex = strpos($this->render, "'", $offset);
if ($closingIndex === false)
{
return false;
}
$closingChar = $this->render[$closingIndex];
if ($closingChar === "'")
{
array_replace_range($this->hl, $i, $closingIndex - $i + 1, Highlight::CHARACTER);
$i = $closingIndex + 1;
return true;
}
}
return false;
}
/**
* Highlight single-line comments
*
* @param int $i
* @param Syntax $opts
* @return bool
*/
protected function highlightComment(int &$i, Syntax $opts): bool
{
if ( ! $opts->comments())
{
return false;
}
$scs = $opts->singleLineCommentStart;
$scsLen = strlen($scs);
if ($scsLen > 0 && substr($this->render, $i, $scsLen) === $scs)
{
array_replace_range($this->hl, $i, $this->rsize - $i, Highlight::COMMENT);
$i = $this->rsize;
return true;
}
return false;
}
/**
* Highlight quote-delimited string literals
*
* @param int $i
* @param Syntax $opts
* @return bool
*/
protected function highlightString(int &$i, Syntax $opts): bool
{
$char = $this->render[$i];
// If there's a separate character type, highlight that separately
if ($opts->hasChar() && $char === "'")
{
return false;
}
if ($opts->strings() && ($char === '"' || $char === '\''))
{
$quote = $char;
$this->hl[$i] = Highlight::STRING;
$i++;
while ($i < $this->rsize)
{
$char = $this->render[$i];
$this->hl[$i] = Highlight::STRING;
// Check for escaped character
if ($char === '\\' && $i+1 < $this->rsize)
{
$this->hl[$i + 1] = Highlight::STRING;
$i += 2;
continue;
}
// End of the string!
$i++;
if ($char === $quote)
{
break;
}
}
return true;
}
return false;
}
/**
* Highlight PHP code based on pre-parsed tokens
*/
protected function highlightPHP(): void
{
$rowNum = $this->idx + 1;
$hasRowTokens = array_key_exists($rowNum, $this->parent->tokens);
if ( ! (
$hasRowTokens &&
$this->idx < $this->parent->numRows
))
{
return;
}
$tokens = $this->parent->tokens[$rowNum];
$inComment = ($this->idx > 0 && $this->parent->rows[$this->idx - 1]->hlOpenComment);
// Keep track of where you are in the line, so that
// multiples of the same tokens can be effectively matched
$offset = 0;
foreach ($tokens as $token)
{
if ($offset >= $this->rsize)
{
break;
}
// A multi-line comment can end in the middle of a line...
if ($inComment)
{
// Try looking for the end of the comment first
$commentEnd = strpos($this->render, '*/');
if ($commentEnd !== FALSE)
{
$inComment = FALSE;
array_replace_range($this->hl, 0, $commentEnd + 2, Highlight::ML_COMMENT);
$offset = $commentEnd;
continue;
}
// Otherwise, just set the whole row
$this->hl = array_fill(0, $this->rsize, Highlight::ML_COMMENT);
$this->hl[$offset] = Highlight::ML_COMMENT;
break;
}
$char = $token['char'];
$charLen = strlen($char);
if ($charLen === 0 || $offset >= $this->rsize)
{
continue;
}
$charStart = strpos($this->render, $char, $offset);
if ($charStart === FALSE)
{
continue;
}
$charEnd = $charStart + $charLen;
// Start of multiline comment/single line comment
if (in_array($token['type'], [T_DOC_COMMENT, T_COMMENT], TRUE))
{
// Single line comments
if (str_contains($char, '//') || str_contains($char, '#'))
{
array_replace_range($this->hl, $charStart, $charLen, Highlight::COMMENT);
break;
}
// Start of multi-line comment
$start = strpos($this->render, '/*', $offset);
$end = strpos($this->render, '*/', $offset);
$hasStart = $start !== FALSE;
$hasEnd = $end !== FALSE;
if ($hasStart)
{
if ($hasEnd)
{
$len = $end - $start + 2;
array_replace_range($this->hl, $start, $len, Highlight::ML_COMMENT);
$inComment = FALSE;
}
else
{
$inComment = TRUE;
array_replace_range($this->hl, $start, $charLen - $offset, Highlight::ML_COMMENT);
$offset = $start + $charLen - $offset;
}
}
if ($inComment)
{
break;
}
}
$tokenHighlight = Highlight::fromPHPToken($token['type']);
$charHighlight = Highlight::fromPHPChar(trim($token['char']));
$highlight = match(true) {
// Matches a predefined PHP token
$token['type'] !== T_RAW && $tokenHighlight !== Highlight::NORMAL
=> $tokenHighlight,
// Matches a specific syntax character
$charHighlight !== Highlight::NORMAL => $charHighlight,
default => Highlight::NORMAL,
};
if ($highlight !== Highlight::NORMAL)
{
array_replace_range($this->hl, $charStart, $charLen, $highlight);
$offset = $charEnd;
}
}
$changed = $this->hlOpenComment !== $inComment;
$this->hlOpenComment = $inComment;
if ($changed && ($this->idx + 1) < $this->parent->numRows)
{
$this->parent->rows[$this->idx + 1]->highlight();
}
}
}