Create shortcut of type 'prop' or 'propfull' in Visual Studio

Asked

Viewed 613 times

7

I would like to create within Visual Studio a shortcut of the type prop or propfull to automatically create a code snippet.

the prop + TAB + TAB

public int MyProperty { get; set; }

the propfull + TAB + TAB

private int myVar;

public int MyProperty
{
    get { return myVar; }
    set { myVar = value; }
}

I wanted to create a shortcut for it to create a chunk of code that I need to repeat several times, someone knows if this is possible?

2 answers

7


Yes, it’s possible, it’s called snippet. The documentation of the resource is found here.

Go to the Tools menu -> Code Snippets Manager and import the snippet that you created. The file that contains your definition follows this template:

<?xml version="1.0" encoding="utf-8"?>  
<CodeSnippets  
    xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">  
    <CodeSnippet Format="1.0.0">  
        <Header>  
            <Title></Title>  
        </Header>  
        <Snippet>  
            <Code Language="">  
                <![CDATA[]]>  
            </Code>  
        </Snippet>  
    </CodeSnippet>  
</CodeSnippets>

In CDATA will the code you want expanded when you enter the chosen keyword (<Shortcut>hello</Shortcut>). It is possible to create "variables" in the template ($SqlConnString$):

<Declarations>  
    <Literal>  
        <ID>SqlConnString</ID>  
        <ToolTip>Replace with a SQL connection string.</ToolTip>  
        <Default>"SQL connection string"</Default>  
    </Literal>  
    <Object>  
        <ID>SqlConnection</ID>  
        <Type>System.Data.SqlClient.SqlConnection</Type>  
        <ToolTip>Replace with a connection object in your application.</ToolTip>  
        <Default>dcConnection</Default>  
    </Object>  
</Declarations>

Has a plugin that helps. Has one from Microsoft. Had the Snippeteditor, but was abandoned. There are also others better commercials.

But it’s nothing compared to available on Resharper. It’s another life.

Before you see if you have not ready. There are several collections of snippets ready.

4

Yes it is possible, this is called a code snippet or in English Snippet, install a Snippet Desinger (totally gratuitous) as the figure below accessed by the menu: Tools -> Extensions and Updates:

inserir a descrição da imagem aqui

and look for the Snippet Desinger:

inserir a descrição da imagem aqui

After installing and restarting your Visual Studio it is simple to create Snippet to use in your projects, in the menu File -> New -> File:

inserir a descrição da imagem aqui

seek the Code Snippet:

inserir a descrição da imagem aqui

will open a screen with the following feature:

inserir a descrição da imagem aqui

following the upper part of configuration:

  • Snippet: Name of the created Snippet
  • Language: Choose the language
  • Shortcut: Snippet name, surname
  • In the gray part the code, you want to create from this shortcut where it can even contain typing parameters that are the texts that are between $.

In this case a Snippet, for SqlConnection as an example: typed sqlcon and pressing TAB that code snippet will be created and in the connection parameter part to type, note:

inserir a descrição da imagem aqui

with two TAB is created:

inserir a descrição da imagem aqui

References:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.