有没有办法访问 Visual Studio 的 CodeSnippet 框架以编程方式操作代码片段?

发布于 2024-08-22 23:41:08 字数 609 浏览 2 评论 0原文

我正在开发一个将大量模板化的项目,但我正在创建的模板也可能在未来的项目中很好地工作,如 Visual Studio 的 CodeSnippets。

所以,我想知道,有没有一种方法可以访问 Visual Studio 的系统,以任何方式、形状或形式加载、解析和显示 CodeSnippets?

例如,如果能够做这样的事情:

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());
}

我知道我可以将 .snippet 文件作为 XML 加载并手动操作它们,但就利用它所做的其他事情而言,例如填写默认令牌值、执行函数来替换令牌,或者控制代码片段文件中标记的可编辑性,如果不需要的话,这比我想要复制的工作要多。

有想法吗?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

卖梦商人 2024-08-29 23:41:08

您应该考虑使用“真正的”模板工具,例如 CodeSmith 或 Visual Studio 2010 的新功能。这些模板在执行期间支持“代码隐藏”。不确定 Visual Studio 工具集,但 CodeSmith 可以通过 API 执行。

片段要简单得多。不要试图让他们成为他们不是的人。如果您想使用片段,可以免费下载一些工具(片段编辑器,http://www.codeplex.com /SnippetEditor) 来帮助更好地设计它们、利用这些功能等,这样您就不必过多地使用原始 XML。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文