在#endregion处自动创建具有相同名称的#region



我想知道是否有办法制作#region Some Region #endregion Some Region。如果没有办法做到这一点,那么使用Resharper可能吗?

希望我在这里想达到的目标很清楚。

编辑:

<?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>

第二次编辑:这是工作,但只有当我制作插入片段时。从intellisense这使用了一些其他片段,我猜测。

那么,有没有一种方法可以从intellisense而不是从插入片段菜单添加我的区域?

如果你想获得的是…

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

您可以使用所谓的"SurroundsWith"片段来完成此操作。这是我图书馆里的一个片段。。。

<?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>

若要在Visual Studio中使用它,请将代码段放在.sippet文件中,并将其保存在代码段目录中,然后转到"工具"=>代码段管理器=>添加添加后,您可以使用标准CTRK K+X访问它。

在内置的region代码段中,它唯一能给你的是添加尾部注释以指示region结束的灵活性。您还可以通过添加额外的扩展来进一步自定义。

注意:当操作完成时,sentinal$end$标记您希望光标落在的位置。

Visual Studio 2017

键入#rTAB1TAB,然后键入区域名称。

这是内置的行为。

按Control+K,S并选择Region

我推荐VSCommands。

看看部分"代码块结束标记改进"

编辑2014年8月25日

它将把代码块的开头(方法名aso。)作为代码块末尾的浅灰色超链接。作为超链接,因为它是可单击的,并且您可以导航到代码块的开头。

Visual Studio的内置版本是Ctrl K+X

您可以将#region的ReSharper默认模板更改为:

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

更新:

奇怪,但如果您更改默认的#区域模板,则什么都不起作用。您需要定义自己的模板,为其设置一个代码段(即reg),并将上面写的代码放入其中。

您不需要。

你可以这样做:

#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

最新更新