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

Fix PlaceOpenBrace rule correction to take comment at the end of line into account #929

Conversation

@bergmeister
Copy link
Collaborator

bergmeister commented Mar 11, 2018

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 PR NA.

  • PR has a meaningful title
    • Use the present tense and imperative mood when describing your changes
  • Summarized changes
  • User facing documentation needed
  • Change is not breaking
  • Make sure you've added a new test if existing tests do not effectively test the code changed
  • This PR is ready to merge and is not work in progress
    • If the PR is work in progress, please add the prefix WIP: to the beginning of the title and remove the prefix when the PR is ready
Copy link
Member

TylerLeonhardt left a comment

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}",

This comment has been minimized.

@TylerLeonhardt

TylerLeonhardt Mar 20, 2018 Member

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?

This comment has been minimized.

@bergmeister

bergmeister Mar 20, 2018 Author Collaborator

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.

'@
Invoke-Formatter -ScriptDefinition $scriptDefinition -Settings $settings | Should -Be $expected
}

This comment has been minimized.

@TylerLeonhardt

TylerLeonhardt Mar 20, 2018 Member

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?

This comment has been minimized.

@bergmeister

bergmeister Mar 20, 2018 Author Collaborator

This is a very good point. We should test this case as well, I have not thought of that.

This comment has been minimized.

@bergmeister

bergmeister Mar 23, 2018 Author Collaborator

I added a test case for this now and added implicit integration tests with the code formatting style presets that VSCode uses.

Copy link
Member

JamesWTruher left a comment

lgtm

bergmeister added 3 commits Mar 23, 2018
…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).
@bergmeister bergmeister merged commit a7d23f7 into PowerShell:development Mar 23, 2018
2 checks passed
2 checks passed
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
license/cla All CLA requirements met.
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.