-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Yaml] Fix wrong line number when comments are inserted in the middle of a block. #17733
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
Changes from 1 commit
88d210f
0644b84
dac814c
2a082b0
343ee3e
bf4501a
c8b34a7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
… of a block.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,6 @@ class Parser | |
private $currentLineNb = -1; | ||
private $currentLine = ''; | ||
private $refs = array(); | ||
private $skippedCommentLines = 0; | ||
|
||
/** | ||
* Constructor. | ||
|
@@ -155,7 +154,7 @@ public function parse($value, $flags = 0) | |
try { | ||
$key = Inline::parseScalar($values['key']); | ||
} catch (ParseException $e) { | ||
$e->setParsedLine($this->getRealCurrentLineNb() + $this->skippedCommentLines + 1); | ||
$e->setParsedLine($this->getRealCurrentLineNb() + 1); | ||
$e->setSnippet($this->currentLine); | ||
|
||
throw $e; | ||
|
@@ -485,10 +484,18 @@ private function moveToNextLine() | |
|
||
/** | ||
* Moves the parser to the previous line. | ||
* | ||
* @return bool | ||
*/ | ||
private function moveToPreviousLine() | ||
{ | ||
if ($this->currentLineNb < 1) { | ||
return false; | ||
} | ||
|
||
$this->currentLine = $this->lines[--$this->currentLineNb]; | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
|
@@ -805,4 +812,44 @@ private function isBlockScalarHeader() | |
{ | ||
return (bool) preg_match('~'.self::BLOCK_SCALAR_HEADER_PATTERN.'$~', $this->currentLine); | ||
} | ||
|
||
/** | ||
* Returns true if the current line is a collection item. | ||
* | ||
* @return bool Returns true if the current line is a collection item line, false otherwise. | ||
*/ | ||
private function isCurrentLineCollectionItem() | ||
{ | ||
$ltrimmedLine = ltrim($this->currentLine, ' '); | ||
|
||
return '' !== $ltrimmedLine && $ltrimmedLine[0] === '-'; | ||
} | ||
|
||
/** | ||
* Tests whether or not the current comment line is in a collection. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
* | ||
* @return bool | ||
*/ | ||
private function isPreviousNonCommentLineIsCollectionItem() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. method name is weird. It contains |
||
{ | ||
$isCollectionItem = false; | ||
$moves = 0; | ||
while($this->moveToPreviousLine()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please add a space before the opening parenthesis (right after the |
||
++$moves; | ||
// If previous line is a comment, move back again. | ||
if ($this->isCurrentLineComment()) { | ||
continue; | ||
} | ||
$isCollectionItem = $this->isCurrentLineCollectionItem(); | ||
break; | ||
} | ||
|
||
// Move parser back to previous line. | ||
while($moves > 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
$this->moveToNextLine(); | ||
--$moves; | ||
} | ||
|
||
return $isCollectionItem; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The description of the returnt ype here is not needed (it's already clear from the method's description), just use
@return true
.