-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathClipboardDiffPackage.cs
247 lines (215 loc) · 8.64 KB
/
ClipboardDiffPackage.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
#region License
/*
ClipboardDiff Visual Studio Extension
Copyright (C) 2011-2014 Einar Egilsson
http://einaregilsson.com/clipboarddiff-visual-studio-extension/
This program is licensed under the MIT license, see the file LICENSE
for details.
*/
#endregion
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.ComponentModel.Design;
using System.Windows.Forms;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Process = System.Diagnostics.Process;
namespace EinarEgilsson.ClipboardDiff
{
[PackageRegistration(UseManagedResourcesOnly = true)]
[InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
[ProvideMenuResource("Menus.ctmenu", 1)]
[Guid("b02989c2-1a8e-4f11-81a4-957f1d18db10")]
[ProvideAutoLoad(UIContextGuids80.NoSolution)]
[ProvideAutoLoad(UIContextGuids80.SolutionExists)]
public sealed class ClipboardDiffPackage : Package
{
public static readonly Guid CommandSetId = new Guid("6f04d587-0360-458b-8501-02b2bc7bb002");
public const int ShowSettingsWindowCommandId = 0x100;
public const int ClipboardDiffCommandId = 0x120;
private const string RegistryRoot = "ClipboardDiff";
private const string RegistryProgram = "DiffProgram";
private const string RegistryArguments = "Arguments";
private string _program;
private string _arguments;
private DTE2 _app;
private readonly string tempFolder = Path.Combine(Path.GetTempPath(), "ClipboardDiff");
private void SaveSettings()
{
var key = UserRegistryRoot.OpenSubKey(RegistryRoot, true);
if (key == null)
{
UserRegistryRoot.CreateSubKey(RegistryRoot);
key = UserRegistryRoot.OpenSubKey(RegistryRoot, true);
}
Debug.Assert(key != null);
key.SetValue(RegistryProgram, _program);
key.SetValue(RegistryArguments, _arguments);
}
private void LoadSettings()
{
var subKey = UserRegistryRoot.OpenSubKey(RegistryRoot);
if (subKey != null)
{
_program = (string)subKey.GetValue(RegistryProgram);
_arguments = (string)subKey.GetValue(RegistryArguments);
}
//Didn't exist, try default paths
if (string.IsNullOrEmpty(_program))
{
foreach (var diffTool in DiffTools.GetCandidates())
{
if (File.Exists(diffTool.Path))
{
_program = diffTool.Path;
_arguments = diffTool.Arguments;
return;
}
}
}
}
protected override void Initialize()
{
base.Initialize();
_app = (DTE2)GetGlobalService(typeof(DTE));
InitializeMenuCommands();
LoadSettings();
}
private void InitializeMenuCommands()
{
var mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
if (mcs != null)
{
var showSettingsCommand = new OleMenuCommand((o, e) => ShowSettingsWindow(),
new CommandID(CommandSetId, ShowSettingsWindowCommandId));
showSettingsCommand.Text = "ClipboardDiff Settings";
mcs.AddCommand(showSettingsCommand);
var diffCommand = new OleMenuCommand((o,e)=>DiffWithClipboard(), new CommandID(CommandSetId, ClipboardDiffCommandId));
diffCommand.Text = "Diff selection against clipboard";
diffCommand.BeforeQueryStatus += (cmd, e) => ((MenuCommand) cmd).Enabled = ClipboardAndSelectionBothHaveText();
mcs.AddCommand(diffCommand);
}
}
private bool ClipboardAndSelectionBothHaveText()
{
return Clipboard.ContainsText()
&& _app.ActiveDocument != null
&& _app.ActiveDocument.Selection is TextSelection
&& ((TextSelection)_app.ActiveDocument.Selection).Text.Length > 0;
}
private void DiffWithClipboard()
{
if (!ClipboardAndSelectionBothHaveText())
{
return;
}
if (!VerifySettings())
{
return;
}
VerifyTempFolder();
string extension = Path.GetExtension(_app.ActiveDocument.Name);
string clipboardFile = WriteClipboardTextToTempFile(extension);
string selectionFile = WriteSelectionTextToTempFile(extension);
StartDiffProgram(clipboardFile, selectionFile);
DeleteOldTempFiles(clipboardFile, selectionFile);
}
private string WriteClipboardTextToTempFile(string extension)
{
string clipboardFile = Path.Combine(tempFolder, "clipboard_" + Timestamp() + extension);
File.WriteAllText(clipboardFile, Clipboard.GetText());
return clipboardFile;
}
private string WriteSelectionTextToTempFile(string extension)
{
string selectionFile = Path.Combine(tempFolder, "selection_" + Timestamp() + extension);
File.WriteAllText(selectionFile, ((TextSelection)_app.ActiveDocument.Selection).Text);
return selectionFile;
}
private static string Timestamp()
{
return DateTime.Now.ToString("yyyyMMdd-HHmmss");
}
private void StartDiffProgram(string clipboardFile, string selectionFile)
{
//Ugly, but the easiest way to detect vsdiffmerge which starts up with a console
//which is ugly and we want to hide...
if (_program.Contains("vsDiffMerge.exe"))
{
var p = new Process();
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.FileName = _program;
p.StartInfo.Arguments = GetArguments(clipboardFile, selectionFile);
p.Start();
}
else
{
Process.Start(_program, GetArguments(clipboardFile, selectionFile));
}
}
private string GetArguments(string clipboardFile, string selectionFile)
{
string args = _arguments.Replace("$FILE1$", "\"" + clipboardFile + "\"").Replace("$FILE2$",
"\"" + selectionFile + "\"");
args = args.Replace("\"\"", "\"");
return args;
}
private bool VerifySettings()
{
if (string.IsNullOrEmpty(_program))
{
MessageBox.Show(
"You have not used Clipboard Diff before. You must first choose which diff tool you want to use.",
"Diff tool missing", MessageBoxButtons.OK, MessageBoxIcon.Information);
ShowSettingsWindow();
}
//True if we have the settings set up, so caller can continue with diff operation
return !string.IsNullOrEmpty(_program);
}
private void DeleteOldTempFiles(string clipboardFile, string selectionFile)
{
foreach (string filename in Directory.GetFiles(tempFolder))
{
//Clean older files that are still hanging around
if (filename != selectionFile && filename != clipboardFile)
{
try
{
File.Delete(filename);
}
// ReSharper disable EmptyGeneralCatchClause
catch
// ReSharper restore EmptyGeneralCatchClause
{
//Do nothing! We just try
}
}
}
}
private void VerifyTempFolder()
{
if (!Directory.Exists(tempFolder))
{
Directory.CreateDirectory(tempFolder);
}
}
private void ShowSettingsWindow()
{
using (var dlg = new Settings(_program ?? "", _arguments ?? "$FILE1$ $FILE2$"))
{
if (dlg.ShowDialog() == DialogResult.OK)
{
_program = dlg.Program;
_arguments = dlg.Arguments;
SaveSettings();
}
}
}
}
}