VS2010可扩展性:自定义文档格式 [英] VS2010 Extensibility: Custom document formatting

查看:96
本文介绍了VS2010可扩展性:自定义文档格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下午好,

我创建了一个Visual Studio程序包,该程序包将Verilog语言注册为有效的内容类型.

I've created a visual studio package that registers the Verilog language as a valid content type.

我的语法高亮显示,概述,智能缩进等都可以使用.

I've got syntax highlighting, outlining, smart indenting, etc all working.

但是,我希望能够使Visual Studio通过Edit->Advanced->Format Document/Selection自动格式化整个文档.目前,这些选项是不可见的,我希望我必须让VS2010(以某种方式)知道可以调用这些方法,并提供正确的方法来进行这种格式化.

However, I would like to be able to get Visual Studio to automatically format the entire document via Edit->Advanced->Format Document/Selection. Currently these options are invisible, and I expect that I have to let VS2010 know (somehow) that these methods can be called, and provide the correct methods to do this formatting.

我似乎在VS2010 SDK和文档中找不到任何有关格式的参考.我希望ISmartIndent是我想要的解决方案,但是似乎此代码仅在空行上运行,或者在按Enter键时运行.

I can't seem to find any reference to formatting in the VS2010 SDK and documentation. I was hoping that ISmartIndent would be the solution I was looking for, but it seems that this code only runs on an empty line, or when the enter key is pressed.

有人对我如何解决此问题有任何提示或想法吗?

Does anyone have any tips or ideas on how I can solve this problem?

谢谢

Giawa

我正在使用VS2010引入的托管扩展框架来完成此任务.我正在用C#编写(并将c#标记添加到了我的问题中).谢谢

I'm using the managed extension framework introduced with VS2010 to accomplish this. I'm writing in C# (and just added the c# tag to my question). Thanks

推荐答案

MEF不是完成创建语言服务任务的正确方法.相反,应该使用托管包框架(MPF)来注册语言服务并执行诸如语法突出显示,概述,解析,格式化,解析等任务.

MEF is not the right way to accomplish the task of creating a language service. Instead, the Managed Package Framework (MPF) should be used to register the language service and perform tasks such as syntax highlighting, outlining, parsing, formatting, parsing, etc.

由于我的问题是关于格式化,因此我将在回答中稍作介绍.您必须覆盖 ReformatSpan 方法在自定义对象中, ve为您的语言服务创建.在 VS2005的MSDN网页上有一个很好的例子(也适用于VS2010).

Since my question was about formatting, I'll cover a little bit of it in my answer. You must override the ReformatSpan method in the custom Source object that you've creating for your language service. There's a good example on the MSDN webpages for VS2005 (applicable to VS2010 as well).

您还可以随时通过直接调用ReformatSpan方法来强制格式化.这是我的代码中的一个有效示例:

You can also force formatting at any time by calling the ReformatSpan method directly. Here's a working example from my code:

Region region = service.GetRegionContainingLine((line > 0 ? line - 1 : 0));

if (region != null)
{
    using (EditArray mgr = new EditArray(this, service.LastActiveTextView, true, "Reformat Brace"))
        this.ReformatSpan(mgr, region.ToSpan());
}

感谢@ James McNellis 为我指明了正确的方向.

Thanks to @James McNellis for pointing me in the correct direction.

这篇关于VS2010可扩展性:自定义文档格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