'Writing conditional snippets in Visual Studio?

I'm trying to create some Visual Studio snippets to decrease my workload.

Ive created a snippet with the shortcut transpos that lets me change a Unity transform position with just four tabs: enter image description here

Sometimes however, you only need to update the X position. Sometimes you only need to update X and Y, but leave Z untouched. Filling in 0 for the Var keyword

I know I could theoretically create a clone of my current snippet, change it's shortcut to transposX, manually edit the code so that it leaves y and z untouched, but I'd have to do this for all 7 variants.

Is it possible to instead have this all happen in one conditional transpos snippet? What I have in mind is that you select the transpos snippet, and it then asks you in one way or another whether you which variant you want (x, y, z, xy, xz, yz or xyz).

I feel like that would be easier to code than writing 7 different snippets and importing them all.

Here is my current snippet code:

<Snippet>
  <Code Language="CSharp">
    <![CDATA[$transformName$.position = new Vector3($transformName$.position.x + $varX$, $transformName$.position.y + $varY$, $transformName$.position.z + $varZ$);]]>
  </Code>
  <Declarations>
    <Literal>
      <ID>transformName</ID>
      <ToolTip>Variable name of the transform.</ToolTip>
      <Default>transform</Default>
    </Literal>
    <Literal>
      <ID>varX</ID>
      <ToolTip>Position X.</ToolTip>
      <Default>Var</Default>
    </Literal>
    <Literal>
      <ID>varY</ID>
      <ToolTip>Position Y.</ToolTip>
      <Default>Var</Default>
    </Literal>
    <Literal>
      <ID>varZ</ID>
      <ToolTip>Position Z.</ToolTip>
      <Default>Var</Default>
    </Literal>
  </Declarations>
</Snippet>


Solution 1:[1]

You just have to change the default value of each x,y,z Literal to 0.

Then you only have to tab three times or put a variable instead of a 0, if necessary.

Additionally, it's worth mentioning that the "new" keyword is associated with garbage collection, best practice is to cache the data in a temporary variable, modify it, and then set the original to that. As done here:

<Snippet>
  <Declarations>
    <Literal>
      <ID>varPos</ID>
      <ToolTip>Variable name of the position.</ToolTip>
      <Default>position</Default>
    </Literal>
    <Literal>
      <ID>varT</ID>
      <ToolTip>Variable name of the transform.</ToolTip>
      <Default>transform</Default>
    </Literal>
    <Literal>
      <ID>varX</ID>
      <ToolTip>x position.</ToolTip>
      <Default>0</Default>
    </Literal>
    <Literal>
      <ID>varY</ID>
      <ToolTip>y position.</ToolTip>
      <Default>0</Default>
    </Literal>
    <Literal>
      <ID>varZ</ID>
      <ToolTip>z position.</ToolTip>
      <Default>0</Default>
    </Literal>
  </Declarations>
  <Code Language="CSharp">
    <![CDATA[Vector3 $varPos$ = $varT$.position; position.x += $varX$; position.y += $varY$; position.z += $varZ$; $varT$.position = $varPos$;$end$]]>
  </Code>
</Snippet>

Solution 2:[2]

There is no way to make a snippet conditional, but as mentioned in a previous answer you can update the defaults. Here is a strategy I use for just such a scenario where I need to have multiple snippets.

  1. Determine the minimum number of snippets to provide coverage over what you are workign on. I try to keep that minimum no larger than 4.

  2. Instead of filling in the values as they appear in the code, add comment line above the main area with each variable instead in order of logical precedence. Each will then shows the default values that can be accepted or changed in order; by stepping through quickly. Makes life easier than jumping around to fill in the replacements.

Here is one, which initially spanned multiple lines, where I added all the defaults in the comment. Filling in, is easy pea-sy over the defaults:

   -- Create $schema$.$table$.$column$ To $targetSchema$.$targetTable$ FK

      alter table [$schema$].[$table$]  ...
  1. Keep the snippet(s) open in a different editor to adjust default values as needed. VS reloads the snippet on each use, so that is handy to change frequent values.

  2. Create a Visual Studio Replacement Regex for specific scenarios. I have learned regex and visual studio's version of regex and certain situations like one line items, lend themselves to a replacement regex such as to swap values Aval1 = Aval2

\s([\w+])[\s=)([^\s;]+)

replace

$2 = $1;  

Result Aval2 = Aval1;

Everything in parenthesis is a match. Each match has a number for its replacement. The above swaps match 1 for match 2. The stuff not match is discarded.

  1. In your snippet update the defaults to suit the specific need (see #3).
  2. 10 years ago Visual Studio had a Macro editor built in and they removed it. (Booo)). I use Notepad++ and Ultra edit macro editors, to create macros on the fly to do specific repetitive things.

This is truly how I work/have been working for the past 25 years. The fact you are doing snippets, probably puts you ahead of 75% of the users of Visual Studio. Welcome to the club.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nafis
Solution 2 ΩmegaMan