Insert a new GUID to Visual Studio 2012
Asked Answered
H

4

24

Is it possible to create a code snippet or something similar to automate the process of generating and inserting GUIDs in to the text editor in Visual Studio 2012? I frequently need to generate new GUIDs (WiX installer for example, as well as our own internal framework).

I used to use a macro to perform this job, creating a new GUID and then inserting it in to the current ActiveDocument. I would the bind the macro to Shift-Ctrl G so I could rapidly insert a new ID without having to launch the Create Guid tool and copy from there.

Macro functionality has now been removed from Visual Studio 2012 so I need an alternative method, I would assume that it is possible to do with an Extension but I am unfamiliar with developing extensions and that approach would seem a little heavy handed!

Any suggestions would be appreciated, failing that then a pointer at any information on what sort of extension would be required to interact with the text window and insert text would be appreciated. I could then make an extension and post it on the Visual Studio Gallery.

Thanks, Anthony

Edit to add - whatever solution is proposed would need to be "triggerable" from a keyboard shortcut. As I stated above, I tied the macro to Ctrl Shift G because it was easy to remember and press while writing code.

Hebron answered 10/9, 2012 at 10:43 Comment(1)
Delphi 6 had this built-in via Ctrl+Alt+Shift+G ...Implement
G
39

You could write a Visual Studio 2012 extension to accomplish this!
If you've never written an Add-in before, this is a simple one to get you started!

