📜 ⬆️ ⬇️

What is Visual Studio .NET snippets? Part two.

This is the second part of the article about snippets. The first was an explanation of what the mechanism is, what it is for, and how to apply it. The second part will discuss how to create snippets.

For example, let's take a very simple generalized situation: the need to store hidden temporary data in the body of the page. Previously, hidden fields were used for this, but asp.net has a ViewState mechanism. I will not touch the topic of using ViewState, its impact on performance and page size. Obviously, you need to use it carefully. So, create a snippet for this code construction:
public int ViewStatePropperty { get { object o = ViewState [ "ViewStatePropperty" ]; return (o! = null )? ( int ) o: 0; } set {ViewState [ "ViewStatePropperty" ] = value ; }} * This source code was highlighted with Source Code Highlighter .


The simplest property whose value will not be stored in a private field, as usual, but in ViewState. Notice that there are three variable elements in this code: type, name, and default value. Moreover, the type string in the code is used twice, and the name string is used three times. Now let's define, finally a snippet. In fact, the snippet is a specially defined xml file and here is the implementation for our example:
<? xml version = "1.0" encoding = "utf-8" ? >
< CodeSnippets xmlns = " schemas.microsoft.com/VisualStudio/2005/CodeSnippet " >
< CodeSnippet Format = "1.0.0" >
< Header >
< Title > propv </ Title >
< Shortcut > propv </ Shortcut >
< Description > A simple snippet example </ Description >
< Author > Xaoc, for the Habrahabr project </ Author >
< SnippetTypes >
< SnippetType > Expansion </ SnippetType >
</ SnippetTypes >
</ Header >
< Snippet >
< Declarations >
< Literal >
< Id > type </ id >
< ToolTip > Property type </ ToolTip >
< Default > int </ default >
</ Literal >
< Literal >
< ID > property </ ID >
< ToolTip > Property name </ ToolTip >
< Default > MyProperty </ Default >
</ Literal >
< Literal >
< Id > default </ id >
< ToolTip > Default value </ ToolTip >
< Default > 0 </ Default >
</ Literal >
</ Declarations >
< Code Language = "csharp" > <! [CDATA [public $ type $ $ property $
{
get
{
object o = ViewState ["$ property $"];
return (o! = null)? ($ type $) o: $ default $;
}
set
{
Viewstate ["$ property $"] = value;
}
} $ end $]] >
</ Code >
</ Snippet >
</ CodeSnippet >
</ CodeSnippets > This is a source code highlighted with Source Code Highlighter .

So, what is the description of this snippet. The main tags are: Header, which describes the outer part, and Snippet, which describes the logic.

In Header, tags with values ​​are defined: names of a snippet, shortcut (this is the name by which you can quickly call a snippet), description, author, and type of snippet. The snippet type can have three values: SurroundsWith, Expansion and Refactoring. The first indicates that the snippet will wrap the selected text (as the standard region snippet does), the second indicates that the text of the snippet is simply inserted, and the third is used for the refactoring mechanism built into the studio and cannot be used in custom snippets.
')
In Snippet, literals are declared, the value of which can be edited when using the snippet, and the snippet code itself. Notice how the literal is defined: its id, hint, and default value. The literal identifier is then used in the body of the snippet code. To distinguish a literal, it is placed between two dollar signs “$”. The Code tag contains, in addition to defining the code of the snippet, several attributes: Language, which points to the target language of the snippet and can be VB, CSharp, VJSharp, or XML; Delimiter, which allows you to redefine the dollar sign to another character; and Kind, which allows you to specify the scope of the snippet and takes the values ​​"method body", "method decl", "type decl", file, or any. In our case, Kind is omitted, but it can be specified as a “type decl”.

After creating a snippet file, you need to somehow inject it into the studio. To do this, use the standard “Code Snippets Manager” dialog in the Tools menu of the main menu of the tool. If you don’t have it there, it means your enviroment is set up and you can find the menu item in Tools \ Customize - Commands - Tools. You can make it easier and call the dialog shortcut ctrl + k, ctrl + b. In the window, you can specify in which section to save the snippet and click the import button to import your snippet into the studio. After that you can type propv in the editor and press tab. You should add a text like this:
public int MyProperty { get { object o = ViewState [ "MyProperty" ]; return (o! = null )? ( int ) o: 0 ; } set {ViewState [ "MyProperty" ] = value ; }} * This source code was highlighted with Source Code Highlighter .


That's all. For completeness I will give a link to the description of the schema of the xml-file of the snippet:
http: //msdn.microsoft.com/ru-ru/library/ms171418 (en-us, vs.80) .aspx (unfortunately, because of the brackets in the URL, Habr incorrectly selects the hyperlink, copy it to the browser as it is ). There you can learn additional knowledge and learn how to make even more complex and useful snippets.

PS: “there are no errors in the article!” - unfortunately, this is impossible to say, but I will be glad if you pay my attention to the error you found.

Source: https://habr.com/ru/post/25095/


All Articles