Automatically create #region with same name at #endregion
Asked Answered
G

7

18

I'm wondering if there is a way to make #region Some Region #endregion Some Region. If there is no way for doing it then maybe is it possible with Resharper?

Hope it's clear what I'm trying to achive here.

Edit:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
    <Header>
        <Title>#region</Title>
        <Shortcut>#region</Shortcut>
        <Description>Code snippet for #region</Description>
        <Author>Microsoft Corporation</Author>
        <SnippetTypes>
            <SnippetType>Expansion</SnippetType>
            <SnippetType>SurroundsWith</SnippetType>
        </SnippetTypes>
    </Header>
    <Snippet>
        <Declarations>
            <Literal>
                <ID>name</ID>
                <ToolTip>Region name</ToolTip>
                <Default>MyRegion</Default>
            </Literal>
        </Declarations>
        <Code Language="csharp"><![CDATA[#region $name$
    $selected$ $end$
#endregion $name$]]>
        </Code>
    </Snippet>
</CodeSnippet>
</CodeSnippets>

Second edit: It's work but only when I make insert snippet. From intellisense this using some other snippet I gues.

So is there a way to add my region from intellisense not from insert snippet menu?

Geocentric answered 8/8, 2013 at 9:12 Comment(3)
In Visual studio you can do it by selecting the text -> Right click -> surround with -> RegionKarney
I guess VS doesnt support that, connect.microsoft.com/VisualStudio/feedback/details/266197/…Bratislava
I don't think this is a good idea. If you ever change the name of the region, you will probably forget to do so in the #endregion part, creating confusing inconsistencies.Jarnagin
A
19

If what you are trying to acheive is...

#region MyRegion
//...lots of code...
#endregion // end of MyRegion

You can do this with a so-called 'SurroundsWith' snippet. Here is such a snippet from my library...

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippet Format="1.0.0"    
   xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <Header>
    <Title>Enregions a block of code</Title>
    <Author>GJV</Author>
    <Shortcut>enr</Shortcut>
    <Description>Surrounds a block of code with region directives</Description>
    <SnippetTypes>
      <SnippetType>SurroundsWith</SnippetType>
      <SnippetType>Expansion</SnippetType>
    </SnippetTypes>
  </Header>
  <Snippet>
    <Declarations>
      <Literal Editable="true">
        <ID>RegionName</ID>
        <ToolTip>Region Name</ToolTip>
        <Default>MyRegion</Default>
      </Literal>
    </Declarations>
    <Code Language="CSharp">  
    <![CDATA[#region $RegionName$$end$         
    $selected$    
    #endregion // end of $RegionName$]]>        
    </Code>
  </Snippet>
</CodeSnippet>

To use it in Visual Studio, put the snippet in a .snippet file and save it in your snippets directory, then go to Tools => Code Snippets Manager => Add. Once it's added, you can use the standard CTRK K+X to access it.

The only thing this gives you over the built-in snippet for region is the flexibility to add the trailing comment to indicate the region's end. You can also further customise this by adding additional expansions.

NOTE: the sentinal $end$ marks where you want the cursor to land when the operation is complete.

Armilla answered 8/8, 2013 at 9:42 Comment(0)
C
9

Visual Studio 2017

Type #r TAB TAB, then type the name of the region.

This is built in behavior.

Cupriferous answered 30/3, 2017 at 18:50 Comment(1)
While this works, It does not add the name of the region at the endregionBilicki
M
6

press Control + K, S and select Region

Mendie answered 13/7, 2017 at 11:54 Comment(0)
C
1

I recommend VSCommands.

Have a look at the part "Code Block End Tagger Improvements"

Edit 25.08.2014

It will put the beginning of the code block (method name aso.) as light grey hyperlink at the end of the code block. As hyperlink because it is clickable and you can navigate to the beginning of the code block.

Caricaria answered 18/8, 2014 at 11:15 Comment(0)
P
1

The built-in version of Visual Studio is Ctrl K+X

Pi answered 28/8, 2016 at 2:36 Comment(1)
That doesn't add the same name in endregion, as the question requested.Halfwitted
W
0

You can change ReSharper default template for #region to:

#region $name$
    $END$
#endregion $name$

Update:

Strange, but if you change default #region template, nothing works. You need to define your own template, set a snippet for it (i.e. reg) and put the code written above in it.

Wholly answered 8/8, 2013 at 9:16 Comment(3)
how to use this override template? I done it but still it using old one :(Geocentric
Updated answer. Try to create new template.Wholly
I've edited build in template for region but it seems to not working ;/ I made edit my post with how I change itGeocentric
O
-3

You don't need to.

You can just do this:

#region Some Region
//I
//am
//assuming
//a
//lot
//of
//code
//you
//want
//to
//hide
//goes
//here
//here
#endregion
//note that it doesn't say Some Region in the endregion
Olcott answered 8/8, 2013 at 9:14 Comment(3)
I think he wants the end region to specify the region name?Iguanodon
@christiandev I know, but why? It makes no difference, unless he wants to do something like this: #region 1 #region 2 #endregion 1 #endregion 2 and that will just cause VS a load of confusion.Beseech
With a large class, the regions are often large and having the #endregion indicate which #region it corresponds to (since the start of the region may be off screen) can help one discern the current location in one's code. I don't think the OP is attempting to nest/overlap regions. I think the OP is trying to do something similar to comments one might leave after a block's closing brace, e.g. if(x) { ... } // end if(x) ... but imagine the body was large and multi-line. It's nothing to help the compiler; just the guy behind the keyboard.Dedededen

© 2022 - 2024 — McMap. All rights reserved.