Walkthrough: Outlining

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at Walkthrough: Outlining.

You can implement language-based features such as outlining by defining the kinds of text regions you want to expand or collapse. You can define regions in the context of a language service, or you can define your own file name extension and content type and apply the region definition to only that type, or you can apply the region definitions to an existing content type (such as "text"). This walkthrough shows how to define and display outlining regions.

Starting in Visual Studio 2015, you do not install the Visual Studio SDK from the download center. It is included as an optional feature in Visual Studio setup. You can also install the VS SDK later on. For more information, see Installing the Visual Studio SDK.

To create a MEF project

  1. Create an VSIX project. Name the solution OutlineRegionTest.

  2. Add an Editor Classifier item template to the project. For more information, see Creating an Extension with an Editor Item Template.

  3. Delete the existing class files.

Outlining regions are marked by a kind of tag (OutliningRegionTag). This tag provides the standard outlining behavior. The outlined region can be expanded or collapsed. The outlined region is marked by a PLUS SIGN if it is collapsed or a MINUS SIGN if it is expanded, and the expanded region is demarcated by a vertical line.

The following steps show how to define a tagger that creates outlining regions for all the regions that are delimited by "[" and "]".

To implement an outlining tagger

  1. Add a class file and name it OutliningTagger.

  2. Import the following namespaces.

    No code example is currently available or this language may not be supported.

  3. Create a class named OutliningTagger, and have it implement ITagger<T>:

    No code example is currently available or this language may not be supported.

  4. Add some fields to track the text buffer and snapshot and to accumulate the sets of lines that should be tagged as outlining regions. This code includes a list of Region objects (to be defined later) that represent the outlining regions.

    No code example is currently available or this language may not be supported.

  5. Add a tagger constructor that initializes the fields, parses the buffer, and adds an event handler to the Changed event.

    No code example is currently available or this language may not be supported.

  6. Implement the GetTags method, which instantiates the tag spans. This example assumes that the spans in the NormalizedSpanCollection passed in to the method are contiguous, although this may not always be the case. This method instantiates a new tag span for each of the outlining regions.

    No code example is currently available or this language may not be supported.

  7. Declare a TagsChanged event handler.

    No code example is currently available or this language may not be supported.

  8. Add a BufferChanged event handler that responds to Changed events by parsing the text buffer.

    No code example is currently available or this language may not be supported.

  9. Add a method that parses the buffer. The example given here is for illustration only. It synchronously parses the buffer into nested outlining regions.

    No code example is currently available or this language may not be supported.

  10. The following helper method gets an integer that represents the level of the outlining, such that 1 is the leftmost brace pair.

    No code example is currently available or this language may not be supported.

  11. The following helper method translates a Region (defined later in this topic) into a SnapshotSpan.

    No code example is currently available or this language may not be supported.

  12. The following code is for illustration only. It defines a PartialRegion class that contains the line number and offset of the start of an outlining region, and also a reference to the parent region (if any). This enables the parser to set up nested outlining regions. A derived Region class contains a reference to the line number of the end of an outlining region.

    No code example is currently available or this language may not be supported.

You must export a tagger provider for your tagger. The tagger provider creates an OutliningTagger for a buffer of the "text" content type, or else returns an OutliningTagger if the buffer already has one.

To implement a tagger provider

  1. Create a class named OutliningTaggerProvider that implements ITaggerProvider, and export it with the ContentType and TagType attributes.

    No code example is currently available or this language may not be supported.

  2. Implement the CreateTagger<T> method by adding an OutliningTagger to the properties of the buffer.

    No code example is currently available or this language may not be supported.

To test this code, build the OutlineRegionTest solution and run it in the experimental instance.

To build and test the OutlineRegionTest solution

  1. Build the solution.

  2. When you run this project in the debugger, a second instance of Visual Studio is instantiated.

  3. Create a text file. Type some text that includes both the opening brace and the closing brace.

    [  
       Hello  
    ]  
    
    
  4. There should be an outlining region that includes both braces. You should be able to click the MINUS SIGN to the left of the open brace to collapse the outlining region. When the region is collapsed, the ellipsis symbol (...) should appear to the left of the collapsed region, and a popup containing the text hover text should appear when you move the pointer over the ellipsis.

Walkthrough: Linking a Content Type to a File Name Extension

Show: