Skip to content

Commit

Permalink
Change applying on save to correct style.
Browse files Browse the repository at this point in the history
  • Loading branch information
Predelnik committed Aug 12, 2015
1 parent 2d29e20 commit 42df98f
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 41 deletions.
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2015 Sergey Semushin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

22 changes: 22 additions & 0 deletions RemoveTrailingWhitespaces/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2015 Sergey Semushin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

File renamed without changes.
7 changes: 6 additions & 1 deletion RemoveTrailingWhitespaces/RemoveTrailingWhitespaces.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.Data.Linq" />
<Reference Include="System.Design" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
Expand Down Expand Up @@ -121,8 +122,8 @@
</COMReference>
</ItemGroup>
<ItemGroup>
<Compile Include="RemoveTrailingWhitespaces.cs" />
<Compile Include="Guids.cs" />
<Compile Include="PkcCmdIDList.cs" />
<Compile Include="Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
Expand Down Expand Up @@ -161,6 +162,10 @@
</VSCTCompile>
</ItemGroup>
<ItemGroup>
<Content Include="LICENSE.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<IncludeInVSIX>true</IncludeInVSIX>
</Content>
<Content Include="Resources\packageicon.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<IncludeInVSIX>true</IncludeInVSIX>
Expand Down
95 changes: 57 additions & 38 deletions RemoveTrailingWhitespaces/RemoveTrailingWhitespacesPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell;
using System.ComponentModel;
using System.Linq;
using EnvDTE;

namespace Predelnik.RemoveTrailingWhitespaces
Expand All @@ -26,6 +27,51 @@ public bool RemoveTrailingWhitespacesOnSave
}
};

internal class RunningDocTableEvents : IVsRunningDocTableEvents3
{
RemoveTrailingWhitespacesPackage _pkg;

public RunningDocTableEvents(RemoveTrailingWhitespacesPackage pkg)
{
_pkg = pkg;
}

public int OnBeforeSave(uint docCookie)
{
if (_pkg.removeOnSave())
{
RunningDocumentInfo runningDocumentInfo = _pkg.rdt.GetDocumentInfo(docCookie);
EnvDTE.Document document = _pkg.dte.Documents.OfType<EnvDTE.Document>().SingleOrDefault(x => x.FullName == runningDocumentInfo.Moniker);
var textDoc = document.Object("TextDocument") as TextDocument;
if (textDoc != null)
RemoveTrailingWhitespacesPackage.removeTrailingWhiteSpaces(textDoc);
}
return VSConstants.S_OK;
}

public int OnAfterAttributeChange(uint docCookie, uint grfAttribs) { return VSConstants.S_OK; }
public int OnAfterAttributeChangeEx(uint docCookie, uint grfAttribs, IVsHierarchy pHierOld,
uint itemidOld, string pszMkDocumentOld, IVsHierarchy pHierNew,
uint itemidNew, string pszMkDocumentNew)
{
return VSConstants.S_OK;
}

public int OnAfterDocumentWindowHide(uint docCookie, IVsWindowFrame pFrame) { return VSConstants.S_OK; }
public int OnAfterFirstDocumentLock(uint docCookie, uint dwRDTLockType, uint dwReadLocksRemaining, uint dwEditLocksRemaining)
{
return VSConstants.S_OK;
}

public int OnAfterSave(uint docCookie) { return VSConstants.S_OK; }
public int OnBeforeDocumentWindowShow(uint docCookie, int fFirstShow, IVsWindowFrame pFrame) { return VSConstants.S_OK; }

public int OnBeforeLastDocumentUnlock(uint docCookie, uint dwRDTLockType, uint dwReadLocksRemaining, uint dwEditLocksRemaining)
{
return VSConstants.S_OK;
}
}

