Question

I'm working on a VS2010 editor extension using MEF. Now I wish to hide code as needed, i.e. make specific code lines invisible. Here is an example: Hiding the regions in Visual Studio

Via MEF, I'm able to access TextView, TextBuffer, etc. But I haven't found a way to hide code, or change color or fonts of the displayed code. I guess the visibility, fonts and color are related. I'm not sure whether VSPackage can do it, even if it can, it seems too much for my requirement. I also thought about DTE object, no idea yet.

Maybe I just got some hope from http://msdn.microsoft.com/en-us/library/bb165040.aspx. However, is it possible to use this in a MEF component? It says to call QueryService for SVsTextManager. What does that mean?

Was it helpful?

Solution

This is what I started with with the #regions extension:

http://msdn.microsoft.com/en-us/library/dd885121.aspx

The tutorial shows you how to highlight text and will give you lots of code to get started.

Basically you need to create a Tagger. The tagger goes through all of code in a file and marks text spans with metadata. Then you can tell VS to format the tagged text however you want. If you want more power, you can use Classifications (There may be tutorials for that too) that allow you to change text size, foreground color, background color etc. The tagger assigns the Classifications in the GetTags method, something like this:

new TagSpan<YourTag>(snapshot, new YourTag(
    ClassificationTypeRegistryService.GetClassificationType("yourClassificationName")));

I doubt you actually want to completely hide code though. If you are editing a file, you have to be able to know that code is there to work with it (maybe you introduce code that conflicts with the hidden code, etc). In the case of the #regions extension, you still have to make sure you don't delete the #endregion line, for example, or put invalid code within the region. This is why it makes the code small and light, rather than completely hiding it.

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