fix: preserve BOM for UTF-8 encoded files#31
Open
michaelhthomas wants to merge 1 commit into
Open
Conversation
Author
|
I'm also willing to make this behavior configurable, #22 seems to indicate that it might be desired behavior in some cases. |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Issue
When formatting a file with a UTF-8 BOM, the plugin stripped it. This was causing a large number of diffs in my organization's repository, as it seems Visual Studio defaults to outputting files in UTF-8 with a BOM (for no apparent reason). This PR attempts to address this and preserve the same encoding as the input.
The root cause was in how
SourceTextwas serialized back to bytes:SourceText.From(stream, encoding: null)correctly detected the BOM and setsourceText.Encodingto a BOM-emittingUTF8Encoding, but the formatters discarded that information by callingEncoding.GetBytes(string), which never writes a preamble regardless of the encoding's configuration (see the remarks section here for more information).Fix
There are a few changes here, not all of which are strictly necessary:
I decided to move where text encoding is performed, from the individual formatters and into
CodeFormatters. SoICodeFormatter.FormatTextnow returnsSourceTextinstead ofbyte[]. If there's any particular opposition to this, I'm happy to do it differently.Added a new extension
SourceTextExtensions.GetBytes()which serializes aSourceTextto bytes usingStreamWriter(stream, sourceText.Encoding). UnlikeEncoding.GetBytes(string),StreamWriterwrites the encoding's preamble (the BOM) at the start of the stream when the encoding specifies one.CodeFormatters.FormatCodenow callsresult.GetBytes()on the returnedSourceTextto get the final byte output.The result is that files with a UTF-8 BOM format have the BOM preserved, while files without one are unaffected.