/// <summary>
/// This is the class that implements the package exposed by this assembly.
///
Expand Down Expand Up @@ -64,11 +110,10 @@ public RemoveTrailingWhitespacesPackage()
/////////////////////////////////////////////////////////////////////////////
// Overridden Package Implementation
#region Package Members
private DTE _dte;
public DTE dte;
private Properties _props;
DocumentEvents _docEvents;
Events _events;
bool _actionAppliedFlag = false;
public RunningDocumentTable rdt;

/// <summary>
/// Initialization of the package; this method is called right after the package is sited, so this is the place
Expand All @@ -78,11 +123,10 @@ protected override void Initialize()
{
Debug.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this.ToString()));
base.Initialize();
_dte = GetGlobalService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
_props = _dte.get_Properties("Remove Trailing Whitespaces", "Options");
_events = _dte.Events;
_docEvents = _events.DocumentEvents;
_docEvents.DocumentSaved += onDocumentSaved;
dte = GetGlobalService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
_props = dte.get_Properties("Remove Trailing Whitespaces", "Options");
rdt = new RunningDocumentTable(this);
rdt.Advise(new RunningDocTableEvents(this));
var mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
if (mcs != null)
{
Expand All @@ -105,7 +149,7 @@ private void onBeforeQueryStatus(object sender, EventArgs e)

private bool isNeededForActiveDocument()
{
var doc = _dte.ActiveDocument;
var doc = dte.ActiveDocument;
if (doc == null)
{
return false;
Expand All @@ -127,43 +171,18 @@ private bool isNeededForActiveDocument()

private void onRemoveTrailingWhitespacesPressed(object sender, EventArgs e)
{
if (_dte.ActiveDocument == null) return;
var textDocument = _dte.ActiveDocument.Object() as TextDocument;
if (dte.ActiveDocument == null) return;
var textDocument = dte.ActiveDocument.Object() as TextDocument;
if (textDocument == null) return;
removeTrailingWhiteSpaces(textDocument);
}

private void onDocumentSaved(Document Document)
{
if (Document == null || !removeOnSave())
return;
var textDocument = Document.Object() as TextDocument;
if (textDocument == null) return;

if (!_actionAppliedFlag)
{
try
{
removeTrailingWhiteSpaces(textDocument);
_actionAppliedFlag = true;
Document.Save();
}
catch (Exception ex)
{
Debug.Print("Trailing Whitespace Removal Exception: " + ex.Message);
}
}
else
_actionAppliedFlag = false;

}

private static void removeTrailingWhiteSpaces(TextDocument textDocument)
public static void removeTrailingWhiteSpaces(TextDocument textDocument)
{
textDocument.ReplacePattern("[^\\S\\r\\n]+(?=\\r?$)", "", (int)vsFindOptions.vsFindOptionsRegularExpression);
}

private bool removeOnSave()
public bool removeOnSave()
{
return (bool)_props.Item("RemoveTrailingWhitespacesOnSave").Value;
}
Expand Down
4 changes: 2 additions & 2 deletions RemoveTrailingWhitespaces/source.extension.vsixmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
<Identity Id="70f718a3-a985-44e9-9e00-4c767c708ace" Version="1.0" Language="en-US" Publisher="Predelnik" />
<DisplayName>RemoveTrailingWhitespaces</DisplayName>
<Description>Trailing whitespace removal tool. Removes either manually or on file save.</Description>
<License>BSD</License>
<License>LICENSE.txt</License>
<Icon>Resources\packageicon.png</Icon>
<PreviewImage>Resources\preview.png</PreviewImage>
<Tags>Formatting, Trailing Whitespaces, Utility</Tags>
</Metadata>
<Installation InstalledByMsi="false">
<InstallationTarget Id="Microsoft.VisualStudio.Pro" Version="[12.0]" />
<InstallationTarget Id="Microsoft.VisualStudio.Pro" Version="[12.0, 14.0]" />
</Installation>
<Dependencies>
<Dependency Id="Microsoft.Framework.NDP" DisplayName="Microsoft .NET Framework" d:Source="Manual" Version="[4.5,)" />
Expand Down

0 comments on commit 42df98f

Please sign in to comment.