Fast code snippet doesn't work everywhere

I have a piece of code string.Format(@"") with a sf label that inserts a fragment and places the cursor between two double quotes. Really comfortable. Of course, I can use it by simply typing sf and double-clicking the tab:

enter image description here

However, I just found that the shortcut does not work in all places. For example, if I create this statement:

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

and I pressed sf , the shortcut does not appear on the intellisense menu, and if I press Tab twice, it will not generate a fragment. Why?

I tried looking for “C # code snippet script 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.

EDIT: Here is the fragment 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> 
+7
c # visual-studio visual-studio-2013 code-snippets
source share
2 answers

According to the docs, the Kind attribute defines where you can use the fragment - you specified the "body of the method" and you should probably specify "any"

+2
source share

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

enter image description here

It looks like this:

 <?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> 

To create and edit fragments, a snippet constructor must be created .

+1
source share

All Articles