Highlight strings, complete step 168

This commit is contained in:
Timothy Warren 2019-10-25 11:49:04 -04:00
parent 17283f5011
commit 3dc3fc01aa
5 changed files with 45 additions and 7 deletions

View File

@ -4,6 +4,7 @@ namespace Kilo;
class Highlight {
public const NORMAL = 0;
public const NUMBER = 1;
public const MATCH = 2;
public const STRING = 1;
public const NUMBER = 2;
public const MATCH = 3;
}

View File

@ -115,6 +115,7 @@ class Row {
$this->hl = array_fill(0, $this->rsize, Highlight::NORMAL);
$prevSep = TRUE;
$inString = '';
$i = 0;
@ -128,6 +129,38 @@ class Row {
return;
}
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 === '\'')
{
$inString = $char;
$this->hl[$i] = Highlight::STRING;
$i++;
continue;
}
}
if ($this->parent->syntax->flags & Syntax::HIGHLIGHT_NUMBERS)
{
// Numbers, including decimal points

View File

@ -4,6 +4,7 @@ namespace Kilo;
class Syntax {
public const HIGHLIGHT_NUMBERS = (1 << 0);
public const HIGHLIGHT_STRINGS = (1 << 1);
public string $filetype = '';
public array $filematch = [];

View File

@ -280,6 +280,9 @@ function syntax_to_color(int $hl): int
{
switch ($hl)
{
case Highlight::STRING:
return 35; // Foreground Magenta
case Highlight::NUMBER:
return 31; // Foreground Red

View File

@ -12,27 +12,27 @@ function get_hldb(): array
Syntax::new(
'C',
['.c', '.h', '.cpp'],
Syntax::HIGHLIGHT_NUMBERS,
Syntax::HIGHLIGHT_NUMBERS | Syntax::HIGHLIGHT_STRINGS,
),
Syntax::new(
'CSS',
['.css', '.less', '.sass', 'scss'],
Syntax::HIGHLIGHT_NUMBERS,
Syntax::HIGHLIGHT_NUMBERS | Syntax::HIGHLIGHT_STRINGS,
),
Syntax::new(
'JavaScript',
['.js', '.jsx', '.ts', '.tsx', '.jsm', '.es'],
Syntax::HIGHLIGHT_NUMBERS,
Syntax::HIGHLIGHT_NUMBERS | Syntax::HIGHLIGHT_STRINGS,
),
Syntax::new(
'PHP',
['.php'],
Syntax::HIGHLIGHT_NUMBERS,
Syntax::HIGHLIGHT_NUMBERS | Syntax::HIGHLIGHT_STRINGS,
),
Syntax::new(
'Rust',
['.rs'],
Syntax::HIGHLIGHT_NUMBERS,
Syntax::HIGHLIGHT_NUMBERS | Syntax::HIGHLIGHT_STRINGS,
),
];
}