Skip to content

Commit

Permalink
Version 1.4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubbielawa committed Feb 17, 2016
1 parent dbb8fbf commit 95736b1
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 11 deletions.
16 changes: 10 additions & 6 deletions Line Endings Unifier/LineEndingsChanger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ public enum LineEndingsList
Macintosh
}

public static string ChangeLineEndings(string line, LineEndings lineEndings, ref int numberOfChanges)
public static string ChangeLineEndings(string line, LineEndings lineEndings, ref int numberOfChanges, out int numberOfIndividualChanges)
{
numberOfIndividualChanges = 0;

switch (lineEndings)
{
case LineEndings.Linux:
numberOfChanges += line.Count(x => x == '\r');
numberOfIndividualChanges += line.Count(x => x == '\r');
line = line.Replace("\r\n", "\n").Replace('\r', '\n');
break;
case LineEndings.Windows:
Expand All @@ -35,30 +37,32 @@ public static string ChangeLineEndings(string line, LineEndings lineEndings, ref
{
if (i < line.Length - 1 && line[i + 1] != '\n')
{
numberOfChanges++;
numberOfIndividualChanges++;
line = line.Insert(i + 1, "\n");
i++;
}
else if (i == line.Length - 1)
{
numberOfChanges++;
numberOfIndividualChanges++;
line = line.Insert(i + 1, "\n");
}
}
else if (line[i] == '\n' && i > 0 && line[i - 1] != '\r')
{
numberOfChanges++;
numberOfIndividualChanges++;
line = line.Insert(i, "\r");
i++;
}
}
break;
case LineEndings.Macintosh:
numberOfChanges += line.Count(x => x == '\n');
numberOfIndividualChanges += line.Count(x => x == '\n');
line = line.Replace("\r\n", "\r").Replace('\n', '\r');
break;
}

numberOfChanges += numberOfIndividualChanges;

return line;
}
}
Expand Down
51 changes: 47 additions & 4 deletions Line Endings Unifier/LineEndingsUnifierPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ protected override void Initialize()
menuItem.Enabled = true;
mcs.AddCommand(menuItem);
}

SetupOutputWindow();
}

void commandEvents_BeforeExecute(string Guid, int ID, object CustomIn, object CustomOut, ref bool CancelDefault)
Expand All @@ -75,7 +77,11 @@ void commandEvents_BeforeExecute(string Guid, int ID, object CustomIn, object Cu

if (currentDocument.Name.EndsWithAny(supportedFileFormats))
{
UnifyLineEndingsInDocument(textDocument, lineEndings, ref tmp);
var numberOfIndividualChanges = 0;
Output("Unifying started...\n");
UnifyLineEndingsInDocument(textDocument, lineEndings, ref tmp, out numberOfIndividualChanges);
Output(string.Format("{0}: changed {1} line endings\n", currentDocument.FullName, numberOfIndividualChanges));
Output("Done\n");
}
}
break;
Expand Down Expand Up @@ -109,6 +115,7 @@ private void UnifyLineEndingsInFile()
{
System.Threading.Tasks.Task.Run(() =>
{
Output("Unifying started...\n");
var numberOfChanges = 0;
var stopWatch = new Stopwatch();
stopWatch.Start();
Expand All @@ -117,6 +124,7 @@ private void UnifyLineEndingsInFile()
var secondsElapsed = stopWatch.ElapsedMilliseconds / 1000.0;
VsShellUtilities.ShowMessageBox(this, string.Format("Successfully changed {0} line endings in {1} seconds!", numberOfChanges, secondsElapsed), "Success",
OLEMSGICON.OLEMSGICON_INFO, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
Output(string.Format("Done in {0} seconds\n", secondsElapsed));
});
}
else
Expand All @@ -142,6 +150,7 @@ private void UnifyLineEndingsInFolder()
{
System.Threading.Tasks.Task.Run(() =>
{
Output("Unifying started...\n");
var numberOfChanges = 0;
var stopWatch = new Stopwatch();
stopWatch.Start();
Expand All @@ -150,6 +159,7 @@ private void UnifyLineEndingsInFolder()
var secondsElapsed = stopWatch.ElapsedMilliseconds / 1000.0;
VsShellUtilities.ShowMessageBox(this, string.Format("Successfully changed {0} line endings in {1} seconds!", numberOfChanges, secondsElapsed), "Success",
OLEMSGICON.OLEMSGICON_INFO, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
Output(string.Format("Done in {0} seconds\n", secondsElapsed));
});
}
}
Expand All @@ -169,6 +179,7 @@ private void UnifyLineEndingsInProject()
{
System.Threading.Tasks.Task.Run(() =>
{
Output("Unifying started...\n");
var numberOfChanges = 0;
var stopWatch = new Stopwatch();
stopWatch.Start();
Expand All @@ -177,6 +188,7 @@ private void UnifyLineEndingsInProject()
var secondsElapsed = stopWatch.ElapsedMilliseconds / 1000.0;
VsShellUtilities.ShowMessageBox(this, string.Format("Successfully changed {0} line endings in {1} seconds!", numberOfChanges, secondsElapsed), "Success",
OLEMSGICON.OLEMSGICON_INFO, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
Output(string.Format("Done in {0} seconds\n", secondsElapsed));
});
}
}
Expand All @@ -202,6 +214,7 @@ private void UnifyLineEndingsInSolution(bool askForLineEnding = true)
{
System.Threading.Tasks.Task.Run(() =>
{
Output("Unifying started...\n");
var stopWatch = new Stopwatch();
stopWatch.Start();
var numberOfChanges = 0;
Expand All @@ -213,18 +226,21 @@ private void UnifyLineEndingsInSolution(bool askForLineEnding = true)
var secondsElapsed = stopWatch.ElapsedMilliseconds / 1000.0;
VsShellUtilities.ShowMessageBox(this, string.Format("Successfully changed {0} line endings in {1} seconds!", numberOfChanges, secondsElapsed), "Success",
OLEMSGICON.OLEMSGICON_INFO, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST);
Output(string.Format("Done in {0} seconds\n", secondsElapsed));
});
}
}
else
{
var lineEndings = this.DefaultLineEnding;

Output("Unifying started...\n");
int numberOfChanges = 0;
foreach (Project project in currentSolution.Projects)
{
UnifyLineEndingsInProjectItems(project.ProjectItems, lineEndings, ref numberOfChanges);
}
Output("Done\n");
}
break;
}
Expand Down Expand Up @@ -262,12 +278,16 @@ private void UnifyLineEndingsInProjectItem(ProjectItem item, LineEndingsChanger.
var document = item.Document;
if (document != null)
{
var numberOfIndividualChanges = 0;

var textDocument = document.Object("TextDocument") as TextDocument;
UnifyLineEndingsInDocument(textDocument, lineEndings, ref numberOfChanges);
UnifyLineEndingsInDocument(textDocument, lineEndings, ref numberOfChanges, out numberOfIndividualChanges);
if (this.OptionsPage.SaveFilesAfterUnifying)
{
document.Save();
}

Output(string.Format("{0}: changed {1} line endings\n", document.FullName, numberOfIndividualChanges));
}

if (documentWindow != null)
Expand All @@ -276,16 +296,39 @@ private void UnifyLineEndingsInProjectItem(ProjectItem item, LineEndingsChanger.
}
}

