forked from microsoft/VSExtensibility
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoveXmlDocComments.cs
111 lines (94 loc) · 4.07 KB
/
RemoveXmlDocComments.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
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 RemoveXmlDocComments : CommentRemoverCommand
{
private const string CommandDescription = "%CommentRemover.RemoveXmlDocComments.DisplayName%";
public RemoveXmlDocComments(
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.ClearDictionary, IconSettings.IconAndText),
EnabledWhen = CommandEnabledWhen,
};
public override async Task ExecuteCommandAsync(IClientContext context, CancellationToken cancellationToken)
{
if (!await this.Extensibility.Shell().ShowPromptAsync("All Xml Docs comments 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);
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 affectedLines = new List<int>();
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 (!affectedLines.Contains(line.LineNumber))
{
affectedLines.Add(line.LineNumber);
}
}
using var edit = view.TextBuffer.CreateEdit();
foreach (var lineNumber in affectedLines)
{
var line = view.TextBuffer.CurrentSnapshot.GetLineFromLineNumber(lineNumber);
edit.Delete(line.Start, line.LengthIncludingLineBreak);
}
edit.Apply();
}
}