Updating the View as Tokens Classifications Change

SyntaxEditor for WPF Forum

Posted 7 years ago by Ian Davis
Version: 13.1.0580
Avatar

In our language, the classification of an identifier can change as text changes in another part of the document. The tagging and classification works fine; however, when text changing in the document causes the identifiers in another part to change classification, the view doesn't know that they changed. The OnTagsChanged is supposed to handle this AFAIK. Given our situation, I don't know the ranges of the identifiers that need to be updated. This leaves us with the coloring of the identifiers being off until something invalidates the editor causing a refresh.

To get around this, we override GetTags and trigger the event saying that the whole snapshot is outdated with OnTagsChanged. Is there a better way to do this?

    public class IdentifierHighlightingClassificationTagger : TokenTagger
    {
        static IdentifierHighlightingClassificationTagger()
        {
            _Register(XxxClassificationTypes.IdentifierHighlight, Brushes.BlueViolet);
            // ..
            // Other classification types are registered with their appropriate colors
        }

        public IdentifierHighlightingClassificationTagger(ICodeDocument document)
            : base(document)
        {
        }

        private static void _Register(IClassificationType classificationType, Brush brush)
        {
            AmbientHighlightingStyleRegistry.Instance.Register(classificationType, new HighlightingStyle(brush));
        }

        public override IEnumerable<TagSnapshotRange<ITokenTag>> GetTags(NormalizedTextSnapshotRangeCollection snapshotRanges, object parameter)
        {
            var tags = base.GetTags(snapshotRanges, parameter);
            OnTagsChanged(new TagsChangedEventArgs(Document.CurrentSnapshot.SnapshotRange)); // Force the view to refresh based on the whole document.
            return tags;
        }

        public override IClassificationType ClassifyToken(IToken token)
        {
            // Custom code that gives classification, this works fine
        }
    }

Comments (1)

Answer - Posted 7 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Ian,

Correct, the TagsChanged event is what you'd want to use to tell the document that ranges have been changed.  For performance reasons you may want to delay that until the user is idle for a second or so, since firing it constantly could affect editing performance.

Using the entire snapshot range is a valid way to tell the view to repaint.  But per our reply to your other thread, this tagger should probably be a classification tagger instead of a token tagger.


Actipro Software Support

The latest build of this product (v2019.1 build 0683) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.