7

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:

enter image description here

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>
9
  • Do any of the default snippets load the intellisense menu? Try "prop" in the same place Commented Oct 1, 2015 at 16:39
  • What does your snippet definition look like? Commented Oct 5, 2015 at 20:56
  • That's not relevant -- none of the snippets are available (see my answer to g williams comment, above) Commented Oct 5, 2015 at 20:57
  • 1
    it actually might, the Snippet's Kind property for example scopes which snippets are active where. I'd expect your snippet to be of kind method body Commented Oct 5, 2015 at 21:03
  • How/where did you define the code snippet? Commented Oct 5, 2015 at 21:07

2 Answers 2

2

According to the docs the Kind attribute determines where you can use the snippet - you have specified "method body" and you should probably specify "any"

Sign up to request clarification or add additional context in comments.

Comments

1

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

enter image description here

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>[email protected]</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.

8 Comments

Thanks, gotta catch a train now but I'll compare this closely to my snippet (see edited question).
I even tried your snippet -- not working. VS 2013. BTW, I created mine with snippet designer.
Any Visual Studio updates installed, I'm on 2013 update 5? Any plugins that may interfere with snippets? Like Resharper, CodeRush or something similar?
Does the code you're editing currently parse/compile without errors? It may be that Intellisense is confused somehow.
I can't tell if I have any updates installed. It's version 12.0.21005.1 REL. I ran Windows Update but there was nothing for VS (and I do have it set to update all MS software, not just Windows). The code compiles fine. I can repro the issue very easily with just a simple new project. I can't even use the snippet in a line like this: System.Diagnostics.Debug.Print(sf);
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.