Ugly progress commit

This commit is contained in:
Timothy Warren 2019-11-06 13:57:19 -05:00
parent 0877bcd6dd
commit da2bec354a
2 changed files with 90 additions and 34 deletions

View File

@ -35,8 +35,9 @@ class Row {
T_DNUMBER => Highlight::NUMBER, T_DNUMBER => Highlight::NUMBER,
T_LNUMBER => Highlight::NUMBER, T_LNUMBER => Highlight::NUMBER,
// Simple string literals // String literals
T_CONSTANT_ENCAPSED_STRING => Highlight::STRING, T_CONSTANT_ENCAPSED_STRING => Highlight::STRING,
T_ENCAPSED_AND_WHITESPACE => Highlight::STRING,
// Simple variables // Simple variables
T_VARIABLE => Highlight::VARIABLE, T_VARIABLE => Highlight::VARIABLE,
@ -133,6 +134,15 @@ class Row {
// Not string literals, but identifiers, keywords, etc. // Not string literals, but identifiers, keywords, etc.
// T_STRING => Highlight::KEYWORD2, // T_STRING => Highlight::KEYWORD2,
// Type casts
T_ARRAY_CAST => Highlight::KEYWORD2,
T_BOOL_CAST => Highlight::KEYWORD2,
T_DOUBLE_CAST => Highlight::KEYWORD2,
T_INT_CAST => Highlight::KEYWORD2,
T_OBJECT_CAST => Highlight::KEYWORD2,
T_STRING_CAST => Highlight::KEYWORD2,
T_UNSET_CAST => Highlight::KEYWORD2,
]; ];
private array $phpCharacterHighlightMap = [ private array $phpCharacterHighlightMap = [
@ -445,10 +455,6 @@ class Row {
} }
$tokens = $this->parent->syntax->tokens[$rowNum]; $tokens = $this->parent->syntax->tokens[$rowNum];
if (empty($tokens))
{
return;
}
$inComment = ($this->idx > 0 && $this->parent->rows[$this->idx - 1]->hlOpenComment); $inComment = ($this->idx > 0 && $this->parent->rows[$this->idx - 1]->hlOpenComment);
@ -456,6 +462,28 @@ class Row {
// multiples of the same tokens can be effectively matched // multiples of the same tokens can be effectively matched
$offset = 0; $offset = 0;
// There will be no tokens if a line is empty, or if in a multi-line comment
if (empty($tokens))
{
if ($inComment && $this->rsize > 0)
{
while ($offset < $this->rsize)
{
if (substr($this->render, $offset, 2) === '*/')
{
array_replace_range($this->hl, $offset, 2, Highlight::ML_COMMENT);
break;
}
$this->hl[$offset] = Highlight::ML_COMMENT;
$offset++;
continue;
}
}
return;
}
foreach ($tokens as $token) foreach ($tokens as $token)
{ {
if ($offset >= $this->rsize) if ($offset >= $this->rsize)
@ -463,9 +491,33 @@ class Row {
break; break;
} }
// A multi-line comment can end in the middle of a line...
while ($inComment)
{
if ($token['type'] === T_WHITESPACE)
{
$char = $token['char'];
$charStart = strpos($this->render, $char, $offset) - 2;
$inComment = FALSE;
array_replace_range($this->hl, $charStart, 2, Highlight::ML_COMMENT);
$offset += 2;
}
if (substr($this->render, $offset, 2) === '*/')
{
$inComment = FALSE;
array_replace_range($this->hl, $offset, 2, Highlight::ML_COMMENT);
$offset += 2;
}
$this->hl[$offset] = Highlight::ML_COMMENT;
$offset++;
continue;
}
$char = $token['char']; $char = $token['char'];
$charLen = strlen($char); $charLen = strlen($char);
if ($charLen === 0) if ($charLen === 0 || $offset >= $this->rsize)
{ {
continue; continue;
} }
@ -474,24 +526,9 @@ class Row {
{ {
continue; continue;
} }
$charEnd = $charStart + $charLen; $charEnd = $charStart + $charLen;
// Comments // Start of multiline comment/single line comment
if ($inComment)
{
if (substr($this->render, $offset, 2) === '*/')
{
$inComment = FALSE;
array_replace_range($this->hl, $offset, 2, Highlight::ML_COMMENT);
$offset += 2;
continue;
}
$this->hl[$offset] = Highlight::ML_COMMENT;
$offset++;
continue;
}
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
@ -511,29 +548,40 @@ class Row {
{ {
break; break;
} }
continue;
} }
// Highlight specific tokens // Highlight specific tokens
if (($token['typeName'] !== 'RAW') && array_key_exists($token['type'], $this->phpTokenHighlightMap)) if ($token['typeName'] !== 'RAW')
{ {
$hl = $this->phpTokenHighlightMap[$token['type']]; if (array_key_exists($token['type'], $this->phpTokenHighlightMap))
array_replace_range($this->hl, $charStart, $charLen, $hl); {
$offset = $charEnd; $hl = $this->phpTokenHighlightMap[$token['type']];
continue; array_replace_range($this->hl, $charStart, $charLen, $hl);
$offset = $charEnd;
continue;
}
// Types/identifiers/keywords that don't have their own token
if ($token['type'] === T_STRING)
{
if (in_array($token['char'], $this->parent->syntax->keywords2, TRUE))
{
array_replace_range($this->hl, $charStart, $charLen, Highlight::KEYWORD2);
$offset = $charEnd;
continue;
}
}
} }
// Highlight raw characters // Highlight raw characters
if (($token['typeName'] === 'RAW') && array_key_exists($token['char'], $this->phpCharacterHighlightMap)) if (($token['typeName'] === 'RAW') && array_key_exists(trim($token['char']), $this->phpCharacterHighlightMap))
{ {
$hl = $this->phpCharacterHighlightMap[$token['char']]; $hl = $this->phpCharacterHighlightMap[trim($token['char'])];
array_replace_range($this->hl, $charStart, $charLen, $hl); array_replace_range($this->hl, $charStart, $charLen, $hl);
$offset = $charEnd; $offset = $charEnd;
continue; continue;
} }
$offset++;
} }
$changed = $this->hlOpenComment !== $inComment; $changed = $this->hlOpenComment !== $inComment;

View File

@ -48,7 +48,15 @@ $foobar = new FooBar();
$x = 3; $x = 3;
// Heredoc $y = [
1,
2,
3
];
/*
Heredoc
*/
$sql = <<<SQL $sql = <<<SQL
SELECT * FROM "foo" WHERE "bar"='baz' AND id={$x}; SELECT * FROM "foo" WHERE "bar"='baz' AND id={$x};
SQL; SQL;