Fix test, PHP highlighting now works as expected

This commit is contained in:
Timothy Warren 2019-11-15 11:35:08 -05:00
parent 8951894493
commit 345239d248
3 changed files with 14 additions and 22 deletions

View File

@ -447,11 +447,11 @@ class Row {
$rowNum = $this->idx + 1; $rowNum = $this->idx + 1;
$hasTokens = isset($this->parent->syntax->tokens); $hasTokens = isset($this->parent->syntax->tokens);
$hasRow = array_key_exists($rowNum, $this->parent->syntax->tokens); $hasRow = $hasTokens && array_key_exists($rowNum, $this->parent->syntax->tokens);
if ( ! ( if ( ! (
isset($this->parent->syntax->tokens) && $hasTokens &&
array_key_exists($rowNum, $this->parent->syntax->tokens) && $hasRow &&
$this->idx < $this->parent->numRows $this->idx < $this->parent->numRows
)) ))
{ {

View File

@ -11,7 +11,7 @@ class PHP {
private array $rawLines; private array $rawLines;
private array $tokens = []; private array $tokens;
private int $lineNum = 1; private int $lineNum = 1;
@ -23,6 +23,7 @@ class PHP {
$this->code = $code; $this->code = $code;
$this->rawLines = $lines; $this->rawLines = $lines;
$this->tokens = array_fill(1, count($lines) - 1, []);
} }
/** /**
@ -71,16 +72,6 @@ class PHP {
} }
} }
// Add "missing" row indexes
$lineCount = count($this->rawLines);
for ($i = 1; $i <= $lineCount; $i++)
{
if ( ! array_key_exists($i, $this->tokens))
{
$this->tokens[$i] = [];
}
}
ksort($this->tokens); ksort($this->tokens);
return $this->tokens; return $this->tokens;
@ -91,6 +82,11 @@ class PHP {
[$type, $rawChar, $currentLine] = $token; [$type, $rawChar, $currentLine] = $token;
$char = tabs_to_spaces($rawChar); $char = tabs_to_spaces($rawChar);
if ($currentLine !== $this->lineNum)
{
$this->lineNum = $currentLine;
}
$current = [ $current = [
'type' => $type, 'type' => $type,
'typeName' => token_name($type), 'typeName' => token_name($type),
@ -98,9 +94,10 @@ class PHP {
'line' => $currentLine, 'line' => $currentLine,
]; ];
if ($char === "\n") // Single new line, or starts with a new line with other whitespace
if (strpos($char, "\n") === 0 && trim($char) === '')
{ {
$this->tokens[$this->lineNum] = $current; $this->tokens[$this->lineNum][] = $current;
$this->lineNum++; $this->lineNum++;
if ( ! array_key_exists($this->lineNum, $this->tokens)) if ( ! array_key_exists($this->lineNum, $this->tokens))
@ -139,11 +136,6 @@ class PHP {
} }
} }
if ($currentLine !== $this->lineNum)
{
$this->lineNum = $currentLine;
}
$this->tokens[$this->lineNum][] = $current; $this->tokens[$this->lineNum][] = $current;
} }

View File

@ -22,7 +22,7 @@ class PHPTest extends TestCase {
{ {
if (empty($lineTokens)) if (empty($lineTokens))
{ {
$this->assertNotEmpty(trim($lines[$index]), 'Token is empty for non-empty line'); $this->assertEmpty(trim($lines[$index]), 'Token is empty for non-empty line');
} }
foreach ($lineTokens as $token) foreach ($lineTokens as $token)