Skip to content

Commit

Permalink
Normalize newlines in Comment::getReformattedText()
Browse files Browse the repository at this point in the history
Normalize CRLF to LF in getReformattedText(). That was, if the
comment is pretty-printed, we will use proper LF newlines, rather
than inserting indentation between the CR and LF.

At the same time, this also makes it easier to emit actual CRLF
newlines with a custom pretty printer.

Fixes #599.
  • Loading branch information
nikic committed May 21, 2023
1 parent 5883189 commit ad8daa1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 37 deletions.
12 changes: 7 additions & 5 deletions lib/PhpParser/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,19 +150,21 @@ public function __toString(): string {
*
* "Reformatted" here means that we try to clean up the whitespace at the
* starts of the lines. This is necessary because we receive the comments
* without trailing whitespace on the first line, but with trailing whitespace
* without leading whitespace on the first line, but with leading whitespace
* on all subsequent lines.
*
* Additionally, this normalizes CRLF newlines to LF newlines.
*
* @return string
*/
public function getReformattedText(): string {
$text = $this->text;
$text = str_replace("\r\n", "\n", $this->text);
$newlinePos = strpos($text, "\n");
if (false === $newlinePos) {
// Single line comments don't need further processing
return $text;
}
if (preg_match('((*BSR_ANYCRLF)(*ANYCRLF)^.*(?:\R\s+\*.*)+$)', $text)) {
if (preg_match('(^.*(?:\n\s+\*.*)+$)', $text)) {
// Multi line comment of the type
//
// /*
Expand All @@ -171,9 +173,9 @@ public function getReformattedText(): string {
// */
//
// is handled by replacing the whitespace sequences before the * by a single space
return preg_replace('(^\s+\*)m', ' *', $this->text);
return preg_replace('(^\s+\*)m', ' *', $text);
}
if (preg_match('(^/\*\*?\s*[\r\n])', $text) && preg_match('(\n(\s*)\*/$)', $text, $matches)) {
if (preg_match('(^/\*\*?\s*\n)', $text) && preg_match('(\n(\s*)\*/$)', $text, $matches)) {
// Multi line comment of the type
//
// /*
Expand Down
54 changes: 22 additions & 32 deletions test/PhpParser/CommentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,47 +33,37 @@ public function provideTestReformatting() {
['// Some text', '// Some text'],
['/* Some text */', '/* Some text */'],
[
'/**
* Some text.
* Some more text.
*/',
'/**
* Some text.
* Some more text.
*/'
"/**\n * Some text.\n * Some more text.\n */",
"/**\n * Some text.\n * Some more text.\n */"
],
[
'/*
Some text.
Some more text.
*/',
'/*
Some text.
Some more text.
*/'
"/**\r\n * Some text.\r\n * Some more text.\r\n */",
"/**\n * Some text.\n * Some more text.\n */"
],
[
'/* Some text.
More text.
Even more text. */',
'/* Some text.
More text.
Even more text. */'
"/*\n Some text.\n Some more text.\n */",
"/*\n Some text.\n Some more text.\n*/"
],
[
'/* Some text.
More text.
Indented text. */',
'/* Some text.
More text.
Indented text. */',
"/*\r\n Some text.\r\n Some more text.\r\n */",
"/*\n Some text.\n Some more text.\n*/"
],
[
"/* Some text.\n More text.\n Even more text. */",
"/* Some text.\n More text.\n Even more text. */"
],
[
"/* Some text.\r\n More text.\r\n Even more text. */",
"/* Some text.\n More text.\n Even more text. */"
],
[
"/* Some text.\n More text.\n Indented text. */",
"/* Some text.\n More text.\n Indented text. */",
],
// invalid comment -> no reformatting
[
'hallo
world',
'hallo
world',
"hello\n world",
"hello\n world",
],
];
}
Expand Down

0 comments on commit ad8daa1

Please sign in to comment.