views:

278

answers:

2

I'm attempting to supplement the help features for my code by providing other developers with code snippets. These produce skeletons of code which demonstrate how to use/call my classes or methods.

I've created a .snippet file and have placed it in the "%Visual Studio Folder%\Code Snippets\Visual Basic\My Snippets" folder. I've used the Code Snippets Manager and ensured that it included this folder so that I can access the snippets.

Everything works well when I have 1 CodeSnippet tag within the root CodeSnippets tag....

When I add more than one CodeSnippet tag to the file (each with their own title, and their own code example) I'm experiencing something strange.

The first CodeSnippet I've added contains code for adding something to my system, the second contains code for editing something in my system, and the third deleting something from the system.

When I use the code snippet by right clicking and selecting "Insert Code Snippet", only the first code snippet in the file shows up as an option. When I select it, the code in the first CodeSnippet is inserted....but so is the code within the other CodeSnippet tags.

Do you have to have a separate XML .snippet file for each code snippet you want to make available?

After reading through MSDN about creating Code Snippets I was under the impression that this could all be done within one file.

It seems that I'm not understanding something very basic here and would love to find the answer but apparently Code Snippets are under used so finding the answer has proven to be a little trickier than I first thought it would be.

Thanks,

-Frinny

A: 

Do you have to have a separate XML .snippet file for each code snippet you want to make available? After reading through MSDN about creating Code Snippets I was under the impression that this could all be done within one file.

I create and use snippets all the time. I wanted to do exactly what you intend. But, I have never gotten multiple snippets in one file to work. I also was under the impression you could do this. I even found an article, based on the beta version of Visual Studio, that demonstrated how to do this, but it didn't work. Moreover, the documentation states:

CodeSnippet Optional element. - Parent element for all code snippet data. There may be zero or more CodeSnippet elements in a CodeSnippets element.

I eventually gave up and created separate files. I have not tried multiple snippets in one file with VS2k8 yet.

Update

I cannot get VB to support multiple snippets in one file using VS2k8. I used the following snippet as a test case. Snippet taken from http://msdn.microsoft.com/en-us/library/ms165394.aspx. Only the first CodeSnippet is read/loaded/used. I also counted all CodeSnippet elements in all snippet files (1143) and no file has more than one. Moreover, this snippet duplicates the behavior you noted in your question. Using the snippet inserts both message box statements.

