forked from microsoft/VSExtensibility
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RemoveTasks.cs
128 lines (108 loc) · 4.51 KB
/
RemoveTasks.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
namespace CommentRemover;
using System.Diagnostics;
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 RemoveTasks : CommentRemoverCommand
{
private const string CommandDescription = "%CommentRemover.RemoveTasks.DisplayName%";
public RemoveTasks(
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.DeleteTaskList, IconSettings.IconAndText),
EnabledWhen = CommandEnabledWhen,
};
public override async Task ExecuteCommandAsync(IClientContext context, CancellationToken cancellationToken)
{
if (!await this.Extensibility.Shell().ShowPromptAsync("All tasks comment 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 the classification feature is not supported by VisualStudio.Extensibility 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);
this.RemoveCommentsFromBuffer(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 void RemoveCommentsFromBuffer(IWpfTextView view, IEnumerable<IMappingSpan> mappingSpans)
{
var affectedSpans = new List<Span>();
var affectedLines = new List<int>();
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 line = view.TextBuffer.CurrentSnapshot.Lines.First(l => l.Extent.IntersectsWith(span));
if (ContainsTaskComment(line))
{
edit.Delete(span);
if (!affectedLines.Contains(line.LineNumber))
{
affectedLines.Add(line.LineNumber);
}
}
}
edit.Apply();
}
using (var edit = view.TextBuffer.CreateEdit())
{
foreach (var lineNumber in affectedLines)
{
var line = view.TextBuffer.CurrentSnapshot.GetLineFromLineNumber(lineNumber);
if (IsLineEmpty(line))
{
edit.Delete(line.Start, line.LengthIncludingLineBreak);
}
}
edit.Apply();
}
}
}