Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Custom Code Snippet Functions

I have a code snippet structures like so:

    private $type$ $lowercaseName$;
    public $type$ $uppercaseName$
    {
        get { return $lowercaseName$; }
        set { $lowercaseName$ = value; }
    }

It generates stuff like:

    private string randomValue;
    public string RandomValue
    {
        get { return randomValue; }
        set { randomValue = value; }
    }

Thats a very oversimplified version... its really a lot more complicated... and its just that much more information to type in. It would be nice if you could type in $uppercaseName$, and then a custom snippet function would assign a value to $lowercaseName$....

But is it even possible to write custom snippet functions? I don't see anything about this in the documentation...

If it is possible... how?

like image 846
Entity Avatar asked Apr 14 '11 12:04

Entity


3 Answers

It is not yet possible. See this suggestion for 2010 that was denied.

http://connect.microsoft.com/VisualStudio/feedback/details/523601/allow-custom-code-snippet-functions

It seems this feature was suggested when snippets were introduced, and has been re-suggested every version, and shot down saying they don't have enough time.

like image 102
Merlyn Morgan-Graham Avatar answered Nov 11 '22 03:11

Merlyn Morgan-Graham


Rather than using the convention of having camelCase fields and PascalCase properties, I have adopted a convention of a using leading underscore for backing fields. The following code snippet works for me.

public $type$ $property$
{
    get { return _$property$;}
    set { _$property$ = value;}
}
private $type$ _$property$;
$end$

And in attempt to snub the "underscore is a prefix and prefixes are bad" holy war, I believ it to be more a convention than a prefix much like upper v. lower initial letters for properties v. parameters. Any use of a field with a leading underscore outside of it's associated property implementation should be code smell.

like image 43
psaxton Avatar answered Nov 11 '22 03:11

psaxton


My answer is Resharper. Live Templates.

It is very annoying having this limitation, but like many other Visual Studio limitations, Resharper nails it. It is a performance hog, so I operate with the code analysis turned off. The speed is then acceptable, and live templates save a lot of time.

There are about twenty other good reasons to have a tool LIKE Resharper to make you a fantastic coder.

I can't write "hello world" without Resharper any more... and I can't debug it without Reflector =P

Also, go to the Extension Manager and click Online Gallery. Type "snippet" in the Search box, and there are a few tools there that look like they might help for free :). The extension gallery is pure productivity gold.

like image 1
Parrhesia Joe Avatar answered Nov 11 '22 02:11

Parrhesia Joe