Formatting Literal Parameters of a C# Code Snippet

Formatting Literal parameters of a C# code snippet

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.

Parse Replacement Literal in Visual Studio Snippet

Unfortunately you can't transform your PropertyName into propertyName directlly. The best way to go about this is to include a third literal to specify the backing field. This prevents the ambiguity in your current literal and only adds an extra couple keystrokes.

The Declarations block looks like this:

  <Declarations>
<Literal Editable="true">
<ID>PropertyType</ID>
<Default>object</Default>
</Literal>

<Literal Editable="true">
<ID>PropertyName</ID>
<Default>PropertyName</Default>
</Literal>

<Literal Editable="true">
<ID>BackingPropertyField</ID>
<Default>backingPropertyField</Default>
</Literal>
</Declarations>

And the code block becomes:




    public $PropertyType$ $PropertyName$ {
get {
if (_$BackingPropertyField$ == null)
_$BackingPropertyField$ = new $PropertyType$();
return _$BackingPropertyField$;
} set {
_$BackingPropertyField$ = value;
this.OnPropertyChanged("$PropertyName$");
} }
]]>
</Code>

You can then just tab through and specify the desired names.

C# Code Snippet literal not expanding correctly

Try the following.

<CodeSnippet Format="1.1.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<Header>
<Title>Guard Guid Check</Title>
<Author>Chris Richner</Author>
<Shortcut>gg</Shortcut>
<Description></Description>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
<SnippetType>SurroundsWith</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>argument</ID>
<ToolTip>Enter argument name</ToolTip>
<Default>argument</Default>
</Literal>
</Declarations>
<Code Language="CSharp"><![CDATA[
Guard.Null(nameof($argument$), $argument$);
]]></Code>
</Snippet>
</CodeSnippet>

Looking through your code there are some things that I dont recognize.
1) I am not sure what the $selected$$end$ are for and 2) I am not sure if the language property (csharp) is case sensitive.

I took your information and placed it in a snippet that I know works correctly from VS 2005 to VS 2013. Then I tested the snippet and it worked correctly for me.

How to lower case a Visual Studio Code Snippet variable?

It is not possible to change literals in a Code Snippets. There are some functions available:

GenerateSwitchCases - Generates a switch statement and a set of case statements for the members of the enumeration specified by the EnumerationLiteral parameter. The EnumerationLiteral parameter must be either a reference to an enumeration literal or an enumeration type.

ClassName - Returns the name of the class that contains the inserted snippet.

SimpleTypeName - Reduces the TypeName parameter to its simplest form in the context in which the snippet was invoked.

But they cannot modify literals.

Source: http://msdn.microsoft.com/en-us/library/ms242312(v=vs.110).aspx

using a Code Snippet for INotifyPropertyChanged

I don't think this can be done with native code snippets feature provided by Visual Studio.

Personally I use Resharper which makes it possible. It can turn code I write like

public string Name { get; set; }

into

private string _name;
public string Name
{
get { return _name; }
set
{
if(value == _name)
return;
_name = value;
OnPropertyChanged("Name");
}
}

It even generates the OnPropertyChanged() method for you.

Visual Studio's code snippet: how to add logic in it?

Unfortunately this type of logic is not available in the Visual Studio snippets functionality.
Having to type out both names is the best you can do.

Here are the only "functions" available to you when creating a code snippet.
MSDN Code Snippet Functions

Products like Resharper provide excellent code snippet (called Templates in Resharper) functionality, with the ability to change the casing of other replacements within the snippet, among many other useful functions.
Resharper Template Info

For example, you would be interested in this macro:

"Value of another variable with the first character in lower case"

Resharper string.format shortcut

Just type it in dumber:

 "Hello " + world

Alt+EnterEnter, done1:

 string.Format("Hello {0}", world);

Obviously, this also works when the whole thing is much more complex. I know it will strip useless calls to .ToString(), and I suspect it will automatically lift any format expressions, like

 int i = 42;
"i = " + i.ToString("X2");

Alt+EnterEnter

 string.Format("i = {0:X2}", i);

1 If you're unlucky/the surrounding code contains many things that trigger Resharper suggestions(?) you might have to position the cursor over one of the + operators



Related Topics



Leave a reply



Submit