VS2010 Extensibility: Custom document formatting

断了今生、忘了曾经 提交于 2020-01-12 10:46:27

问题


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

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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/6768282/vs2010-extensibility-custom-document-formatting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!