private void UnifyLineEndingsInDocument(TextDocument textDocument, LineEndingsChanger.LineEndings lineEndings, ref int numberOfChanges)
private void UnifyLineEndingsInDocument(TextDocument textDocument, LineEndingsChanger.LineEndings lineEndings, ref int numberOfChanges, out int numberOfIndividualChanges)
{
var startPoint = textDocument.StartPoint.CreateEditPoint();
var endPoint = textDocument.EndPoint.CreateEditPoint();

var text = startPoint.GetText(endPoint.AbsoluteCharOffset);
var changedText = LineEndingsChanger.ChangeLineEndings(text, lineEndings, ref numberOfChanges);
var changedText = LineEndingsChanger.ChangeLineEndings(text, lineEndings, ref numberOfChanges, out numberOfIndividualChanges);
startPoint.ReplaceText(text.Length, changedText, (int)vsEPReplaceTextOptions.vsEPReplaceTextKeepMarkers);
}

private void SetupOutputWindow()
{
this.outputWindow = ServiceProvider.GlobalProvider.GetService(typeof(SVsOutputWindow)) as IVsOutputWindow;
this.guid = new Guid("0F44E2D1-F5FA-4d2d-AB30-22BE8ECD9789");
var windowTitle = "Line Endings Unifier";
this.outputWindow.CreatePane(ref this.guid, windowTitle, 1, 1);
}

private void Output(string message)
{
if (this.OptionsPage.WriteReport)
{
IVsOutputWindowPane outputWindowPane;
this.outputWindow.GetPane(ref this.guid, out outputWindowPane);

outputWindowPane.OutputString(message);
}
}

private IVsOutputWindow outputWindow;

private Guid guid;

private CommandEvents commandEvents;

private LineEndingsChanger.LineEndings DefaultLineEnding
Expand Down
10 changes: 10 additions & 0 deletions Line Endings Unifier/OptionsPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class OptionsPage : DialogPage
private bool forceDefaultLineEndingOnSave = false;
private string supportedFileFormats = ".cpp; .c; .h; .hpp; .cs; .js; .vb; .txt";
private bool saveFilesAfterUnifying = false;
private bool writeReport = false;

[Category("Line Endings Unifier")]
[DisplayName("Default Line Ending")]
Expand Down Expand Up @@ -49,5 +50,14 @@ public bool SaveFilesAfterUnifying
get { return saveFilesAfterUnifying; }
set { saveFilesAfterUnifying = value; }
}

[Category("Line Endings Unifier")]
[DisplayName("Write Report To The Output Window")]
[Description("Set this to TRUE if you want the extension to write a report in the Output window")]
public bool WriteReport
{
get { return writeReport; }
set { writeReport = value; }
}
}
}
2 changes: 1 addition & 1 deletion Line Endings Unifier/source.extension.vsixmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Identifier Id="1ce34aed-d80b-4e02-afc9-bd0bd3848443">
<Name>Line Endings Unifier</Name>
<Author>Jakub Bielawa</Author>
<Version>1.3.2</Version>
<Version>1.4.1</Version>
<Description xml:space="preserve">Unify line endings in a whole solution, a specific project, a chosen folder or a certain source file by right clicking on a solution, a project or a file in the Solution Explorer.</Description>
<License>license.txt</License>
<Locale>1033</Locale>
Expand Down

0 comments on commit 95736b1

Please sign in to comment.