-
Notifications
You must be signed in to change notification settings - Fork 12
/
CommentSelection.cs
74 lines (63 loc) · 2.58 KB
/
CommentSelection.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
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Editor;
using Microsoft.VisualStudio.TextManager.Interop;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VSCommandIdConstants = Microsoft.VisualStudio.VSConstants.VSStd2KCmdID;
using SassyStudio.Commands;
namespace SassyStudio.Scss.Commands
{
class CommentSelection : CommandTargetBase
{
readonly ITextBuffer Buffer;
public CommentSelection(IVsTextView adapter, IWpfTextView textView)
: base(adapter, textView, typeof(VSCommandIdConstants).GUID, (uint)VSCommandIdConstants.COMMENTBLOCK, (uint)VSCommandIdConstants.COMMENT_BLOCK, (uint)VSCommandIdConstants.UNCOMMENT_BLOCK, (uint)VSCommandIdConstants.UNCOMMENTBLOCK)
{
Buffer = textView.TextBuffer;
}
protected override bool IsEnabled()
{
return true;
}
protected override bool Execute(uint commandId, uint execOptions, IntPtr pvaIn, IntPtr pvaOut)
{
if (TextView.Selection.IsEmpty) return false;
var snapshot = Buffer.CurrentSnapshot;
int start = TextView.Selection.Start.Position.Position;
int end = TextView.Selection.End.Position.Position;
using (var edit = Buffer.CreateEdit())
{
while (start < end)
{
var line = snapshot.GetLineFromPosition(start);
var text = line.GetText();
switch (commandId)
{
case (uint)VSCommandIdConstants.COMMENTBLOCK:
case (uint)VSCommandIdConstants.COMMENT_BLOCK:
{
if (!string.IsNullOrEmpty(text))
edit.Insert(line.Start.Position, "//");
break;
}
case (uint)VSCommandIdConstants.UNCOMMENTBLOCK:
case (uint)VSCommandIdConstants.UNCOMMENT_BLOCK:
{
if (text.StartsWith("//", StringComparison.OrdinalIgnoreCase))
edit.Delete(line.Start.Position, 2);
break;
}
default: break;
}
start = line.EndIncludingLineBreak.Position;
}
edit.Apply();
}
return true;
}
}
}