Code snippet shortcut doesn't work everywhere
Asked Answered
G

2

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>
Geodynamics answered 1/10, 2015 at 16:28 Comment(9)
Do any of the default snippets load the intellisense menu? Try "prop" in the same placeRedletter
What does your snippet definition look like?Outright
That's not relevant -- none of the snippets are available (see my answer to g williams comment, above)Geodynamics
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 bodyOutright
How/where did you define the code snippet?Claritaclarity
@Outright -- You're right about the snippet kind being "method body". The other two available are "type declaration" and "method declaration", neither of which work. Does that mean there are place you just can't use snippets?Geodynamics
@ThomasWeller -- Using Snippet DesignerGeodynamics
It depends on the kind. As I said before. You can set it to Any so it works anywhere. But I just created my own snippet in 2015 and 2013 and it just works.Outright
@Outright -- I've edited the question to include the snippet XML. How does it compare to the one that works for you?Geodynamics
F
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"

Flag answered 5/10, 2015 at 21:25 Comment(0)
O
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.

Outright answered 5/10, 2015 at 21:27 Comment(8)
Thanks, gotta catch a train now but I'll compare this closely to my snippet (see edited question).Geodynamics
I even tried your snippet -- not working. VS 2013. BTW, I created mine with snippet designer.Geodynamics
Any Visual Studio updates installed, I'm on 2013 update 5? Any plugins that may interfere with snippets? Like Resharper, CodeRush or something similar?Outright
Does the code you're editing currently parse/compile without errors? It may be that Intellisense is confused somehow.Outright
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);Geodynamics
I ran VS in safe mode (no addins) and the snippet still didn't work.Geodynamics
You should be on 12.0.40629.00 update 5. You can download update 5 from visualstudio.com.Outright
Okay, I'm all up to date. Running in safe mode. Still not working. No snippet shortcuts are available when the cursor is inside parens (i went through the alphabet just to be sure).Geodynamics

© 2022 - 2024 — McMap. All rights reserved.