-
Notifications
You must be signed in to change notification settings - Fork 48
/
RemoveAllExceptTaskComments.cs
165 lines (139 loc) · 5.73 KB
/
RemoveAllExceptTaskComments.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
namespace CommentRemover;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.Editor;
using Microsoft.VisualStudio.Extensibility;
using Microsoft.VisualStudio.Extensibility.Commands;
using Microsoft.VisualStudio.Extensibility.Shell;
using Microsoft.VisualStudio.Extensibility.VSSdkCompatibility;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Tagging;
using Microsoft.VisualStudio.TextManager.Interop;
[VisualStudioContribution]
internal class RemoveAllExceptTaskComments : CommentRemoverCommand
{
private const string CommandDescription = "%CommentRemover.RemoveAllExceptTaskComments.DisplayName%";
public RemoveAllExceptTaskComments(
TraceSource traceSource,
AsyncServiceProviderInjection<DTE, DTE2> dte,
MefInjection<IBufferTagAggregatorFactoryService> bufferTagAggregatorFactoryService,
MefInjection<IVsEditorAdaptersFactoryService> editorAdaptersFactoryService,
AsyncServiceProviderInjection<SVsTextManager, IVsTextManager> textManager)
: base(traceSource, dte, bufferTagAggregatorFactoryService, editorAdaptersFactoryService, textManager)
{
}
/// <inheritdoc />
public override CommandConfiguration CommandConfiguration => new(CommandDescription)
{
Icon = new(ImageMoniker.KnownValues.TaskList, IconSettings.IconAndText),
EnabledWhen = CommandEnabledWhen,
};
public override async Task ExecuteCommandAsync(IClientContext context, CancellationToken cancellationToken)
{
if (!await this.Extensibility.Shell().ShowPromptAsync("All comments, except tasks, will be removed from the current document. Are you sure?", PromptOptions.OKCancel, cancellationToken))
{
return;
}
using var reporter = await this.Extensibility.Shell().StartProgressReportingAsync("Removing comments", options: new(isWorkCancellable: false), cancellationToken);
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
// Not using context.GetActiveTextViewAsync here because VisualStudio.Extensibility doesn't support classification yet.
var view = await this.GetCurrentTextViewAsync();
var mappingSpans = await this.GetClassificationSpansAsync(view, "comment");
if (!mappingSpans.Any())
{
return;
}
var dte = await this.Dte.GetServiceAsync();
try
{
dte.UndoContext.Open(CommandDescription);
DeleteFromBuffer(view, mappingSpans);
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
{
this.TraceSource.TraceInformation(ex.ToString());
}
finally
{
dte.UndoContext.Close();
}
}
private static void DeleteFromBuffer(IWpfTextView view, IEnumerable<IMappingSpan> mappingSpans)
{
var affectedLines = new List<int>();
RemoveCommentSpansFromBuffer(view, mappingSpans, affectedLines);
RemoveAffectedEmptyLines(view, affectedLines);
}
private static void RemoveCommentSpansFromBuffer(IWpfTextView view, IEnumerable<IMappingSpan> mappingSpans, IList<int> affectedLines)
{
using var edit = view.TextBuffer.CreateEdit();
foreach (var mappingSpan in mappingSpans)
{
var start = mappingSpan.Start.GetPoint(view.TextBuffer, PositionAffinity.Predecessor);
var end = mappingSpan.End.GetPoint(view.TextBuffer, PositionAffinity.Successor);
if (!start.HasValue || !end.HasValue)
{
continue;
}
var span = new Span(start.Value, end.Value - start.Value);
var lines = view.TextBuffer.CurrentSnapshot.Lines.Where(l => l.Extent.IntersectsWith(span));
bool skip = false;
foreach (var line in lines)
{
if (ContainsTaskComment(line))
{
skip = true;
}
else
{
if (!affectedLines.Contains(line.LineNumber))
{
affectedLines.Add(line.LineNumber);
}
}
}
if (skip)
{
continue;
}
var mappingText = view.TextBuffer.CurrentSnapshot.GetText(span.Start, span.Length);
string empty = Regex.Replace(mappingText, "([\\S]+)", string.Empty);
edit.Replace(span.Start, span.Length, empty);
}
edit.Apply();
}
private static void RemoveAffectedEmptyLines(IWpfTextView view, IList<int> affectedLines)
{
if (!affectedLines.Any())
{
return;
}
using var edit = view.TextBuffer.CreateEdit();
foreach (var lineNumber in affectedLines)
{
var line = view.TextBuffer.CurrentSnapshot.GetLineFromLineNumber(lineNumber);
if (IsLineEmpty(line))
{
// Strip next line if empty
if (view.TextBuffer.CurrentSnapshot.LineCount > line.LineNumber + 1)
{
var next = view.TextBuffer.CurrentSnapshot.GetLineFromLineNumber(lineNumber + 1);
if (IsLineEmpty(next))
{
edit.Delete(next.Start, next.LengthIncludingLineBreak);
}
}
edit.Delete(line.Start, line.LengthIncludingLineBreak);
}
}
edit.Apply();
}
}