Skip to content

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

Conversation

bergmeister
Copy link
Collaborator

@bergmeister 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 TylerLeonhardt left a 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}",
Copy link
Member

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?

Copy link
Collaborator Author

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.

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

Copy link
Member

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?

Copy link
Collaborator Author

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.

Copy link
Collaborator Author

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.

Copy link
Contributor

@JamesWTruher JamesWTruher left a 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
…ext block (they screwed up following tests for some reason).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants