【问题标题】:Automatically create #region with same name at #endregion在#endregion 自动创建具有相同名称的#region
【发布时间】:2013-08-09 22:27:56
【问题描述】:

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

第二次修改: 这是有效的,但只有当我插入 sn-p 时。从智能感知这使用其他一些 sn-p 我猜。

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

【问题讨论】:

  • 在 Visual Studio 中,您可以通过选择文本 -> 右键单击​​ -> 环绕 -> 区域来实现
  • 我认为这不是一个好主意。如果您更改了区域名称,您可能会忘记在 #endregion 部分中这样做,从而造成令人困惑的不一致。

标签: c# .net visual-studio resharper code-snippets


【解决方案1】:

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

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

您可以使用所谓的“SurroundsWith”sn-p 来做到这一点。这是我图书馆里的这样一个 sn-p...

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

与内置的 sn-p for region 相比,它唯一能让您灵活地添加尾随注释以指示区域的结束。您还可以通过添加额外的扩展来进一步自定义它。

注意:句号 $end$ 标记了操作完成后您希望光标所在的位置。

【讨论】:

    【解决方案2】:

    Visual Studio 2017

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

    这是内置的行为。

    【讨论】:

    • 虽然这有效,但它不会在 endregion 处添加区域名称
    【解决方案3】:

    按 Control + K、S 并选择区域

    【讨论】:

      【解决方案4】:

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

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

      更新:

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

      【讨论】:

      • 如何使用这个覆盖模板?我做到了,但仍然使用旧的:(
      • 更新了答案。尝试创建新模板。
      • 我已经为区域编辑了内置模板,但它似乎不起作用;/我编辑了我的帖子并修改了它
      【解决方案5】:

      我推荐VSCommands

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

      编辑 25.08.2014

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

      【讨论】:

        【解决方案6】:

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

        【讨论】:

        • 这不会在 endregion 中添加与问题所要求的相同的名称。
        【解决方案7】:

        你不需要。

        你可以这样做:

        #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
        

        【讨论】:

        • 我想他是想让结束区域指定区域名称?
        • @christiandev 我知道,但为什么呢?没有区别,除非他想做这样的事情:#region 1 #region 2 #endregion 1 #endregion 2,这只会导致 VS 混乱。
        • 对于一个大类,区域通常很大,并且#endregion 指示它对应于哪个#region(因为区域的开头可能不在屏幕上)可以帮助人们辨别当前位置一个人的代码。我不认为 OP 试图嵌套/重叠区域。我认为 OP 正在尝试做一些类似于 cmets 的事情,在一个块的右括号之后可能会离开,例如if(x) { ... } // end if(x) ...但想象一下身体很大而且多行。对编译器没有任何帮助;只是键盘后面的那个人。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-05
        • 1970-01-01
        相关资源
        最近更新 更多