Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top