diff --git a/RemoveTrailingWhitespaces/RemoveTrailingWhitespaces.csproj b/RemoveTrailingWhitespaces/RemoveTrailingWhitespaces.csproj index ae0a962..553f882 100644 --- a/RemoveTrailingWhitespaces/RemoveTrailingWhitespaces.csproj +++ b/RemoveTrailingWhitespaces/RemoveTrailingWhitespaces.csproj @@ -62,58 +62,6 @@ 4 true - - - True - - - True - - - True - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - {00020430-0000-0000-C000-000000000046} - 2 - 0 - 0 - primary - False - False - - diff --git a/RemoveTrailingWhitespaces/RemoveTrailingWhitespacesPackage.cs b/RemoveTrailingWhitespaces/RemoveTrailingWhitespacesPackage.cs index 863f3d7..3a30ee6 100644 --- a/RemoveTrailingWhitespaces/RemoveTrailingWhitespacesPackage.cs +++ b/RemoveTrailingWhitespaces/RemoveTrailingWhitespacesPackage.cs @@ -47,12 +47,7 @@ public int OnBeforeSave(uint docCookie) { if (_pkg.RemoveOnSave()) { - RunningDocumentInfo runningDocumentInfo = new RunningDocumentInfo(_pkg.rdt, docCookie); - EnvDTE.Document document = _pkg.dte.Documents.OfType().SingleOrDefault(x => x.FullName == runningDocumentInfo.Moniker); - if (document == null) - return VSConstants.S_OK; - if (document.Object("TextDocument") is TextDocument textDoc) - _pkg.RemoveTrailingWhiteSpaces(textDoc); + _pkg.RemoveTrailingWhiteSpaces(docCookie); } return VSConstants.S_OK; } @@ -100,6 +95,11 @@ public int OnBeforeLastDocumentUnlock(uint docCookie, uint dwRDTLockType, uint d [ProvideOptionPage(typeof(OptionsPage), "Remove Trailing Whitespaces", "Options", 1000, 1001, true)] [ProvideMenuResource("Menus.ctmenu", 1)] [ProvideAutoLoad("{f1536ef8-92ec-443c-9ed7-fdadf150da82}", PackageAutoLoadFlags.BackgroundLoad)] + [ProvideUIContextRule("{f1536ef8-92ec-443c-9ed7-fdadf150da82}", + name: "Trigger for autoloading the RemoveTrailingWhitespaces extension", + expression: "DocOpen", + termNames: new[] { "DocOpen" }, + termValues: new[] { "HierSingleSelectionName:.$" })] public sealed class RemoveTrailingWhitespacesPackage : AsyncPackage { /// @@ -118,7 +118,7 @@ public RemoveTrailingWhitespacesPackage() ///////////////////////////////////////////////////////////////////////////// // Overridden Package Implementation #region Package Members - public DTE dte; + public _DTE dte; public IVsRunningDocumentTable rdt; public IFindService findService; private uint rdtCookie; @@ -130,7 +130,7 @@ public RemoveTrailingWhitespacesPackage() /// protected override async Task InitializeAsync(System.Threading.CancellationToken cancellationToken, IProgress progress) { - dte = await GetServiceAsync(typeof(EnvDTE.DTE)) as EnvDTE.DTE; + dte = await GetServiceAsync(typeof(_DTE)) as _DTE; Assumes.Present(dte); rdt = await GetServiceAsync(typeof(SVsRunningDocumentTable)) as IVsRunningDocumentTable; Assumes.Present(rdt); @@ -186,7 +186,9 @@ private void OnRemoveTrailingWhitespacesPressed(object sender, EventArgs e) { if (dte.ActiveDocument == null) return; if (!(dte.ActiveDocument.Object() is TextDocument textDocument)) return; - RemoveTrailingWhiteSpaces(textDocument); + + uint docCookie = GetDocCookie(dte.ActiveDocument.FullName); + RemoveTrailingWhiteSpaces(docCookie); } private IFinder GetFinder(string findWhat, string replacement, ITextBuffer textBuffer) @@ -196,33 +198,12 @@ private IFinder GetFinder(string findWhat, string replacement, ITextBuffer textB return finderFactory.Create(textBuffer.CurrentSnapshot); } - internal static ITextBuffer GettextBufferAt(TextDocument textDocument, IComponentModel componentModel, IServiceProvider serviceProvider) + internal static ITextBuffer GettextBufferAt(IVsTextBuffer textBuffer, IComponentModel componentModel) { - ThreadHelper.ThrowIfNotOnUIThread(); - IVsWindowFrame windowFrame; - if (VsShellUtilities.IsDocumentOpen( - serviceProvider, - textDocument.Parent.FullName, - Guid.Empty, - out var _, - out var _, - out windowFrame)) - { - IVsTextView view = VsShellUtilities.GetTextView(windowFrame); - IVsTextLines lines; - if (view.GetBuffer(out lines) == 0) - { - var buffer = lines as IVsTextBuffer; - if (buffer != null) - { - var editorAdapterFactoryService = componentModel.GetService(); - return editorAdapterFactoryService.GetDataBuffer(buffer); - } - } - } - - return null; + var editorAdapterFactoryService = componentModel.GetService(); + return editorAdapterFactoryService.GetDataBuffer(textBuffer); } + private static void ReplaceAll(ITextBuffer textBuffer, IEnumerable replacements) { if (replacements.Any()) @@ -239,9 +220,49 @@ private static void ReplaceAll(ITextBuffer textBuffer, IEnumerable - + RemoveTrailingWhitespaces Trailing whitespace removal tool. Removes either manually or on file save. LICENSE.txt