Snippet在VS2015中插入了额外的换行符

tho*_*8wp 37 c# visual-studio code-snippets visual-studio-2015

我已经制作了一个在Visual Studio中使用的自定义代码段.在VS2013中,它按预期工作,但自从在VS2015(社区版)中使用它以来,它一直在代码之前插入一个额外的换行符(当我按Tab键/第二次输入时).

这似乎只是自定义代码段的问题(内置的工作正常).有人知道是什么原因引起的吗?这很烦人.

作为旁注,只有在我在一行空代码上激活代码片段时才会发生这种情况.如果我在现有代码之后执行此操作,则不会插入换行符.不幸的是,该片段是一个声明,所以这没有多大帮助.

这是几乎完全从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>
Run Code Online (Sandbox Code Playgroud)

Cra*_*g A 56

您可以通过在代码段文本中的某处放置$ end $来阻止前面的换行符.例:

<![CDATA[$FieldName$ = fields.add($FieldName$, "$FieldName$");$end$]]>
Run Code Online (Sandbox Code Playgroud)

  • 还有一个`$ start $`吗? (3认同)
  • 这是 [完整规范参考](https://docs.microsoft.com/en-us/visualstudio/ide/code-snippets-schema-reference#code)。只有“两个保留字可用于 `Code` 元素的文本:`$end$` 和 `$selected$`。” @Ciwan (3认同)
  • VS2019也有同样的问题。最后添加 `$end$` 对我有用。 (3认同)