Skip to content

Commit 0b91fd3

Browse files
authored
Merge pull request #52 from m-tmatma/feature/RenewGUIDActiveDocument
renew guid active document
2 parents afccb05 + 93f4734 commit 0b91fd3

File tree

5 files changed

+180
-9
lines changed

5 files changed

+180
-9
lines changed

CreateGUIDVSPlugin/CreateGUIDVSPlugin.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
</Compile>
6868
<Compile Include="Properties\AssemblyInfo.cs" />
6969
<Compile Include="RenewGuidCommand.cs" />
70+
<Compile Include="RenewGuidCurrentCommand.cs" />
7071
<Compile Include="Utility\Configuration.cs" />
7172
<Compile Include="Utility\FormatGuid.cs" />
7273
<Compile Include="Utility\ProcessTemplate.cs" />
@@ -90,6 +91,7 @@
9091
<Content Include="Resources\CopyGuidCommand.png" />
9192
<Content Include="Resources\InsertGuidCommand.png" />
9293
<Content Include="Resources\RenewGuidCommand.png" />
94+
<Content Include="Resources\RenewGuidCurrentCommand.png" />
9395
<Content Include="Resources\VSPackage.ico" />
9496
<Content Include="stylesheet.css" />
9597
<VSCTCompile Include="VSPackage.vsct">
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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+
}
1.14 KB
Loading

CreateGUIDVSPlugin/VSPackage.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ protected override void Initialize()
135135
CopyGuidCommand.Initialize(this);
136136
InsertGuidCommand.Initialize(this);
137137
RenewGuidCommand.Initialize(this);
138+
RenewGuidCurrentCommand.Initialize(this);
138139

139140
#if DEBUG
140141
AddOutputWindow("Create GUID");

CreateGUIDVSPlugin/VSPackage.vsct

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,17 @@
8585
<ButtonText>Renew Guid</ButtonText>
8686
</Strings>
8787
</Button>
88+
<Button guid="guidVSPackageCmdSet" id="cmdidRenewGuidCurrentCommand" priority="0x0100" type="Button">
89+
<Parent guid="guidVSPackageCmdSet" id="MyMenuGroup" />
90+
<Icon guid="guidImages3" id="bmpPic1" />
91+
<CommandFlag>IconIsMoniker</CommandFlag>
92+
<CommandFlag>DefaultInvisible</CommandFlag>
93+
<CommandFlag>DynamicVisibility</CommandFlag>
94+
<CommandFlag>TextChanges</CommandFlag>
95+
<Strings>
96+
<ButtonText>Renew Guid in current docuemnt</ButtonText>
97+
</Strings>
98+
</Button>
8899
</Buttons>
89100

90101
<!--The bitmaps section is used to define the bitmaps that are used for the commands.-->
@@ -97,6 +108,7 @@
97108
<Bitmap guid="guidImages" href="Resources\CopyGuidCommand.png" usedList="bmpPic1, bmpPic2, bmpPicSearch, bmpPicX, bmpPicArrows, bmpPicStrikethrough" />
98109
<Bitmap guid="guidImages1" href="Resources\InsertGuidCommand.png" usedList="bmpPic1, bmpPic2, bmpPicSearch, bmpPicX, bmpPicArrows, bmpPicStrikethrough" />
99110
<Bitmap guid="guidImages2" href="Resources\RenewGuidCommand.png" usedList="bmpPic1, bmpPic2, bmpPicSearch, bmpPicX, bmpPicArrows, bmpPicStrikethrough" />
111+
<Bitmap guid="guidImages3" href="Resources\RenewGuidCurrentCommand.png" usedList="bmpPic1, bmpPic2, bmpPicSearch, bmpPicX, bmpPicArrows, bmpPicStrikethrough" />
100112
</Bitmaps>
101113
</Commands>
102114

@@ -133,6 +145,7 @@
133145
<IDSymbol name="CopyGuidCommandId" value="0x0100" />
134146
<IDSymbol value="4129" name="cmdidInsertGuidCommand" />
135147
<IDSymbol value="4130" name="cmdidRenewGuidCommand" />
148+
<IDSymbol value="4131" name="cmdidRenewGuidCurrentCommand" />
136149
</GuidSymbol>
137150

138151
<GuidSymbol name="guidImages" value="{7ab86d3e-1142-4cbd-a3b5-72d3b744ec50}">
@@ -152,6 +165,24 @@
152165
<IDSymbol name="bmpPicArrows" value="5" />
153166
<IDSymbol name="bmpPicStrikethrough" value="6" />
154167
</GuidSymbol>
168+
169+
<GuidSymbol value="{7e549cd1-1a8b-424c-93d4-b6e16a8912b5}" name="guidImages2">
170+
<IDSymbol name="bmpPic1" value="1" />
171+
<IDSymbol name="bmpPic2" value="2" />
172+
<IDSymbol name="bmpPicSearch" value="3" />
173+
<IDSymbol name="bmpPicX" value="4" />
174+
<IDSymbol name="bmpPicArrows" value="5" />
175+
<IDSymbol name="bmpPicStrikethrough" value="6" />
176+
</GuidSymbol>
177+
178+
<GuidSymbol value="{ba788c3f-281f-4450-8428-7bf39b53619c}" name="guidImages3">
179+
<IDSymbol name="bmpPic1" value="1" />
180+
<IDSymbol name="bmpPic2" value="2" />
181+
<IDSymbol name="bmpPicSearch" value="3" />
182+
<IDSymbol name="bmpPicX" value="4" />
183+
<IDSymbol name="bmpPicArrows" value="5" />
184+
<IDSymbol name="bmpPicStrikethrough" value="6" />
185+
</GuidSymbol>
155186

156187
<!-- GUID and ID for not defined Visual Studio SDK -->
157188

@@ -169,14 +200,5 @@
169200
<GuidSymbol name="guidJSONDocumentContextMenu" value="{f718ca06-cf4f-4a0c-9106-e79e9ee5e7cd}">
170201
<IDSymbol name="idJSONContextMenu" value="0x3" />
171202
</GuidSymbol>
172-
173-
<GuidSymbol value="{7e549cd1-1a8b-424c-93d4-b6e16a8912b5}" name="guidImages2">
174-
<IDSymbol name="bmpPic1" value="1" />
175-
<IDSymbol name="bmpPic2" value="2" />
176-
<IDSymbol name="bmpPicSearch" value="3" />
177-
<IDSymbol name="bmpPicX" value="4" />
178-
<IDSymbol name="bmpPicArrows" value="5" />
179-
<IDSymbol name="bmpPicStrikethrough" value="6" />
180-
</GuidSymbol>
181203
</Symbols>
182204
</CommandTable>

0 commit comments

Comments
 (0)