-
Notifications
You must be signed in to change notification settings - Fork 363
Expand file tree
/
Copy pathSpadeContextDeleteCommand.cs
More file actions
100 lines (87 loc) · 3.55 KB
/
SpadeContextDeleteCommand.cs
File metadata and controls
100 lines (87 loc) · 3.55 KB
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
using EnvDTE;
using SteveCadwallader.CodeMaid.Helpers;
using SteveCadwallader.CodeMaid.Model.CodeItems;
using SteveCadwallader.CodeMaid.Properties;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace SteveCadwallader.CodeMaid.Integration.Commands
{
/// <summary>
/// A command that provides for deleting a member within Spade.
/// </summary>
internal sealed class SpadeContextDeleteCommand : BaseCommand
{
private readonly UndoTransactionHelper _undoTransactionHelper;
/// <summary>
/// Initializes a new instance of the <see cref="SpadeContextDeleteCommand" /> class.
/// </summary>
/// <param name="package">The hosting package.</param>
internal SpadeContextDeleteCommand(CodeMaidPackage package)
: base(package, PackageGuids.GuidCodeMaidMenuSet, PackageIds.CmdIDCodeMaidSpadeContextDelete)
{
_undoTransactionHelper = new UndoTransactionHelper(package, Resources.CodeMaidDeleteItems);
}
/// <summary>
/// A singleton instance of this command.
/// </summary>
public static SpadeContextDeleteCommand Instance { get; private set; }
/// <summary>
/// Initializes a singleton instance of this command.
/// </summary>
/// <param name="package">The hosting package.</param>
/// <returns>A task.</returns>
public static Task InitializeAsync(CodeMaidPackage package)
{
Instance = new SpadeContextDeleteCommand(package);
return Instance.SwitchAsync(on: true);
}
/// <summary>
/// Called to update the current status of the command.
/// </summary>
protected override void OnBeforeQueryStatus()
{
bool visible = false;
var spade = Package.Spade;
if (spade != null)
{
visible = spade.SelectedItems.Any(IsDeletable);
}
Visible = visible;
}
/// <summary>
/// Called to execute the command.
/// </summary>
protected override void OnExecute()
{
base.OnExecute();
var spade = Package.Spade;
if (spade != null)
{
// Delay the check of start/end points until execution time, to avoid an intermediate state issue.
var items = spade.SelectedItems.Where(IsDeletable).Where(x => x.StartPoint != null && x.EndPoint != null);
_undoTransactionHelper.Run(() =>
{
// Iterate through items in reverse order (reduces line number updates during removal).
foreach (var item in items.OrderByDescending(x => x.StartLine))
{
var start = item.StartPoint.CreateEditPoint();
start.Delete(item.EndPoint);
start.DeleteWhitespace(vsWhitespaceOptions.vsWhitespaceOptionsVertical);
start.Insert(Environment.NewLine);
}
});
spade.Refresh();
}
}
/// <summary>
/// Determines if the specified item is a candidate for deletion.
/// </summary>
/// <param name="codeItem">The code item.</param>
/// <returns>True if the code item can be deleted, otherwise false.</returns>
private static bool IsDeletable(BaseCodeItem codeItem)
{
return !(codeItem is CodeItemRegion) || !((CodeItemRegion)codeItem).IsPseudoGroup;
}
}
}