Docblocks for some public methods, and a few highlighting tweaks
Some checks failed
timw4mail/php-kilo/pipeline/head There was a failure building this commit

This commit is contained in:
Timothy Warren 2021-04-14 13:15:31 -04:00
parent 5746d15117
commit 2956999737
2 changed files with 70 additions and 9 deletions

View File

@ -23,10 +23,21 @@ class Row {
*/ */
public array $hl = []; public array $hl = [];
/**
* Are we in the middle of highlighting a multi-line comment?
*/
private bool $hlOpenComment = FALSE; private bool $hlOpenComment = FALSE;
private const T_RAW = -1; private const T_RAW = -1;
/**
* 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 public static function new(Document $parent, string $chars, int $idx): self
{ {
return new self( return new self(
@ -36,6 +47,11 @@ class Row {
); );
} }
/**
* Create an empty Row
*
* @return self
*/
public static function default(): self public static function default(): self
{ {
return new self( return new self(
@ -82,11 +98,21 @@ class Row {
} }
} }
/**
* Convert the row contents to a string for saving
*
* @return string
*/
public function __toString(): string public function __toString(): string
{ {
return $this->chars . "\n"; return $this->chars . "\n";
} }
/**
* Set the properties to display for var_dump
*
* @return array
*/
public function __debugInfo(): array public function __debugInfo(): array
{ {
return [ return [
@ -109,6 +135,12 @@ class Row {
return ! $this->parent->isEmpty(); 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 public function insert(int $at, string $c): void
{ {
if ($at < 0 || $at > $this->size) if ($at < 0 || $at > $this->size)
@ -119,19 +151,29 @@ class Row {
// Safely insert into arbitrary position in the existing string // Safely insert into arbitrary position in the existing string
$this->chars = substr($this->chars, 0, $at) . $c . substr($this->chars, $at); $this->chars = substr($this->chars, 0, $at) . $c . substr($this->chars, $at);
$this->highlight(); $this->update();
$this->parent->dirty = true; $this->parent->dirty = true;
} }
/**
* Append $s to the current row
*
* @param string $s
*/
public function append(string $s): void public function append(string $s): void
{ {
$this->chars .= $s; $this->chars .= $s;
$this->highlight(); $this->update();
$this->parent->dirty = true; $this->parent->dirty = true;
} }
/**
* Delete the character at the specified index
*
* @param int $at
*/
public function delete(int $at): void public function delete(int $at): void
{ {
if ($at < 0 || $at >= $this->size) if ($at < 0 || $at >= $this->size)
@ -140,18 +182,29 @@ class Row {
} }
$this->chars = substr_replace($this->chars, '', $at, 1); $this->chars = substr_replace($this->chars, '', $at, 1);
$this->highlight(); $this->update();
$this->parent->dirty = true; $this->parent->dirty = true;
} }
/**
* 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 // ! Syntax Highlighting
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/**
* Parse the current file to apply syntax highlighting
*/
public function highlight(): void public function highlight(): void
{ {
$this->render = tabs_to_spaces($this->chars);
$this->hl = array_fill(0, $this->rsize, Highlight::NORMAL); $this->hl = array_fill(0, $this->rsize, Highlight::NORMAL);
if ($this->parent->fileType->name === 'PHP') if ($this->parent->fileType->name === 'PHP')
@ -214,6 +267,7 @@ class Row {
|| $this->highlightString($i, $syntax) || $this->highlightString($i, $syntax)
|| $this->highlightNumber($i, $syntax) || $this->highlightNumber($i, $syntax)
) { ) {
$i++;
continue; continue;
} }
@ -395,7 +449,7 @@ class Row {
protected function highlightMultilineComments(int &$i, Syntax $opts): bool protected function highlightMultilineComments(int &$i, Syntax $opts): bool
{ {
if ( ! $opts->comments()) if ( ! $opts->mlComments())
{ {
return false; return false;
} }
@ -569,7 +623,7 @@ class Row {
$tokenHighlight = Highlight::fromPHPToken($token['type']); $tokenHighlight = Highlight::fromPHPToken($token['type']);
$charHighlight = Highlight::fromPHPChar(trim($token['char'])); $charHighlight = Highlight::fromPHPChar(trim($token['char']));
$hl = match(true) { $highlight = match(true) {
// Matches a predefined PHP token // Matches a predefined PHP token
$token['type'] !== self::T_RAW && $tokenHighlight !== Highlight::NORMAL $token['type'] !== self::T_RAW && $tokenHighlight !== Highlight::NORMAL
=> $tokenHighlight, => $tokenHighlight,
@ -585,9 +639,9 @@ class Row {
default => Highlight::NORMAL, default => Highlight::NORMAL,
}; };
if ($hl !== Highlight::NORMAL) if ($highlight !== Highlight::NORMAL)
{ {
array_replace_range($this->hl, $charStart, $charLen, $hl); array_replace_range($this->hl, $charStart, $charLen, $highlight);
$offset = $charEnd; $offset = $charEnd;
} }
} }

View File

@ -89,8 +89,15 @@ class Syntax {
return $this->hasCharType && $this->highlightCharacters; return $this->hasCharType && $this->highlightCharacters;
} }
public function mlComments(): bool
{
return $this->highlightComments
&& strlen($this->multiLineCommentStart) !== 0
&& strlen($this->multiLineCommentStart) !== 0;
}
public function comments(): bool public function comments(): bool
{ {
return $this->highlightComments; return $this->highlightComments && strlen($this->singleLineCommentStart) !== 0;
} }
} }