locked
How to enumerate Collapsed Regions in the VS2015 code editor window? RRS feed

  • Question

  • HI,

    I need to enumerate Collapsed Regions in the code window of VS 2015. I have a solution that worked for VS 2013 that uses interfaces in the following:

      using Microsoft.VisualStudio.Editor;
      using Microsoft.VisualStudio.Text;
      using Microsoft.VisualStudio.Text.Editor;
      using Microsoft.VisualStudio.Text.Outlining;

    However, when I converted my project to VS2015, I get errors that .Text is not found etc.

    I posted this question in StackOverflow and get the folloing reply:

    Use MEF to import an IOutliningManagerService, call GetOutliningManager() with your TextView, then use its methods to see the collapsible regions

    Please help me by providing a working example if you can to enumerate Collapsed regions.

    Joginder Nahil
    www.starprinttools.com

    Monday, April 6, 2015 8:01 AM

Answers

  • If it's complaining that it can't find the namespaces, you probably just need to remove and re-add the references so that they use the VS 2015 SDK versions.  The code may work then.  If not, the following code I use to enumerate and find collapsed regions works in my project once I fix the references.  This is just a rough outline but should give you the general idea:

    // Use MEF to import the service provider or use your package instance [Import] private SVsServiceProvider provider = null; // Get the component model service and use it to get the outlining manager var componentModel = provider.GetService(typeof(SComponentModel)) as IComponentModel; IOutliningManager outliningManager = null; if(componentModel != null) { var outliningManagerService = componentModel.GetService<IOutliningManagerService>(); if(outliningManagerService != null) outliningManager = outliningManagerService.GetOutliningManager(currentTextView); } // Use the outlining manager to enumerate the regions intersecting the given set of spans if(outliningManager != null) foreach(var region in outliningManager.GetCollapsedRegions(span, false)) if(region.IsCollapsed) outliningManager.Expand(region);

    Eric

    • Proposed as answer by EWoodruff Monday, April 6, 2015 4:40 PM
    • Marked as answer by J S Nahil Tuesday, April 7, 2015 12:19 PM
    Monday, April 6, 2015 4:40 PM

All replies

  • If it's complaining that it can't find the namespaces, you probably just need to remove and re-add the references so that they use the VS 2015 SDK versions.  The code may work then.  If not, the following code I use to enumerate and find collapsed regions works in my project once I fix the references.  This is just a rough outline but should give you the general idea:

    // Use MEF to import the service provider or use your package instance [Import] private SVsServiceProvider provider = null; // Get the component model service and use it to get the outlining manager var componentModel = provider.GetService(typeof(SComponentModel)) as IComponentModel; IOutliningManager outliningManager = null; if(componentModel != null) { var outliningManagerService = componentModel.GetService<IOutliningManagerService>(); if(outliningManagerService != null) outliningManager = outliningManagerService.GetOutliningManager(currentTextView); } // Use the outlining manager to enumerate the regions intersecting the given set of spans if(outliningManager != null) foreach(var region in outliningManager.GetCollapsedRegions(span, false)) if(region.IsCollapsed) outliningManager.Expand(region);

    Eric

    • Proposed as answer by EWoodruff Monday, April 6, 2015 4:40 PM
    • Marked as answer by J S Nahil Tuesday, April 7, 2015 12:19 PM
    Monday, April 6, 2015 4:40 PM
  • Hi Eric,

    It was references issue as you suggested. Once fixed, all is well

    Joginder Nahil

    Tuesday, April 7, 2015 12:19 PM
  • Hi Eric,

    The provided code sample looks very useful, but I can't seem to manage to get hold of the ITextView object that is required for acquiring the IOutliningManager.

    I've got an IVsTextView that I acquire from an IVsTextManager2, but how do get at the ITextView representing the text that is presented by the IVsTextView?

    Saturday, November 2, 2019 2:20 PM
  • Never mind, this article got me on track: https://docs.microsoft.com/en-us/visualstudio/extensibility/walkthrough-customizing-the-text-view?view=vs-2019
    Saturday, November 2, 2019 3:44 PM