开发者

VS2010 Extensibility: Custom document formatting

Good afternoon,

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.

However, I would like to be able to get Visual Studio to automatically format the entire document via Edit->Advanced->Format Document/S开发者_StackOverflowelection. 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.

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?

Thanks,

Giawa

Edit: 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 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.

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).

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

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


Here you go: http://msdn.microsoft.com/en-us/library/ee197665.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