Skip to content

Add file-path support to Invoke-Formatter - #2230

Draft
Christoph Bergmeister (bergmeister) with Copilot wants to merge 8 commits into
mainfrom
copilot/add-path-parameter-to-invoke-formatter
Draft

Christoph Bergmeister (bergmeister) with Copilot wants to merge 8 commits into
mainfrom
copilot/add-path-parameter-to-invoke-formatter

Conversation

Copilot AI commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Invoke-Formatter currently accepts script text only. Add a file-based parameter set that formats files in place while retaining their encoding, line endings, and indentation style.

  • File formatting: Add -Path; keep -ScriptDefinition and its existing parameter behavior.
  • File properties: Detect encoding and line endings, and infer tabs or spaces when no indentation kind is explicitly configured. Respect -WhatIf.
  • Example:
    Invoke-Formatter -Path ./script.ps1

Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>
Copilot AI changed the title [WIP] Add -Path parameter to Invoke-Formatter Add file-path support to Invoke-Formatter Oct 22, 2026
@bergmeister
Christoph Bergmeister (bergmeister) added this pull request to stack #2233 October 22, 2026 23:15
@bergmeister
Christoph Bergmeister (bergmeister) removed this pull request from stack #2233 October 22, 2026 23:18
* Document formatter confirmation parameters

Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>

* Handle pipeline binding help values in tests

Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>

* Clarify -Confirm and -WhatIf parameter descriptions

Updated descriptions for -Confirm and -WhatIf parameters to clarify their effects when using the Path and ScriptDefinition options.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>
Co-authored-by: Christoph Bergmeister <c.bergmeister@gmail.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Encoding handling can alter or corrupt files, and indentation inference can incorrectly change their style.

3 open issues
What changed in this PR

Adds in-place file formatting to Invoke-Formatter for issue #775, while retaining the existing script-text parameter set.

Changes:

  • Adds -Path, file-property detection, indentation inference, and -WhatIf support.
  • Adds file-formatting tests and updates pipeline-input help checks.
  • Documents file formatting and confirmation behavior.
File Description
Tests/​Engine/​ModuleHelp.Tests.ps1 Updates checks for pipeline-binding help metadata.
Tests/​Engine/​InvokeFormatter.tests.ps1 Tests file formatting, encoding, indentation, and -WhatIf.
Engine/​Commands/​InvokeFormatterCommand.cs Implements path-based formatting and file-property handling.
docs/​Cmdlets/​Invoke-Formatter.md Documents the new parameter set and usage.

Review effort: Balanced

Comment thread Engine/Commands/InvokeFormatterCommand.cs Outdated
Comment thread Engine/Commands/InvokeFormatterCommand.cs Outdated
Comment thread Engine/Commands/InvokeFormatterCommand.cs Outdated
Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>
Refactor encoding handling and indentation logic.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

BOM-less legacy-encoded scripts fail to format, leaving the encoding-preservation requirement unmet.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

Low severity Correct help syntax and parameter set declarations

docs/​Cmdlets/​Invoke-Formatter.md:21

The help source does not represent the new mutually exclusive parameter sets correctly: both invocations share one unnamed syntax block, and -ScriptDefinition still declares Parameter Sets: (All) at line 145. This file feeds New-ExternalHelp in build.psm1:65, so these declarations also affect generated help. Give ScriptDefinition (Default) and Path separate ### headings and syntax blocks, following docs/Cmdlets/Invoke-ScriptAnalyzer.md:16–53, and change the ScriptDefinition parameter metadata to Parameter Sets: ScriptDefinition.

3 resolved since last review

Review effort: Balanced

Comment thread Engine/Commands/InvokeFormatterCommand.cs Outdated
Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>
Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

CR-only files encounter a formatting failure, and the legacy-encoding regression is not portable across supported Windows PowerShell environments.

3 open issues
1 resolved since last review

Review effort: Balanced

int spaceIndentedLines = 0;
string firstIndentationKind = "space";
bool foundIndentation = false;
string[] lines = scriptDefinition.Split('\n');
[System.IO.File]::ReadAllBytes($path)[0..1] | Should -Be @(255, 254)
}

It 'Best-effort formats BOM-less files using a legacy encoding' {
Comment thread Tests/Engine/InvokeFormatter.tests.ps1 Outdated
Comment on lines +59 to +60
$scriptDefinition = '$name=''café'''
$expected = '$name = ''café'''
* Initial plan

* Handle CR-only formatter line endings

Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>

* Make legacy encoding fixture source-independent

Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add -Path parameter to Invoke-Formatter

3 participants