Cut down on some highlighting boilerplate

This commit is contained in:
Timothy Warren 2019-11-06 14:48:21 -05:00
parent da2bec354a
commit d3bf9294cc

View File

@ -528,13 +528,20 @@ class Row {
} }
$charEnd = $charStart + $charLen; $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 // Start of multiline comment/single line comment
if (in_array($token['type'], [T_DOC_COMMENT, T_COMMENT], TRUE)) if (in_array($token['type'], [T_DOC_COMMENT, T_COMMENT], TRUE))
{ {
// Single line comments // Single line comments
if (strpos($token['char'], '//') !== FALSE) if (strpos($token['char'], '//') !== FALSE)
{ {
array_replace_range($this->hl, $charStart, $charLen, Highlight::COMMENT); $highlightRange(Highlight::COMMENT);
break; break;
} }
@ -556,8 +563,7 @@ class Row {
if (array_key_exists($token['type'], $this->phpTokenHighlightMap)) if (array_key_exists($token['type'], $this->phpTokenHighlightMap))
{ {
$hl = $this->phpTokenHighlightMap[$token['type']]; $hl = $this->phpTokenHighlightMap[$token['type']];
array_replace_range($this->hl, $charStart, $charLen, $hl); $highlightRange($hl);
$offset = $charEnd;
continue; continue;
} }
@ -566,8 +572,7 @@ class Row {
{ {
if (in_array($token['char'], $this->parent->syntax->keywords2, TRUE)) if (in_array($token['char'], $this->parent->syntax->keywords2, TRUE))
{ {
array_replace_range($this->hl, $charStart, $charLen, Highlight::KEYWORD2); $highlightRange(Highlight::KEYWORD2);
$offset = $charEnd;
continue; continue;
} }
} }
@ -578,8 +583,7 @@ class Row {
if (($token['typeName'] === 'RAW') && array_key_exists(trim($token['char']), $this->phpCharacterHighlightMap)) if (($token['typeName'] === 'RAW') && array_key_exists(trim($token['char']), $this->phpCharacterHighlightMap))
{ {
$hl = $this->phpCharacterHighlightMap[trim($token['char'])]; $hl = $this->phpCharacterHighlightMap[trim($token['char'])];
array_replace_range($this->hl, $charStart, $charLen, $hl); $highlightRange($hl);
$offset = $charEnd;
continue; continue;
} }
} }