Skip to content

Commit aa412d9

Browse files
authored
Merge pull request #19 from m-tmatma/feature/disable-copy-GUID-and-Insert-GUID-command
disable copy GUID and Insert GUID command when text is selected.
2 parents 33b482a + cc544cb commit aa412d9

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-10
lines changed

CreateGUIDVSPlugin/CopyGuidCommand.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ internal sealed class CopyGuidCommand
2929
/// </summary>
3030
private readonly VSPackage package;
3131

32+
/// <summary>
33+
/// default command text
34+
/// </summary>
35+
private const string DefaultCommandText = "Copy Guid";
3236

3337
/// <summary>
3438
/// control whether an menu item is displayed or not.
@@ -40,11 +44,23 @@ private void BeforeQueryStatus(object sender, EventArgs e)
4044
{
4145
var dte = this.package.GetDTE();
4246
var activeDocument = dte.ActiveDocument;
43-
44-
command.Visible = true;
47+
var selection = (EnvDTE.TextSelection)activeDocument.Selection;
48+
var seltext = selection.Text;
49+
if (!string.IsNullOrEmpty(seltext))
50+
{
51+
// text is selected.
52+
command.Enabled = false;
53+
command.Text = DefaultCommandText + " (Unselect text to enable this command)";
54+
}
55+
else
56+
{
57+
// text is not selected.
58+
command.Enabled = true;
59+
command.Text = DefaultCommandText;
60+
}
4561
}
4662
}
47-
63+
4864
/// <summary>
4965
/// Initializes a new instance of the <see cref="CopyGuidCommand"/> class.
5066
/// Adds our command handlers for menu (commands must exist in the command table file)

CreateGUIDVSPlugin/InsertGuidCommand.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ internal sealed class InsertGuidCommand
2929
/// </summary>
3030
private readonly VSPackage package;
3131

32+
/// <summary>
33+
/// default command text
34+
/// </summary>
35+
private const string DefaultCommandText = "Insert Guid";
3236

3337
/// <summary>
3438
/// control whether an menu item is displayed or not.
@@ -40,11 +44,23 @@ private void BeforeQueryStatus(object sender, EventArgs e)
4044
{
4145
var dte = this.package.GetDTE();
4246
var activeDocument = dte.ActiveDocument;
43-
44-
command.Visible = true;
47+
var selection = (EnvDTE.TextSelection)activeDocument.Selection;
48+
var seltext = selection.Text;
49+
if (!string.IsNullOrEmpty(seltext))
50+
{
51+
// text is selected.
52+
command.Enabled = false;
53+
command.Text = DefaultCommandText + " (Unselect text to enable this command)";
54+
}
55+
else
56+
{
57+
// text is not selected.
58+
command.Enabled = true;
59+
command.Text = DefaultCommandText;
60+
}
4561
}
4662
}
47-
63+
4864
/// <summary>
4965
/// Initializes a new instance of the <see cref="InsertGuidCommand"/> class.
5066
/// Adds our command handlers for menu (commands must exist in the command table file)

CreateGUIDVSPlugin/RenewGuidCommand.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ internal sealed class RenewGuidCommand
3737
/// </summary>
3838
private void BeforeQueryStatus(object sender, EventArgs e)
3939
{
40-
bool Enabled = false;
4140
OleMenuCommand command = sender as OleMenuCommand;
4241
if (command != null)
4342
{
@@ -47,14 +46,16 @@ private void BeforeQueryStatus(object sender, EventArgs e)
4746
var seltext = selection.Text;
4847
if (!string.IsNullOrEmpty(seltext))
4948
{
50-
Enabled = true;
49+
// text is selected.
50+
command.Enabled = true;
5151
command.Text = DefaultCommandText;
5252
}
5353
else
5454
{
55-
command.Text = DefaultCommandText + " (Select text that you want to replace with new GUIDs.)";
55+
// text is not selected.
56+
command.Enabled = false;
57+
command.Text = DefaultCommandText + " (Select text to enable this command)";
5658
}
57-
command.Enabled = Enabled;
5859
}
5960
}
6061

CreateGUIDVSPlugin/VSPackage.vsct

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
<CommandFlag>IconIsMoniker</CommandFlag>
5959
<CommandFlag>DefaultInvisible</CommandFlag>
6060
<CommandFlag>DynamicVisibility</CommandFlag>
61+
<CommandFlag>TextChanges</CommandFlag>
6162
<Strings>
6263
<ButtonText>Copy Guid</ButtonText>
6364
</Strings>
@@ -68,6 +69,7 @@
6869
<CommandFlag>IconIsMoniker</CommandFlag>
6970
<CommandFlag>DefaultInvisible</CommandFlag>
7071
<CommandFlag>DynamicVisibility</CommandFlag>
72+
<CommandFlag>TextChanges</CommandFlag>
7173
<Strings>
7274
<ButtonText>Insert Guid</ButtonText>
7375
</Strings>

0 commit comments

Comments
 (0)