Fix indentation detection after opening tag

Fixes #982.
This commit is contained in:
Nikita Popov 2024-03-02 18:59:44 +01:00
parent ec02613432
commit 70c96493b4
2 changed files with 16 additions and 1 deletions

View File

@ -251,7 +251,7 @@ class TokenStream {
private function calcIndentMap(): array {
$indentMap = [];
$indent = 0;
foreach ($this->tokens as $token) {
foreach ($this->tokens as $i => $token) {
$indentMap[] = $indent;
if ($token->id === \T_WHITESPACE) {
@ -259,6 +259,10 @@ class TokenStream {
$newlinePos = \strrpos($content, "\n");
if (false !== $newlinePos) {
$indent = \strlen($content) - $newlinePos - 1;
} elseif ($i === 1 && $this->tokens[0]->id === \T_OPEN_TAG &&
$this->tokens[0]->text[\strlen($this->tokens[0]->text) - 1] === "\n") {
// Special case: Newline at the end of opening tag followed by whitespace.
$indent = \strlen($content);
}
}
}

View File

@ -50,3 +50,14 @@ class Test {
// some code
}
}
-----
<?php
class Example {
}
-----
$stmts[0]->setDocComment(new Comment\Doc("/** foo */"));
-----
<?php
/** foo */
class Example {
}