【问题标题】:Snippet inserts extra newline in VS2015Snippet 在 VS2015 中插入额外的换行符
【发布时间】:2016-03-18 10:09:02
【问题描述】:

我制作了一个自定义 sn-p 用于 Visual Studio。在 VS2013 中,它按预期工作,但自从在 VS2015(社区版)中使用它以来,它一直在代码前插入一个额外的换行符(就在我第二次按 tab/enter 时)。

这似乎只是自定义 sn-p 的问题(内置的可以正常工作)。有谁知道这可能是什么原因造成的?这很烦人。

附带说明,这仅在我在空代码行上激活 sn-p 时才会发生。如果我在现有代码之后执行此操作,则不会插入换行符。不幸的是,sn-p 是一个语句,所以这并没有多大帮助。

这是 sn-p,几乎完全从 VS 示例中复制:

<?xml version="1.0" encoding="utf-8" ?> 
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/CodeSnippet">
<CodeSnippet Format="1.0.0">

    <!-- The header contains information describing the snippet. -->
    <Header>

      <!-- The Title of the snippet, this will be shown in the snippets manager. -->
      <Title>Insert Field Add</Title>

      <!-- The description of the snippet. -->
      <Description>Inserts a basic field add for a DataObject</Description>

      <!-- The author of the snippet. -->
      <Author>Thomas Price</Author>

      <!-- The set of characters that must be keyed in to insert the snippet. -->
      <Shortcut>fadd</Shortcut>

      <!-- The set of snippet types we're dealing with - either Expansion or -->
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>          

    </Header>

    <!-- Now we have the snippet itself. -->
    <Snippet>
        <!-- Create any declarations that we use in the snippet. -->
        <Declarations>
          <Literal>
            <ID>FieldName</ID>
            <ToolTip>Enter the field name</ToolTip>
            <Default>field</Default>
          </Literal>
        </Declarations>

        <!-- Sepecify the code language and the actual snippet content. -->
        <Code Language="CSharp" Kind="any">
            <![CDATA[$FieldName$ = fields.add($FieldName$, "$FieldName$");]]>
        </Code>
    </Snippet>
</CodeSnippet>

【问题讨论】:

标签: c# visual-studio visual-studio-2015 code-snippets


【解决方案1】:

您可以通过在 sn-p 文本中的某处放置 $end$ 来阻止前面的换行符。示例:

<![CDATA[$FieldName$ = fields.add($FieldName$, "$FieldName$");$end$]]>

【讨论】:

  • 还有$start$吗?
  • 这是full specification reference。只有“Code 元素的文本中有两个保留字可供使用:$end$$selected$。” @Ciwan
  • 这似乎很明显,但要使其正常工作,您不能在 CDATA[ 括号后添加换行符,即您必须在其后立即开始您的 sn-p。我有一个多行 sn-p,在 CDATA[ 之后很自然地想要一个换行符,并且由于我之前尝试过删除换行符而没有任何效果,所以我花了一点时间才意识到你需要这两个东西($end$ 并且没有换行符)。
  • 这是因为在插入 sn-p 之后告诉 VS 应该将光标放在哪里,可以防止光标在插入的代码之前。
  • VS2019 有同样的问题。最后添加$end$ 对我有用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-19
  • 1970-01-01
相关资源
最近更新 更多