Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically Add Regions to Code in Visual Studio

My team absolutely loves using regions, and with that in mind it's pretty much become a de-facto standard in our code. I recently came to realization that I'm sick of writing or ctrl+c / ctrl+v'ing these in every time I create a class, test method, etc...

I was wondering if it is possible (via macros or some other functionality) to have Visual Studio automatically add these into your code.

For example, If I add a new class file to my project, can you perform some sort of magic to have visual studio generate the file as:

namespace Test
{
    class MyClass
    {
        #region ------------ members --------------
        #endregion

        #region ------------ properties --------------
        #endregion

        #region ------------ methods --------------
        #endregion
    }
}

Where I really get annoyed by not currently knowing how to do this, is when I'm writing unit tests. This may be a bit trickier, but I was trying to find a way to add --set up-- and --run test-- regions automatically to test methods because our team is adamant about using them.

So, when I go to create a new test method

[TestMethod]
public void WhenCondition_WillProduceExpectedResult()
{
}

Visual Studio would automatically add these two regions to the method, such as:

[TestMethod]
public void WhenCondition_WillProduceExpectedResult()
{
   #region ------------- set up -------------
   #endregion 

   #region ------------- run test -------------
   #endregion 
}

Not sure if this can be done, and if it can, whether it'd be via a vs-macro, or extension. Any help is much appreciated!

like image 608
Eric Stallcup Avatar asked Nov 26 '12 15:11

Eric Stallcup


People also ask

How do I expand all regions in Visual Studio?

CTRL + M expands region.

How do I change the region in Visual Studio?

Use the #Region directive to specify a block of code to expand or collapse when using the outlining feature of Visual Studio IDE. You can place, or nest, regions within other regions to group similar regions together.

How do you declare a region in C#?

Where to use #region directive in C#? It lets you specify a block of code that you can expand or collapse when using the outlining feature of the Visual Studio Code Editor. It should be terminated with #endregion. Let us see how to define a region using #region.


1 Answers

You could create a simple code snippet like the following one:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
    xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Simple</Title>
      <Shortcut>simple</Shortcut>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>name</ID>
          <ToolTip>Replace with the name of the action</ToolTip>
          <Default>Action</Default>
        </Literal>
      </Declarations>
      <Code Language="csharp">
        <![CDATA[
        public void $name$()
        {
            #region ------------- set up -------------
            #endregion 

            #region ------------- run test -------------
            #endregion 
        }
        ]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

Save that file into C:\Users\<your_user>\Documents\Visual Studio 2010\Code Snippets\Visual C#\My Code Snippets.

Now you just need to reopen Visual Studio, type 'simple' into a class and press Tab key twice.

like image 159
Glauco Vinicius Avatar answered Sep 20 '22 12:09

Glauco Vinicius