-
Notifications
You must be signed in to change notification settings - Fork 20
/
MacroCommandFilter.cs
331 lines (287 loc) · 13.5 KB
/
MacroCommandFilter.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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.OLE.Interop;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using VSTextMacros.Dialogs;
using VSTextMacros.Model;
namespace VSTextMacros
{
// This filter is added to every text editor
public class MacroCommandFilter : IOleCommandTarget
{
// A reference to the next IOleCommandTarget in the chain
public IOleCommandTarget Next { get; set; }
// A reference to the manager that can show/hide the visual cue when recording
private MacroAdornmentManager AdornmentManager { get; set; }
// Constructor
public MacroCommandFilter(MacroAdornmentManager adornmentManager)
{
this.AdornmentManager = adornmentManager;
}
// Executes the menu commands and does the recording of other commands
public int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
{
if (pguidCmdGroup == GuidList.guidMacrosCmdSet)
{
// Record/stop
if (nCmdID == PkgCmdIDList.idRecordMacro)
{
if (Macro.CurrentMacro == null || !Macro.CurrentMacro.IsRecording)
StartRecording();
else
StopRecording();
return VSConstants.S_OK;
}
// Playback
if (nCmdID == PkgCmdIDList.idPlaybackMacro)
{
if (Macro.CurrentMacro == null)
{
MessageBox.Show("Can't playback: no macro was recorded");
}
else
{
if (Macro.CurrentMacro.IsRecording)
StopRecording();
Playback(Macro.CurrentMacro);
}
return VSConstants.S_OK;
}
// Playback multiple times
if (nCmdID == PkgCmdIDList.idPlaybackMacroMultipleTimes)
{
if (Macro.CurrentMacro == null)
{
MessageBox.Show("Can't playback: no macro was recorded");
}
else
{
if (Macro.CurrentMacro.IsRecording)
StopRecording();
var dlg = new RepeatMacroMultipleTimesDialog();
dlg.ShowDialog();
Playback(Macro.CurrentMacro, dlg.Times);
}
return VSConstants.S_OK;
}
// Save macro
if (nCmdID == PkgCmdIDList.idSaveMacro)
{
if (Macro.CurrentMacro == null)
{
MessageBox.Show("Can't save macro: no macro was recorded");
}
else
{
if (Macro.CurrentMacro.IsRecording)
StopRecording();
ShowSaveMacroDialog(Macro.CurrentMacro);
}
return VSConstants.S_OK;
}
// Open saved macros
if (nCmdID == PkgCmdIDList.idOpenSavedMacros)
{
var list = SavedMacros.GetMacroList();
var dialog = new SavedMacrosDialog(list);
dialog.ShowDialog();
return VSConstants.S_OK;
}
// Open macros directory
if (nCmdID == PkgCmdIDList.isOpenMacrosDirectory)
{
var directory = VSTextMacrosPackage.Current.MacroDirectory;
Process.Start("explorer.exe", "\"" + directory + "\"");
return VSConstants.S_OK;
}
}
if (pguidCmdGroup == GuidList.guidMacrosRunSavedCmdSet)
{
// Run saved macro #
if (nCmdID >= PkgCmdIDList.idRunSavedMacro1 && nCmdID <= PkgCmdIDList.idRunSavedMacro9)
{
var index = nCmdID - PkgCmdIDList.idRunSavedMacro1;
var macroList = SavedMacros.GetMacroList();
if (index < macroList.Count)
{
var macro = SavedMacros.GetSavedMacro(macroList[(int)index].Guid);
Playback(macro);
}
return VSConstants.S_OK;
}
}
// Are we recording?
if (Macro.CurrentMacro != null && Macro.CurrentMacro.IsRecording)
{
// Recordable command?
if (RecordableCommands.Commands.ContainsKey(pguidCmdGroup) && RecordableCommands.Commands[pguidCmdGroup].Contains(nCmdID))
{
#if DEBUG
Trace.WriteLine(string.Format("Recorded - Guid: {0}, ID: {1}, Opts: {2}, In: {3}", pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn != IntPtr.Zero ? "yes" : "no"));
#endif
// For the TYPECHAR command, read the actual character code
if (pguidCmdGroup == VSConstants.VSStd2K && nCmdID == (uint)VSConstants.VSStd2KCmdID.TYPECHAR)
Macro.CurrentMacro.AddCommand(pguidCmdGroup, nCmdID, nCmdexecopt, GetTypedChar(pvaIn));
else
Macro.CurrentMacro.AddCommand(pguidCmdGroup, nCmdID, nCmdexecopt, null);
}
#if DEBUG
else
{
Trace.WriteLine(string.Format("Not recorded - Guid: {0}, ID: {1}, Opts: {2}, In: {3}", pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn != IntPtr.Zero ? "yes" : "no"));
}
#endif
}
return Next.Exec(ref pguidCmdGroup, nCmdID, nCmdexecopt, pvaIn, pvaOut);
}
// Updates menus states
public int QueryStatus(ref Guid pguidCmdGroup, uint cCmds, OLECMD[] prgCmds, IntPtr pCmdText)
{
if (pguidCmdGroup == GuidList.guidMacrosCmdSet)
{
// Arguments check
if (prgCmds == null)
return VSConstants.E_POINTER;
for (int i = 0; i < cCmds; i++)
{
// Updates the text of the 'Start/Stop Recording' menu item
if (prgCmds[i].cmdID == PkgCmdIDList.idRecordMacro)
{
// Copy the text to the OLECMDTEXT structure
if (pCmdText != IntPtr.Zero)
{
var cmdText = (OLECMDTEXT)Marshal.PtrToStructure(pCmdText, typeof(OLECMDTEXT));
if (cmdText.cmdtextf == (uint)OLECMDTEXTF.OLECMDTEXTF_NAME)
{
var menuText = Macro.CurrentMacro == null || !Macro.CurrentMacro.IsRecording ?
"&Start recording macro" :
"&Stop recording macro";
SetText(pCmdText, menuText);
}
}
// Enable the menu
prgCmds[i].cmdf = (uint)(OLECMDF.OLECMDF_SUPPORTED | OLECMDF.OLECMDF_ENABLED);
}
// Enables/disables the 'Playback' and 'Save Macro' menu items
if (prgCmds[i].cmdID == PkgCmdIDList.idPlaybackMacro || prgCmds[i].cmdID == PkgCmdIDList.idPlaybackMacroMultipleTimes || prgCmds[i].cmdID == PkgCmdIDList.idSaveMacro)
{
if (Macro.CurrentMacro == null)
prgCmds[i].cmdf = (uint)(OLECMDF.OLECMDF_SUPPORTED);
else
prgCmds[i].cmdf = (uint)(OLECMDF.OLECMDF_SUPPORTED | OLECMDF.OLECMDF_ENABLED);
}
}
return VSConstants.S_OK;
}
if (pguidCmdGroup == GuidList.guidMacrosRunSavedCmdSet)
{
// Arguments check
if (prgCmds == null)
return VSConstants.E_POINTER;
for (int i = 0; i < cCmds; i++)
{
if (prgCmds[i].cmdID >= PkgCmdIDList.idRunSavedMacro1 && prgCmds[i].cmdID <= PkgCmdIDList.idRunSavedMacro9)
{
var index = prgCmds[i].cmdID - PkgCmdIDList.idRunSavedMacro1;
var macros = SavedMacros.GetMacroList();
var enabled = index < macros.Count;
if (pCmdText != IntPtr.Zero)
{
var cmdText = (OLECMDTEXT)Marshal.PtrToStructure(pCmdText, typeof(OLECMDTEXT));
if (cmdText.cmdtextf == (uint)OLECMDTEXTF.OLECMDTEXTF_NAME)
{
var menuText = enabled ?
$"Run Saved Macro &{index + 1}: {macros[(int)index].Name}" :
$"Run Saved Macro &{index + 1}";
SetText(pCmdText, menuText);
}
}
if (enabled)
prgCmds[i].cmdf = (uint)(OLECMDF.OLECMDF_SUPPORTED | OLECMDF.OLECMDF_ENABLED);
else
prgCmds[i].cmdf = (uint)(OLECMDF.OLECMDF_SUPPORTED);
}
}
return VSConstants.S_OK;
}
return Next.QueryStatus(ref pguidCmdGroup, cCmds, prgCmds, pCmdText);
}
// Starts recording a macro
private void StartRecording()
{
Macro.StartNew();
AdornmentManager.ShowVisual();
}
// Stops recording a macro
private void StopRecording()
{
Macro.CurrentMacro.StopRecording();
Macro.SaveToFile(Macro.CurrentMacro, Path.Combine(VSTextMacrosPackage.Current.MacroDirectory, "Current.xml"));
AdornmentManager.HideVisual();
}
// Replays a macro
private void Playback(Macro macro, int times = 1)
{
var ownUndoContext = !VSTextMacrosPackage.Current.DTE.UndoContext.IsOpen;
if (ownUndoContext)
VSTextMacrosPackage.Current.DTE.UndoContext.Open("Macro");
var pvaIn = Marshal.AllocCoTaskMem(16);
try
{
for (int i = 0; i < times; i++)
{
foreach (var command in macro.Commands)
{
var pguidCmdGroup = command.CommandGroup;
if (command.Character != null)
{
Marshal.GetNativeVariantForObject((ushort)command.Character, pvaIn);
Next.Exec(ref pguidCmdGroup, command.CommandID, command.CommandOptions, pvaIn, IntPtr.Zero);
continue;
}
var cmdSet = RecordableCommands.Commands[pguidCmdGroup];
if (cmdSet != null && cmdSet.TryGetValue(command.CommandID, out RecordableCommands.Command cmd) && !String.IsNullOrEmpty(cmd.Cmd))
VSTextMacrosPackage.Current.DTE.ExecuteCommand(cmd.Cmd);
else
Next.Exec(ref pguidCmdGroup, command.CommandID, command.CommandOptions, IntPtr.Zero, IntPtr.Zero);
}
}
}
finally
{
if (pvaIn != IntPtr.Zero)
Marshal.FreeCoTaskMem(pvaIn);
}
if (ownUndoContext)
VSTextMacrosPackage.Current.DTE.UndoContext.Close();
}
// Shows the save macro dialog
private void ShowSaveMacroDialog(Macro macro, string name = null)
{
var dialog = new SaveMacroDialog(name);
if (dialog.ShowDialog() == DialogResult.OK)
SavedMacros.SaveMacro(macro, dialog.MacroName);
}
// Gets the character at the given pointer
private static char GetTypedChar(IntPtr pvaIn)
{
return (char)(ushort)Marshal.GetObjectForNativeVariant(pvaIn);
}
// Copies text to a OLECMDTEXT structure
private static void SetText(IntPtr pCmdText, string text)
{
var cmdText = (OLECMDTEXT)Marshal.PtrToStructure(pCmdText, typeof(OLECMDTEXT));
var offsetRgwz = Marshal.OffsetOf(typeof(OLECMDTEXT), "rgwz");
var offsetCwActual = Marshal.OffsetOf(typeof(OLECMDTEXT), "cwActual");
var bytes = text.ToCharArray();
var maxBytes = Math.Min((int)cmdText.cwBuf - 1, bytes.Length);
Marshal.Copy(bytes, 0, (IntPtr)((long)pCmdText + (long)offsetRgwz), maxBytes);
Marshal.WriteInt16((IntPtr)((long)pCmdText + (long)offsetRgwz + maxBytes * 2), 0);
Marshal.WriteInt32((IntPtr)((long)pCmdText + (long)offsetCwActual), maxBytes + 1);
}
}
}