-
Notifications
You must be signed in to change notification settings - Fork 363
Expand file tree
/
Copy pathJoinLinesCommand.cs
More file actions
95 lines (83 loc) · 3.3 KB
/
JoinLinesCommand.cs
File metadata and controls
95 lines (83 loc) · 3.3 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
using EnvDTE;
using SteveCadwallader.CodeMaid.Helpers;
using SteveCadwallader.CodeMaid.Properties;
using System.Threading.Tasks;
namespace SteveCadwallader.CodeMaid.Integration.Commands
{
/// <summary>
/// A command that provides for joining lines together.
/// </summary>
internal sealed class JoinLinesCommand : BaseCommand
{
private readonly UndoTransactionHelper _undoTransactionHelper;
/// <summary>
/// Initializes a new instance of the <see cref="JoinLinesCommand" /> class.
/// </summary>
/// <param name="package">The hosting package.</param>
internal JoinLinesCommand(CodeMaidPackage package)
: base(package, PackageGuids.GuidCodeMaidMenuSet, PackageIds.CmdIDCodeMaidJoinLines)
{
_undoTransactionHelper = new UndoTransactionHelper(package, Resources.CodeMaidJoin);
}
/// <summary>
/// A singleton instance of this command.
/// </summary>
public static JoinLinesCommand 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 Task InitializeAsync(CodeMaidPackage package)
{
Instance = new JoinLinesCommand(package);
return package.SettingsMonitor.WatchAsync(s => s.Feature_JoinLines, 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)
{
var textSelection = activeTextDocument.Selection;
if (textSelection != null)
{
_undoTransactionHelper.Run(() => JoinText(textSelection));
}
}
}
/// <summary>
/// Joins the text within the specified text selection.
/// </summary>
/// <param name="textSelection">The text selection.</param>
private void JoinText(TextSelection textSelection)
{
// If the selection has no length, try to pick up the next line.
if (textSelection.IsEmpty)
{
textSelection.LineDown(true);
textSelection.EndOfLine(true);
}
const string pattern = @"[ \t]*\r?\n[ \t]*";
const string replacement = @" ";
// Substitute all new lines (and optional surrounding whitespace) with a single space.
TextDocumentHelper.SubstituteAllStringMatches(textSelection, pattern, replacement);
// Move the cursor forward, clearing the selection.
textSelection.CharRight();
}
}
}