Improve highlighting for non-PHP languages
timw4mail/php-kilo/pipeline/head There was a failure building this commit Details

This commit is contained in:
Timothy Warren 2021-04-09 21:19:46 -04:00
parent dcaf922796
commit b980a6feb0
3 changed files with 72 additions and 13 deletions

View File

@ -22,12 +22,20 @@ class FileType {
'.c', '.h', '.cpp', '.cxx', '.cc', '.hpp' => Syntax::new(
'C',
[
'continue', 'typedef', 'switch', 'return', 'static', 'while', 'break', 'struct',
'union', 'class', 'else', 'enum', 'for', 'case', 'if',
'auto', 'break', 'case', 'const', 'continue', 'default', 'do', 'typedef', 'switch', 'return',
'static', 'while', 'break', 'struct', 'extern', 'union', 'class', 'else', 'enum', 'for', 'case',
'if', 'inline', 'register', 'restrict', 'return', 'sizeof', 'switch', 'typedef', 'union', 'volatile'
],
[
'#include', 'unsigned', '#define', '#ifndef', 'double', 'signed', '#endif',
'#ifdef', 'float', '#error', '#undef', 'long', 'char', 'int', 'void', '#if',
'#ifdef', 'float', '#error', '#undef', '#elif', 'long', 'char', 'int', 'void', '#if',
'uint32_t', 'wchar_t', 'int32_t', 'int64_t', 'uint64_t', 'int16_t', 'uint16_t',
'uint8_t', 'int8_t',
],
[
'<=>', '<<=', '>>=',
'++', '--', '==', '!=', '>=', '<=', '&&', '||', '<<', '>>',
'+=', '-=', '*=', '/=', '%=', '&=', '|=', '^=', '->', '::',
],
hasCharType: true,
highlightCharacters: true,
@ -47,6 +55,10 @@ class FileType {
'uint8', 'uint16', 'uint32', 'uint64', 'int8', 'int16', 'int32', 'int64', 'float32', 'float64',
'uint', 'int', 'uintptr', 'complex64', 'complex128', 'byte', 'rune', '[]',
],
[
'&^=', '...', '>>=', '<<=', '--', '%=', '>>', ':=', '++', '/=', '<<', '>=',
'<-', '^=', '*=', '<=', '||', '|=', '-=', '!=', '==', '&&', '&=', '+=',
],
hasCharType: true,
highlightCharacters: true,
),

View File

@ -224,6 +224,21 @@ class Row {
return false;
}
protected function highlightChar(int &$i, array $chars, int $syntaxType): bool
{
$char = $this->render[$i];
if (in_array($char, $chars, TRUE))
{
$this->hl[$i] = $syntaxType;
$i += 1;
return true;
}
return false;
}
protected function highlightPrimaryKeywords(int &$i, Syntax $opts): bool
{
return $this->highlightWord($i, $opts->keywords1, Highlight::KEYWORD1);
@ -234,20 +249,48 @@ class Row {
return $this->highlightWord($i, $opts->keywords2, Highlight::KEYWORD2);
}
protected function highlightChar(int &$i, Syntax $opts): bool
protected function highlightOperators(int &$i, Syntax $opts): bool
{
return $this->highlightWord($i, $opts->operators, Highlight::OPERATOR);
}
protected function highlightCommonOperators(int &$i): bool
{
return $this->highlightChar(
$i,
['+', '-', '*', '/', '<', '^', '>', '%', '=', ':', ',', ';', '&', '~'],
Highlight::OPERATOR
);
}
protected function highlightCommonDelimeters(int &$i): bool
{
return $this->highlightChar(
$i,
['{', '}', '[', ']', '(', ')'],
Highlight::DELIMITER
);
}
protected function highlightCharacter(int &$i, Syntax $opts): bool
{
$char = $this->render[$i];
$nextChar = $this->render[$i + 1];
if ($opts->characters() && $char === "'")
{
$nextChar = $this->render[$i + 1];
$closingIndex = ($nextChar === '\\') ? $i + 3 : $i + 2;
$offset = ($nextChar === '\\') ? $i + 2 : $i + 1;
$closingIndex = strpos($this->render, "'", $i + 1);
if ($closingIndex === false)
{
return false;
}
$closingChar = $this->render[$closingIndex];
if ($closingChar === "'")
{
array_replace_range($this->hl, $i, $closingIndex, Highlight::CHARACTER);
$i = $closingIndex;
array_replace_range($this->hl, $i, $closingIndex - $i + 1, Highlight::CHARACTER);
$i = $closingIndex + 1;
return true;
}
@ -376,9 +419,6 @@ class Row {
while ($i < $this->rsize)
{
$char = $this->render[$i];
$prevHl = ($i > 0) ? $this->hl[$i - 1] : Highlight::NORMAL;
// Multi-line comments
if ($mcsLen > 0 && $mceLen > 0 && $inString === '')
{
@ -407,11 +447,14 @@ class Row {
}
if (
$this->highlightChar($i, $syntax)
|| $this->highlightComment($i, $syntax)
$this->highlightComment($i, $syntax)
|| $this->highlightMultilineComments($i, $syntax)
|| $this->highlightPrimaryKeywords($i, $syntax)
|| $this->highlightSecondaryKeywords($i, $syntax)
|| $this->highlightOperators($i, $syntax)
|| $this->highlightCommonOperators($i)
|| $this->highlightCommonDelimeters($i)
|| $this->highlightCharacter($i, $syntax)
|| $this->highlightString($i, $syntax)
|| $this->highlightNumber($i, $syntax)
) {

View File

@ -10,6 +10,7 @@ class Syntax {
string $name,
array $keywords1 = [],
array $keywords2 = [],
array $operators = [],
string $slcs = '//',
string $mcs = '/*',
string $mce = '*/',
@ -24,6 +25,7 @@ class Syntax {
$name,
$keywords1,
$keywords2,
$operators,
$slcs,
$mcs,
$mce,
@ -47,6 +49,8 @@ class Syntax {
public array $keywords1,
/** Secondary set of language keywords */
public array $keywords2,
/** Operators for the current language */
public array $operators,
/** Syntax to start a single line comment */
public string $singleLineCommentStart,
/** Syntax to start a multi-line comment */