<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2008/CodeSnippet"&gt;
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>
        SECOND Snippet
      </Title>
    </Header>
    <Snippet>
      <Code Language="VB">
        <![CDATA[MsgBox("SECOND SNIPPET")]]>
      </Code>
    </Snippet>
  </CodeSnippet>
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>
        FIRST Snippet
      </Title>
    </Header>
    <Snippet>
      <Code Language="VB">
        <![CDATA[MsgBox("FIRST SNIPPET")]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

More Info

I got to thinking that this has to be a bug that slipped through because Microsoft never put multiple snippets in the provided snippet files that come with Visual Studio, or VB.NET doesn't support multiple snippets due to difficulties integrating with the enhanced statement completion capabilities of the editor.

AMissico
This is very disappointing to hear but I'm starting to come to the same conclusion. I have several different classes and functions in the namespace I'm providing help for. It would be such a shame to have to create a snippet file for every single one of them. I might actually just drop the idea because it'd be very difficult to maintain and actually include into projects (too many files).
Frinavale
You just have to place the snippets into the user's "My Code Snippets" directory. I would treat it just like a project and create an installation. (Which is a good idea. I am using a batch file now.) Honestly, we are programmers, so IMHO handling multiple files is a non-issue.
AMissico
Have you thought about maintainability? I have several hundreds of code snippets that I have to maintain for several different namespaces. This would mean that I would have several hundred code snippet files. I don't want to be sifting through all of the files trying to figure out which one has the function I need to update code snippet for. I just want to have 1 code snippet file for each namespace so that I can easily find the functions I need to update/modify/maintain. It seems absurd to have to install so many files onto the client's machine and absurd to have to maintain so many files too
Frinavale
Yes, that is why I suggest putting them into a project. You can assign an editor such as "Snippet Editor" or some other editor you like. Create a folder for each namespace. Add a Setup project that will install the snippet files. You can use Visual Studio features, such as "Find in Files", to address your maintainability issues. For instance, because you are in Visual Studio, you can write the code in the same project, or better yet, add a Test project, where you can create and test your snippets.
AMissico
The number of files is not an issue. I have one web site project that contains 1167 files, and my own personal libraries with test projects contains 1005 files. The Enterprise 4.1 Library is over 3500 files.
AMissico
Just a thought, if you have so many snippets, shouldn't you consider making the code generic (not generics) and move the code into a library?
AMissico
A: 

This is possible in VS2008

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2008/CodeSnippet"&gt;
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Remarks Full</Title>
      <Shortcut>remarksf</Shortcut>
      <Description>Code snippet for inserting Remarks template</Description>
      <Author>CooksterC</Author>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>VersionNo</ID>
          <ToolTip>Version Number which change will be released</ToolTip>
        </Literal>
        <Literal>
          <ID>FaultNo</ID>
          <ToolTip>Fault Number which change fixed</ToolTip>
        </Literal>
        <Literal>
          <ID>Comments</ID>
          <ToolTip>Description of the change</ToolTip>
        </Literal>
        <Literal>
          <ID>Date</ID>
          <ToolTip>Todays date</ToolTip>
        </Literal>
      </Declarations>
      <Code Language="csharp">
        <![CDATA[/// <remarks>
     ///     <list type="table">
     ///         <listheader>
     ///             <description>Version Number</description>
     ///             <description>Fault Number</description>
     ///             <description>Edit Date</description>
     ///             <description>Edit User</description>
     ///             <description>Comments</description>
     ///         </listheader>        
     ///         <item>
     ///             <description>$Version No$</description>
     ///             <description>$FaultNo$</description>
     ///             <description>$Date$</description>
     ///             <description>CooksterC</description>
     ///             <description>$Comments$</description>
     ///         </item>
     ///     </list>
     /// </remarks>]]>
      </Code>
    </Snippet>
  </CodeSnippet>
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Remarks</Title>
      <Shortcut>remarks</Shortcut>
      <Description>Code snippet for inserting Remarks template</Description>
      <Author>CooksterC</Author>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Declarations>
        <Literal>
          <ID>VersionNo</ID>
          <ToolTip>Version Number which change will be released</ToolTip>
        </Literal>
        <Literal>
          <ID>FaultNo</ID>
          <ToolTip>Fault Number which change fixed</ToolTip>
        </Literal>
        <Literal>
          <ID>Comments</ID>
          <ToolTip>Description of the change</ToolTip>
        </Literal>
        <Literal>
          <ID>Date</ID>
          <ToolTip>Todays date</ToolTip>
        </Literal>
      </Declarations>
      <Code Language="csharp">
        <![CDATA[///         <item>
     ///             <description>$Version No$</description>
     ///             <description>$FaultNo$</description>
     ///             <description>$Date$</description>
     ///             <description>CooksterC</description>
     ///             <description>$Comments$</description>
     ///         </item>
     ///     </list>]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

Each tag is defined in a greater snippets tag. I have one snippet file for my 20 or so snippets I use on a regular basis.

CooksterC
Maybe the Format="1.0.0" has something to do with this...I'll have to try this later. Thanks for your help CooksterC.
Frinavale
I just checked it and I have the Format="1.0.0" attribute in my CodeSnippet tags. It does not work.
Frinavale
@CooksterC; How about posting a working example with two code snippets so Frinavale and I can get this working.
AMissico
Not that we are going to get an answer. :O)
AMissico
@AMissico I don't think there is an answer to this problem. I am going to a .NET user group meeting in about 10 days and I think that someone there might be able to answer the question. I'll let you know what I find. Cheers!
Frinavale
@Frinavale: I think support for multiple snippets in one file were in beta then removed in the release of Visual Studio.
AMissico
I got the above snippet file to work in VS2k8/C#.
AMissico
Using example from http://msdn.microsoft.com/en-us/library/ms165394.aspx, I cannot get multiple snippets to work with VB.
AMissico