Code snippet shortcut doesn't work everywhere

I have a code snippet string.Format(@"") with a shortcut sf that inserts the snippet and places the cursor in between the two double quotes. Really convenient. I can normally use it, of course, by just typing sf and hitting tab twice:

However, I've just discovered that the shortcut doesn't work in all locations. For example, if I'm building this statement:

if(true) throw new FormatException() // <-- cursor is inside these parens

and I hit sf , the shortcut does not appear in the intellisense menu, and if I hit Tab twice, it doesn't generate the snippet. Why?

I have tried searching for "C# code snippet shortcut sometimes doesn't work", "C# code snippet shortcut doesn't work", "visual studio code snippet sometimes doesn't work" among others, and I can't find anything useful about it.

EDIT: Here is the snippet definition:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
      <Title>String.Format with @</Title>
      <Author>Rory</Author>
      <Description>
      </Description>
      <HelpUrl>
      </HelpUrl>
      <Shortcut>sf</Shortcut>
    </Header>
    <Snippet>
      <Declarations>
        <Literal Editable="true">
          <ID>anchor</ID>
          <ToolTip>
          </ToolTip>
          <Default>
          </Default>
          <Function>
          </Function>
        </Literal>
      </Declarations>
      <Code Language="csharp" Delimiter="$" Kind="method body"><![CDATA[string.Format(@"$selected$$end$")]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

根据文档,Kind属性决定了你可以在哪里使用代码片段 - 你已经指定了“method body”,你应该指定“any”


I just created and tested (in VS2013 and VS2015) my own version of your snippet and it works as expected:

在这里输入图像描述

This is what it looks like:



<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
      <Title>SnippetFile1</Title>
      <Author>me@example.com</Author>
      <Description>
      </Description>
      <HelpUrl>
      </HelpUrl>
      <Shortcut>sf</Shortcut>
    </Header>
    <Snippet>
      <Code Language="csharp" Delimiter="$"><![CDATA[string.Format(@"$end$")]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

For creating and editing Snippets, the Snippet Designer is a must have.

链接地址: http://www.djcxy.com/p/29438.html

上一篇: C ++

下一篇: 代码段快捷方式无处不在