Highlight single-line comments, finish step 171

This commit is contained in:
Timothy Warren 2019-10-25 12:08:21 -04:00
parent 3dc3fc01aa
commit cf0436dc7d
5 changed files with 29 additions and 5 deletions

View File

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

View File

@ -114,6 +114,8 @@ class Row {
{
$this->hl = array_fill(0, $this->rsize, Highlight::NORMAL);
$scs = $this->parent->syntax->singleLineCommentStart;
$prevSep = TRUE;
$inString = '';
@ -129,6 +131,14 @@ class Row {
return;
}
// Single-line comments
if ($scs !== '' && empty($inString) && substr($this->render, $i, strlen($scs)) === $scs)// strncmp($char, $scs, strlen($scs)) === 0)
{
array_replace_range($this->hl, $i, $this->rsize - $i, Highlight::COMMENT);
break;
}
// String/Char literals
if ($this->parent->syntax->flags & Syntax::HIGHLIGHT_STRINGS)
{
if ($inString !== '')
@ -161,9 +171,9 @@ class Row {
}
}
// Numbers, including decimal points
if ($this->parent->syntax->flags & Syntax::HIGHLIGHT_NUMBERS)
{
// Numbers, including decimal points
if (
($char === '.' && $prevHl === Highlight::NUMBER) ||
(($prevSep || $prevHl === Highlight::NUMBER) && is_digit($char))

View File

@ -8,14 +8,19 @@ class Syntax {
public string $filetype = '';
public array $filematch = [];
public string $singleLineCommentStart = '//';
public int $flags = 0;
public static function new(string $name, array $extList, int $flags): self
public static function new(string $name, array $extList, string $slcs, int $flags): self
{
$self = new self();
$self->filetype = $name;
$self->filematch = $extList;
$self->singleLineCommentStart = '//';
$self->flags = $flags;
return $self;

View File

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

View File

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