Walkthrough: Displaying Matching Braces

 

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: Displaying Matching Braces.

You can implement language-based features such as brace matching by defining the braces you want to match, and then adding a text marker tag to the matching braces when the caret is on one of the braces. You can define braces in the context of a language, or you can define your own file name extension and content type and apply the tags to just that type, or you can apply the tags to an existing content type (such as "text"). The following walkthrough shows how to apply brace matching tags to the "text" content type.

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 Editor Classifier project. Name the solution BraceMatchingTest.

  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.

To get a brace highlighting effect that resembles the one that is used in Visual Studio, you can implement a tagger of type TextMarkerTag. The following code shows how to define the tagger for brace pairs at any level of nesting. In this example, the brace pairs []. [], and {} are defined in the tagger constructor, but in a full language implementation the relevant brace pairs would be defined in the language specification.

To implement a brace matching tagger

  1. Add a class file and name it BraceMatching.

  2. Import the following namespaces.

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

  3. Define a class BraceMatchingTagger that inherits from ITagger<T> of type TextMarkerTag.

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

  4. Add properties for the text view, the source buffer, and the current snapshot point, and also a set of brace pairs.

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

  5. In the tagger constructor, set the properties and subscribe to the view change events PositionChanged and LayoutChanged. In this example, for illustrative purposes, the matching pairs are also defined in the constructor.

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

  6. As part of the ITagger<T> implementation, declare a TagsChanged event.

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

  7. The event handlers update the current caret position of the CurrentChar property and raise the TagsChanged event.

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

  8. Implement the GetTags method to match braces either when the current character is an open brace or when the previous character is a close brace, as in Visual Studio. When the match is found, this method instantiates two tags, one for the open brace and one for the close brace.

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

  9. The following private methods find the matching brace at any level of nesting. The first method finds the close character that matches the open character:

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

  10. The following helper method finds the open character that matches a close character:

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

In addition to implementing a tagger, you must also implement and export a tagger provider. In this case, the content type of the provider is "text". This means that brace matching will appear in all types of text files, but a fuller implementation would apply brace matching only to a specific content type.

To implement a brace matching tagger provider

  1. Declare a tagger provider that inherits from IViewTaggerProvider, name it BraceMatchingTaggerProvider, and export it with a ContentTypeAttribute of "text" and a TagTypeAttribute of TextMarkerTag.

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

  2. Implement the CreateTagger<T> method to instantiate a BraceMatchingTagger.

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

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

To build and test BraceMatchingTest 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 and type some text that includes matching braces.

    hello {  
    goodbye}  
    
    {}  
    
    {hello}  
    
    
  4. When you position the caret before an open brace, both that brace and the matching close brace should be highlighted. When you position the cursor just after the close brace, both that brace and the matching open brace should be highlighted.

Walkthrough: Linking a Content Type to a File Name Extension

Show: