-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCommentCommand.cs
52 lines (44 loc) · 1.95 KB
/
CommentCommand.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
using System;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.Linq;
using Deveroom.VisualStudio.Editor.Commands.Infrastructure;
using Deveroom.VisualStudio.Monitoring;
using Deveroom.VisualStudio.ProjectSystem;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.Text.Tagging;
namespace Deveroom.VisualStudio.Editor.Commands
{
[Export(typeof(IDeveroomFeatureEditorCommand))]
public class CommentCommand : DeveroomEditorCommandBase, IDeveroomFeatureEditorCommand
{
public override DeveroomEditorCommandTargetKey[] Targets => new[]
{
new DeveroomEditorCommandTargetKey(VSConstants.VSStd2K, VSConstants.VSStd2KCmdID.COMMENTBLOCK),
new DeveroomEditorCommandTargetKey(VSConstants.VSStd2K, VSConstants.VSStd2KCmdID.COMMENT_BLOCK)
};
[ImportingConstructor]
public CommentCommand(IIdeScope ideScope, IBufferTagAggregatorFactoryService aggregatorFactory, IMonitoringService monitoringService) : base(ideScope, aggregatorFactory, monitoringService)
{
}
public override bool PreExec(IWpfTextView textView, DeveroomEditorCommandTargetKey commandKey, IntPtr inArgs = default(IntPtr))
{
MonitoringService.MonitorCommandCommentUncomment();
var selectionSpan = GetSelectionSpan(textView);
var lines = GetSpanFullLines(selectionSpan).ToArray();
Debug.Assert(lines.Length > 0);
int indent = lines.Min(l => l.GetText().TakeWhile(char.IsWhiteSpace).Count());
using (var textEdit = selectionSpan.Snapshot.TextBuffer.CreateEdit())
{
foreach (var line in lines)
{
textEdit.Insert(line.Start.Position + indent, "#");
}
textEdit.Apply();
}
SetSelectionToChangedLines(textView, lines);
return true;
}
}
}