none
VS2015 intellisense inserting code snippet shortcut instead of code snippet

    Question

  • I'm having a similar issue described in this post: https://social.msdn.microsoft.com/Forums/en-US/505e7ae3-ddbc-4c90-9482-e9e8315983ed/code-snippet?forum=csharpgeneral&prof=required

    I'm using the snippet designer extension in VS2015.  When I select my snippet shortcut from the intellisense drop-down list and press TAB twice, the shortcut name is inserted to my editor instead of the actual snippet code. 

    Conversely if I right-click in my editor and select the insert-snippet option from the context menu, I can select the same snippet and the code inserts correctly.

    Here's the full text of the snippet that isn't working:

    <?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>ForEachCountHighlight</Title>
           <Author>tylerit</Author>
           <Description>
           </Description>
           <HelpUrl>
           </HelpUrl>
           <Shortcut>Foreach Count and Highlight</Shortcut>
         </Header>
         <Snippet>
           <Declarations>
             <Literal Editable="true">
               <ID>type</ID>
               <ToolTip>Type of control in collection</ToolTip>
               <Default>type</Default>
               <Function>
               </Function>
             </Literal>
             <Literal Editable="true">
               <ID>div</ID>
               <ToolTip>variable or name of control in collection</ToolTip>
               <Default>div</Default>
               <Function>
               </Function>
             </Literal>
           </Declarations>
           <Code Language="csharp" Delimiter="$"><![CDATA[int count = 0;
     string controlDef = string.Empty;

    foreach ($type$  item in $div$.FindMatchingControls().Cast<$type$>())
     {
     controlDef = item.GetProperty("ControlDefinition").ToString();
     count = count + 1;
     item.DrawHighlight();
     Console.WriteLine(controlDef);
     Console.WriteLine(" ");
     }

    Console.WriteLine(count);]]></Code>
         </Snippet>
       </CodeSnippet>
     </CodeSnippets>

    Tuesday, October 18, 2016 2:03 PM

All replies

  • Hi chris4182,

    Since your problem is more related to Visual Studio General Questions.I will move it to Visual Studio General QuestionsForumfor support.

    The Visual C# forum discuss and ask questions about the C# programming language, IDE, libraries, samples, and tools.

    If you have some grammar or code errors, please feel free to contact us. We will try our best to give you a solution.

    Thanks for your understanding and cooperation.

    Best Regards,

    Wendy


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, October 19, 2016 3:15 AM
  • Hi Chris,

    Thank you for posting in MSDN forum.

    I did a test of the snippet on my VS 2015, and it works, here is my screenshot:

    Here are my steps to create this custom code snippet:

    1. Create a XML file and copy your code in it, save as "CSharpCodeSnippet.snippet".

    2. Add this snippet to my Visual Studio installation by using the Code Snippets Manager. Open the Code Snippets Manager

    • Click the Import button.

    • Go to the location where you saved the code snippet in the previous procedure, select it, and click Open.

    • The Import Code Snippet dialog opens, asking you to choose where to add the snippet from the choices in the right pane. One of the choices should be My Code Snippets. Select it and click Finish, then OK.

    3. Press "Ctrl K+ Crtl X" and add this snippet to my application and it works.

    Refer to :

    https://msdn.microsoft.com/en-us/library/ms165394.aspx

    So you may follow my step to add this again to see if it works, or you could tell us your steps or give us a screenshot about it, we are willing to help you troubleshooting it.

    Best Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.

    Click HERE to participate the survey.

    Wednesday, October 19, 2016 8:31 AM
    Moderator
  • Thanks for Trying my snippet Lana.  I followed your steps above, and it still is not working:

    in VS2015:

    file --> open --> XML File.

    Pasted the snippet code above into the file and saved it.

    tools --> Code Snippet manager

    Imported the new snippet file.

    There has to be something wrong in the snippet code itself;  I have some other snippets that are working fine.  Below is an example of one that is working OK:

    <?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>ComboBox Page Object</Title>
          <Author>admin</Author>
          <Description>
          </Description>
          <HelpUrl>
          </HelpUrl>
          <Shortcut>combobox</Shortcut>
        </Header>
        <Snippet>
          <Declarations>
            <Literal Editable="true">
              <ID>ObjectName</ID>
              <ToolTip>Object Name</ToolTip>
              <Default>objectName</Default>
              <Function>
              </Function>
            </Literal>
            <Literal Editable="true">
              <ID>windowTitle</ID>
              <ToolTip>Window Title if Applicable</ToolTip>
              <Default>windowTitle</Default>
              <Function>
              </Function>
            </Literal>
            <Literal Editable="true">
              <ID>xpath</ID>
              <ToolTip>xpath to the control</ToolTip>
              <Default>xpath</Default>
              <Function>
              </Function>
            </Literal>
            <Literal Editable="true">
              <ID>displayName</ID>
              <ToolTip>Friendly Display name for results logs</ToolTip>
              <Default>displayName</Default>
              <Function>
              </Function>
            </Literal>
          </Declarations>
          <Code Language="csharp" Delimiter="$"><![CDATA[public static readonly string[] cbo$ObjectName$ = { "$windowTitle$", "$xpath$", "$displayName$" };]]></Code>
        </Snippet>
      </CodeSnippet>
    < /CodeSnippets>

    Wednesday, October 19, 2016 12:26 PM
  • Hi chris,

    Since the first code snippet continue failing, how about sharing us a screenshot about the error information?

    If you think that there is something wrong in this snippet code itself, in this situation, I wouldn't test it successfully, but actually I did.

    On my side, the snippet code in the first is ok, please delete the XML file of this foreach code first, then create a new XML file and copy the code in it again, make sure the file suffix is ".snippet"

    Import the code snippet as usual, check the intellisense of inserting code snippet to check if it exists.

    Please tell us the result , and we will try to analyze the root cause.

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, October 20, 2016 8:48 AM
    Moderator
  • Hi Lana -

    OK, I deleted the existing snippet file and started over as  you suggested.

    After pasting the code into the new file, this is the result:

    Then I imported the snippet file. When I type 'for' in my editor, intellisense shows the snippet in the list

    But when I press TAB TAB on my keyboard, this is the result in my editor:

    There are no other errors or messages. 

    Thank you



    Thursday, October 20, 2016 11:38 AM
  • Hi Chris,

    I test your code snippet again, see:

    Works fine, as you said before, other code snippet also works well, it means that your Code snippets Manager is ok.

    When importing this one, please make sure the .snippet file saved well and imported to your snippets list successfully.

    How about finding another computer with VS, test this code snippet to check if it works fine. We need to determine if there is any wrong step about importing this code snippet.

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Friday, October 21, 2016 2:11 AM
    Moderator
  • Here is the snippet file after I initially saved it:

    Here is the file showing up in my list of snippets after I import it.

    One other thing I just thought of is what version of VS are you using?  is it the release version, or the release with updates?  I sent the snippet to another co-worker and it's not working on that machine either.  We are using the same version:

    Friday, October 21, 2016 12:30 PM
  • Lana - Version probably won't matter, as I just upgraded my instance of VS to update 3, rebooted my computer, and the behavior hasn't changed.

    Friday, October 21, 2016 4:12 PM
  • Hi Chris,

    Currently, I think I'm not able to help you solve this issue, but I will invite more senior Microsoft engineer to help you troubleshoot.

    Thank you for your understanding.

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 24, 2016 8:45 AM
    Moderator