Here are the steps to create this type of add-in:

  1. Create a New Project in Visual Studio 2012
  2. Choose Templates -> Other Project Types -> Extensibility -> Visual Studio Add-in
  3. Name your project, click OK.
  4. Follow the Add-in wizard steps. When prompted, check the box for: "Yes, create a 'Tools' menu item." Optionally, I also check "My Add-in will never put up modal UI..."
  5. Finish the wizard and implement the follow code in Exec(...)

    public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
    {
        handled = false;
        if(executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
        {
            if (commandName == this.GetType().Namespace + ".Connect." + this.GetType().Namespace)
            {
                if (_applicationObject.ActiveDocument != null)
                {
                    TextSelection objSel = (EnvDTE.TextSelection)(_applicationObject.ActiveDocument.Selection);
    
                    objSel.Insert(Guid.NewGuid().ToString());
                }
    
                handled = true;
                return;
            }
        }
    }
    
  6. Build the project, and deploy AddInName.dll, AddInName.AddIn, and AddInName.xml to c:\users\username\documents\Visual Studio 2012\Addins. [If the Addins folder doesn't exist, create it]

  7. In Visual Studio 2012, under Tools -> Addin Manager, Check the box on the left next to AddInName.
  8. Restart Visual Studio 2012
  9. You should now see the AddInName listed under Tools. [probably with a Smiley face!]
  10. When you click on this, it should insert a new GUID at your cursor's location.
  11. Map this to a hotkey by navigating to Tools -> Options -> Environment -> Keyboard, search for AddInName, and bind a hotkey to it.

Voila! Hotkey GUID generation and a little bit of Visual Studio Add-in know how. :)

Glyceride answered 21/9, 2012 at 19:18 Comment(5)
That is exactly the kind of thing I wanted to know! I wasn't afraid of writing an extension, I just wasn't sure what exactly I needed. The documentation for extensions seems a big lacking (or I just haven't found it). Plus there are quite a few different flavours of add-in it would appear, some more daunting than others. Thanks for the help!Hebron
Just in case anyone finds this question/answer looking for a similar thing, I have created a GitHub project that does exactly what is in this answer. It can be downloaded from github.com/antwilliams/VsGuidGenerator or download the binary from github.com/downloads/antwilliams/VsGuidGenerator/…. No guarantees provided in any way, shape or form.Hebron
There is already a vs extention GuidInserter2. You can find it here GuidInserter. You can use the shortcut Ctl + K + JMarmoset
I must be missing something. I followed this to the letter and when I click on the smiley face, absolutely nothing happens to the location of my cursor. Odd...Blandishment
I updated the code snippet to fix the issue @Blandishment described. The problem was that Visual Studio uses code generation to hardcode a reference to the namespace that you chose to give your addon in the Exec() function. The updated sample above is namespace agnostic, so you can simply replace the entire Exec() function above without worry about what you call the add-in.Glyceride
I
52

ReSharper allows you to insert a new guid by typing "nguid" and pressing tab.

Obviously this is a tad on the expensive side just for the ability to generate a Guid however ReSharper has many other useful features that might be worth considering.

Inconsiderable answered 10/9, 2012 at 13:28 Comment(3)
Hmmm, can I award a half tick for this answer!? ;) I have a love/hate relationship with ReSharper, every time I've tried it I've ended up installing it for busybodying my code. I am sure I can configure the settings to be exactly what I want but it always seems to be too much effort! That aside, it is interesting that that functionality is included in ReSharper and I will give it another go! I would like to know if there are any other alternatives however since ReSharper to generate new GUIDs is akin to using a thermonuclear bomb to crack a nut!Hebron
I was looking around and happen to scroll far enough downward for this. Amazing! Never would have realized it had this functionality without this answer >.<Borst
Was this removed? Doesn't seem to work with ReSharper 8.2.3. Or do I have to enable it?Slayton
G
39

You could write a Visual Studio 2012 extension to accomplish this!
If you've never written an Add-in before, this is a simple one to get you started!

Here are the steps to create this type of add-in:

  1. Create a New Project in Visual Studio 2012
  2. Choose Templates -> Other Project Types -> Extensibility -> Visual Studio Add-in
  3. Name your project, click OK.
  4. Follow the Add-in wizard steps. When prompted, check the box for: "Yes, create a 'Tools' menu item." Optionally, I also check "My Add-in will never put up modal UI..."
  5. Finish the wizard and implement the follow code in Exec(...)

    public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
    {
        handled = false;
        if(executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
        {
            if (commandName == this.GetType().Namespace + ".Connect." + this.GetType().Namespace)
            {
                if (_applicationObject.ActiveDocument != null)
                {
                    TextSelection objSel = (EnvDTE.TextSelection)(_applicationObject.ActiveDocument.Selection);
    
                    objSel.Insert(Guid.NewGuid().ToString());
                }
    
                handled = true;
                return;
            }
        }
    }
    
  6. Build the project, and deploy AddInName.dll, AddInName.AddIn, and AddInName.xml to c:\users\username\documents\Visual Studio 2012\Addins. [If the Addins folder doesn't exist, create it]

  7. In Visual Studio 2012, under Tools -> Addin Manager, Check the box on the left next to AddInName.
  8. Restart Visual Studio 2012
  9. You should now see the AddInName listed under Tools. [probably with a Smiley face!]
  10. When you click on this, it should insert a new GUID at your cursor's location.
  11. Map this to a hotkey by navigating to Tools -> Options -> Environment -> Keyboard, search for AddInName, and bind a hotkey to it.

Voila! Hotkey GUID generation and a little bit of Visual Studio Add-in know how. :)

Glyceride answered 21/9, 2012 at 19:18 Comment(5)
That is exactly the kind of thing I wanted to know! I wasn't afraid of writing an extension, I just wasn't sure what exactly I needed. The documentation for extensions seems a big lacking (or I just haven't found it). Plus there are quite a few different flavours of add-in it would appear, some more daunting than others. Thanks for the help!Hebron
Just in case anyone finds this question/answer looking for a similar thing, I have created a GitHub project that does exactly what is in this answer. It can be downloaded from github.com/antwilliams/VsGuidGenerator or download the binary from github.com/downloads/antwilliams/VsGuidGenerator/…. No guarantees provided in any way, shape or form.Hebron
There is already a vs extention GuidInserter2. You can find it here GuidInserter. You can use the shortcut Ctl + K + JMarmoset
I must be missing something. I followed this to the letter and when I click on the smiley face, absolutely nothing happens to the location of my cursor. Odd...Blandishment
I updated the code snippet to fix the issue @Blandishment described. The problem was that Visual Studio uses code generation to hardcode a reference to the namespace that you chose to give your addon in the Exec() function. The updated sample above is namespace agnostic, so you can simply replace the entire Exec() function above without worry about what you call the add-in.Glyceride
B
0

My SCLAssist extension has this feature. You can bind the "Paste GUID" to a key mapping. SCLAssist is free.

Baber answered 18/3, 2014 at 22:9 Comment(0)
A
0

Looks like they brought it back - in VS 2015 (I'm using the Community version) there is a Tools > Create GUID option.

Ambience answered 23/6, 2016 at 11:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.