From 3dc3fc01aaf8f20ed31da57890b0dd1ef3c85d03 Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Fri, 25 Oct 2019 11:49:04 -0400 Subject: [PATCH] Highlight strings, complete step 168 --- src/Highlight.php | 5 +++-- src/Row.php | 33 +++++++++++++++++++++++++++++++++ src/Syntax.php | 1 + src/functions.php | 3 +++ src/hldb.php | 10 +++++----- 5 files changed, 45 insertions(+), 7 deletions(-) diff --git a/src/Highlight.php b/src/Highlight.php index 9c7832a..771727f 100644 --- a/src/Highlight.php +++ b/src/Highlight.php @@ -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; } \ No newline at end of file diff --git a/src/Row.php b/src/Row.php index faf987e..eb2bbd2 100644 --- a/src/Row.php +++ b/src/Row.php @@ -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 diff --git a/src/Syntax.php b/src/Syntax.php index ed51492..e7c31dd 100644 --- a/src/Syntax.php +++ b/src/Syntax.php @@ -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 = []; diff --git a/src/functions.php b/src/functions.php index c5493d0..9860583 100644 --- a/src/functions.php +++ b/src/functions.php @@ -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 diff --git a/src/hldb.php b/src/hldb.php index bec0773..f3305eb 100644 --- a/src/hldb.php +++ b/src/hldb.php @@ -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, ), ]; }