forked from codecadwallader/codemaid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommentFormatCommand.cs
More file actions
110 lines (97 loc) · 4.14 KB
/
CommentFormatCommand.cs
File metadata and controls
110 lines (97 loc) · 4.14 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
101
102
103
104
105
106
107
108
109
110
using EnvDTE;
using SteveCadwallader.CodeMaid.Helpers;
using SteveCadwallader.CodeMaid.Logic.Formatting;
using SteveCadwallader.CodeMaid.Properties;
using System.Threading.Tasks;
namespace SteveCadwallader.CodeMaid.Integration.Commands
{
/// <summary>
/// A command that provides for formatting the current comment.
/// </summary>
internal sealed class CommentFormatCommand : BaseCommand
{
private readonly CommentFormatLogic _commentFormatLogic;
private readonly UndoTransactionHelper _undoTransactionHelper;
/// <summary>
/// Initializes a new instance of the <see cref="CommentFormatCommand" /> class.
/// </summary>
/// <param name="package">The hosting package.</param>
internal CommentFormatCommand(CodeMaidPackage package)
: base(package, PackageGuids.GuidCodeMaidMenuSet, PackageIds.CmdIDCodeMaidCommentFormat)
{
_undoTransactionHelper = new UndoTransactionHelper(package, Resources.CodeMaidFormatComment);
_commentFormatLogic = CommentFormatLogic.GetInstance(package);
}
/// <summary>
/// A singleton instance of this command.
/// </summary>
public static CommentFormatCommand Instance { get; private set; }
/// <summary>
/// Gets the active text document, otherwise null.
/// </summary>
private TextDocument ActiveTextDocument => Package.ActiveDocument?.GetTextDocument();
/// <summary>
/// Initializes a singleton instance of this command.
/// </summary>
/// <param name="package">The hosting package.</param>
/// <returns>A task.</returns>
public static async Task InitializeAsync(CodeMaidPackage package)
{
Instance = new CommentFormatCommand(package);
await package.SettingsMonitor.WatchAsync(s => s.Feature_CommentFormat, Instance.SwitchAsync);
}
/// <summary>
/// Called to update the current status of the command.
/// </summary>
protected override void OnBeforeQueryStatus()
{
Enabled = ActiveTextDocument != null;
}
/// <summary>
/// Called to execute the command.
/// </summary>
protected override void OnExecute()
{
base.OnExecute();
var activeTextDocument = ActiveTextDocument;
if (activeTextDocument != null && activeTextDocument.Selection != null)
{
var prefix = CodeCommentHelper.GetCommentPrefix(activeTextDocument);
if (prefix != null)
{
var selection = activeTextDocument.Selection;
EditPoint start;
EditPoint end;
if (selection.IsEmpty)
{
start = selection.ActivePoint.CreateEditPoint();
end = start.CreateEditPoint();
end.EndOfLine();
}
else
{
start = selection.TopPoint.CreateEditPoint();
start.StartOfLine();
end = selection.BottomPoint.CreateEditPoint();
end.EndOfLine();
}
bool foundComments = false;
_undoTransactionHelper.Run(() => foundComments = _commentFormatLogic.FormatComments(activeTextDocument, start, end));
if (foundComments)
{
Package.IDE.StatusBar.Text = Resources.CodeMaidFinishedFormattingTheComment;
}
else
{
Package.IDE.StatusBar.Text = string.Format(
foundComments
? Resources.CodeMaidFinishedFormattingTheComments0
: Resources.CodeMaidDidNotFindANonCodeComment0ToReformat,
selection.IsEmpty ? Resources.UnderTheCursor : Resources.InTheSelection
);
}
}
}
}
}
}