diff --git a/src/Row.php b/src/Row.php index 3485491..d9296dc 100644 --- a/src/Row.php +++ b/src/Row.php @@ -528,13 +528,20 @@ class Row { } $charEnd = $charStart + $charLen; + // Probably not great to create a closure in a loop like this, but + // this halves the boilerplate for each type of syntax + $highlightRange = function (int $hl) use ($charLen, $charStart, $charEnd, &$offset) { + array_replace_range($this->hl, $charStart, $charLen, $hl); + $offset = $charEnd; + }; + // Start of multiline comment/single line comment if (in_array($token['type'], [T_DOC_COMMENT, T_COMMENT], TRUE)) { // Single line comments if (strpos($token['char'], '//') !== FALSE) { - array_replace_range($this->hl, $charStart, $charLen, Highlight::COMMENT); + $highlightRange(Highlight::COMMENT); break; } @@ -556,8 +563,7 @@ class Row { if (array_key_exists($token['type'], $this->phpTokenHighlightMap)) { $hl = $this->phpTokenHighlightMap[$token['type']]; - array_replace_range($this->hl, $charStart, $charLen, $hl); - $offset = $charEnd; + $highlightRange($hl); continue; } @@ -566,8 +572,7 @@ class Row { { if (in_array($token['char'], $this->parent->syntax->keywords2, TRUE)) { - array_replace_range($this->hl, $charStart, $charLen, Highlight::KEYWORD2); - $offset = $charEnd; + $highlightRange(Highlight::KEYWORD2); continue; } } @@ -578,8 +583,7 @@ class Row { if (($token['typeName'] === 'RAW') && array_key_exists(trim($token['char']), $this->phpCharacterHighlightMap)) { $hl = $this->phpCharacterHighlightMap[trim($token['char'])]; - array_replace_range($this->hl, $charStart, $charLen, $hl); - $offset = $charEnd; + $highlightRange($hl); continue; } }