From 42df98ff80e039f2eb3c15952cd31c659f8b2ae2 Mon Sep 17 00:00:00 2001 From: Sergey Semushin Date: Thu, 6 Aug 2015 00:34:15 +0300 Subject: [PATCH] Change applying on save to correct style. --- LICENSE | 22 +++++ RemoveTrailingWhitespaces/LICENSE.txt | 22 +++++ ...TrailingWhitespaces.cs => PkcCmdIDList.cs} | 0 .../RemoveTrailingWhitespaces.csproj | 7 +- .../RemoveTrailingWhitespacesPackage.cs | 95 +++++++++++-------- .../source.extension.vsixmanifest | 4 +- 6 files changed, 109 insertions(+), 41 deletions(-) create mode 100644 LICENSE create mode 100644 RemoveTrailingWhitespaces/LICENSE.txt rename RemoveTrailingWhitespaces/{RemoveTrailingWhitespaces.cs => PkcCmdIDList.cs} (100%) diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..895b310 --- /dev/null +++ b/LICENSE @@ -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. + diff --git a/RemoveTrailingWhitespaces/LICENSE.txt b/RemoveTrailingWhitespaces/LICENSE.txt new file mode 100644 index 0000000..895b310 --- /dev/null +++ b/RemoveTrailingWhitespaces/LICENSE.txt @@ -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. + diff --git a/RemoveTrailingWhitespaces/RemoveTrailingWhitespaces.cs b/RemoveTrailingWhitespaces/PkcCmdIDList.cs similarity index 100% rename from RemoveTrailingWhitespaces/RemoveTrailingWhitespaces.cs rename to RemoveTrailingWhitespaces/PkcCmdIDList.cs diff --git a/RemoveTrailingWhitespaces/RemoveTrailingWhitespaces.csproj b/RemoveTrailingWhitespaces/RemoveTrailingWhitespaces.csproj index 0fefb2b..781e106 100644 --- a/RemoveTrailingWhitespaces/RemoveTrailingWhitespaces.csproj +++ b/RemoveTrailingWhitespaces/RemoveTrailingWhitespaces.csproj @@ -59,6 +59,7 @@ + @@ -121,8 +122,8 @@ - + True True @@ -161,6 +162,10 @@ + + Always + true + Always true diff --git a/RemoveTrailingWhitespaces/RemoveTrailingWhitespacesPackage.cs b/RemoveTrailingWhitespaces/RemoveTrailingWhitespacesPackage.cs index 25c267f..20f28d0 100644 --- a/RemoveTrailingWhitespaces/RemoveTrailingWhitespacesPackage.cs +++ b/RemoveTrailingWhitespaces/RemoveTrailingWhitespacesPackage.cs @@ -9,6 +9,7 @@ using Microsoft.VisualStudio.OLE.Interop; using Microsoft.VisualStudio.Shell; using System.ComponentModel; +using System.Linq; using EnvDTE; namespace Predelnik.RemoveTrailingWhitespaces @@ -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().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; + } + } + /// /// This is the class that implements the package exposed by this assembly. /// @@ -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; /// /// Initialization of the package; this method is called right after the package is sited, so this is the place @@ -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) { @@ -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; @@ -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; } diff --git a/RemoveTrailingWhitespaces/source.extension.vsixmanifest b/RemoveTrailingWhitespaces/source.extension.vsixmanifest index 1c239df..ab320ed 100644 --- a/RemoveTrailingWhitespaces/source.extension.vsixmanifest +++ b/RemoveTrailingWhitespaces/source.extension.vsixmanifest @@ -4,13 +4,13 @@ RemoveTrailingWhitespaces Trailing whitespace removal tool. Removes either manually or on file save. - BSD + LICENSE.txt Resources\packageicon.png Resources\preview.png Formatting, Trailing Whitespaces, Utility - +