views:

425

answers:

2

Hi, guys!

I'm writing another code coverage tool for .NET with Visual Studio 2008 integration.
Everything goes well except one thing: I can't find a way to highlight some code chunks.

I need it to inform user about covered and not covered blocks.
You can see example of the feature I want on the next screenshot (from native VS code coverage toolset): Coverage Example

Can someone provide me a code snippet that highlights text in the code view window?
Links to appropriate MSDN articles related to VS2008 are also appreciated!

Thanks in advance.

WBR
Alex

+1  A: 

I've found the answer, see code below:

// retrieving IVsTextManager and highlight id
DTE2 applicationObject = ...; // get it during addin init
Microsoft.VisualStudio.OLE.Interop.IServiceProvider serviceProvider = (Microsoft.VisualStudio.OLE.Interop.IServiceProvider)applicationObject;
Guid SID = typeof(SVsTextManager).GUID;
Guid IID = typeof(IVsTextManager).GUID;
IntPtr output;
serviceProvider.QueryService(ref SID, ref IID, out output);
IVsTextManager textManager = (IVsTextManager)Marshal.GetObjectForIUnknown(output);
int highlightID;
Guid highlightGuid = ...; // your highlighted text style guid
textManager.GetRegisteredMarkerTypeID(ref highlightGuid, out highlightID);

// highlighting text block in the active view
IVsTextView view;
int result = textManager.GetActiveView(0, null, out view);
IVsTextLines buffer;
view.GetBuffer(out buffer);
buffer.CreateLineMarker(highlightID, startLine, startColumn, endLine, endColumn, null, null);

More examples could be found in MetaScroll Visual Studio Addin.

Rageous
Worth noting that, for VS2010, the related APIs were significantly reworked (old ones are stil available for back-compat, but new ones are much easier to use).
Pavel Minaev
Thanks for the hint, I'll take a look at the new APIs after upgrading to VS2010.
Rageous
A: 

Another example, but for VS2010: http://dotneteers.net/blogs/divedeeper/archive/2008/11/04/LearnVSXNowPart38.aspx

Rageous