@@ -47,12 +47,7 @@ public int OnBeforeSave(uint docCookie)
4747 {
4848 if ( _pkg . RemoveOnSave ( ) )
4949 {
50- RunningDocumentInfo runningDocumentInfo = new RunningDocumentInfo ( _pkg . rdt , docCookie ) ;
51- EnvDTE . Document document = _pkg . dte . Documents . OfType < EnvDTE . Document > ( ) . SingleOrDefault ( x => x . FullName == runningDocumentInfo . Moniker ) ;
52- if ( document == null )
53- return VSConstants . S_OK ;
54- if ( document . Object ( "TextDocument" ) is TextDocument textDoc )
55- _pkg . RemoveTrailingWhiteSpaces ( textDoc ) ;
50+ _pkg . RemoveTrailingWhiteSpaces ( docCookie ) ;
5651 }
5752 return VSConstants . S_OK ;
5853 }
@@ -118,7 +113,7 @@ public RemoveTrailingWhitespacesPackage()
118113 /////////////////////////////////////////////////////////////////////////////
119114 // Overridden Package Implementation
120115 #region Package Members
121- public DTE dte ;
116+ public _DTE dte ;
122117 public IVsRunningDocumentTable rdt ;
123118 public IFindService findService ;
124119 private uint rdtCookie ;
@@ -130,7 +125,7 @@ public RemoveTrailingWhitespacesPackage()
130125 /// </summary>
131126 protected override async Task InitializeAsync ( System . Threading . CancellationToken cancellationToken , IProgress < ServiceProgressData > progress )
132127 {
133- dte = await GetServiceAsync ( typeof ( EnvDTE . DTE ) ) as EnvDTE . DTE ;
128+ dte = await GetServiceAsync ( typeof ( _DTE ) ) as _DTE ;
134129 Assumes . Present ( dte ) ;
135130 rdt = await GetServiceAsync ( typeof ( SVsRunningDocumentTable ) ) as IVsRunningDocumentTable ;
136131 Assumes . Present ( rdt ) ;
@@ -186,7 +181,9 @@ private void OnRemoveTrailingWhitespacesPressed(object sender, EventArgs e)
186181 {
187182 if ( dte . ActiveDocument == null ) return ;
188183 if ( ! ( dte . ActiveDocument . Object ( ) is TextDocument textDocument ) ) return ;
189- RemoveTrailingWhiteSpaces ( textDocument ) ;
184+
185+ uint docCookie = GetDocCookie ( dte . ActiveDocument . FullName ) ;
186+ RemoveTrailingWhiteSpaces ( docCookie ) ;
190187 }
191188
192189 private IFinder GetFinder ( string findWhat , string replacement , ITextBuffer textBuffer )
@@ -196,33 +193,12 @@ private IFinder GetFinder(string findWhat, string replacement, ITextBuffer textB
196193 return finderFactory . Create ( textBuffer . CurrentSnapshot ) ;
197194 }
198195
199- internal static ITextBuffer GettextBufferAt ( TextDocument textDocument , IComponentModel componentModel , IServiceProvider serviceProvider )
196+ internal static ITextBuffer GettextBufferAt ( IVsTextBuffer textBuffer , IComponentModel componentModel )
200197 {
201- ThreadHelper . ThrowIfNotOnUIThread ( ) ;
202- IVsWindowFrame windowFrame ;
203- if ( VsShellUtilities . IsDocumentOpen (
204- serviceProvider ,
205- textDocument . Parent . FullName ,
206- Guid . Empty ,
207- out var _ ,
208- out var _ ,
209- out windowFrame ) )
210- {
211- IVsTextView view = VsShellUtilities . GetTextView ( windowFrame ) ;
212- IVsTextLines lines ;
213- if ( view . GetBuffer ( out lines ) == 0 )
214- {
215- var buffer = lines as IVsTextBuffer ;
216- if ( buffer != null )
217- {
218- var editorAdapterFactoryService = componentModel . GetService < IVsEditorAdaptersFactoryService > ( ) ;
219- return editorAdapterFactoryService . GetDataBuffer ( buffer ) ;
220- }
221- }
222- }
223-
224- return null ;
198+ var editorAdapterFactoryService = componentModel . GetService < IVsEditorAdaptersFactoryService > ( ) ;
199+ return editorAdapterFactoryService . GetDataBuffer ( textBuffer ) ;
225200 }
201+
226202 private static void ReplaceAll ( ITextBuffer textBuffer , IEnumerable < FinderReplacement > replacements )
227203 {
228204 if ( replacements . Any ( ) )
@@ -239,9 +215,49 @@ private static void ReplaceAll(ITextBuffer textBuffer, IEnumerable<FinderReplace
239215 }
240216 }
241217
242- public void RemoveTrailingWhiteSpaces ( TextDocument textDocument )
218+ public uint GetDocCookie ( string docFullName )
243219 {
244- var textBuffer = GettextBufferAt ( textDocument , componentModel , this ) ;
220+ IVsHierarchy hierarchy = null ;
221+ uint itemid = 0 ;
222+ IntPtr docDataUnk = IntPtr . Zero ;
223+ uint lockCookie = 0 ;
224+
225+ IEnumRunningDocuments allDocs ;
226+ if ( VSConstants . S_OK != rdt . GetRunningDocumentsEnum ( out allDocs ) )
227+ return 0 ;
228+ uint [ ] array = new uint [ 1 ] ;
229+ uint pceltFetched = 0 ;
230+ while ( VSConstants . S_OK == allDocs . Next ( 1 , array , out pceltFetched ) && ( pceltFetched == 1 ) )
231+ {
232+ uint pgrfRDTFlags ;
233+ uint pdwReadLocks ;
234+ uint pdwEditLocks ;
235+ string pbstrMkDocument ;
236+ IVsHierarchy ppHier ;
237+ uint pitemid ;
238+ IntPtr ppunkDocData ;
239+ rdt . GetDocumentInfo ( array [ 0 ] , out pgrfRDTFlags , out pdwReadLocks , out pdwEditLocks , out pbstrMkDocument , out ppHier , out pitemid , out ppunkDocData ) ;
240+ if ( pbstrMkDocument == docFullName )
241+ return array [ 0 ] ;
242+ }
243+
244+ return 0 ;
245+ }
246+
247+ public void RemoveTrailingWhiteSpaces ( uint docCookie )
248+ {
249+ RunningDocumentInfo runningDocumentInfo = new RunningDocumentInfo ( rdt , docCookie ) ;
250+
251+ IVsHierarchy hierarchy = null ;
252+ uint itemid = 0 ;
253+ IntPtr docDataUnk = IntPtr . Zero ;
254+ uint lockCookie = 0 ;
255+
256+ int hr = rdt . FindAndLockDocument ( ( uint ) _VSRDTFLAGS . RDT_ReadLock , runningDocumentInfo . Moniker , out hierarchy , out itemid , out docDataUnk , out lockCookie ) ;
257+ if ( hr != VSConstants . S_OK || ! ( Marshal . GetUniqueObjectForIUnknown ( docDataUnk ) is IVsTextBuffer vsTextBuffer ) )
258+ return ;
259+
260+ var textBuffer = GettextBufferAt ( vsTextBuffer , componentModel ) ;
245261 ReplaceAll ( textBuffer , GetFinder ( "[^\\ S\\ r\\ n]+(?=\\ r?$)" , "" , textBuffer ) . FindForReplaceAll ( ) ) ;
246262 }
247263
0 commit comments