-
Notifications
You must be signed in to change notification settings - Fork 395
Fix PlaceOpenBrace rule correction to take comment at the end of line into account #929
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
Fix PlaceOpenBrace rule correction to take comment at the end of line into account #929
Conversation
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.
Just a couple comments. Otherwise, LGTM! Although I should not be the only sign off on this.
corrections.Add( | ||
new CorrectionExtent( | ||
precedingExpression.Extent.StartLineNumber, | ||
lCurly.Extent.EndLineNumber, | ||
precedingExpression.Extent.StartColumnNumber, | ||
lCurly.Extent.EndColumnNumber, | ||
precedingExpression.Text + " " + lCurly.Text, | ||
$"{precedingExpression.Text} {lCurly.Text}{optionalComment}", |
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.
without knowing too much about this project... should there be a space here between the }{
:
{lCurly.Text}{optionalComment}
for when optionalComment
isn't empty string and actually is a comment?
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.
That is the point of having the separate optionalComment variable, if it is not an empty string, then the variable itself contains the preceding space character. This way I avoid appending a space character when there is no comment.
Tests/Rules/PlaceOpenBrace.tests.ps1
Outdated
'@ | ||
Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings | Should -Be $expected | ||
} | ||
|
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.
Is it possible to have the reverse of this?
We have:
foreach ($x in $y) { # PSSA is awesome
Get-Something
}
and want:
foreach ($x in $y) # PSSA is awesome
{
Get-Something
}
would it be worth having a test for that?
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.
This is a very good point. We should test this case as well, I have not thought of that.
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.
I added a test case for this now and added implicit integration tests with the code formatting style presets that VSCode uses.
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.
lgtm
…n the next line to a brace style where the brace is on the same line as suggested in the PR. Added more assertions for an integration test with code formatting styles
…nalyzer into FixPlaceOpenBraceWhenCommentIsPresent
…ext block (they screwed up following tests for some reason).
PR Summary
Fixes #826 which came indirectly via 2 raised VSCode issues.
Fixes #800
Basically when a comment was at the end of the line, then if the rule had settings to move the brace on the same line, it moved it at the end of the line AFTER the comment.
This was because the correction just took the last token. This PR checks if the last token is a comment and if so takes the second last token if possible and appends the comment after the opening brace (including a preceding space character).
Other than that some minor tidy up.
I have locally tested that this also works in PSCore.
PR Checklist
Note: Tick the boxes below that apply to this pull request by putting an
x
between the square brackets. Please mark anything not applicable to this PRNA
.WIP:
to the beginning of the title and remove the prefix when the PR is ready