-
Notifications
You must be signed in to change notification settings - Fork 364
/
Copy pathTextDocumentHelper.cs
474 lines (413 loc) · 18.8 KB
/
TextDocumentHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
using EnvDTE;
using Microsoft.VisualStudio.Editor;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Operations;
using Microsoft.VisualStudio.TextManager.Interop;
using SteveCadwallader.CodeMaid.Model.CodeItems;
using System;
using System.Collections.Generic;
using System.Linq;
namespace SteveCadwallader.CodeMaid.Helpers
{
/// <summary>
/// A static helper class for working with text documents.
/// </summary>
/// <remarks>
///
/// Note: All POSIXRegEx text replacements search against '\n' but insert/replace with
/// Environment.NewLine. This handles line endings correctly.
/// </remarks>
internal static class TextDocumentHelper
{
#region Internal Constants
/// <summary>
/// The common set of options to be used for find and replace patterns.
/// </summary>
internal const FindOptions StandardFindOptions = FindOptions.UseRegularExpressions;
#endregion Internal Constants
#region Internal Methods
/// <summary>
/// Finds all matches of the specified pattern within the specified text document.
/// </summary>
/// <param name="textDocument">The text document.</param>
/// <param name="patternString">The pattern string.</param>
/// <returns>The set of matches.</returns>
internal static IEnumerable<EditPoint> FindMatches(TextDocument textDocument, string patternString)
{
var matches = new List<EditPoint>();
UIThread.Run(() =>
{
ThreadHelper.ThrowIfNotOnUIThread();
if (TryGetTextBufferAt(textDocument.Parent.FullName, out ITextBuffer textBuffer))
{
IFinder finder = GetFinder(patternString, textBuffer);
var findMatches = finder.FindAll();
foreach (var match in findMatches)
{
matches.Add(GetEditPointForSnapshotPosition(textDocument, textBuffer.CurrentSnapshot, match.Start));
}
}
});
return matches;
}
/// <summary>
/// Finds all matches of the specified pattern within the specified text selection.
/// </summary>
/// <param name="textSelection">The text selection.</param>
/// <param name="patternString">The pattern string.</param>
/// <returns>The set of matches.</returns>
internal static IEnumerable<EditPoint> FindMatches(TextSelection textSelection, string patternString)
{
var matches = new List<EditPoint>();
UIThread.Run(() =>
{
ThreadHelper.ThrowIfNotOnUIThread();
if (TryGetTextBufferAt(textSelection.Parent.Parent.FullName, out ITextBuffer textBuffer))
{
IFinder finder = GetFinder(patternString, textBuffer);
var findMatches = finder.FindAll(GetSnapshotSpanForTextSelection(textBuffer.CurrentSnapshot, textSelection));
foreach (var match in findMatches)
{
matches.Add(GetEditPointForSnapshotPosition(textSelection.Parent, textBuffer.CurrentSnapshot, match.Start));
}
}
});
return matches;
}
/// <summary>
/// Finds the first match of the specified pattern within the specified text document, otherwise null.
/// </summary>
/// <param name="textDocument">The text document.</param>
/// <param name="patternString">The pattern string.</param>
/// <returns>The first match, otherwise null.</returns>
internal static EditPoint FirstOrDefaultMatch(TextDocument textDocument, string patternString)
{
EditPoint result = null;
UIThread.Run(() =>
{
ThreadHelper.ThrowIfNotOnUIThread();
if (TryGetTextBufferAt(textDocument.Parent.FullName, out ITextBuffer textBuffer))
{
IFinder finder = GetFinder(patternString, textBuffer);
if (finder.TryFind(out Span match))
{
result = GetEditPointForSnapshotPosition(textDocument, textBuffer.CurrentSnapshot, match.Start);
}
}
});
return result;
}
/// <summary>
/// Attempts to find next match starting with <paramref name="startPoint"/> and if sucessful,
/// updates <paramref name="endPoint"/> to point to the end position of the match.
/// </summary>
/// <param name="startPoint"></param>
/// <param name="endPoint"></param>
/// <param name="patternString"></param>
/// <returns>True if successful, false if no match found.</returns>
internal static bool TryFindNextMatch(EditPoint startPoint, ref EditPoint endPoint, string patternString)
{
bool result = false;
EditPoint resultEndPoint = null;
UIThread.Run(() =>
{
ThreadHelper.ThrowIfNotOnUIThread();
if (TryGetTextBufferAt(startPoint.Parent.Parent.FullName, out ITextBuffer textBuffer))
{
IFinder finder = GetFinder(patternString, textBuffer);
if (finder.TryFind(GetSnapshotPositionForTextPoint(textBuffer.CurrentSnapshot, startPoint), out Span match))
{
resultEndPoint = GetEditPointForSnapshotPosition(startPoint.Parent, textBuffer.CurrentSnapshot, match.End);
result = true;
}
}
});
endPoint = resultEndPoint;
return result;
}
/// <summary>
/// Gets the text between the specified start point and the first match.
/// </summary>
/// <param name="startPoint">The start point.</param>
/// <param name="matchString">The match string.</param>
/// <returns>The matching text, otherwise null.</returns>
internal static string GetTextToFirstMatch(TextPoint startPoint, string matchString)
{
string result = null;
UIThread.Run(() =>
{
ThreadHelper.ThrowIfNotOnUIThread();
if (TryGetTextBufferAt(startPoint.Parent.Parent.FullName, out ITextBuffer textBuffer))
{
IFinder finder = GetFinder(matchString, textBuffer);
if (finder.TryFind(GetSnapshotPositionForTextPoint(textBuffer.CurrentSnapshot, startPoint), out Span match))
{
result = textBuffer.CurrentSnapshot.GetText(startPoint.AbsoluteCharOffset, match.Start - startPoint.AbsoluteCharOffset);
}
}
});
return result;
}
/// <summary>
/// Inserts a blank line before the specified point except where adjacent to a brace.
/// </summary>
/// <param name="point">The point.</param>
internal static void InsertBlankLineBeforePoint(EditPoint point)
{
if (point.Line <= 1) return;
point.LineUp(1);
point.StartOfLine();
string text = point.GetLine();
if (RegexNullSafe.IsMatch(text, @"^\s*[^\s\{]")) // If it is not a scope boundary, insert newline.
{
point.EndOfLine();
point.Insert(Environment.NewLine);
}
}
/// <summary>
/// Inserts a blank line after the specified point except where adjacent to a brace.
/// </summary>
/// <param name="point">The point.</param>
internal static void InsertBlankLineAfterPoint(EditPoint point)
{
if (point.AtEndOfDocument) return;
point.LineDown(1);
point.StartOfLine();
string text = point.GetLine();
if (RegexNullSafe.IsMatch(text, @"^\s*[^\s\}]"))
{
point.Insert(Environment.NewLine);
}
}
/// <summary>
/// Attempts to move the cursor to the specified code item.
/// </summary>
/// <param name="document">The document.</param>
/// <param name="codeItem">The code item.</param>
/// <param name="centerOnWhole">True if the whole element should be used for centering.</param>
internal static void MoveToCodeItem(Document document, BaseCodeItem codeItem, bool centerOnWhole)
{
var textDocument = document.GetTextDocument();
if (textDocument == null) return;
try
{
object viewRangeEnd = null;
TextPoint navigatePoint = null;
codeItem.RefreshCachedPositionAndName();
textDocument.Selection.MoveToPoint(codeItem.StartPoint, false);
if (centerOnWhole)
{
viewRangeEnd = codeItem.EndPoint;
}
var codeItemElement = codeItem as BaseCodeItemElement;
if (codeItemElement != null)
{
navigatePoint = codeItemElement.CodeElement.GetStartPoint(vsCMPart.vsCMPartNavigate);
}
textDocument.Selection.AnchorPoint.TryToShow(vsPaneShowHow.vsPaneShowCentered, viewRangeEnd);
if (navigatePoint != null)
{
textDocument.Selection.MoveToPoint(navigatePoint, false);
}
else
{
textDocument.Selection.FindText(codeItem.Name, (int)vsFindOptions.vsFindOptionsMatchInHiddenText);
textDocument.Selection.MoveToPoint(textDocument.Selection.AnchorPoint, false);
}
}
catch (Exception)
{
// Move operation may fail if element is no longer available.
}
finally
{
// Always set focus within the code editor window.
document.Activate();
}
}
/// <summary>
/// Attempts to select the text of the specified code item.
/// </summary>
/// <param name="document">The document.</param>
/// <param name="codeItem">The code item.</param>
internal static void SelectCodeItem(Document document, BaseCodeItem codeItem)
{
var textDocument = document.GetTextDocument();
if (textDocument == null) return;
try
{
codeItem.RefreshCachedPositionAndName();
textDocument.Selection.MoveToPoint(codeItem.StartPoint, false);
textDocument.Selection.MoveToPoint(codeItem.EndPoint, true);
textDocument.Selection.SwapAnchor();
}
catch (Exception)
{
// Select operation may fail if element is no longer available.
}
finally
{
// Always set focus within the code editor window.
document.Activate();
}
}
/// <summary>
/// Substitutes all occurrences in the specified text document of the specified pattern
/// string with the specified replacement string.
/// </summary>
/// <param name="textDocument">The text document.</param>
/// <param name="patternString">The pattern string.</param>
/// <param name="replacementString">The replacement string.</param>
internal static void SubstituteAllStringMatches(TextDocument textDocument, string patternString, string replacementString)
{
UIThread.Run(() =>
{
ThreadHelper.ThrowIfNotOnUIThread();
if (TryGetTextBufferAt(textDocument.Parent.FullName, out ITextBuffer textBuffer))
{
IFinder finder = GetFinder(patternString, replacementString, textBuffer);
ReplaceAll(textBuffer, finder.FindForReplaceAll());
}
});
}
/// <summary>
/// Substitutes all occurrences in the specified text selection of the specified pattern
/// string with the specified replacement string.
/// </summary>
/// <param name="textSelection">The text selection.</param>
/// <param name="patternString">The pattern string.</param>
/// <param name="replacementString">The replacement string.</param>
internal static void SubstituteAllStringMatches(TextSelection textSelection, string patternString, string replacementString)
{
UIThread.Run(() =>
{
ThreadHelper.ThrowIfNotOnUIThread();
if (TryGetTextBufferAt(textSelection.Parent.Parent.FullName, out ITextBuffer textBuffer))
{
IFinder finder = GetFinder(patternString, replacementString, textBuffer);
ReplaceAll(textBuffer, finder.FindForReplaceAll(GetSnapshotSpanForTextSelection(textBuffer.CurrentSnapshot, textSelection)));
}
});
}
/// <summary>
/// Substitutes all occurrences between the specified start and end points of the specified
/// pattern string with the specified replacement string.
/// </summary>
/// <param name="startPoint">The start point.</param>
/// <param name="endPoint">The end point.</param>
/// <param name="patternString">The pattern string.</param>
/// <param name="replacementString">The replacement string.</param>
internal static void SubstituteAllStringMatches(EditPoint startPoint, EditPoint endPoint, string patternString, string replacementString)
{
UIThread.Run(() =>
{
ThreadHelper.ThrowIfNotOnUIThread();
if (TryGetTextBufferAt(startPoint.Parent.Parent.FullName, out ITextBuffer textBuffer))
{
IFinder finder = GetFinder(patternString, replacementString, textBuffer);
ReplaceAll(textBuffer, finder.FindForReplaceAll(GetSnapshotSpanForExtent(textBuffer.CurrentSnapshot, startPoint, endPoint)));
}
});
}
#endregion Internal Methods
#region Private Methods
private static EditPoint GetEditPointForSnapshotPosition(TextDocument textDocument, ITextSnapshot textSnapshot, int position)
{
ThreadHelper.ThrowIfNotOnUIThread();
var editPoint = textDocument.CreateEditPoint();
var textSnapshotLine = textSnapshot.GetLineFromPosition(position);
editPoint.MoveToLineAndOffset(textSnapshotLine.LineNumber + 1, position - textSnapshotLine.Start.Position + 1);
return editPoint;
}
private static IFinder GetFinder(string findWhat, ITextBuffer textBuffer)
{
var findService = CodeMaidPackage.Instance.ComponentModel.GetService<IFindService>();
var finderFactory = findService.CreateFinderFactory(findWhat, StandardFindOptions);
return finderFactory.Create(textBuffer.CurrentSnapshot);
}
private static IFinder GetFinder(string findWhat, string replaceWith, ITextBuffer textBuffer)
{
var findService = CodeMaidPackage.Instance.ComponentModel.GetService<IFindService>();
var finderFactory = findService.CreateFinderFactory(findWhat, replaceWith, StandardFindOptions);
return finderFactory.Create(textBuffer.CurrentSnapshot);
}
private static Span GetSnapshotSpanForTextSelection(ITextSnapshot textSnapshot, TextSelection selection)
{
ThreadHelper.ThrowIfNotOnUIThread();
var startPosition = GetSnapshotPositionForTextPoint(textSnapshot, selection.AnchorPoint);
var endPosition = GetSnapshotPositionForTextPoint(textSnapshot, selection.ActivePoint);
if (startPosition <= endPosition)
{
return new Span(startPosition, endPosition - startPosition);
}
else
{
return new Span(endPosition, startPosition - endPosition);
}
}
private static int GetSnapshotPositionForTextPoint(ITextSnapshot textSnapshot, TextPoint textPoint)
{
ThreadHelper.ThrowIfNotOnUIThread();
var textSnapshotLine = textSnapshot.GetLineFromLineNumber(textPoint.Line - 1);
return textSnapshotLine.Start.Position + textPoint.LineCharOffset - 1;
}
private static Span GetSnapshotSpanForExtent(ITextSnapshot textSnapshot, EditPoint startPoint, EditPoint endPoint)
{
ThreadHelper.ThrowIfNotOnUIThread();
var startPosition = GetSnapshotPositionForTextPoint(textSnapshot, startPoint);
var endPosition = GetSnapshotPositionForTextPoint(textSnapshot, endPoint);
if (startPosition <= endPosition)
{
return new Span(startPosition, endPosition - startPosition);
}
else
{
return new Span(endPosition, startPosition - endPosition);
}
}
private static void ReplaceAll(ITextBuffer textBuffer, IEnumerable<FinderReplacement> replacements)
{
if (replacements.Any())
{
using (var edit = textBuffer.CreateEdit())
{
foreach (var match in replacements)
{
edit.Replace(match.Match, match.Replace);
}
edit.Apply();
}
}
}
private static bool TryGetTextBufferAt(string filePath, out ITextBuffer textBuffer)
{
IVsWindowFrame windowFrame;
if (VsShellUtilities.IsDocumentOpen(
CodeMaidPackage.Instance,
filePath,
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 = CodeMaidPackage.Instance.ComponentModel.GetService<IVsEditorAdaptersFactoryService>();
textBuffer = editorAdapterFactoryService.GetDataBuffer(buffer);
return true;
}
}
}
textBuffer = null;
return false;
}
#endregion Private Methods
}
}