1

I was wondering if anyone knows how you can make a Visual Studio snippet autoformat the input to a Literal shortcut field.

Say I want to create a snippet for a property. I want it the CDATA to look like:

private bool _$propertyName$;

private bool $propertyName$ { get { ... } set {...} }

so that when the user enters the $propertyName$ whilst calling the snippet he only has to fill in IsGrounded and then have the outputted code look like this:

private bool _isGrounded;

private bool IsGrounded { get { ... } set {...} }

The only way I know of on how to do this is by having a seperate $backingField$ and $propertyName$ literals in my snippet declarations that the user would have to fill out seperately, like so:

private bool _$backingFieldName$;

private bool $propertyName$ { get { ... } set {...} }

Is that truly the only way? Then the user would have to fill in isGrounded and IsGrounded. I would rather have them enter $propertyName$ once and then autoformat that into isGrounded and IsGrounded in their respective fields.

Here is my current snippet code using the workaround I just talked about. Is it possible to reduce the amount of fields you need to fill in to just the access and property name shortcut fields?

I know about the existance of the default prop and propfull snippets, but they do not offer exactly what I want.

<Snippet>
  <Code Language="CSharp">
    <![CDATA[
    private bool _$backingFieldName$;

    $access1$ bool $propertyName$
    {
        $access2$ get
        {
            return _$backingFieldName$;
        }

        $access3$ set
        {
            _$backingFieldName$ = value;
        }
    }
    ]]>
  </Code>
  <Declarations>
    <Literal>
      <ID>backingFieldName</ID>
      <ToolTip>The name for the backing field.</ToolTip>
      <Default>name</Default>
    </Literal>
    <Literal>
      <ID>propertyName</ID>
      <ToolTip>The name for the property.</ToolTip>
      <Default>Name</Default>
    </Literal>
    <Literal>
      <ID>access1</ID>
      <ToolTip>Access modifier for the property.</ToolTip>
      <Default>private</Default>
    </Literal>
    <Literal>
      <ID>access2</ID>
      <ToolTip>Access modifier for the getter.</ToolTip>
      <Default>private</Default>
    </Literal>
    <Literal>
      <ID>access3</ID>
      <ToolTip>Access modifier for the setter.</ToolTip>
      <Default>private</Default>
    </Literal>
  </Declarations>
</Snippet>
KamielDev
  • 481
  • 3
  • 13

1 Answers1

1

I found a Similar problem and searched for a lot of information to try to solve this problem but had no luck.

My solution to step back is to enter the first letter separately.

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>propertyName</Title>
      <Shortcut>propn</Shortcut>
    </Header>
    <Snippet>
  <Code Language="CSharp">
    <![CDATA[
    private bool _$p$$roperty$;

    $access1$ bool $P$$roperty$
    {
        $access2$ get
        {
            return _$p$$roperty$;
        }

        $access3$ set
        {
            _$p$$roperty$ = value;
        }
    }
    ]]>
  </Code>
  <Declarations>
    <Literal>
      <ID>p</ID>
      <ToolTip>The name for the lowercase initials.</ToolTip>
      <Default>n</Default>
    </Literal>
    <Literal>
      <ID>roperty</ID>
      <ToolTip>The name for the property.</ToolTip>
      <Default>ame</Default>
    </Literal>
    <Literal>
      <ID>P</ID>
      <ToolTip>The name for the Uppercase first letter.</ToolTip>
      <Default>N</Default>
    </Literal>
    <Literal>
      <ID>access1</ID>
      <ToolTip>Access modifier for the property.</ToolTip>
      <Default>private</Default>
    </Literal>
    <Literal>
      <ID>access2</ID>
      <ToolTip>Access modifier for the getter.</ToolTip>
      <Default>private</Default>
    </Literal>
    <Literal>
      <ID>access3</ID>
      <ToolTip>Access modifier for the setter.</ToolTip>
      <Default>private</Default>
    </Literal>
  </Declarations>
</Snippet>
  </CodeSnippet>
</CodeSnippets>

Of course, if your naming is fixed, you can change $p$ and $P$ to i and I, which are ridiculous alternatives.

Jiale Xue - MSFT
  • 3,560
  • 1
  • 6
  • 21
  • Thanks for your answer. I guess that's another way of achieving something similar. I'll leave this post unanswered for now. If there is no solution maybe I should open a feature request. – KamielDev Nov 03 '21 at 10:41