2

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?

Community
  • 1
  • 1
Neo
  • 169
  • 2
  • 4
  • 12

1 Answers1

2

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.

NotDan
  • 31,709
  • 36
  • 116
  • 156
  • ClassificationTag is more powerful. The foreground color and font size could not be applied when using TextMarkerTag. I'm trying out the outliningtag. Maybe what I want is a mixture of outlining and small&light code. Did you completely hide those code between #region and #endregion in I-HATE-#REGION? – Neo Apr 09 '12 at 08:45
  • Yeah, Classifications seem to work better. I think I may have had the same problem when I first started where I couldn't change font sizes until I used Classifications. I never completely hide code, take a look at the screenshot on the #region extension.. I just make it a smaller font and lighter color text so your eyes don't notice it as easily. You probably don't want to HIDE code completely b/c you usually still have to manage the code that is there. – NotDan Apr 10 '12 at 02:10
  • I noticed that even if I set such a small fontsize that I cannot see it, the line(now blank) still takes space as the visible ones. I wonder if there are many lines of code in the #region, will I-HATE-#REGION perform as is shown? – Neo Apr 12 '12 at 14:51
  • Number of lines in the #region don't affect anything (for my implementation). It only affects the #region and the #endregion lines (not the code in between). I've never tried setting the font size lower than 7 or 9pt. – NotDan Apr 13 '12 at 00:36
  • I just tried I-HATE-#REGION. At first I thought it would hide the code between #region and #endregion. Anyway, I got the point. Thanks! – Neo Apr 13 '12 at 03:09