forked from microsoft/VSExtensibility
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RemoveRegions.cs
108 lines (93 loc) · 4.01 KB
/
RemoveRegions.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
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.Editor;
using Microsoft.VisualStudio.Text.Tagging;
using Microsoft.VisualStudio.TextManager.Interop;
[VisualStudioContribution]
internal class RemoveRegions : CommentRemoverCommand
{
private const string CommandDescription = "%CommentRemover.RemoveRegions.DisplayName%";
public RemoveRegions(
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.Custom("DeleteRegions"), IconSettings.IconAndText),
EnabledWhen = CommandEnabledWhen,
};
public override async Task ExecuteCommandAsync(IClientContext context, CancellationToken cancellationToken)
{
if (!await this.Extensibility.Shell().ShowPromptAsync("All regions 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 dte = await this.Dte.GetServiceAsync();
try
{
dte.UndoContext.Open(CommandDescription);
this.RemoveRegionsFromBuffer(view);
}
#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 RemoveRegionsFromBuffer(IWpfTextView view)
{
using var edit = view.TextBuffer.CreateEdit();
foreach (var line in view.TextBuffer.CurrentSnapshot.Lines.Reverse())
{
if (line.Extent.IsEmpty)
{
continue;
}
string text = line.GetText()
.TrimStart('/', '*')
.Replace("<!--", string.Empty)
.TrimStart();
if (text.StartsWith("#region", StringComparison.OrdinalIgnoreCase) ||
text.StartsWith("#endregion", StringComparison.OrdinalIgnoreCase) ||
text.StartsWith("#end region", StringComparison.OrdinalIgnoreCase))
{
// Strip next line if empty
if (view.TextBuffer.CurrentSnapshot.LineCount > line.LineNumber + 1)
{
var next = view.TextBuffer.CurrentSnapshot.GetLineFromLineNumber(line.LineNumber + 1);
if (IsLineEmpty(next))
{
edit.Delete(next.Start, next.LengthIncludingLineBreak);
}
}
edit.Delete(line.Start, line.LengthIncludingLineBreak);
}
}
edit.Apply();
}
}