-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add formatter for the charset .editorconfig option
- Loading branch information
Showing
19 changed files
with
250 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.Options; | ||
using Microsoft.CodeAnalysis.Text; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.VisualStudio.CodingConventions; | ||
|
||
namespace Microsoft.CodeAnalysis.Tools.Formatters | ||
{ | ||
internal sealed class CharsetFormatter : DocumentFormatter | ||
{ | ||
protected override string FormatWarningDescription => Resources.Fix_file_encoding; | ||
|
||
private static Encoding Utf8 => new UTF8Encoding(encoderShouldEmitUTF8Identifier: false); | ||
private static Encoding Latin1 => Encoding.GetEncoding("iso-8859-1"); | ||
|
||
protected override Task<SourceText> FormatFileAsync( | ||
Document document, | ||
SourceText sourceText, | ||
OptionSet options, | ||
ICodingConventionsSnapshot codingConventions, | ||
FormatOptions formatOptions, | ||
ILogger logger, | ||
CancellationToken cancellationToken) | ||
{ | ||
return Task.Run(() => | ||
{ | ||
if (!TryGetCharset(codingConventions, out var encoding) | ||
|| sourceText.Encoding.Equals(encoding)) | ||
{ | ||
return sourceText; | ||
} | ||
|
||
return SourceText.From(sourceText.ToString(), encoding); | ||
}); | ||
} | ||
|
||
private static bool TryGetCharset(ICodingConventionsSnapshot codingConventions, out Encoding encoding) | ||
{ | ||
if (codingConventions.TryGetConventionValue("charset", out string charsetOption)) | ||
{ | ||
encoding = GetCharset(charsetOption); | ||
return true; | ||
} | ||
|
||
encoding = null; | ||
return false; | ||
} | ||
|
||
public static Encoding GetCharset(string charsetOption) | ||
{ | ||
switch (charsetOption) | ||
{ | ||
case "latin1": | ||
return Latin1; | ||
default: | ||
case "utf-8": | ||
return Utf8; | ||
case "utf-8-bom": | ||
return Encoding.UTF8; // UTF-8 with BOM Marker | ||
case "utf-16be": | ||
return Encoding.BigEndianUnicode; // Big Endian with BOM Marker | ||
case "utf-16le": | ||
return Encoding.Unicode; // Little Endian with BOM Marker | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.