Why does changing a Document's Language clear all tags (including bookmarks)?

SyntaxEditor for WPF Forum

The latest build of this product (v24.1.2) was released 3 months ago, which was before this thread was created.
Posted 23 days ago by Joseph Albahari
Version: 24.1.2
Avatar

Repro:

var editor = new SyntaxEditor { Text = "Line1\nLine2\nLine3", IsIndicatorMarginVisible = true };
editor.Document.IndicatorManager.Bookmarks.Add (editor.Document.CurrentSnapshot.Lines[1]);
editor.Document.Language = new SyntaxLanguage("test");
 
Is it because BookmarkTaggerIndicator calls the base constructor with isForLanguage:true? However, I get the same behavior with the following code, despite TestTagger calling IndicatorTaggerBase's constructor with isForLanguage:false
 
var editor = new SyntaxEditor { Text = "Line1\nLine2\nLine3", IsIndicatorMarginVisible = true };
Editor.Document.IndicatorManager.Add<TestTagger, BookmarkIndicatorTag> (
      editor.Document.CurrentSnapshot.Lines [1], new BookmarkIndicatorTag());

editor.Document.Language = new SyntaxLanguage("test");
 
class TestTagger : IndicatorTaggerBase<BookmarkIndicatorTag>
{
    public TestTagger (ICodeDocument doc) : base (
        nameof (TestTagger),
        [new Ordering(TaggerKeys.Token, OrderPlacement.Before)], doc, false)
    {        
    }
}

Comments (2)

Answer - Posted 23 days ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

What's happening here is that the IIndicatorManager will register a CodeDocumentTaggerProvider<BookmarkIndicatorTag> language service on the fly when you first add a tag with your code above.  The service is only registered if the IIndicatorManager didn't locate an existing related tagger (in this case TestTagger) in the document.Properties.

When IsForLanguage is false on the tagger, it does retain the existing tagger instance in document.Properties (and doesn't Close the tagger) but the new tag aggregator that is created for the view following a language change doesn't "see" it since the CodeDocumentTaggerProvider<BookmarkIndicatorTag> language service is not registered on the new syntax language.

You could work around this by doing this kind of code right after the new syntax language is created but before it's assigned to the document:

language.RegisterService(new CodeDocumentTaggerProvider<TestTagger>(typeof(TestTagger)));

That would allow the new tag aggregator to locate the existing TestTagger instance in the document.Properties and your existing bookmark should show up.


Actipro Software Support

Posted 22 days ago by Joseph Albahari
Avatar

Thanks

Add Comment

Please log in to a validated account to post comments.