locked
VS 2010 Language Extension - Classification Problem RRS feed

  • Question

  • Hi!

    I am trying to add some basic syntax highlighting for my own content type in Visual Studio 2010, I used the OOK example provided by msdn to get started.
    Here is my problem:

    When I try to change a tokentype on a tag, the tag changes it's tokentype but the color for the tag does not change from the old color to the new.

    Example:

    ....
    if(counter > 0)
    ....
    counter here is classified as an error becouse it's undefiend

    if i change to:

    ...
    int counter =1;
    if(counter > 0)
    ...
    counter on both position changes to tokentype "variable" but the "counter" in the if-statement is still marked as an errer. This only goes away if I type something on the same line as the if-statement.

    Any suggestions?

    Thursday, July 19, 2012 8:44 AM

Answers

  • The following link helped me solve my problem.

    http://social.msdn.microsoft.com/Forums/en-CA/vseditor/thread/89b34254-49e1-46b1-9425-0ca7946db898

    • Marked as answer by Hasselass Tuesday, July 24, 2012 6:27 AM
    Tuesday, July 24, 2012 6:27 AM

All replies

  • Hi Hasselass,

    I don't know your extensibility project very well.

    You means the color doesn't change when the tokentype is changed. Can you debug your codes whether the function is called?

    Some sample codes about your issue is appreciated.

    Thank you for your understanding and support.

    Best regards,


    Ego [MSFT]
    MSDN Community Support | Feedback to us

    Friday, July 20, 2012 6:28 AM
  • Hi!
    Giving you part of the code (I hope I'll cover the importent parts)

    Class TokenTagger
    .
    .
    .
    public IEnumerable<ITagSpan<ObTokenTag>> GetTags(NormalizedSnapshotSpanCollection spans)
    {
        foreach (SnapshotSpan curSpan in spans)
        {
            foreach (string token in Tokens)
            {
                var tokenSpan = new SnapshotSpan(curSpan.Snapshot, new Span(curLoc, token.Length));
                .
                .
                .
                //check if the word before this one is a Datatype, define this word as variable
                if (changeTokenType(token))
                {
                TagToReturn = new Tag(token, "ObVariables", "variable"); //TokenName, TokenType, TokenTooltip
                addNewObTag(TagToReturn);
                }
                if (tokenSpan.IntersectsWith(curSpan))
                {
                    yield return new TagSpan<TokenTag>(tokenSpan, new TokenTag(TagToReturn, _ObTypes[token]));
                }
          }
        }
    }
    private static void addNewObTag(ObTags obtag)
    {
          if (!_ObTypes.ContainsKey(obtag.getName()))
          {
            _ObTypes.Add(obtag.getName(), obtag.getTokenType2());
          }
          else
          {
            _ObTypes.Remove(obtag.getName()); //remove the old entry
            _ObTypes.Add(obtag.getName(), obtag.getTokenType2());
          }
    }

    .
    .
    .
    //End class

    Class classifier : ITagger<ClassificationTag>
    {
    .
    .
    .
        internal ObClassifier(ITextBuffer buffer,
                               ITagAggregator<ObTokenTag> ObTagAggregator,
                               IClassificationTypeRegistryService typeService)
        {
          _buffer = buffer;
          _aggregator = ObTagAggregator;

          //dictionary that maps token types to named classification types
          _ObTypes = new Dictionary<ObTokenTypes, IClassificationType>();
          _ObTypes[ObTokenTypes.ObDatatypes] = typeService.GetClassificationType("ObDatatypes");
          _ObTypes[ObTokenTypes.ObStandardKeyWord] = typeService.GetClassificationType(PredefinedClassificationTypeNames.Keyword);
          _ObTypes[ObTokenTypes.ObVariables] = typeService.GetClassificationType("ObVariables");
          _ObTypes[ObTokenTypes.ObError] = typeService.GetClassificationType("ObError");

        }

        public IEnumerable<ITagSpan<ClassificationTag>> GetTags(NormalizedSnapshotSpanCollection spans)
        {
           ITextSnapshot newSnapshot = _buffer.CurrentSnapshot;
           SnapshotSpan curSpan;
           foreach (var line in newSnapshot.Lines)
           {
              curSpan = new SnapshotSpan(newSnapshot, line.Start, line.Length);
              foreach (var tagSpan in this._aggregator.GetTags(curSpan))
              {
                var tagSpans = tagSpan.Span.GetSpans(newSnapshot);
                yield return
                    new TagSpan<ClassificationTag>(tagSpans[0],
                                                   new ClassificationTag(_ObTypes[tagSpan.Tag.type]));
              }
           }
        }
    .
    .
    .
    //End class

    So to clarify my problem, the moment a token changes it's type from e.g errorType --> variableType,
    Every appearence of the token in the text will change it's type to variableType (you can see that because the tooltip is changing)
    BUT if you have different coloring for the types, the coloring only changes on the current line the snapshot is taken.
    I don't know which function is used for controling the coloring, I figured the coloring is connected to the classificationtypes


          
    Friday, July 20, 2012 11:13 AM
  • The following link helped me solve my problem.

    http://social.msdn.microsoft.com/Forums/en-CA/vseditor/thread/89b34254-49e1-46b1-9425-0ca7946db898

    • Marked as answer by Hasselass Tuesday, July 24, 2012 6:27 AM
    Tuesday, July 24, 2012 6:27 AM
  • Hi Hasselass,

    I’m glad to hear that you got it working.

    Thank you for sharing your solutions & experience here. It will be very beneficial for other community members who have similar questions.

    Best regards,


    Ego [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, July 24, 2012 7:51 AM