Skip to content

Commit d0ba92d

Browse files
Copilotbergmeister
andauthored
Remove formatter encoding parameter
Co-authored-by: bergmeister <9250262+bergmeister@users.noreply.github.com>
1 parent e113020 commit d0ba92d

3 files changed

Lines changed: 54 additions & 37 deletions

File tree

‎Engine/Commands/InvokeFormatterCommand.cs‎

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,6 @@ public class InvokeFormatterCommand : PSCmdlet, IOutputWriter
6565
[ValidateCount(4, 4)]
6666
public int[] Range { get; set; }
6767

68-
/// <summary>
69-
/// The encoding to use when reading files without a byte-order mark.
70-
/// </summary>
71-
[Parameter(Mandatory = false)]
72-
[ValidateNotNullOrEmpty]
73-
public string Encoding { get; set; } = "utf-8";
74-
7568
#if DEBUG
7669
/// <summary>
7770
/// Attaches to an instance of a .Net debugger
@@ -161,14 +154,22 @@ private void FormatFile(string path)
161154
{
162155
Encoding encoding;
163156
string scriptDefinition;
164-
var requestedEncoding = System.Text.Encoding.GetEncoding(
165-
Encoding,
166-
EncoderFallback.ExceptionFallback,
167-
DecoderFallback.ExceptionFallback);
168-
using (var reader = new StreamReader(path, requestedEncoding, true))
157+
try
158+
{
159+
using (var reader = new StreamReader(path, new UTF8Encoding(false, true), true))
160+
{
161+
scriptDefinition = reader.ReadToEnd();
162+
encoding = reader.CurrentEncoding;
163+
}
164+
}
165+
catch (DecoderFallbackException) when (!HasByteOrderMark(path))
169166
{
170-
scriptDefinition = reader.ReadToEnd();
171-
encoding = reader.CurrentEncoding;
167+
encoding = GetLegacyFallbackEncoding();
168+
using (var reader = new StreamReader(path, encoding, true))
169+
{
170+
scriptDefinition = reader.ReadToEnd();
171+
encoding = reader.CurrentEncoding;
172+
}
172173
}
173174

174175
bool inferIndentationKind = inputSettings.RuleArguments.TryGetValue(IndentationRule, out var indentationSettings)
@@ -199,6 +200,39 @@ private void FormatFile(string path)
199200
WriteObject(formattedScriptDefinition);
200201
}
201202

203+
private static bool HasByteOrderMark(string path)
204+
{
205+
using (var stream = File.OpenRead(path))
206+
{
207+
int firstByte = stream.ReadByte();
208+
int secondByte = stream.ReadByte();
209+
int thirdByte = stream.ReadByte();
210+
int fourthByte = stream.ReadByte();
211+
212+
return firstByte == 0xEF && secondByte == 0xBB && thirdByte == 0xBF
213+
|| firstByte == 0xFF && secondByte == 0xFE
214+
|| firstByte == 0xFE && secondByte == 0xFF
215+
|| firstByte == 0x00 && secondByte == 0x00 && thirdByte == 0xFE && fourthByte == 0xFF
216+
|| firstByte == 0xFF && secondByte == 0xFE && thirdByte == 0x00 && fourthByte == 0x00;
217+
}
218+
}
219+
220+
private static Encoding GetLegacyFallbackEncoding()
221+
{
222+
if (Encoding.Default.CodePage != 65001)
223+
{
224+
return Encoding.GetEncoding(
225+
Encoding.Default.CodePage,
226+
EncoderFallback.ExceptionFallback,
227+
DecoderFallback.ExceptionFallback);
228+
}
229+
230+
#if CORECLR
231+
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
232+
#endif
233+
return Encoding.GetEncoding(1252, EncoderFallback.ExceptionFallback, DecoderFallback.ExceptionFallback);
234+
}
235+
202236
private static string GetIndentationKind(string scriptDefinition)
203237
{
204238
int tabIndentedLines = 0;

‎Tests/Engine/InvokeFormatter.tests.ps1‎

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

57-
It 'Formats BOM-less files using the specified encoding' {
57+
It 'Best-effort formats BOM-less files using a legacy encoding' {
5858
$path = Join-Path $TestDrive 'legacy-encoding.ps1'
5959
$scriptDefinition = '$name=''café'''
6060
$expected = '$name = ''café'''
6161
$encoding = [System.Text.Encoding]::GetEncoding(1252)
6262
[System.IO.File]::WriteAllBytes($path, $encoding.GetBytes($scriptDefinition))
6363

64-
Invoke-Formatter -Path $path -Encoding 'windows-1252' | Should -Be $expected
64+
Invoke-Formatter -Path $path | Should -Be $expected
6565

6666
[System.IO.File]::ReadAllBytes($path) | Should -Be $encoding.GetBytes($expected)
6767
}

‎docs/Cmdlets/Invoke-Formatter.md‎

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ Formats script text or a file based on the input settings or default settings.
1616

1717
```
1818
Invoke-Formatter [-ScriptDefinition] <string> [[-Settings] <Object>] [[-Range] <int[]>]
19-
[-Encoding <string>] [-WhatIf] [-Confirm] [<CommonParameters>]
19+
[-WhatIf] [-Confirm] [<CommonParameters>]
2020
Invoke-Formatter -Path <string> [[-Settings] <Object>] [[-Range] <int[]>]
21-
[-Encoding <string>] [-WhatIf] [-Confirm] [<CommonParameters>]
21+
[-WhatIf] [-Confirm] [<CommonParameters>]
2222
```
2323

2424
## DESCRIPTION
@@ -28,8 +28,8 @@ The `Invoke-Formatter` cmdlet formats a string input or a file according to defi
2828
defined in `Settings/CodeFormatting.psd1`. When **Path** is used, the file is formatted in place.
2929
The file's detected encoding and line-ending style are retained, and the default settings use the
3030
file's detected tab or space indentation style. Use `-WhatIf` to preview whether a file would be
31-
changed without writing it. Use **Encoding** to specify the encoding for files without a byte-order
32-
mark (BOM); a detected BOM takes precedence.
31+
changed without writing it. BOM-less files are interpreted as UTF-8 when valid; otherwise the system
32+
ANSI encoding or Windows-1252 is used as a best-effort fallback.
3333

3434
## EXAMPLES
3535

@@ -97,23 +97,6 @@ Invoke-Formatter -Path /path/to/script.ps1
9797

9898
## PARAMETERS
9999

100-
### -Encoding
101-
102-
The encoding to use when reading and writing files without a byte-order mark (BOM). Files with a BOM
103-
continue to use the encoding detected from the BOM. The default is UTF-8.
104-
105-
```yaml
106-
Type: String
107-
Parameter Sets: (All)
108-
Aliases:
109-
110-
Required: False
111-
Position: Named
112-
Default value: utf-8
113-
Accept pipeline input: False
114-
Accept wildcard characters: False
115-
```
116-
117100
### -Range
118101

119102
The range within which formatting should take place. The value of this parameter must be an array of

0 commit comments

Comments
 (0)