Using Visual Studio 2013 Express, I thought I'd try creating a snippet that declares and instantiates a new object.
It should look like this (with the default Object and val, and the cursor at the specified place:
Object val = new Object(<cursor_here>);
But my snippet does not work; it does not display the default values Object and val. It looks like this:
= new (<cursor_here>);
...and I have to manually enter the Type (twice) and variable name.
What did I do wrong? Here is the snippet XML:
<?xml version="1.0" encoding="UTF-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2008/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Declare New</Title>
<SnippetTypes>
<SnyppetType>Expansion</SnyppetType>
</SnippetTypes>
</Header>
<Snippet>
<Literal Editable="true">
<ID>thing</ID>
<Default>Object</Default>
</Literal>
<Literal Editable="true">
<ID>name</ID>
<Default>val</Default>
</Literal>
<Code Language="CSharp" Kind="method body">
<![CDATA[$thing$ $name$ = new $thing$($end$)]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
Related
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?
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,
When I am inserting a snippet into the source code the literals immediately get replaced with empty string and there is no prompt to enter a value for them. How do you insert a snippet to get prompt for the literals? For example here is a small snippet:
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
<Title>Test</Title>
<Author>RM</Author>
<Description>New Transaction</Description>
<HelpUrl>
</HelpUrl>
<Shortcut>test</Shortcut>
</Header>
<Snippet>
<Declarations>
<Literal Editable="true">
<ID>test</ID>
<ToolTip>tool tip</ToolTip>
<Default>Test
</Default>
<Function></Function>
</Literal>
</Declarations>
<Code Language="csharp" Delimiter="$"><![CDATA[$test$
$test$
$test$$end$]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
UPDATE: I have updated the script as was suggested by adding a default value and $end$ however this is still not working. If I insert the snippet (using Ctrl-K, X) the default value is inserted. When I change one of the values only that one is changing but not the others.
You need to set the
<Default></Default>
to something. That is needed to give the snippet something to highlight for editing.
Also you should add
$end$
at the end of the code part.
To get the text to update just enter your new text and press enter and all three variables should update.
Try:
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
<Title>Test</Title>
<Author>RM</Author>
<Description>New Transaction</Description>
<HelpUrl>
</HelpUrl>
<Shortcut>test123</Shortcut>
</Header>
<Snippet>
<Declarations>
<Literal Editable="true">
<ID>test</ID>
<ToolTip>tool tip</ToolTip>
<Default>test</Default>
<Function></Function>
</Literal>
</Declarations>
<Code Language="csharp" Delimiter="$"><![CDATA[$test$
$test$
$test$$end$]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
I am trying to create a Visual Studio snippet and cannot get it to add basically a blank line. See below for my example:
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.1.0">
<Header>
<Title>Bootstrap Row</Title>
<Shortcut>brow</Shortcut>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
<SnippetType>SurroundsWith</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Code Language="HTML"><![CDATA[<div class="row">
$selected$$end$
</div>]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
What I am trying to get is:
<div class="row">
CursorHere
</div>
What I actually am getting is:
<div class="row">
CursorHere</div>
Any tips or tricks to get this to do what im trying?
Thanks!
Insert $end$ just before ]] in CDATA block. Full specification: https://learn.microsoft.com/en-us/visualstudio/ide/code-snippets-schema-reference?view=vs-2019#code
By telling VS where it should place the cursor after the snippet is inserted, you prevent the new line and placing the cursor before the inserted code.
Here is one thing I have tried and it worked but not sure if there is still not a better way out there!
I added basically a literal to the snippet and just gave it a default of "code" so it works how I wanted and I can just begin typing to overwrite what I actually want there.
Here it is:
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.1.0">
<Header>
<Title>Bootstrap Row</Title>
<Shortcut>brow</Shortcut>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
<SnippetType>SurroundsWith</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>classes</ID>
<ToolTip>Add Addition Classes</ToolTip>
<Default></Default>
</Literal>
<Literal>
<ID>text</ID>
<ToolTip></ToolTip>
<Default>code</Default>
</Literal>
</Declarations>
<Code Language="HTML"><![CDATA[<div class="row $classes$">
$selected$$text$$end$
</div>]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
Output:
<div class="row ">
code
</div>
Like I said above code is already selected so you can just start typing without having to delete.
Thanks
I'm often writing a c# code that goes like this:
void func(object myObject)
{
if (myObject == null)
{
throw new ArgumentNullException("myObject");
}
...
How do I write an auto-complete in Visual Studio 2012 for that type of code, so that I don't have to keep typing this all the time?
Since there hasn't been an interest, I'm going to close on this question by posting my own solution. Putting following file into C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC#\Snippets\1033\Visual C#\ifn.snippet will do the trick:
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>ifn</Title>
<Shortcut>ifn</Shortcut>
<Description>Code snippet for if (arg==null)</Description>
<Author>sam green</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
<SnippetType>SurroundsWith</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>argument</ID>
<Default>arg</Default>
<ToolTip>Argument</ToolTip>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[if ($argument$ == null)
{
throw new ArgumentException("$argument$");
}$end$]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
I defined a ReSharper shortcut for this using the keywords "argnull" that auto-generates and guesses the parameter I want. It's about the same effect as your solution, I think, though you don't need to muck with XML or where to place files.
Here's an example of a ReSharper VB.NET approach:
if $VAR$ is nothing then
throw new ArgumentNullException("$VAR$")
end if
To improve on #galet's answer, here's mine:
Uses a succinct single-line if() throw; statement.
Uses the more precise ArgumentNullException instead of ArgumentException.
Uses nameof() to avoid magic-strings (and so will work perfectly with Refactor-Renames).
Uses the (safer) is null operator:
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>ifn</Title>
<Shortcut>ifn</Shortcut>
<Description>Code snippet for if (arg==null)</Description>
<Author>https://stackoverflow.com/questions/18905475</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
<SnippetType>SurroundsWith</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>argument</ID>
<Default>arg</Default>
<ToolTip>Argument</ToolTip>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[if( $argument$ is null ) throw new ArgumentNullException( nameof($argument$) );$end$]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
I have a very simple code snitppet that looks like this:
<asp:Label Text="$Name$" runat="server" AssociatedControlID="$txtName$" />
<asp:TextBox runat="server" ID="$txtName$" />
<br />
What I was hoping was that the $txtName$ would be changed in both places, but it doesn't, only the first instance is changed and I can't even tab over to the second instance.
Is there a work around to this?
This should work and does for me.
Can you show the complete snippet file? Here is one that works, type and fieldName are replaced multiple times:
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<Header>
<Title>Non-automatically implemented property</Title>
<Author>Richard Cox</Author>
<Shortcut>propf</Shortcut>
<Description>Property with exlicit field</Description>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>propName</ID>
<ToolTip>Property Name</ToolTip>
<Default>Name</Default>
</Literal>
<Literal>
<ID>fieldName</ID>
<ToolTip>Field Name</ToolTip>
<Default>field</Default>
</Literal>
<Literal>
<ID>type</ID>
<ToolTip>Property type</ToolTip>
<Default>string</Default>
</Literal>
</Declarations>
<Code Language="csharp">
<![CDATA[private $type$ $fieldName$;
public $type$ $propName$ {
get { return $fieldName$;}
set { $fieldName$ = value;}
}
$end$]]>
</Code>
</Snippet>
</CodeSnippet>
It turns out that the replacement was working, but not as I typed. So I can type what I need and then to get the additional properties changed I have to take the focus away from the snippet, so pressing the down arrow key does the job.
interesting scenario starts when you would like to create surround snippet with $selected$ twice. for instance to create CheckArgumentForNull snippet to check argument for null and in case of null raise ArgumentNullException with parameter name. Only later got replaced.
Any ideas? Of course we can use expansion only snippet and type argument name by ourselves.
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
<SnippetType>SurroundsWith</SnippetType>
</SnippetTypes>
<Title>notnull</Title>
<Author>Viiar Consulting</Author>
<Description>Code snippet for checking whether is argument null</Description>
<HelpUrl>
</HelpUrl>
<Shortcut>notnull</Shortcut>
</Header>
<Snippet>
<Imports>
<Import>
<Namespace>System</Namespace>
</Import>
</Imports>
<Declarations>
<Literal Editable="true">
<ID>argument</ID>
<ToolTip>
</ToolTip>
<Default>argument</Default>
<Function>
</Function>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[if ($selected$ == null)
{
throw new ArgumentNullException("$selected$");
}
$end$]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>