Refactor to use new enums

This commit is contained in:
Timothy Warren 2021-12-03 11:59:42 -05:00
parent 9214c16595
commit 36da56c519
9 changed files with 111 additions and 111 deletions

View File

@ -1,4 +1,4 @@
FROM php:8-alpine3.13
FROM php:alpine
RUN apk add --no-cache --virtual .persistent-deps libffi-dev \
&& docker-php-ext-configure ffi --with-ffi \

2
Jenkinsfile vendored
View File

@ -5,7 +5,7 @@ pipeline {
}
}
stages {
stage('PHP 7.4') {
stage('PHP 8.1') {
steps {
sh 'apk add --no-cache -X http://dl-cdn.alpinelinux.org/alpine/edge/testing php8-phpdbg'
sh 'curl -sS https://getcomposer.org/installer | php'

View File

@ -58,12 +58,12 @@ class ANSI {
/**
* Generate the ascii sequence for basic foreground text color
*
* @param int $color
* @param Enum\Color $color
* @return string
*/
public static function color(int $color): string
public static function color(Enum\Color $color): string
{
return self::escapeSequence('%dm', $color);
return self::escapeSequence('%dm', $color->value);
}
/**

View File

@ -95,14 +95,14 @@ class Document {
return $res;
}
public function insert(Point $at, string $c): void
public function insert(Point $at, string|KeyType $c): void
{
if ($at->y > $this->numRows)
{
return;
}
if ($c === KeyType::ENTER || $c === RawKeyCode::CARRIAGE_RETURN)
if ($c === KeyType::Enter || $c === RawKeyCode::CARRIAGE_RETURN)
{
$this->insertNewline($at);
$this->dirty = true;

View File

@ -233,7 +233,7 @@ class Editor {
// ! Find
// ------------------------------------------------------------------------
protected function findCallback(string $query, string $key): void
protected function findCallback(string $query, string|KeyType $key): void
{
static $lastMatch = NO_MATCH;
static $direction = SearchDirection::FORWARD;
@ -254,11 +254,11 @@ class Editor {
}
$direction = match ($key) {
KeyType::ARROW_UP, KeyType::ARROW_LEFT => SearchDirection::BACKWARD,
KeyType::ArrowUp, KeyType::ArrowLeft => SearchDirection::BACKWARD,
default => SearchDirection::FORWARD
};
$arrowKeys = [KeyType::ARROW_UP, KeyType::ARROW_DOWN, KeyType::ARROW_LEFT, KeyType::ARROW_RIGHT];
$arrowKeys = [KeyType::ArrowUp, KeyType::ArrowDown, KeyType::ArrowLeft, KeyType::ArrowRight];
// Reset search state with non arrow-key input
if ( ! in_array($key, $arrowKeys, true))
@ -286,7 +286,7 @@ class Editor {
for ($i = 0; $i < $this->document->numRows; $i++)
{
$current += $direction;
$current += $direction->value;
if ($current === -1)
{
$current = $this->document->numRows - 1;
@ -313,7 +313,7 @@ class Editor {
$savedHlLine = $current;
$savedHl = $row->hl;
// Update the highlight array of the relevant row with the 'MATCH' type
array_replace_range($row->hl, $match, strlen($query), Highlight::MATCH);
array_replace_range($row->hl, $match, strlen($query), Highlight::SearchMatch);
break;
}
@ -432,7 +432,7 @@ class Editor {
$this->outputBuffer .= ANSI::color($currentColor);
}
}
else if ($hl[$i] === Highlight::NORMAL)
else if ($hl[$i] === Highlight::Normal)
{
if ($currentColor !== -1)
{
@ -574,7 +574,7 @@ class Editor {
$c = Terminal::readKey();
$isModifier = in_array($c, $modifiers, TRUE);
if ($c === KeyType::ESCAPE || ($c === RawKeyCode::ENTER && $buffer !== ''))
if ($c === KeyType::Escape || ($c === RawKeyCode::ENTER && $buffer !== ''))
{
$this->setStatusMessage('');
if ($callback !== NULL)
@ -584,7 +584,7 @@ class Editor {
return ($c === RawKeyCode::ENTER) ? $buffer : '';
}
if ($c === KeyType::DELETE || $c === KeyType::BACKSPACE)
if ($c === KeyType::Delete || $c === KeyType::Backspace)
{
$buffer = substr($buffer, 0, -1);
}
@ -618,32 +618,32 @@ class Editor {
$this->quitAttempt();
return;
case RawKeyCode::CTRL('s'):
$this->save();
break;
case RawKeyCode::CTRL('s'):
$this->save();
break;
case RawKeyCode::CTRL('f'):
$this->find();
break;
case RawKeyCode::CTRL('f'):
$this->find();
break;
case KeyType::DELETE:
case KeyType::BACKSPACE:
case KeyType::Delete:
case KeyType::Backspace:
$this->removeChar($c);
break;
case KeyType::ARROW_UP:
case KeyType::ARROW_DOWN:
case KeyType::ARROW_LEFT:
case KeyType::ARROW_RIGHT:
case KeyType::PAGE_UP:
case KeyType::PAGE_DOWN:
case KeyType::HOME:
case KeyType::END:
case KeyType::ArrowUp:
case KeyType::ArrowDown:
case KeyType::ArrowLeft:
case KeyType::ArrowRight:
case KeyType::PageUp:
case KeyType::PageDown:
case KeyType::Home:
case KeyType::End:
$this->moveCursor($c);
break;
case RawKeyCode::CTRL('l'):
case KeyType::ESCAPE:
case KeyType::Escape:
// Do nothing
break;
@ -664,7 +664,7 @@ class Editor {
// ! Editor operation helpers
// ------------------------------------------------------------------------
protected function moveCursor(string $key): void
protected function moveCursor(KeyType $key): void
{
$x = $this->cursor->x;
$y = $this->cursor->y;
@ -676,7 +676,7 @@ class Editor {
switch ($key)
{
case KeyType::ARROW_LEFT:
case KeyType::ArrowLeft:
if ($x !== 0)
{
$x--;
@ -689,7 +689,7 @@ class Editor {
}
break;
case KeyType::ARROW_RIGHT:
case KeyType::ArrowRight:
if ($x < $row->size)
{
$x++;
@ -701,33 +701,33 @@ class Editor {
}
break;
case KeyType::ARROW_UP:
case KeyType::ArrowUp:
if ($y !== 0)
{
$y--;
}
break;
case KeyType::ARROW_DOWN:
case KeyType::ArrowDown:
if ($y < $this->document->numRows)
{
$y++;
}
break;
case KeyType::PAGE_UP:
case KeyType::PageUp:
$y = saturating_sub($y, $this->terminalSize->rows);
break;
case KeyType::PAGE_DOWN:
case KeyType::PageDown:
$y = saturating_add($y, $this->terminalSize->rows, $this->document->numRows);
break;
case KeyType::HOME:
case KeyType::Home:
$x = 0;
break;
case KeyType::END:
case KeyType::End:
if ($y < $this->document->numRows)
{
$x = $row->size;
@ -753,22 +753,22 @@ class Editor {
}
}
protected function insertChar(string $c): void
protected function insertChar(string|KeyType $c): void
{
$this->document->insert($this->cursor, $c);
$this->moveCursor(KeyType::ARROW_RIGHT);
$this->moveCursor(KeyType::ArrowRight);
}
protected function removeChar(string $ch): void
protected function removeChar(string|KeyType $ch): void
{
if ($ch === KeyType::DELETE)
if ($ch === KeyType::Delete)
{
$this->document->delete($this->cursor);
}
if ($ch === KeyType::BACKSPACE && ($this->cursor->x > 0 || $this->cursor->y > 0))
if ($ch === KeyType::Backspace && ($this->cursor->x > 0 || $this->cursor->y > 0))
{
$this->moveCursor(KeyType::ARROW_LEFT);
$this->moveCursor(KeyType::ArrowLeft);
$this->document->delete($this->cursor);
}
}

View File

@ -197,7 +197,7 @@ class Row {
*/
public function highlight(): void
{
$this->hl = array_fill(0, $this->rsize, Highlight::NORMAL);
$this->hl = array_fill(0, $this->rsize, Highlight::Normal);
if ($this->parent->fileType->name === 'PHP')
{
@ -230,10 +230,10 @@ class Row {
{
if ($inComment)
{
$this->hl[$i] = Highlight::ML_COMMENT;
$this->hl[$i] = Highlight::MultiLineComment;
if (substr($this->render, $i, $mceLen) === $mce)
{
array_replace_range($this->hl, $i, $mceLen, Highlight::ML_COMMENT);
array_replace_range($this->hl, $i, $mceLen, Highlight::MultiLineComment);
$i += $mceLen;
$inComment = FALSE;
continue;
@ -245,7 +245,7 @@ class Row {
if (substr($this->render, $i, $mcsLen) === $mcs)
{
array_replace_range($this->hl, $i, $mcsLen, Highlight::ML_COMMENT);
array_replace_range($this->hl, $i, $mcsLen, Highlight::MultiLineComment);
$i += $mcsLen;
$inComment = TRUE;
continue;
@ -287,7 +287,7 @@ class Row {
protected function highlightNumber(int &$i, Syntax $opts): bool
{
$char = $this->render[$i];
if ($opts->numbers() && is_digit($char) && $this->hl[$i] === Highlight::NORMAL)
if ($opts->numbers() && is_digit($char) && $this->hl[$i] === Highlight::Normal)
{
if ($i > 0)
{
@ -300,7 +300,7 @@ class Row {
while (true)
{
$this->hl[$i] = Highlight::NUMBER;
$this->hl[$i] = Highlight::Number;
$i++;
if ($i < strlen($this->render))
@ -326,11 +326,11 @@ class Row {
*
* @param int $i
* @param array $keywords
* @param int $syntaxType
* @param Highlight $syntaxType
* @param bool $requireSeparator
* @return bool
*/
protected function highlightWord(int &$i, array $keywords, int $syntaxType, bool $requireSeparator = true): bool
protected function highlightWord(int &$i, array $keywords, Highlight $syntaxType, bool $requireSeparator = true): bool
{
if ($i > 0 && $requireSeparator)
{
@ -365,12 +365,12 @@ class Row {
*
* @param int $i
* @param array $chars
* @param int $syntaxType
* @param Highlight $syntaxType
* @return bool
*/
protected function highlightChar(int &$i, array $chars, int $syntaxType): bool
protected function highlightChar(int &$i, array $chars, Highlight $syntaxType): bool
{
if ($this->hl[$i] !== Highlight::NORMAL)
if ($this->hl[$i] !== Highlight::Normal)
{
return false;
}
@ -397,7 +397,7 @@ class Row {
*/
protected function highlightPrimaryKeywords(int &$i, Syntax $opts): bool
{
return $this->highlightWord($i, $opts->keywords1, Highlight::KEYWORD1);
return $this->highlightWord($i, $opts->keywords1, Highlight::Keyword1);
}
/**
@ -409,7 +409,7 @@ class Row {
*/
protected function highlightSecondaryKeywords(int &$i, Syntax $opts): bool
{
return $this->highlightWord($i, $opts->keywords2, Highlight::KEYWORD2);
return $this->highlightWord($i, $opts->keywords2, Highlight::Keyword2);
}
/**
@ -421,7 +421,7 @@ class Row {
*/
protected function highlightOperators(int &$i, Syntax $opts): bool
{
return $this->highlightWord($i, $opts->operators, Highlight::OPERATOR, false);
return $this->highlightWord($i, $opts->operators, Highlight::Operator, false);
}
/**
@ -441,7 +441,7 @@ class Row {
return $this->highlightChar(
$i,
['+', '-', '*', '/', '<', '^', '>', '%', '=', ':', ',', ';', '&', '~', '!', '|', '.'],
Highlight::OPERATOR
Highlight::Operator
);
}
@ -456,7 +456,7 @@ class Row {
return $this->highlightChar(
$i,
['{', '}', '[', ']', '(', ')'],
Highlight::DELIMITER
Highlight::Delimiter
);
}
@ -489,7 +489,7 @@ class Row {
$closingChar = $this->render[$closingIndex];
if ($closingChar === "'")
{
array_replace_range($this->hl, $i, $closingIndex - $i + 1, Highlight::CHARACTER);
array_replace_range($this->hl, $i, $closingIndex - $i + 1, Highlight::Character);
$i = $closingIndex + 1;
return true;
@ -518,7 +518,7 @@ class Row {
if ($scsLen > 0 && substr($this->render, $i, $scsLen) === $scs)
{
array_replace_range($this->hl, $i, $this->rsize - $i, Highlight::COMMENT);
array_replace_range($this->hl, $i, $this->rsize - $i, Highlight::Comment);
$i = $this->rsize;
return true;
@ -547,18 +547,18 @@ class Row {
if ($opts->strings() && ($char === '"' || $char === '\''))
{
$quote = $char;
$this->hl[$i] = Highlight::STRING;
$this->hl[$i] = Highlight::String;
$i++;
while ($i < $this->rsize)
{
$char = $this->render[$i];
$this->hl[$i] = Highlight::STRING;
$this->hl[$i] = Highlight::String;
// Check for escaped character
if ($char === '\\' && $i+1 < $this->rsize)
{
$this->hl[$i + 1] = Highlight::STRING;
$this->hl[$i + 1] = Highlight::String;
$i += 2;
continue;
}
@ -617,14 +617,14 @@ class Row {
if ($commentEnd !== FALSE)
{
$inComment = FALSE;
array_replace_range($this->hl, 0, $commentEnd + 2, Highlight::ML_COMMENT);
array_replace_range($this->hl, 0, $commentEnd + 2, Highlight::MultiLineComment);
$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;
$this->hl = array_fill(0, $this->rsize, Highlight::MultiLineComment);
$this->hl[$offset] = Highlight::MultiLineComment;
break;
}
@ -647,7 +647,7 @@ class Row {
// Single line comments
if (str_contains($char, '//') || str_contains($char, '#'))
{
array_replace_range($this->hl, $charStart, $charLen, Highlight::COMMENT);
array_replace_range($this->hl, $charStart, $charLen, Highlight::Comment);
break;
}
@ -662,13 +662,13 @@ class Row {
if ($hasEnd)
{
$len = $end - $start + 2;
array_replace_range($this->hl, $start, $len, Highlight::ML_COMMENT);
array_replace_range($this->hl, $start, $len, Highlight::MultiLineComment);
$inComment = FALSE;
}
else
{
$inComment = TRUE;
array_replace_range($this->hl, $start, $charLen - $offset, Highlight::ML_COMMENT);
array_replace_range($this->hl, $start, $charLen - $offset, Highlight::MultiLineComment);
$offset = $start + $charLen - $offset;
}
}
@ -684,16 +684,16 @@ class Row {
$highlight = match(true) {
// Matches a predefined PHP token
$token['type'] !== T_RAW && $tokenHighlight !== Highlight::NORMAL
$token['type'] !== T_RAW && $tokenHighlight !== Highlight::Normal
=> $tokenHighlight,
// Matches a specific syntax character
$charHighlight !== Highlight::NORMAL => $charHighlight,
$charHighlight !== Highlight::Normal => $charHighlight,
default => Highlight::NORMAL,
default => Highlight::Normal,
};
if ($highlight !== Highlight::NORMAL)
if ($highlight !== Highlight::Normal)
{
array_replace_range($this->hl, $charStart, $charLen, $highlight);
$offset = $charEnd;

View File

@ -22,7 +22,7 @@ class Syntax {
bool $hasCharType = false,
bool $highlightCharacters = false,
bool $hasCommonOperators = true,
string $syntaxFamily = SyntaxFamily::C,
SyntaxFamily $syntaxFamily = SyntaxFamily::C,
): self
{
return new self(
@ -84,7 +84,7 @@ class Syntax {
/** should we highlight common operators? */
private bool $hasCommonOperators,
/** What kind of general syntax family does this file belong to? */
private string $syntaxFamily,
private SyntaxFamily $syntaxFamily,
) {}
public function numbers(): bool
@ -124,7 +124,7 @@ class Syntax {
return $this->hasCommonOperators;
}
public function syntaxFamily(): string
public function syntaxFamily(): SyntaxFamily
{
return $this->syntaxFamily;
}

View File

@ -81,35 +81,35 @@ class Terminal {
* Get the last key input from the terminal and convert to a
* more useful format
*
* @return string
* @return string|KeyType
*/
public static function readKey(): string
public static function readKey(): string|KeyType
{
$c = Terminal::read();
return match($c)
{
// Unambiguous mappings
RawKeyCode::ARROW_DOWN => KeyType::ARROW_DOWN,
RawKeyCode::ARROW_LEFT => KeyType::ARROW_LEFT,
RawKeyCode::ARROW_RIGHT => KeyType::ARROW_RIGHT,
RawKeyCode::ARROW_UP => KeyType::ARROW_UP,
RawKeyCode::DELETE => KeyType::DELETE,
RawKeyCode::ENTER => KeyType::ENTER,
RawKeyCode::PAGE_DOWN => KeyType::PAGE_DOWN,
RawKeyCode::PAGE_UP => KeyType::PAGE_UP,
RawKeyCode::ARROW_DOWN => KeyType::ArrowDown,
RawKeyCode::ARROW_LEFT => KeyType::ArrowLeft,
RawKeyCode::ARROW_RIGHT => KeyType::ArrowRight,
RawKeyCode::ARROW_UP => KeyType::ArrowUp,
RawKeyCode::DELETE => KeyType::Delete,
RawKeyCode::ENTER => KeyType::Enter,
RawKeyCode::PAGE_DOWN => KeyType::PageDown,
RawKeyCode::PAGE_UP => KeyType::PageUp,
// Backspace
RawKeyCode::CTRL('h'), RawKeyCode::BACKSPACE => KeyType::BACKSPACE,
RawKeyCode::CTRL('h'), RawKeyCode::BACKSPACE => KeyType::Backspace,
// Escape
RawKeyCode::CTRL('l'), RawKeyCode::ESCAPE => KeyType::ESCAPE,
RawKeyCode::CTRL('l'), RawKeyCode::ESCAPE => KeyType::Escape,
// Home Key
"\eOH", "\e[7~", "\e[1~", ANSI::RESET_CURSOR => KeyType::HOME,
"\eOH", "\e[7~", "\e[1~", ANSI::RESET_CURSOR => KeyType::Home,
// End Key
"\eOF", "\e[4~", "\e[8~", "\e[F" => KeyType::END,
"\eOF", "\e[4~", "\e[8~", "\e[F" => KeyType::End,
default => $c,
};

View File

@ -8,25 +8,25 @@ use Aviat\Kilo\Enum\Highlight;
/**
* Configure syntax highlighting colors
*
* @param int $hl
* @return int
* @param Highlight $hl
* @return Color
*/
function get_syntax_color(int $hl): int {
function get_syntax_color(Highlight $hl): Color {
return match ($hl)
{
Highlight::COMMENT => Color::FG_CYAN,
Highlight::ML_COMMENT => Color::FG_BRIGHT_BLACK,
Highlight::KEYWORD1 => Color::FG_YELLOW,
Highlight::KEYWORD2 => Color::FG_GREEN,
Highlight::STRING => Color::FG_MAGENTA,
Highlight::CHARACTER => Color::FG_BRIGHT_MAGENTA,
Highlight::NUMBER => Color::FG_BRIGHT_RED,
Highlight::OPERATOR => Color::FG_BRIGHT_GREEN,
Highlight::VARIABLE => Color::FG_BRIGHT_CYAN,
Highlight::DELIMITER => Color::FG_BLUE,
Highlight::INVALID => Color::BG_BRIGHT_RED,
Highlight::MATCH => Color::INVERT,
Highlight::IDENTIFIER => Color::FG_BRIGHT_WHITE,
Highlight::Comment => Color::FG_CYAN,
Highlight::MultiLineComment => Color::FG_BRIGHT_BLACK,
Highlight::Keyword1 => Color::FG_YELLOW,
Highlight::Keyword2 => Color::FG_GREEN,
Highlight::String => Color::FG_MAGENTA,
Highlight::Character => Color::FG_BRIGHT_MAGENTA,
Highlight::Number => Color::FG_BRIGHT_RED,
Highlight::Operator => Color::FG_BRIGHT_GREEN,
Highlight::Variable => Color::FG_BRIGHT_CYAN,
Highlight::Delimiter => Color::FG_BLUE,
Highlight::Invalid => Color::BG_BRIGHT_RED,
Highlight::SearchMatch => Color::INVERT,
Highlight::Identifier => Color::FG_BRIGHT_WHITE,
default => Color::FG_WHITE,
};
}