|
| 1 | +using System; |
| 2 | +using System.ComponentModel.Design; |
| 3 | +using System.Globalization; |
| 4 | +using System.Text; |
| 5 | +using System.Windows.Forms; |
| 6 | +using Microsoft.VisualStudio.Shell; |
| 7 | +using Microsoft.VisualStudio.Shell.Interop; |
| 8 | +using CreateGUIDVSPlugin.Utility; |
| 9 | + |
| 10 | +namespace CreateGUIDVSPlugin |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Command handler |
| 14 | + /// </summary> |
| 15 | + internal sealed class RenewGuidCurrentCommand |
| 16 | + { |
| 17 | + /// <summary> |
| 18 | + /// Command ID. |
| 19 | + /// </summary> |
| 20 | + public const int CommandId = 4131; |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// Command menu group (command set GUID). |
| 24 | + /// </summary> |
| 25 | + public static readonly Guid CommandSet = new Guid("21faf2f8-6789-4d5a-bda3-c063d68e1b7d"); |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// VS Package that provides this command, not null. |
| 29 | + /// </summary> |
| 30 | + private readonly VSPackage package; |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// default command text |
| 34 | + /// </summary> |
| 35 | + private const string DefaultCommandText = "Renew Guid for current document"; |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// control whether an menu item is displayed or not. |
| 39 | + /// </summary> |
| 40 | + private void BeforeQueryStatus(object sender, EventArgs e) |
| 41 | + { |
| 42 | + OleMenuCommand command = sender as OleMenuCommand; |
| 43 | + if (command != null) |
| 44 | + { |
| 45 | + var dte = this.package.GetDTE(); |
| 46 | + var activeDocument = dte.ActiveDocument; |
| 47 | + if (activeDocument != null) |
| 48 | + { |
| 49 | + command.Enabled = true; |
| 50 | + command.Text = DefaultCommandText; |
| 51 | + } |
| 52 | + else |
| 53 | + { |
| 54 | + command.Enabled = false; |
| 55 | + command.Text = DefaultCommandText + " (activate document to enable this command)"; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Initializes a new instance of the <see cref="RenewGuidCurrentCommand"/> class. |
| 62 | + /// Adds our command handlers for menu (commands must exist in the command table file) |
| 63 | + /// </summary> |
| 64 | + /// <param name="package">Owner package, not null.</param> |
| 65 | + private RenewGuidCurrentCommand(VSPackage package) |
| 66 | + { |
| 67 | + if (package == null) |
| 68 | + { |
| 69 | + throw new ArgumentNullException("package"); |
| 70 | + } |
| 71 | + |
| 72 | + this.package = package; |
| 73 | + |
| 74 | + OleMenuCommandService commandService = this.ServiceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService; |
| 75 | + if (commandService != null) |
| 76 | + { |
| 77 | + var menuCommandID = new CommandID(CommandSet, CommandId); |
| 78 | + var menuItem = new OleMenuCommand(this.MenuItemCallback, menuCommandID); |
| 79 | + menuItem.BeforeQueryStatus += this.BeforeQueryStatus; |
| 80 | + commandService.AddCommand(menuItem); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /// <summary> |
| 85 | + /// Gets the instance of the command. |
| 86 | + /// </summary> |
| 87 | + public static RenewGuidCurrentCommand Instance |
| 88 | + { |
| 89 | + get; |
| 90 | + private set; |
| 91 | + } |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// Gets the service provider from the owner package. |
| 95 | + /// </summary> |
| 96 | + private IServiceProvider ServiceProvider |
| 97 | + { |
| 98 | + get |
| 99 | + { |
| 100 | + return this.package; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// Initializes the singleton instance of the command. |
| 106 | + /// </summary> |
| 107 | + /// <param name="package">Owner package, not null.</param> |
| 108 | + public static void Initialize(VSPackage package) |
| 109 | + { |
| 110 | + Instance = new RenewGuidCurrentCommand(package); |
| 111 | + } |
| 112 | + |
| 113 | + /// <summary> |
| 114 | + /// This function is the callback used to execute the command when the menu item is clicked. |
| 115 | + /// See the constructor to see how the menu item is associated with this function using |
| 116 | + /// OleMenuCommandService service and MenuCommand class. |
| 117 | + /// </summary> |
| 118 | + /// <param name="sender">Event sender.</param> |
| 119 | + /// <param name="e">Event args.</param> |
| 120 | + private void MenuItemCallback(object sender, EventArgs e) |
| 121 | + { |
| 122 | + var dte = this.package.GetDTE(); |
| 123 | + var activeDocument = dte.ActiveDocument; |
| 124 | + |
| 125 | + if (activeDocument != null) |
| 126 | + { |
| 127 | + var textView = this.package.GetTextView(); |
| 128 | + if (textView != null) |
| 129 | + { |
| 130 | + if (textView.HasAggregateFocus) |
| 131 | + { |
| 132 | + var buffer = textView.TextBuffer; |
| 133 | + var edit = buffer.CreateEdit(); |
| 134 | + |
| 135 | + var snapshot = buffer.CurrentSnapshot; |
| 136 | + var replaceWithNewGuid = new ReplaceWithNewGuid(); |
| 137 | + var output = replaceWithNewGuid.ReplaceSameGuidToSameGuid(snapshot.GetText()); |
| 138 | + |
| 139 | + edit.Replace(0, buffer.CurrentSnapshot.Length, output); |
| 140 | + edit.Apply(); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments