From 17283f50118d854f44911175f93e134ae1b3778c Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Fri, 25 Oct 2019 10:47:44 -0400 Subject: [PATCH] Improve highlighting loop and language support --- .gitignore | 2 ++ kilo | 13 +++++++++++++ src/Editor.php | 12 +++++------- src/hldb.php | 22 +++++++++++++++++++++- 4 files changed, 41 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 5e04216..e1e5ef9 100644 --- a/.gitignore +++ b/.gitignore @@ -167,3 +167,5 @@ tags [._]*.un~ # End of https://www.gitignore.io/api/vim,emacs,composer,jetbrains+all + +kilo.log diff --git a/kilo b/kilo index d5eda09..f3efaf3 100755 --- a/kilo +++ b/kilo @@ -10,6 +10,19 @@ define('KILO_VERSION', '0.0.1'); define('KILO_TAB_STOP', 4); define('KILO_QUIT_TIMES', 3); +// Log notices/errors/warnings to file +set_error_handler(static function (int $no, $str, $file, $line, $context) { + $msg = print_r([ + 'errno' => $no, + 'message' => $str, + 'file' => $file, + 'line' => $line, + 'context' => $context, + ], TRUE); + file_put_contents('kilo.log', $msg, FILE_APPEND); + +}, -1); + // Set up autoloading require_once __DIR__ . '/src/functions.php'; require_once __DIR__ . '/src/hldb.php'; diff --git a/src/Editor.php b/src/Editor.php index e547b39..04890c4 100644 --- a/src/Editor.php +++ b/src/Editor.php @@ -112,16 +112,16 @@ class Editor { foreach (get_hldb() as $syntax) { - $i = 0; - while ($syntax->filematch[$i]) + foreach ($syntax->filematch as $searchExt) { - $is_ext = ($syntax->filematch[$i][0] === '.'); + $is_ext = (strpos($searchExt, '.') === 0); if ( - ($is_ext && ( ! strcmp($ext, $syntax->filematch[$i]))) || - (( ! $is_ext) && strpos($this->filename, $syntax->filematch{$i}) !== FALSE) + ($is_ext && ( ! strcmp($ext, $searchExt))) || + (( ! $is_ext) && strpos($this->filename, $searchExt) !== FALSE) ) { $this->syntax = $syntax; + // Update the syntax highlighting for all the rows of the file for ($i = 0; $i < $this->numRows; $i++) { $this->rows[$i]->update(); @@ -129,8 +129,6 @@ class Editor { return; } - - $i++; } } } diff --git a/src/hldb.php b/src/hldb.php index 138c519..bec0773 100644 --- a/src/hldb.php +++ b/src/hldb.php @@ -10,10 +10,30 @@ function get_hldb(): array { $db = [ Syntax::new( - 'c', + 'C', ['.c', '.h', '.cpp'], Syntax::HIGHLIGHT_NUMBERS, ), + Syntax::new( + 'CSS', + ['.css', '.less', '.sass', 'scss'], + Syntax::HIGHLIGHT_NUMBERS, + ), + Syntax::new( + 'JavaScript', + ['.js', '.jsx', '.ts', '.tsx', '.jsm', '.es'], + Syntax::HIGHLIGHT_NUMBERS, + ), + Syntax::new( + 'PHP', + ['.php'], + Syntax::HIGHLIGHT_NUMBERS, + ), + Syntax::new( + 'Rust', + ['.rs'], + Syntax::HIGHLIGHT_NUMBERS, + ), ]; }