@@ -47,12 +47,7 @@ public int OnBeforeSave(uint docCookie)
47
47
{
48
48
if ( _pkg . RemoveOnSave ( ) )
49
49
{
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 ) ;
56
51
}
57
52
return VSConstants . S_OK ;
58
53
}
@@ -118,7 +113,7 @@ public RemoveTrailingWhitespacesPackage()
118
113
/////////////////////////////////////////////////////////////////////////////
119
114
// Overridden Package Implementation
120
115
#region Package Members
121
- public DTE dte ;
116
+ public _DTE dte ;
122
117
public IVsRunningDocumentTable rdt ;
123
118
public IFindService findService ;
124
119
private uint rdtCookie ;
@@ -130,7 +125,7 @@ public RemoveTrailingWhitespacesPackage()
130
125
/// </summary>
131
126
protected override async Task InitializeAsync ( System . Threading . CancellationToken cancellationToken , IProgress < ServiceProgressData > progress )
132
127
{
133
- dte = await GetServiceAsync ( typeof ( EnvDTE . DTE ) ) as EnvDTE . DTE ;
128
+ dte = await GetServiceAsync ( typeof ( _DTE ) ) as _DTE ;
134
129
Assumes . Present ( dte ) ;
135
130
rdt = await GetServiceAsync ( typeof ( SVsRunningDocumentTable ) ) as IVsRunningDocumentTable ;
136
131
Assumes . Present ( rdt ) ;
@@ -186,7 +181,9 @@ private void OnRemoveTrailingWhitespacesPressed(object sender, EventArgs e)
186
181
{
187
182
if ( dte . ActiveDocument == null ) return ;
188
183
if ( ! ( dte . ActiveDocument . Object ( ) is TextDocument textDocument ) ) return ;
189
- RemoveTrailingWhiteSpaces ( textDocument ) ;
184
+
185
+ uint docCookie = GetDocCookie ( dte . ActiveDocument . FullName ) ;
186
+ RemoveTrailingWhiteSpaces ( docCookie ) ;
190
187
}
191
188
192
189
private IFinder GetFinder ( string findWhat , string replacement , ITextBuffer textBuffer )
@@ -196,33 +193,12 @@ private IFinder GetFinder(string findWhat, string replacement, ITextBuffer textB
196
193
return finderFactory . Create ( textBuffer . CurrentSnapshot ) ;
197
194
}
198
195
199
- internal static ITextBuffer GettextBufferAt ( TextDocument textDocument , IComponentModel componentModel , IServiceProvider serviceProvider )
196
+ internal static ITextBuffer GettextBufferAt ( IVsTextBuffer textBuffer , IComponentModel componentModel )
200
197
{
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 ) ;
225
200
}
201
+
226
202
private static void ReplaceAll ( ITextBuffer textBuffer , IEnumerable < FinderReplacement > replacements )
227
203
{
228
204
if ( replacements . Any ( ) )
@@ -239,9 +215,49 @@ private static void ReplaceAll(ITextBuffer textBuffer, IEnumerable<FinderReplace
239
215
}
240
216
}
241
217
242
- public void RemoveTrailingWhiteSpaces ( TextDocument textDocument )
218
+ public uint GetDocCookie ( string docFullName )
243
219
{
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 ) ;
245
261
ReplaceAll ( textBuffer , GetFinder ( "[^\\ S\\ r\\ n]+(?=\\ r?$)" , "" , textBuffer ) . FindForReplaceAll ( ) ) ;
246
262
}
247
263
0 commit comments