Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending Visual Studio's "Code Snippet" functionality

When I write a test method, I type "testm", hit tab and magically see:

[TestMethod]
public void MethodName()
{

}

When I type the methodName it is highlighted (can't show that here) as a "field" that I'm filling in. I'm sure you're all familiar with this behavior.

Personally, I like names for my test methods like Can_My_Method_Do_That_Thing instead of CanMyMethodDoThatThing. I find them much easier to read, and most times they're really a sentence anyway.

For reasons I'd rather not get into, I have a difficult time typing all those _ characters and I'd like to be able to use the space bar, and have the spaces in the name automatically replaced when I hit "Enter".

I hear that Visual Studio is extendable and customizable and so on. Is it extensible enough to do this?

like image 293
Scott Baker Avatar asked Feb 02 '26 15:02

Scott Baker


1 Answers

You can implement and use your own code snippets and Microsoft provides a very nice guide on how this could be done: Walkthrough: Implementing Code Snippets

To have a quick look at how the "testm" Expansion (that's the Snippet Type) is "partially" implemented you may go to c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC#\Snippets\1033\Test\ directory and edit the testmethod.snippet file.

I have never implemented this kind of "Expansion" myself, but Microsoft's Extending the Editor website is a really good source of info of how this can be achieved. This is where you should start lookin'.

like image 138
nowaq Avatar answered Feb 05 '26 07:02

nowaq



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!