Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS2010 Code Snippet Shortcut Not Showing

I've created a code snippet in VS2010. It isn't showing as a shortcut when I start typing. I've called it propnch.

It is available when I use Ctrl-K, Ctrk-X but when I just start typing prop... it isn't showing as an option.

Have I missed some kind of setting somewhere?

I had screen shots, but I don't think SO lets you upload any.

Edit: Screen Shots

I can see my snippet with Ctrl-K, Ctrl-X (its gone grey when I ctrl-PrtScn to take the screenshot)

enter image description here

But It doesn't appear with the other snippet shortcuts.

enter image description here

The snippet code is here (taken from this tutorial) and is in the "Documents\Visual Studio 2010\Code Snippets\Visual C#\My Code Snippets" folder.

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
    <Header>
        <Title>propnch</Title>
        <Shortcut>propnch</Shortcut>
        <Description>Code snippet for property and backing field and ensure 
  that it invokes INotifyPropertyChanigng and INotifyPropertyChanged</Description>
        <Author>Abhishek</Author>
        <SnippetTypes>
            <SnippetType>Expansion</SnippetType>
        </SnippetTypes>
    </Header>
    <Snippet>
        <Declarations>
            <Literal>
                <ID>type</ID>
                <ToolTip>Property type</ToolTip>
                <Default>int</Default>
            </Literal>
            <Literal>
                <ID>property</ID>
                <ToolTip>Property name</ToolTip>
                <Default>MyProperty</Default>
            </Literal>
            <Literal>
                <ID>field</ID>
                <ToolTip>The variable backing this property</ToolTip>
                <Default>myVar</Default>
            </Literal>
        </Declarations>
        <Code Language="csharp"><![CDATA[

private $type$ $field$;

public $type$ $property$
{
    get 
    { 
        return $field$;
    }
    set 
    { 
        this.OnPropertyChanging("$property$");
        $field$ = value;
        this.OnPropertyChanged("$property$");
    }
}
$end$]]>
        </Code>
    </Snippet>
</CodeSnippet>

like image 824
big_tommy_7bb Avatar asked Apr 20 '11 16:04

big_tommy_7bb


2 Answers

It turns out this is a design flaw for the xml editor in VS2010. In the C# editor, you just type the shortcut and press 'tab'. IN the xml editor, it requires two more keystrokes.

To quote from the documentation:

To insert snippets using the shortcut name
    1. Position the cursor where you want to insert the XML snippet.
    2. Type < in the editor pane.
    3. Press ESC to close the IntelliSense complete word list.
    4. Type the shortcut name of the snippet, and press TAB to invoke the XML snippet.
like image 166
Oliver Avatar answered Sep 29 '22 05:09

Oliver


According to screenshots, you have ReSharper installed and it overrides VS's IntelliSense behavior. You can either turn off Resharper's overriding or just add right in it a new LiveTemplate. More details here:

http://www.jetbrains.com/resharper/webhelp/Templates__Applying_Templates__Inserting_Imported_Code_Snippets.html

In my case I just added a new ReSharper template:

private $type$ _$loweredProperty$;

public $type$ $property$
{
    get { return _$loweredProperty$;}
    set 
    {
        if (_$loweredProperty$ == value) return;
        _$loweredProperty$ = value;
        OnPropertyChanged("$property$");
    }
}

adn it works even better: you have to type only two words - type and property name. The backing field will appear with lowered first letter. You must set "$loweredProperty$" to non-editable macros and point it to $property$. That's just a couple of clicks in Template editor.

like image 29
Vlad Ogay Avatar answered Sep 29 '22 05:09

Vlad Ogay