Skip to content

Commit

Permalink
Version 1.8
Browse files Browse the repository at this point in the history
- added new option - Remove Trailing Whitespace
  • Loading branch information
jakubbielawa committed Nov 22, 2017
1 parent 6df80db commit 88698f2
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 29 deletions.
1 change: 1 addition & 0 deletions LineEndingsUnifier/LineEndingsUnifier.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<Compile Include="PkgCmdID.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="DocumentSaveListener.cs" />
<Compile Include="TrailingWhitespaceRemover.cs" />
<Compile Include="Utilities.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
20 changes: 12 additions & 8 deletions LineEndingsUnifier/LineEndingsUnifierPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private int DocumentSaveListener_BeforeSave(uint docCookie)

var supportedFileFormats = this.SupportedFileFormats;
var supportedFileNames = this.SupportedFileNames;

if (currentDocument.Name.EndsWithAny(supportedFileFormats) || currentDocument.Name.EqualsAny(supportedFileNames))
{
var numberOfIndividualChanges = 0;
Expand All @@ -96,7 +96,7 @@ private int DocumentSaveListener_BeforeSave(uint docCookie)

return VSConstants.S_OK;
}

private void UnifyLineEndingsInFileEventHandler(object sender, EventArgs e)
{
var selectedItem = this.IDE.SelectedItems.Item(1);
Expand All @@ -107,7 +107,7 @@ private void UnifyLineEndingsInFileEventHandler(object sender, EventArgs e)
{
var supportedFileFormats = this.SupportedFileFormats;
var supportedFileNames = this.SupportedFileNames;

if (item.Name.EndsWithAny(supportedFileFormats) || item.Name.EqualsAny(supportedFileNames))
{
System.Threading.Tasks.Task.Run(() =>
Expand Down Expand Up @@ -192,7 +192,7 @@ private void UnifyLineEndingsInProjectEventHandler(object sender, EventArgs e)
private void UnifyLineEndingsInSolutionEventHandler(object sender, EventArgs e)
{
var currentSolution = this.IDE.Solution;

var properties = currentSolution.Properties;
foreach (Property property in properties)
{
Expand Down Expand Up @@ -230,7 +230,7 @@ private void UnifyLineEndingsInProjectItems(ProjectItems projectItems, LineEndin
{
var supportedFileFormats = this.SupportedFileFormats;
var supportedFileNames = this.SupportedFileNames;

foreach (ProjectItem item in projectItems)
{
if (item.ProjectItems != null && item.ProjectItems.Count > 0)
Expand Down Expand Up @@ -262,9 +262,9 @@ private void UnifyLineEndingsInProjectItem(ProjectItem item, LineEndingsChanger.
{
var numberOfIndividualChanges = 0;
var numberOfAllLineEndings = 0;
if (!this.OptionsPage.TrackChanges ||
(this.OptionsPage.TrackChanges && this.changeLog != null && (!this.changeLog.ContainsKey(document.FullName) ||

if (!this.OptionsPage.TrackChanges ||
(this.OptionsPage.TrackChanges && this.changeLog != null && (!this.changeLog.ContainsKey(document.FullName) ||
this.changeLog[document.FullName].LineEndings != lineEndings ||
this.changeLog[document.FullName].Ticks < File.GetLastWriteTime(document.FullName).Ticks)))
{
Expand Down Expand Up @@ -299,6 +299,10 @@ private void UnifyLineEndingsInDocument(TextDocument textDocument, LineEndingsCh
var endPoint = textDocument.EndPoint.CreateEditPoint();

var text = startPoint.GetText(endPoint.AbsoluteCharOffset);
if (this.OptionsPage.RemoveTrailingWhitespace)
{
text = TrailingWhitespaceRemover.RemoveTrailingWhitespace(text);
}
var changedText = LineEndingsChanger.ChangeLineEndings(text, lineEndings, ref numberOfChanges, out numberOfIndividualChanges, out numberOfAllLineEndings);

if (this.OptionsPage.AddNewlineOnLastLine)
Expand Down
10 changes: 10 additions & 0 deletions LineEndingsUnifier/OptionsPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class OptionsPage : DialogPage
private bool unifyOnlyOpenFiles = false;
private bool addNewlineOnLastLine = false;
private bool trackChanges = false;
private bool removeTrailingWhitespace = false;

[Category("Line Endings Unifier")]
[DisplayName("Default Line Ending")]
Expand Down Expand Up @@ -116,5 +117,14 @@ public bool TrackChanges
trackChanges = value;
}
}

[Category("Line Endings Unifier")]
[DisplayName("Remove Trailing Whitespace")]
[Description("Set this to TRUE if you want the extension to remove trailing whitespace characters while unifying newline characters")]
public bool RemoveTrailingWhitespace
{
get { return removeTrailingWhitespace; }
set { removeTrailingWhitespace = value; }
}
}
}
25 changes: 25 additions & 0 deletions LineEndingsUnifier/TrailingWhitespaceRemover.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JakubBielawa.LineEndingsUnifier
{
public static class TrailingWhitespaceRemover
{
public static string RemoveTrailingWhitespace(string text)
{
var stringBuilder = new StringBuilder();

var lines = text.Split(new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
for (var i = 0; i < lines.Length - 1; i++)
{
stringBuilder.AppendLine(lines[i].TrimEnd());
}
stringBuilder.Append(lines[lines.Length - 1].TrimEnd());

return stringBuilder.ToString();
}
}
}
42 changes: 21 additions & 21 deletions LineEndingsUnifier/source.extension.vsixmanifest
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="1ce34aed-d80b-4e02-afc9-bd0bd3848443" Version="1.7" Language="en-US" Publisher="Jakub Bielawa" />
<DisplayName>Line Endings Unifier</DisplayName>
<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>
</Metadata>
<Installation>
<InstallationTarget Id="Microsoft.VisualStudio.Community" Version="[11.0,15.0]" />
<InstallationTarget Version="[11.0,16.0)" Id="Microsoft.VisualStudio.Pro" />
<InstallationTarget Version="[11.0,16.0)" Id="Microsoft.VisualStudio.Enterprise" />
</Installation>
<Dependencies>
<Dependency Id="Microsoft.Framework.NDP" DisplayName="Microsoft .NET Framework" d:Source="Manual" Version="[4.5,)" />
<Dependency Id="Microsoft.VisualStudio.MPF.11.0" DisplayName="Visual Studio MPF 11.0" d:Source="Installed" Version="[11.0,12.0)" />
</Dependencies>
<Prerequisites>
<Prerequisite Id="Microsoft.VisualStudio.Component.CoreEditor" Version="[11.0,16.0)" DisplayName="Visual Studio core editor" />
</Prerequisites>
<Assets>
<Asset Type="Microsoft.VisualStudio.VsPackage" d:Source="Project" d:ProjectName="%CurrentProject%" Path="|%CurrentProject%;PkgdefProjectOutputGroup|" />
</Assets>
<Metadata>
<Identity Id="1ce34aed-d80b-4e02-afc9-bd0bd3848443" Version="1.8" Language="en-US" Publisher="Jakub Bielawa" />
<DisplayName>Line Endings Unifier</DisplayName>
<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>
</Metadata>
<Installation>
<InstallationTarget Id="Microsoft.VisualStudio.Community" Version="[11.0,15.0]" />
<InstallationTarget Version="[11.0,16.0)" Id="Microsoft.VisualStudio.Pro" />
<InstallationTarget Version="[11.0,16.0)" Id="Microsoft.VisualStudio.Enterprise" />
</Installation>
<Dependencies>
<Dependency Id="Microsoft.Framework.NDP" DisplayName="Microsoft .NET Framework" d:Source="Manual" Version="[4.5,)" />
<Dependency Id="Microsoft.VisualStudio.MPF.11.0" DisplayName="Visual Studio MPF 11.0" d:Source="Installed" Version="[11.0,12.0)" />
</Dependencies>
<Prerequisites>
<Prerequisite Id="Microsoft.VisualStudio.Component.CoreEditor" Version="[11.0,16.0)" DisplayName="Visual Studio core editor" />
</Prerequisites>
<Assets>
<Asset Type="Microsoft.VisualStudio.VsPackage" d:Source="Project" d:ProjectName="%CurrentProject%" Path="|%CurrentProject%;PkgdefProjectOutputGroup|" />
</Assets>
</PackageManifest>

0 comments on commit 88698f2

Please sign in to comment.