4
votes

I'm working on a project that is going to be heavily templated, but the templates I'm creating might also work well in future projects as CodeSnippets for Visual Studio.

So, I was wondering, is there a way to access Visual Studio's system of loading, parsing, and displaying the CodeSnippets in any way, shape, or form?

For example, it would be really slick to be able to do something like this:

var snippet = LoadSnippet("~/path/to/code.snippet");
if (snippet != null)
{
    snippet.TryReplace("$token$", "Some Value");
    // and then do something with snippet.ToString(), or...
    Console.WriteLine(snippet.GetCSharpString());
}

I know I could just load the .snippet files as XML and manipulate them manually, but in terms of taking advantage of the other things it does such as fill in default token values, execute functions to replace tokens, or control the editability of the tokens in the snippet file, that's more work than I want to duplicate if I don't have to.

Ideas?

Thanks!

1

1 Answers

2
votes

You should consider a "real" templating tool such as CodeSmith, or the new feature of Visual Studio 2010. These templates support "code-behind" during execution. Not sure of the Visual Studio toolset, but CodeSmith can be executed via API.

Snippets are much much much simpler. Don't try to make them something they are not. If you wish to use snippets there are some free-download tools (Snippet Editor, http://www.codeplex.com/SnippetEditor) to help better design them, take advantage of the features, etc, so you aren't in the raw XML so much.