Search code examples
visual-studiovisual-studio-2019code-snippets

VS 2019 custom code snippet with literals not expanding correctly


I write a lot of configuration code. I built this snippet to make all of that repetitive code quicker to write. But it doesn't expand correctly.

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>New Configuration Property</Title>
            <Shortcut>newproperty</Shortcut>
        </Header>
        <Snippet>
            <Imports>
                <Import>
                    <Namespace>System.Configuration</Namespace>
                </Import>
            </Imports>
            <Declarations>
                <Literal>
                    <ID>$ConfigName$</ID>
                    <ToolTip>The name of the element property in the .config file</ToolTip>
                    <Default>configName</Default>
                </Literal>
                <Literal>
                    <ID>$PropertyName$</ID>
                    <ToolTip>The name of the class property</ToolTip>
                    <Default>PropertyName</Default>
                </Literal>
            </Declarations>
            <Code Language="CSharp" kind="" delimiter="$">
                <![CDATA[[ConfigurationProperty("$ConfigName$", IsRequired = true)]
public string $PropertyName$
{
    get { return (string)this["$ConfigName$"]; }
    set { this["$ConfigName$"] = value; }
}$selected$$end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

And this is what the output looks like.

        [ConfigurationProperty("", IsRequired = true)]
        public string 
{
    get { return (string) this[""]; }
        set { this[""] = value; }
}

There are no prompts for the literals. Any suggestions?


Solution

  • Just make it easy and simple by doing it like:

      <?xml version="1.0" encoding="utf-8"?>
        <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
          <CodeSnippet Format="1.0.0">
            <Header>
              <Title>New Configuration Property</Title>
              <Shortcut>newproperty</Shortcut>
            </Header>
            <Snippet>
              <Imports>
                <Import>
                  <Namespace>System.Configuration</Namespace>
                </Import>
              </Imports>
              <Declarations>
                <Literal>
                  <ID>ConfigName</ID>
                  <ToolTip>The name of the element property in the .config file</ToolTip>
                  <Default>configName</Default>
                </Literal>
                <Literal>
                  <ID>PropertyName</ID>
                  <ToolTip>The name of the class property</ToolTip>
                  <Default>PropertyName</Default>
                </Literal>
              </Declarations>
              <Code Language="csharp">
                <![CDATA[[ConfigurationProperty("$ConfigName$", IsRequired = true)]
        public string $PropertyName$
        {
            get { return (string)this["$ConfigName$"]; }
            set { this["$ConfigName$"] = value; }
        }
       $selected$ $end$]]>
              </Code>
            </Snippet>
          </CodeSnippet>
        </CodeSnippets>
    

    You dont need the two parts = kind="" delimiter="$" and you dont need to have dollar sign inside the ID, instead of doing: $ConfigName$ you can do: ConfigName Just look the above xml snippet, and the language should be good alternative if you wrote small letters like: csharp instead of Csharp or CSharp. I hope it will help you out,