Skip to content

Commit ae4cac8

Browse files
Copilotbergmeister
andauthored
Preserve CR-only line endings when formatting files (#2234)
* 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>
1 parent d0ba92d commit ae4cac8

3 files changed

Lines changed: 18 additions & 4 deletions

File tree

‎Engine/Commands/InvokeFormatterCommand.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
using System.Collections.Generic;
66
using System.IO;
77
using System.Globalization;
8+
using System.Linq;
89
using System.Text;
910
using System.Management.Automation;
1011
using System.Management.Automation.Language;
12+
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Extensions;
1113

1214
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands
1315
{
@@ -239,7 +241,7 @@ private static string GetIndentationKind(string scriptDefinition)
239241
int spaceIndentedLines = 0;
240242
string firstIndentationKind = "space";
241243
bool foundIndentation = false;
242-
string[] lines = scriptDefinition.Split('\n');
244+
string[] lines = scriptDefinition.GetLines().ToArray();
243245
Parser.ParseInput(scriptDefinition, out Token[] tokens, out ParseError[] errors);
244246
var countedLines = new HashSet<int>();
245247

‎Engine/Extensions.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static class Extensions
1818
/// <returns></returns>
1919
public static IEnumerable<string> GetLines(this string text)
2020
{
21-
return text.Split('\n').Select(line => line.TrimEnd('\r'));
21+
return text.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
2222
}
2323

2424
/// <summary>

‎Tests/Engine/InvokeFormatter.tests.ps1‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,22 @@ Describe "Invoke-Formatter Cmdlet" {
5454
[System.IO.File]::ReadAllBytes($path)[0..1] | Should -Be @(255, 254)
5555
}
5656

57+
It 'Formats a file with CR-only line endings' {
58+
$path = Join-Path $TestDrive 'cr-only.ps1'
59+
$scriptDefinition = "function foo {`r`t`$x=1`r}"
60+
$expected = "function foo {`r`t`$x = 1`r}"
61+
[System.IO.File]::WriteAllText($path, $scriptDefinition)
62+
63+
Invoke-Formatter -Path $path | Should -Be $expected
64+
65+
[System.IO.File]::ReadAllText($path) | Should -Be $expected
66+
}
67+
5768
It 'Best-effort formats BOM-less files using a legacy encoding' {
5869
$path = Join-Path $TestDrive 'legacy-encoding.ps1'
59-
$scriptDefinition = '$name=''café'''
60-
$expected = '$name = ''café'''
70+
$cafe = 'caf' + [char]0xE9
71+
$scriptDefinition = "`$name='$cafe'"
72+
$expected = "`$name = '$cafe'"
6173
$encoding = [System.Text.Encoding]::GetEncoding(1252)
6274
[System.IO.File]::WriteAllBytes($path, $encoding.GetBytes($scriptDefinition))
6375

0 commit comments

Comments
 (0)