Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework comments support for pretty-printing #266

Draft
wants to merge 7 commits into
base: 2.1.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,9 @@ parameters:
identifier: property.dynamicName
count: 2
path: src/Printer/Printer.php

-
message: '#^Variable property access on PHPStan\\PhpDocParser\\Ast\\Node\.$#'
identifier: property.dynamicName
count: 1
path: tests/PHPStan/Parser/TypeParserTest.php
17 changes: 9 additions & 8 deletions src/Ast/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,26 @@

use function trim;

class Comment
class Comment implements Node
{

public string $text;

public int $startLine;
use NodeAttributes;

public int $startIndex;
public string $text;

public function __construct(string $text, int $startLine = -1, int $startIndex = -1)
public function __construct(string $text)
{
$this->text = $text;
$this->startLine = $startLine;
$this->startIndex = $startIndex;
}

public function getReformattedText(): string
{
return trim($this->text);
}

public function __toString(): string
{
return $this->getReformattedText();
}

}
25 changes: 23 additions & 2 deletions src/Parser/TokenIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPStan\PhpDocParser\Parser;

use LogicException;
use PHPStan\PhpDocParser\Ast\Attribute;
use PHPStan\PhpDocParser\Ast\Comment;
use PHPStan\PhpDocParser\Lexer\Lexer;
use function array_pop;
Expand Down Expand Up @@ -232,8 +233,18 @@ public function skipNewLineTokens(): void
public function skipNewLineTokensAndConsumeComments(): void
{
if ($this->currentTokenType() === Lexer::TOKEN_COMMENT) {
$this->comments[] = new Comment($this->currentTokenValue(), $this->currentTokenLine(), $this->currentTokenIndex());
$startLine = $this->currentTokenLine();
$startIndex = $this->currentTokenIndex();
$text = $this->currentTokenValue();

$this->next();

$c = new Comment($text);
$c->setAttribute(Attribute::START_LINE, $startLine);
$c->setAttribute(Attribute::START_INDEX, $startIndex);
$c->setAttribute(Attribute::END_LINE, $this->currentTokenLine());
$c->setAttribute(Attribute::END_INDEX, $this->currentTokenIndex());
$this->comments[] = $c;
}

if (!$this->isCurrentTokenType(Lexer::TOKEN_PHPDOC_EOL)) {
Expand All @@ -246,8 +257,18 @@ public function skipNewLineTokensAndConsumeComments(): void
continue;
}

$this->comments[] = new Comment($this->currentTokenValue(), $this->currentTokenLine(), $this->currentTokenIndex());
$startLine = $this->currentTokenLine();
$startIndex = $this->currentTokenIndex();
$text = $this->currentTokenValue();

$this->next();

$c = new Comment($text);
$c->setAttribute(Attribute::START_LINE, $startLine);
$c->setAttribute(Attribute::START_INDEX, $startIndex);
$c->setAttribute(Attribute::END_LINE, $this->currentTokenLine());
$c->setAttribute(Attribute::END_INDEX, $this->currentTokenIndex());
$this->comments[] = $c;
} while ($foundNewLine === true);
}

Expand Down
Loading
Loading