forked from microsoft/VSExtensibility
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommentRemoverCommand.cs
130 lines (106 loc) · 4.85 KB
/
CommentRemoverCommand.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
namespace CommentRemover;
using System.Diagnostics;
using System.Text.RegularExpressions;
using EnvDTE;
using EnvDTE80;
using Microsoft;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Editor;
using Microsoft.VisualStudio.Extensibility;
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;
using Microsoft.VisualStudio.Threading;
internal abstract class CommentRemoverCommand : Microsoft.VisualStudio.Extensibility.Commands.Command
{
protected static readonly ActivationConstraint CommandEnabledWhen = ActivationConstraint.ClientContext(ClientContextKey.Shell.ActiveSelectionFileName, @"\.(cs|vb|fs)$");
private static readonly string[] TaskCaptions = { "TODO", "HACK", "UNDONE", "UNRESOLVEDMERGECONFLICT" };
public CommentRemoverCommand(
TraceSource traceSource,
AsyncServiceProviderInjection<DTE, DTE2> dte,
MefInjection<IBufferTagAggregatorFactoryService> bufferTagAggregatorFactoryService,
MefInjection<IVsEditorAdaptersFactoryService> editorAdaptersFactoryService,
AsyncServiceProviderInjection<SVsTextManager, IVsTextManager> textManager)
{
this.TraceSource = traceSource;
this.Dte = dte;
this.BufferTagAggregatorFactoryService = bufferTagAggregatorFactoryService;
this.EditorAdaptersFactoryService = editorAdaptersFactoryService;
this.TextManager = textManager;
}
protected AsyncServiceProviderInjection<DTE, DTE2> Dte { get; private set; }
protected MefInjection<IBufferTagAggregatorFactoryService> BufferTagAggregatorFactoryService { get; private set; }
protected MefInjection<IVsEditorAdaptersFactoryService> EditorAdaptersFactoryService { get; private set; }
protected AsyncServiceProviderInjection<SVsTextManager, IVsTextManager> TextManager { get; private set; }
protected TraceSource TraceSource { get; private set; }
protected static bool IsLineEmpty(ITextSnapshotLine line)
{
var text = line.GetText().Trim();
return string.IsNullOrWhiteSpace(text)
|| text == "<!--"
|| text == "-->"
|| text == "<%%>"
|| text == "<%"
|| text == "%>"
|| Regex.IsMatch(text, @"<!--(\s+)?-->");
}
protected static bool IsXmlDocComment(ITextSnapshotLine line)
{
var text = line.GetText().Trim();
Microsoft.VisualStudio.Utilities.IContentType contentType = line.Snapshot.TextBuffer.ContentType;
if (contentType.IsOfType("CSharp") && text.StartsWith("///", StringComparison.Ordinal))
{
return true;
}
if (contentType.IsOfType("FSharp") && text.StartsWith("///", StringComparison.Ordinal))
{
return true;
}
if (contentType.IsOfType("Basic") && text.StartsWith("'''", StringComparison.Ordinal))
{
return true;
}
return false;
}
protected static bool ContainsTaskComment(ITextSnapshotLine line)
{
string text = line.GetText().ToUpperInvariant();
foreach (var task in TaskCaptions)
{
if (text.Contains(task + ":"))
{
return true;
}
}
return false;
}
protected async Task<IEnumerable<IMappingSpan>> GetClassificationSpansAsync(IWpfTextView view, string classificationName)
{
if (view is null)
{
return Enumerable.Empty<IMappingSpan>();
}
IBufferTagAggregatorFactoryService bufferTagAggregatorFactoryService = await this.BufferTagAggregatorFactoryService.GetServiceAsync();
ITagAggregator<IClassificationTag> classifier = bufferTagAggregatorFactoryService.CreateTagAggregator<IClassificationTag>(view.TextBuffer);
var snapshot = new SnapshotSpan(view.TextBuffer.CurrentSnapshot, 0, view.TextBuffer.CurrentSnapshot.Length);
return from s in classifier.GetTags(snapshot).Reverse()
where s.Tag.ClassificationType.Classification.IndexOf(classificationName, StringComparison.OrdinalIgnoreCase) > -1
select s.Span;
}
protected async Task<IWpfTextView> GetCurrentTextViewAsync()
{
IVsEditorAdaptersFactoryService editorAdapter = await this.EditorAdaptersFactoryService.GetServiceAsync();
var view = editorAdapter.GetWpfTextView(await this.GetCurrentNativeTextViewAsync());
Assumes.Present(view);
return view;
}
protected async Task<IVsTextView> GetCurrentNativeTextViewAsync()
{
var textManager = await this.TextManager.GetServiceAsync();
ErrorHandler.ThrowOnFailure(textManager.GetActiveView(1, null, out IVsTextView activeView));
return activeView;
}
}