Scott Langham
Scott Langham

Reputation: 60391

Share state between VSIX Editor Language Extension components

I've created Classifier, ErrorTagger, SignatureHelp and TextCompletion classes. There are all created by providers which are MEF componenents which VS discovers.

The question is, I would like to share a C# object between these components to share state and to allow me to save reparsing the same text from each component. How can I do this? I need this state to be per file being edited.

VS discovers the providers and creates my components using them. But, how can I pass in or share state between them? I did use a global singleton, but when I've got multiple files being edited in VS, they all share the same singleton, but I need a different instance for each file being edited.

Upvotes: 1

Views: 88

Answers (1)

Jason Malinowski
Jason Malinowski

Reputation: 19021

One common approach is ITextView and ITextBuffer both have a Properties property which you can use as a generic bag to put things in. There's even a GetOrCreateSignletonProperty helper on it. It's common to have some sort of component that watches an ITextBuffer for changes and does parsing that way, and each other component just grabs this per-text-buffer object.

Classifiers and taggers can also implement IDisposable, and ITextViews have a close event, so it's also possible to just have a static dictionary somewhere from ITextBuffer/ITextView to your own type and manage lifetime that way.

Either way you do it -- test! It's very easy to accidentally screw up and leak the ITextBuffer or ITextView.

Upvotes: 1

Related Questions