diff --git a/src/Highlight.php b/src/Highlight.php index 771727f..2358094 100644 --- a/src/Highlight.php +++ b/src/Highlight.php @@ -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; } \ No newline at end of file diff --git a/src/Row.php b/src/Row.php index eb2bbd2..09a33ec 100644 --- a/src/Row.php +++ b/src/Row.php @@ -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)) diff --git a/src/Syntax.php b/src/Syntax.php index e7c31dd..7959916 100644 --- a/src/Syntax.php +++ b/src/Syntax.php @@ -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; diff --git a/src/functions.php b/src/functions.php index 9860583..3b4fae1 100644 --- a/src/functions.php +++ b/src/functions.php @@ -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 diff --git a/src/hldb.php b/src/hldb.php index f3305eb..8489bcc 100644 --- a/src/hldb.php +++ b/src/hldb.php @@ -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, ), ];