Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add charset support #44

Merged
merged 1 commit into from
Jun 17, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/EditorConfig.VisualStudio/Logic/Cleaning/CodeCleanupManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using EnvDTE;
using System;
using System.Text;
using System.Linq;
using Microsoft.VisualStudio.Text;
using EditorConfig.Core;
Expand Down Expand Up @@ -102,7 +103,11 @@ internal void Cleanup(Document document, ITextBuffer textBuffer)
/// <param name="textBuffer">The text buffer for the document.</param>
private void RunCodeCleanupGeneric(Document document, ITextBuffer textBuffer)
{
ITextDocument textDocument;

var doc = (TextDocument)document.Object("TextDocument");
textBuffer.Properties.TryGetProperty(typeof(ITextDocument), out textDocument);

var path = doc.Parent.FullName;

FileConfiguration settings;
Expand All @@ -113,6 +118,9 @@ private void RunCodeCleanupGeneric(Document document, ITextBuffer textBuffer)
{
ITextSnapshot snapshot = edit.Snapshot;

if (settings.Charset != null && textDocument != null)
FixDocumentCharset(textDocument, settings.Charset.Value);

if (settings.TryKeyAsBool("trim_trailing_whitespace"))
TrimTrailingWhitespace(snapshot, edit);

Expand All @@ -126,6 +134,20 @@ private void RunCodeCleanupGeneric(Document document, ITextBuffer textBuffer)
}
}

private void FixDocumentCharset(ITextDocument document, Charset charset)
{
if (charset == Charset.Latin1)
document.Encoding = Encoding.GetEncoding("ISO-8859-1");
else if (charset == Charset.UTF8)
document.Encoding = new UTF8Encoding(false);
else if (charset == Charset.UTF8BOM)
document.Encoding = new UTF8Encoding(true);
else if (charset == Charset.UTF16LE)
document.Encoding = Encoding.Unicode;
else if (charset == Charset.UTF16BE)
document.Encoding = Encoding.BigEndianUnicode;
}

internal void TrimTrailingWhitespace(ITextSnapshot snapshot, ITextEdit edit)
{
foreach (ITextSnapshotLine line in snapshot.Lines)
Expand Down