37

Is there any way that I can change how a Literal of a code snippet renders when it is used in the code that the snippet generates?

Specifically I'd like to know if I can have a literal called say, $PropertyName$ and then get the snippet engine to render "_$PropertyName$ where the first character is made lowercase.

I can't afford R#. Please help :)

Michael Lang
  • 2,157
  • 3
  • 22
  • 29
  • See my suggestion to [Add scripting to code snippets (T4 + Roslyn)](https://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/5805587-add-scripting-to-code-snippets-t4-roslyn). Unfortunately I got no response to this suggestion. – Olivier Jacot-Descombes Apr 17 '15 at 18:50

3 Answers3

23

Unfortunately there seems to be no way. Snippets offer amazingly limited support for transformation functions as you can see.

You have to stick with the VS standard solution, which is to write two literals: one for the property name, and the other for the member variable name.

Michael Lang
  • 2,157
  • 3
  • 22
  • 29
Caerbanog
  • 1,295
  • 1
  • 12
  • 18
  • 2
    I'm afraid I've not. I ended up buying a personal license of R# – Michael Lang Feb 26 '14 at 20:09
  • 3
    [As of Visual Studio 2013](http://msdn.microsoft.com/en-us/library/ms242312(v=vs.120).aspx), the three available functions are still only: `GenerateSwitchCases`, `ClassName`, and `SimpleTypeName`. – cod3monk3y Jul 17 '14 at 13:59
  • **APPALLING!!!** Is there a Visual Studio UserVoice entry for this? I'd give my votes for additional functions. I'm really perplexed how come they didn't even think of the other string-manipulating functions to add. Casing included. – Robert Koritnik Jan 07 '15 at 11:25
  • There is now: https://visualstudio.uservoice.com/forums/121579-visual-studio-ide/suggestions/34816990-add-camelcase-transformation-to-visual-studio-code – Squirrelkiller Jul 12 '18 at 19:08
7

You can enter a upper first letter, then a property name, then a lower first letter. Try this snippet:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <Header>
    <Title>Notifiable Property</Title>
    <Author>Nikolay Makhonin</Author>
    <Shortcut>propn</Shortcut>
    <Description>Property With in Built Property Changed method implementation.</Description>
    <SnippetTypes>
      <SnippetType>SurroundsWith</SnippetType>
      <SnippetType>Expansion</SnippetType>
    </SnippetTypes>
  </Header>
  <Snippet>
    <Declarations>
        <Literal>
            <ID>Type</ID>
            <Default>Type</Default>
        </Literal>
        <Literal>
            <ID>P</ID>
            <Default>P</Default>
        </Literal>
        <Literal>
            <ID>roperty</ID>
            <Default>ropertyName</Default>
        </Literal>
        <Literal>
            <ID>p</ID>
            <Default>p</Default>
        </Literal>
        <Literal>
            <ID>Ownerclass</ID>
            <ToolTip>The owning class of this Property.</ToolTip>
            <Function>ClassName()</Function>
            <Default>Ownerclass</Default>
        </Literal>
    </Declarations>
    <Code Language="CSharp">
      <![CDATA[#region $P$$roperty$

        private Field<$Type$> _$p$$roperty$;
        public static readonly string $P$$roperty$PropertyName = GetPropertyName(() => (($Ownerclass$)null).$P$$roperty$);
        public $Type$ $P$$roperty$
        {
            get { return _$p$$roperty$; }
            set { Set(ref _$p$$roperty$, value); }
        }

        #endregion

]]>
    </Code>
  </Snippet>
</CodeSnippet>
Nikolay Makhonin
  • 1,087
  • 12
  • 18
6

a "fix" may be to use a prefix in the naming or the member variable, i.e.:

string m_$name$;
string $name$
{
 get{return m_$name$;}
 set{m_$name$=value;}
};
fred
  • 61
  • 1
  • 1
  • 5
    Yeah, but it doesn't allow you to change the casing of $name$ to make it camelCase. (Typically people use `"_property"` as the backing for a property named `"Property"`, etc. Also "m_" is super deprecated... since like vb6...? please don't ever use it). – BrainSlugs83 Aug 13 '14 at 01:43
  • 1
    Sorry for necro-commenting, but "m_" is not deprecated at all. It's a great practice which I think should be used more. Unless you and anybody else never ever have to read that code again - yeah, because that never happened. – Simone May 19 '18 at 14:41