GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Do not increase indentation after LParen if the previous token is a Newline and the next token is not a Newline by bergmeister · Pull Request #1469 · PowerShell/PSScriptAnalyzer

Conversation

@bergmeister

@bergmeister Christoph Bergmeister (bergmeister) commented May 3, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator

PR Summary

Fixes #1407 cc Felix Becker (@felixbecker)

This is to fix a long-standing bug in the formatter that has been present probably since day 1.
There are a few cases that were similar to this one where an LParen and LBrace are both on the same line cause an increase of indentation that is twice as much as needed.

(foo | bar {
    baz
})

Therefore this PR adds a check to see if the LParen is the first token on a line by checking the previous token is of kind Newline. Then it also checks that the following token (comments have to be skipped) is also not a Newline so that the following case would still indent:

$result = (
    Get-Something
).Property

In order to prevent the RParen code from de-indenting too much, we have to keep a stack of when we skipped the indentation caused by tokens that require a closing RParen (which are LParen, AtParen and DollarParen).

PR Checklist

  • PR has a meaningful title
    • Use the present tense and imperative mood when describing your changes
  • Summarized changes
  • Change is not breaking
  • Make sure all .cs, .ps1 and .psm1 files have the correct copyright header
  • Make sure you've added a new test if existing tests do not effectively test the code changed and/or updated documentation
  • 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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

felixfbecker reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@bergmeister
Do not increase indentation after LParen the previous token is a Newl…
8a7847e
…ine and the next token is not a Newline
@bergmeister Christoph Bergmeister (bergmeister) added the Area - Formatter label May 3, 2020
@bergmeister Christoph Bergmeister (bergmeister) changed the title Do not increase indentation after LParen the previous token is a Newline and the next token is not a Newline Do not increase indentation after LParen if the previous token is a Newline and the next token is not a Newline May 3, 2020
Christoph Bergmeister (bergmeister) added 3 commits May 3, 2020 19:05
@bergmeister
TryPop not availabe in full .net. Also cleanup code
20c110a
@bergmeister
add test cases
194022d
@bergmeister
cleanup code comment
bd738a7
@bergmeister
Christoph Bergmeister (bergmeister) marked this pull request as ready for review May 3, 2020 22:12
Christoph Bergmeister
re-trigger ci
607615c
@rjmholt
Rob Holt (rjmholt) requested review from James Truher (JamesWTruher) and Rob Holt (rjmholt) July 16, 2020 16:37
rjmholt
Rob Holt (rjmholt) reviewed Jul 21, 2020
View reviewed changes
Comment thread Rules/UseConsistentIndentation.cs
case TokenKind.AtParen:
case TokenKind.LParen:
case TokenKind.LCurly:
AddViolation(token, indentationLevel++, diagnosticRecords, ref onNewLine);

@rjmholt Rob Holt (rjmholt) Jul 21, 2020

Copy link
Copy Markdown
Contributor

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 would break the ++ here out into a preceding statement. Even though the C# language spec guarantees the order of evaluation of arguments (not the case in other languages), it's still more explicit to embed the side-effect as its own statement above

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@bergmeister Christoph Bergmeister (bergmeister) Jul 21, 2020

Copy link
Copy Markdown
Collaborator Author

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.

There's 3 other places in this file where indentationLevel++ is passed into a method and similar to your other style suggestion here, I think we should rather have a PR after that to apply style changes consistently. Unfortunately the code has already too much vertical method length for my taste, therefore I personally don't see the value given the price of one more line but that is not a strong opinion and would still accept your proposed style changes if you feel it's worthwhile.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@rjmholt Rob Holt (rjmholt) Jul 21, 2020

Copy link
Copy Markdown
Contributor

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'm happy with it as is

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Rules/UseConsistentIndentation.cs
case TokenKind.DollarParen:
case TokenKind.AtParen:
lParenSkippedIndentation.Push(false);
AddViolation(token, indentationLevel++, diagnosticRecords, ref onNewLine);

@rjmholt Rob Holt (rjmholt) Jul 21, 2020

Copy link
Copy Markdown
Contributor

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.

ditto

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Rules/UseConsistentIndentation.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/UseConsistentIndentation.cs
if (lParenSkippedIndentation.Count > 0)
{
matchingLParenIncreasedIndentation = lParenSkippedIndentation.Pop();
}

@rjmholt Rob Holt (rjmholt) Jul 21, 2020

Copy link
Copy Markdown
Contributor

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.

Suggested change
}
}

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@bergmeister Christoph Bergmeister (bergmeister) Jul 21, 2020

Copy link
Copy Markdown
Collaborator Author

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 would make it inconsistent with the existing style where there is one newline only between the many case statements, which makes it nice to read in logically separated blocks of reasonable size IMHO.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@rjmholt Rob Holt (rjmholt) Jul 21, 2020

Copy link
Copy Markdown
Contributor

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.

Well the current PSSA codebase style is such that I won't argue. I think blocks should always be separated by newlines (and would have included another newline below the if below if I'd noticed), but not willing to spend too much energy on it

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@bergmeister Christoph Bergmeister (bergmeister) Jul 21, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator Author

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'm not a fan of the PSSA style either TBH (especially the long methods where high level context is needed). I suggest a follow-up style PR to make the whole file consistent with this style or define something like an editorconfig file so that we could apply the preferred format across all files automatically.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Rules/UseConsistentIndentation.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/UseConsistentIndentation.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/UseConsistentIndentation.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/UseConsistentIndentation.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread pssa-1.19.0-blog.md Resolved

Uh oh!

There was an error while loading. Please reload this page.

Christoph Bergmeister added 4 commits July 21, 2020 20:45
Christoph Bergmeister
Merge branch 'master' of https://github.com/powershell/psscriptanalyzer…
5fd495c
… into issue1407
Christoph Bergmeister
remove accidentally checked in pssa 1.19.0 blog
a47555f
Christoph Bergmeister
Simplify 'if (onNewLine) { onNewLine = false; }' to 'onNewLine = fals…
5f2201e
…e;' and fix spelling in comment
Christoph Bergmeister
- Simplify token checking code and add comment around stack
b3bf36d
- Remove accidentally checked in file
rjmholt
Rob Holt (rjmholt) approved these changes Jul 22, 2020
View reviewed changes
@rjmholt
Rob Holt (rjmholt) self-requested a review November 3, 2020 19:04
rjmholt
Rob Holt (rjmholt) approved these changes Nov 10, 2020
View reviewed changes
@rjmholt
Rob Holt (rjmholt) merged commit 00f5363 into PowerShell:master Nov 10, 2020
@liamjpeters Liam Peters (liamjpeters) mentioned this pull request Apr 12, 2024
Unable to format document in special circumstances #1994
Closed
6 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@JamesWTruher James Truher (JamesWTruher) Awaiting requested review from JamesWTruher
1 more reviewer
@rjmholt Rob Holt (rjmholt) rjmholt approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

Area - Formatter

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

PSUseConsistentIndentation: Indentation wrong if pipeline is wrapped in parentheses

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@bergmeister @rjmholt
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.