php-kilo/src/Row.php

425 lines
9.4 KiB
PHP
Raw Normal View History

2019-10-24 16:57:27 -04:00
<?php declare(strict_types=1);
2019-11-08 16:27:08 -05:00
namespace Aviat\Kilo;
use Aviat\Kilo\Enum\Highlight;
2020-02-05 14:50:31 -05:00
use Aviat\Kilo\Enum\KeyCode;
2019-10-24 16:57:27 -04:00
/**
2021-03-03 16:35:58 -05:00
* @property-read int $size
* @property-read int $rsize
2019-10-24 16:57:27 -04:00
*/
class Row {
2019-11-19 15:57:51 -05:00
use Traits\MagicProperties;
2019-10-24 16:57:27 -04:00
2019-11-05 12:28:10 -05:00
private string $chars = '';
2019-10-24 16:57:27 -04:00
public string $render = '';
2019-10-25 16:36:03 -04:00
public array $hl = [];
public int $idx;
2019-10-25 10:28:15 -04:00
// This feels dirty...
private Editor $parent;
2019-10-25 16:36:03 -04:00
private bool $hlOpenComment = FALSE;
2019-10-25 10:28:15 -04:00
private const T_RAW = -1;
2019-10-25 16:36:03 -04:00
public static function new(Editor $parent, string $chars, int $idx): self
2019-10-24 16:57:27 -04:00
{
2019-10-25 10:28:15 -04:00
$self = new self();
$self->chars = $chars;
$self->parent = $parent;
2019-10-25 16:36:03 -04:00
$self->idx = $idx;
2019-10-24 16:57:27 -04:00
2019-10-25 10:28:15 -04:00
return $self;
2019-10-24 16:57:27 -04:00
}
2019-10-25 10:28:15 -04:00
private function __construct() {}
2021-03-03 16:35:58 -05:00
public function __get(string $name): mixed
2019-10-24 16:57:27 -04:00
{
2021-03-03 11:50:29 -05:00
return match ($name)
2019-10-24 16:57:27 -04:00
{
2021-03-03 11:50:29 -05:00
'size' => strlen($this->chars),
'rsize' => strlen($this->render),
'chars' => $this->chars,
default => NULL,
};
2019-10-24 16:57:27 -04:00
}
2021-03-03 16:35:58 -05:00
public function __set(string $name, mixed $value): void
2019-11-05 12:28:10 -05:00
{
2021-03-03 16:35:58 -05:00
if ($name === 'chars')
2019-11-05 12:28:10 -05:00
{
$this->chars = $value;
$this->update();
}
}
2019-10-24 16:57:27 -04:00
public function __toString(): string
{
return $this->chars . "\n";
}
2019-11-20 15:03:48 -05:00
public function __debugInfo(): array
2019-11-08 21:48:46 -05:00
{
return [
'size' => $this->size,
'rsize' => $this->rsize,
'chars' => $this->chars,
'render' => $this->render,
'hl' => $this->hl,
2019-11-08 21:48:46 -05:00
'hlOpenComment' => $this->hlOpenComment,
];
}
2019-10-25 10:28:15 -04:00
public function insertChar(int $at, string $c): void
{
if ($at < 0 || $at > $this->size)
{
2019-11-20 15:03:48 -05:00
$this->appendString($c);
return;
2019-10-25 10:28:15 -04:00
}
// Safely insert into arbitrary position in the existing string
$this->chars = substr($this->chars, 0, $at) . $c . substr($this->chars, $at);
$this->update();
$this->parent->dirty++;
}
public function appendString(string $s): void
{
$this->chars .= $s;
$this->update();
$this->parent->dirty++;
}
public function deleteChar(int $at): void
{
if ($at < 0 || $at >= $this->size)
{
return;
}
$this->chars = substr_replace($this->chars, '', $at, 1);
$this->update();
$this->parent->dirty++;
}
2019-10-24 16:57:27 -04:00
public function update(): void
{
2019-11-08 13:32:31 -05:00
$this->render = tabs_to_spaces($this->chars);
2019-10-24 16:57:27 -04:00
$this->updateSyntax();
}
// ------------------------------------------------------------------------
// ! Syntax Highlighting
// ------------------------------------------------------------------------
public function updateSyntax(): void
2019-10-24 16:57:27 -04:00
{
$this->hl = array_fill(0, $this->rsize, Highlight::NORMAL);
2019-11-20 15:03:48 -05:00
if ($this->parent->syntax === NULL)
2019-11-08 16:27:08 -05:00
{
return;
}
if ($this->parent->syntax->filetype === 'PHP')
{
$this->updateSyntaxPHP();
return;
}
$keywords1 = $this->parent->syntax->keywords1;
$keywords2 = $this->parent->syntax->keywords2;
$scs = $this->parent->syntax->singleLineCommentStart;
2019-10-25 16:36:03 -04:00
$mcs = $this->parent->syntax->multiLineCommentStart;
$mce = $this->parent->syntax->multiLineCommentEnd;
$scsLen = strlen($scs);
$mcsLen = strlen($mcs);
$mceLen = strlen($mce);
2019-10-24 16:57:27 -04:00
$prevSep = TRUE;
2019-10-25 11:49:04 -04:00
$inString = '';
2019-10-25 16:36:03 -04:00
$inComment = ($this->idx > 0 && $this->parent->rows[$this->idx - 1]->hlOpenComment);
2019-10-24 16:57:27 -04:00
$i = 0;
while ($i < $this->rsize)
{
$char = $this->render[$i];
$prevHl = ($i > 0) ? $this->hl[$i - 1] : Highlight::NORMAL;
// Single-line comments
2019-10-25 16:36:03 -04:00
if ($scsLen > 0 && $inString === '' && $inComment === FALSE
&& substr($this->render, $i, $scsLen) === $scs)
{
array_replace_range($this->hl, $i, $this->rsize - $i, Highlight::COMMENT);
break;
}
2019-10-25 16:36:03 -04:00
// Multi-line comments
if ($mcsLen > 0 && $mceLen > 0 && $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;
$prevSep = TRUE;
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;
}
}
// String/Char literals
2019-10-25 11:49:04 -04:00
if ($this->parent->syntax->flags & Syntax::HIGHLIGHT_STRINGS)
{
if ($inString !== '')
{
$this->hl[$i] = Highlight::STRING;
// Check for escaped character
if ($char === '\\' && $i+1 < $this->rsize)
{
$this->hl[$i + 1] = Highlight::STRING;
$i += 2;
continue;
}
if ($char === $inString)
{
$inString = '';
}
$i++;
$prevSep = 1;
continue;
}
if ( $char === '"' || $char === '\'')
2019-10-25 11:49:04 -04:00
{
$inString = $char;
$this->hl[$i] = Highlight::STRING;
$i++;
continue;
}
}
// Numbers, including decimal points
2019-10-25 10:28:15 -04:00
if ($this->parent->syntax->flags & Syntax::HIGHLIGHT_NUMBERS)
2019-10-24 16:57:27 -04:00
{
2019-10-25 10:28:15 -04:00
if (
($char === '.' && $prevHl === Highlight::NUMBER) ||
(($prevSep || $prevHl === Highlight::NUMBER) && is_digit($char))
)
{
$this->hl[$i] = Highlight::NUMBER;
$i++;
$prevSep = FALSE;
continue;
}
2019-10-24 16:57:27 -04:00
}
// Keywords
if ($prevSep)
{
2019-11-05 12:28:10 -05:00
$findKeywords = function (array $keywords, int $syntaxType) use (&$i): void
{
foreach ($keywords as $k)
{
$klen = strlen($k);
$nextCharOffset = $i + $klen;
$isEndOfLine = $nextCharOffset >= $this->rsize;
2020-02-05 14:50:31 -05:00
$nextChar = ($isEndOfLine) ? KeyCode::NULL : $this->render[$nextCharOffset];
2019-11-05 12:28:10 -05:00
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);
}
2019-10-24 16:57:27 -04:00
$prevSep = is_separator($char);
$i++;
}
2019-10-25 16:36:03 -04:00
$changed = $this->hlOpenComment !== $inComment;
$this->hlOpenComment = $inComment;
if ($changed && $this->idx + 1 < $this->parent->numRows)
{
// @codeCoverageIgnoreStart
$this->parent->rows[$this->idx + 1]->updateSyntax();
// @codeCoverageIgnoreEnd
2019-10-25 16:36:03 -04:00
}
2019-10-24 16:57:27 -04:00
}
protected function updateSyntaxPHP():void
{
$rowNum = $this->idx + 1;
$hasRowTokens = array_key_exists($rowNum, $this->parent->tokens);
2019-11-14 11:12:32 -05:00
if ( ! (
$hasRowTokens &&
2019-11-14 11:12:32 -05:00
$this->idx < $this->parent->numRows
))
2019-11-05 12:56:12 -05:00
{
// @codeCoverageIgnoreStart
2019-11-05 12:56:12 -05:00
return;
// @codeCoverageIgnoreEnd
2019-11-05 12:56:12 -05:00
}
2019-11-05 13:21:37 -05:00
$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)
{
// @codeCoverageIgnoreStart
break;
// @codeCoverageIgnoreEnd
}
2019-11-06 13:57:19 -05:00
// A multi-line comment can end in the middle of a line...
2019-11-08 12:02:00 -05:00
if ($inComment)
{
2019-11-08 12:02:00 -05:00
// Try looking for the end of the comment first
$commentEnd = strpos($this->render, '*/');
if ($commentEnd !== FALSE)
2019-11-06 13:57:19 -05:00
{
$inComment = FALSE;
array_replace_range($this->hl, 0, $commentEnd + 2, Highlight::ML_COMMENT);
2019-11-14 17:11:10 -05:00
$offset = $commentEnd;
2019-11-08 12:02:00 -05:00
continue;
}
2019-11-08 12:02:00 -05:00
// Otherwise, just set the whole row
$this->hl = array_fill(0, $this->rsize, Highlight::ML_COMMENT);
$this->hl[$offset] = Highlight::ML_COMMENT;
2019-11-08 12:02:00 -05:00
break;
}
2019-11-06 13:57:19 -05:00
2021-03-03 13:14:44 -05:00
$char = $token['char']; // ?? '';
2019-11-06 13:57:19 -05:00
$charLen = strlen($char);
if ($charLen === 0 || $offset >= $this->rsize)
{
// @codeCoverageIgnoreStart
2019-11-06 13:57:19 -05:00
continue;
// @codeCoverageIgnoreEnd
2019-11-06 13:57:19 -05:00
}
$charStart = strpos($this->render, $char, $offset);
if ($charStart === FALSE)
{
continue;
}
2019-11-08 12:02:00 -05:00
$charEnd = $charStart + $charLen;
2019-11-06 13:57:19 -05:00
// Start of multiline comment/single line comment
if (in_array($token['type'], [T_DOC_COMMENT, T_COMMENT], TRUE))
{
// Single line comments
2021-03-03 11:50:29 -05:00
if (str_contains($char, '//') || str_contains($char, '#'))
{
2019-11-08 12:02:00 -05:00
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);
2019-11-14 11:12:32 -05:00
$hasStart = $start !== FALSE;
$hasEnd = $end !== FALSE;
if ($hasStart)
{
2019-11-14 11:12:32 -05:00
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;
}
2019-11-08 12:02:00 -05:00
}
if ($inComment)
{
break;
}
2019-10-29 17:24:04 -04:00
}
2021-03-03 16:35:58 -05:00
$tokenHighlight = php_token_to_highlight($token['type']);
$charHighlight = php_char_to_highlight(trim($token['char']));
2019-11-06 13:57:19 -05:00
2021-03-03 16:35:58 -05:00
$hl = match(true) {
// Matches a predefined PHP token
$token['type'] !== self::T_RAW && $tokenHighlight !== Highlight::NORMAL
=> $tokenHighlight,
// Matches a specific syntax character
$charHighlight !== Highlight::NORMAL => $charHighlight,
// Types/identifiers/keywords that don't have their own token, but are
// defined as keywords
in_array($token['char'], $this->parent->syntax->keywords2 ?? [], TRUE)
=> Highlight::KEYWORD2,
2021-03-03 16:35:58 -05:00
default => Highlight::NORMAL,
};
if ($hl !== Highlight::NORMAL)
{
2019-11-08 12:02:00 -05:00
array_replace_range($this->hl, $charStart, $charLen, $hl);
$offset = $charEnd;
}
}
$changed = $this->hlOpenComment !== $inComment;
$this->hlOpenComment = $inComment;
2021-03-03 16:35:58 -05:00
if ($changed && ($this->idx + 1) < $this->parent->numRows)
{
// @codeCoverageIgnoreStart
$this->parent->rows[$this->idx + 1]->updateSyntax();
// @codeCoverageIgnoreEnd
}
}
2019-10-24 16:57:27 -04:00
}