14

我想知道是否有办法制作#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>

第二次编辑: 它有效,但只有当我制作插入片段时。我猜从智能感知这个使用其他一些片段。

那么有没有办法从智能感知而不是从插入片段菜单中添加我的区域?

4

7 回答 7

13

如果你想要实现的是......

#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 中使用它,请将代码片段放在 .snippet 文件中并将其保存在代码片段目录中,然后转到工具 => 代码片段管理器 => 添加。添加后,您可以使用标准的 CTRK K+X 来访问它。

这为您提供了区域内置片段的唯一功能是添加尾随注释以指示区域结束的灵活性。您还可以通过添加额外的扩展来进一步自定义它。

注意:前哨 $end$ 标记了您希望光标在操作完成后到达的位置。

于 2013-08-08T09:42:19.060 回答
9

视觉工作室 2017

键入#r TAB TAB,然后键入区域的名称。

这是内置的行为。

于 2017-03-30T18:50:17.423 回答
3

按 Control + K、S 并选择区域

于 2017-07-13T11:54:45.103 回答
1

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

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

更新:

奇怪,但是如果您更改默认的#region 模板,则没有任何效果。您需要定义自己的模板,为其设置一个片段(即reg)并将上面编写的代码放入其中。

于 2013-08-08T09:16:06.920 回答
1

我推荐VSCommands

查看“代码块结束标记器改进”部分

编辑 25.08.2014

它将代码块的开头(方法名称 aso.)作为浅灰色超链接放在代码块的末尾。作为超链接,因为它是可点击的,您可以导航到代码块的开头。

于 2014-08-18T11:15:26.343 回答
1

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

于 2016-08-28T02:36:52.887 回答
-3

你不需要。

你可以这样做:

#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
于 2013-08-08T09:14:00.800 回答