title: Creating contextual classifier in Visual Studio
body: <p>I'm writing a simple extension for visual studio that should provide syntax highlighting. It works fine for contextless scenarios where I just use regex to identify text span class:</p>
  
  <pre><code>internal class SolKeyword : IClassifier
  {
      private readonly List&lt;(Regex, IClassificationType)&gt; _map;
  
      /// &lt;summary&gt;
      /// Initializes a new instance of the &lt;see cref="SolKeyword"/&gt; class.
      /// &lt;/summary&gt;
      /// &lt;param name="registry"&gt;Classification registry.&lt;/param&gt;
      internal SolKeyword(IClassificationTypeRegistryService registry)
      {
          _map = new List&lt;(Regex, IClassificationType)&gt;
          {
              (new Regex(@"/\*.+\*/", RegexOptions.Compiled), registry.GetClassificationType(Classification.SolComment)),
              (new Regex(@""".*?""", RegexOptions.Compiled), registry.GetClassificationType(Classification.SolString)),
              (new Regex($@"\b({string.Join("|", VerboseConstant.BuiltinTypes.Concat(VerboseConstant.Keywords))})\b", RegexOptions.Compiled), registry.GetClassificationType(Classification.SolKeyword)),
              (new Regex($@"\b({VerboseConstant.Operators})\b", RegexOptions.Compiled), registry.GetClassificationType(Classification.SolKeyword)),
              (new Regex(@"-?(\d+(\.\d*)*)|(\.\d+)", RegexOptions.Compiled), registry.GetClassificationType(Classification.SolNumber)),
          };
      }
  }
  
  ...
  </code></pre>
  
  <p>And then it's used via standard mechanism:</p>
  
  <pre><code>[Export(typeof(IClassifierProvider))]
  [ContentType("sol")]
  internal class SolEditorClassifierProvider : IClassifierProvider
  {
      // Disable "Field is never assigned to..." compiler's warning. Justification: the field is assigned by MEF.
  #pragma warning disable 649
  
      /// &lt;summary&gt;
      /// Classification registry to be used for getting a reference
      /// to the custom classification type later.
      /// &lt;/summary&gt;
      [Import]
      private IClassificationTypeRegistryService classificationRegistry;
  
  #pragma warning restore 649
  
      #region IClassifierProvider
  
      /// &lt;summary&gt;
      /// Gets a classifier for the given text buffer.
      /// &lt;/summary&gt;
      /// &lt;param name="buffer"&gt;The &lt;see cref="ITextBuffer"/&gt; to classify.&lt;/param&gt;
      /// &lt;returns&gt;A classifier for the text buffer, or null if the provider cannot do so in its current state.&lt;/returns&gt;
      public IClassifier GetClassifier(ITextBuffer buffer)
      {
          return buffer.Properties.GetOrCreateSingletonProperty&lt;SolKeyword&gt;(creator: () =&gt; new SolKeyword(this.classificationRegistry));
      }
  ...
  </code></pre>
  
  <p>However, i'm stuck contextual classes like <code>struct</code>. I need to add functionallity as if there is <code>struct Foo</code> anywhere in text file then all <code>Foo</code>'s should be highlighted. So my question is how could it be implemented and how could it work appropriately with typing (when <code>Foo</code> is changed to <code>Bar</code> or something). I think I may somehow use some type of aggregation in <code>GetClassifier</code> method but I'm not sure it's the right way to do it.</p>