Create your own code snippets (CodeSnippet) and snippets codesnippet

Source: Internet
Author: User

Create your own code snippets (CodeSnippet) and snippets codesnippet
Preface

At work, we often write repeated code snippets, such as automatic attributes, for loops, and actions. In this case, Visual Studio has provided us with a very convenient feature-code snippets, we can simply enter a few letters to generate a large code segment.

However, some repetitive code is not provided by Visual Studio at work, so we need to compile the snippet template by ourselves, this article describes how to create and use your own code snippets.

I. Where is the template file?

In tool-code snippet Manager (shortcut: Ctrl + K, B), you can open the code snippet manager, for example:

Select the language to which the template belongs. Then you can see the group of the language. Click the group to view the segment template in the group, for example:

Here we can see the path of the template.

Enter the path from the resource manager and you will see the template file with the suffix Snippet. The file is essentially an XML file, which can be opened and edited directly in VS or notepad.

Ii. Template File Format

The opened template file is like this (for is used as an example here). I made remarks on the meaning of each node.

 

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <CodeSnippets xmlns =" http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet "> 3 <CodeSnippet Format =" 1.0.0 "> 4 <Header> 5 <Title> for </Title> <! -- The title displayed on the prompt page and manager --> 6 <Shortcut> for </Shortcut> <! -- Shortcut code for calling and shortcuts in the manager --> 7 <Description> for loop code snippets </Description> <! -- Description displayed on the prompt interface and manager --> 8 <Author> Microsoft Corporation </Author> <! -- Display the author information in the manager --> 9 <SnippetTypes> <! -- Display the fragment type in the manager. Generally, only Expansion is retained. --> 10 <SnippetType> Expansion </SnippetType> <! -- Extension type. Basically, all part templates contain this type --> 11 <SnippetType> SurroundsWith </SnippetType> <! -- Outer code: you can insert this code block outside the Code where the cursor is located. The shortcut key is Ctrl + K, S --> 12 <! -- Some templates still belong to the Refactoring type. There is no shortcut key or Expansion type for this type, we will not describe here --> 13 </SnippetTypes> 14 </Header> 15 <Snippet> 16 <Declarations> <! -- Template variable --> 17 <Literal> 18 <ID> index </ID> <! -- Variable ID, called in the template by $ ID $ --> 19 <Default> I </Default> <! -- Default variable value --> 20 <ToolTip> index </ToolTip> <! -- Comment the variable when the focus is on the variable --> 21 </Literal> 22 <Literal> 23 <ID> max </ID> 24 <Default> length </Default> 25 <toolTip> maximum length </ToolTip> 26 </Literal> 27 </Declarations> 28 <! -- The following is the template content --> 29 <Code Language = "csharp"> <! [CDATA [for (int $ index $ = 0; $ index $ <$ max $; $ index $ ++) 30 {31 $ selected $ end $32}]> 33 </Code> 34 </Snippet> 35 </CodeSnippet> 36 </CodeSnippets>

When creating a template, we can directly export a VS built-in template to make changes. The Code body is written in the Code mark and used <! [CDATA [and]>

We can see that the preceding template does not declare two identifiers in Declarations, as shown below:

$ Selecteds $ because the code snippet is an extension type, you can add (Ctrl + K, S) to the periphery of the currently selected code During encoding, so the identifier represents the currently selected code

$ End $ This identifier indicates the position of the cursor at the end (after the variable is adjusted, press Enter or start with no variable fragment ).

Iii. Template File Import

This is simple. Copy the edited template directly to the path where the system template is located.

 

Author: Vulper

You are welcome to reprint it. Please indicate the source for reprinting!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.