Some progress on multi-line comments in PHP
This commit is contained in:
parent
155340df66
commit
0877bcd6dd
88
src/Row.php
88
src/Row.php
@ -22,12 +22,14 @@ class Row {
|
|||||||
|
|
||||||
private array $phpTokenHighlightMap = [
|
private array $phpTokenHighlightMap = [
|
||||||
// Delimiters
|
// Delimiters
|
||||||
T_CLOSE_TAG => Highlight::DELIMITER,
|
|
||||||
T_ARRAY => Highlight::DELIMITER,
|
T_ARRAY => Highlight::DELIMITER,
|
||||||
T_CURLY_OPEN => Highlight::DELIMITER,
|
T_CURLY_OPEN => Highlight::DELIMITER,
|
||||||
T_DOLLAR_OPEN_CURLY_BRACES => Highlight::DELIMITER,
|
T_DOLLAR_OPEN_CURLY_BRACES => Highlight::DELIMITER,
|
||||||
T_OPEN_TAG => Highlight::DELIMITER,
|
T_OPEN_TAG => Highlight::DELIMITER,
|
||||||
T_OPEN_TAG_WITH_ECHO => Highlight::DELIMITER,
|
T_OPEN_TAG_WITH_ECHO => Highlight::DELIMITER,
|
||||||
|
T_CLOSE_TAG => Highlight::DELIMITER,
|
||||||
|
T_START_HEREDOC => Highlight::DELIMITER,
|
||||||
|
T_END_HEREDOC => Highlight::DELIMITER,
|
||||||
|
|
||||||
// Number literals
|
// Number literals
|
||||||
T_DNUMBER => Highlight::NUMBER,
|
T_DNUMBER => Highlight::NUMBER,
|
||||||
@ -432,15 +434,23 @@ class Row {
|
|||||||
|
|
||||||
protected function updateSyntaxPHP():void
|
protected function updateSyntaxPHP():void
|
||||||
{
|
{
|
||||||
if ( ! isset($this->parent->syntax->tokens))
|
$rowNum = $this->idx + 1;
|
||||||
|
|
||||||
|
if (
|
||||||
|
( ! isset($this->parent->syntax->tokens))
|
||||||
|
|| ( ! array_key_exists($rowNum, $this->parent->syntax->tokens))
|
||||||
|
|| $this->idx > $this->parent->numRows)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// The index for the tokens should exist
|
$tokens = $this->parent->syntax->tokens[$rowNum];
|
||||||
$tokens = $this->parent->syntax->tokens[$this->idx + 1];
|
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);
|
||||||
|
|
||||||
// Keep track of where you are in the line, so that
|
// Keep track of where you are in the line, so that
|
||||||
// multiples of the same tokens can be effectively matched
|
// multiples of the same tokens can be effectively matched
|
||||||
@ -455,15 +465,65 @@ class Row {
|
|||||||
|
|
||||||
$char = $token['char'];
|
$char = $token['char'];
|
||||||
$charLen = strlen($char);
|
$charLen = strlen($char);
|
||||||
|
if ($charLen === 0)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
$charStart = strpos($this->render, $char, $offset);
|
$charStart = strpos($this->render, $char, $offset);
|
||||||
if ($charStart === FALSE)
|
if ($charStart === FALSE)
|
||||||
{
|
{
|
||||||
$offset++;
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$charEnd = $charStart + $charLen;
|
$charEnd = $charStart + $charLen;
|
||||||
|
|
||||||
|
// Comments
|
||||||
|
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))
|
||||||
|
{
|
||||||
|
// Single line comments
|
||||||
|
if (strpos($token['char'], '//') !== FALSE)
|
||||||
|
{
|
||||||
|
array_replace_range($this->hl, $charStart, $charLen, Highlight::COMMENT);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start of multi-line comment
|
||||||
|
$start = strpos($this->render, '/*', $offset);
|
||||||
|
$inComment = strpos($this->render, '*/', $offset) === FALSE;
|
||||||
|
array_replace_range($this->hl, $start, strlen($char) - $offset, Highlight::ML_COMMENT);
|
||||||
|
$offset = $start + strlen($char) - $offset;
|
||||||
|
|
||||||
|
if ($inComment)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Highlight specific tokens
|
||||||
|
if (($token['typeName'] !== 'RAW') && array_key_exists($token['type'], $this->phpTokenHighlightMap))
|
||||||
|
{
|
||||||
|
$hl = $this->phpTokenHighlightMap[$token['type']];
|
||||||
|
array_replace_range($this->hl, $charStart, $charLen, $hl);
|
||||||
|
$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($token['char'], $this->phpCharacterHighlightMap))
|
||||||
{
|
{
|
||||||
@ -473,14 +533,14 @@ class Row {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Highlight specific tokens
|
$offset++;
|
||||||
if (array_key_exists($token['type'], $this->phpTokenHighlightMap))
|
}
|
||||||
{
|
|
||||||
$hl = $this->phpTokenHighlightMap[$token['type']];
|
$changed = $this->hlOpenComment !== $inComment;
|
||||||
array_replace_range($this->hl, $charStart, $charLen, $hl);
|
$this->hlOpenComment = $inComment;
|
||||||
$offset = $charEnd;
|
if ($changed && $this->idx + 1 < $this->parent->numRows)
|
||||||
continue;
|
{
|
||||||
}
|
$this->parent->rows[$this->idx + 1]->updateSyntax();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -340,6 +340,12 @@ function get_php_tokens(string $code): array
|
|||||||
|
|
||||||
[$type, $char, $currentLine] = $token;
|
[$type, $char, $currentLine] = $token;
|
||||||
|
|
||||||
|
// Only return the first line of a multi-line token
|
||||||
|
if ($char !== "\n" && strpos($char, "\n") !== FALSE)
|
||||||
|
{
|
||||||
|
$char = explode("\n", $char)[0];
|
||||||
|
}
|
||||||
|
|
||||||
$current = [
|
$current = [
|
||||||
'type' => $type,
|
'type' => $type,
|
||||||
'typeName' => token_name($type),
|
'typeName' => token_name($type),
|
||||||
|
9
test.php
9
test.php
@ -3,6 +3,13 @@
|
|||||||
interface Ifoo {}
|
interface Ifoo {}
|
||||||
|
|
||||||
abstract class Foo implements Ifoo {
|
abstract class Foo implements Ifoo {
|
||||||
|
/**
|
||||||
|
* @param int $a
|
||||||
|
* @param float $b
|
||||||
|
* @param array $c
|
||||||
|
* @param callable $d
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
abstract public function bar(int $a, float $b, array $c, callable $d): string;
|
abstract public function bar(int $a, float $b, array $c, callable $d): string;
|
||||||
|
|
||||||
protected function doNothing(): void {}
|
protected function doNothing(): void {}
|
||||||
@ -46,7 +53,7 @@ $sql = <<<SQL
|
|||||||
SELECT * FROM "foo" WHERE "bar"='baz' AND id={$x};
|
SELECT * FROM "foo" WHERE "bar"='baz' AND id={$x};
|
||||||
SQL;
|
SQL;
|
||||||
|
|
||||||
// Nowdoc
|
/* Nowdoc */
|
||||||
$template = <<<'TEMPLATE'
|
$template = <<<'TEMPLATE'
|
||||||
<foo>{x}</foo>
|
<foo>{x}</foo>
|
||||||
TEMPLATE;
|
TEMPLATE;
|
||||||
|
Loading…
Reference in New Issue
Block a user