2

Documentation for extending Visual Studio is virtually nonexistent, and I managed to assemble a few lines of functioning code hacked together from a dozen or more obscure sources around the interwebs before crashing into a brick wall.

All I need to do is subscribe to an event that is fired after a snippet is inserted. Sounds easy enough, or so I thought. Almost immediately into my research I stumbled upon the following morsel:

IVsExpansionClient.OnAfterInsertion

That describes perfectly my intention, so this MUST be the answer! After looking around my SDK assemblies for longer than I would like to admit, I finally ended up yanking the assembly (Microsoft.VisualStudio.TextManager.Interop.8.0.dll) out of my GAC so that I could reference it explicitly in my solution.

Now that I have access to the interface, I just need to figure out how to get an instance of it.

IVsExpansionManager

Ah HA! That MUST somehow provide me with a mechanism for obtaining an instance of IVsExpansionClient, right? Well, not exactly. At least not in a way that I can see. I have stitched together the following code:

IVsExpansionManager expansionManager = null; IVsTextManager2 textManager = (IVsTextManager2)Package.GetGlobalService(typeof(SVsTextManager)); int manager = textManager.GetExpansionManager(out expansionManager);

Which gives me a IVsExpansionManager COM object, but that is about as far as I can get.

I have taken notice of another potential provider:

IVsExpansionEvents

I thought perhaps like solution events, document events, window events or text events, I might be able to invoke DTE to hook these events, but they appear to be absent.

There are methods that accept IVsExpansionClient as a parameter such as:

IVsExpansion.InsertNamedExpansion

So there simply must be a way to fetch an instance of this object, right? I suppose it's possible to create my own class that implements the IVsExpansionClient interface, but I wouldn't know where to begin with implementing the interface members, and I still wouldn't know how to instantiate it in a meaningful way within my package.

I am grateful to anyone who can point me in a different direction, or provide a working sample that I can adapt for my needs. Thanks for taking the time to read through this, I appreciate your time.

EDIT: I want to receive a notification that a snippet has been inserted into the active document window. Ideally, I would like the snippet itself to be included in the delegate event args, as I have some processing to do on the inserted snippet...and it would be cumbersome to process the entire document, or try to identify the recently inserted snippet without context.

XamlZealot
  • 693
  • 5
  • 12
  • What is it that you call a snippet? Any segment of code? Or saved snippets from the IDE? – Erwin Mayer Aug 24 '15 at 08:05
  • A VS Snippet. That lives in every Visual Studio Code Snippets directory. The XML that allows you insert a property declaration by typing 'prop' and tabbing? My group uses dozens of customized snippets, and I need to receive a notification when a Visual Studio code snippet is inserted, similar to typing 'prop' and tabbing. – XamlZealot Aug 24 '15 at 19:32

1 Answers1

0

Maybe you want to explain what you actually want to achieve. The documentation of the IVsExpansionClient interface states:

Notes to implementers This interface is implemented by a VSPackage that supports insertion of code snippets.

I don´t see why one would like to consume an instance of it, because it´s an interface allowing the package to receive notifications/callbacks, if something related to code-snippets is going to happen (it provides nohting else than callback functions).

It states furthermore...

Notes to Callers This interface is instantiated and passed to the InvokeInsertionUI method in the IVsExpansionManager interface. This interface is also instantiated and passed to the methods in the IVsExpansion interface.

Matze
  • 5,100
  • 6
  • 46
  • 69
  • What I want to achieve: I want to receive a notification that a snippet has been inserted into the active document window. Ideally, I would like the snippet itself to be included in the delegate event args, as I have some processing to do on the inserted snippet...and it would be cumbersome to process the entire document, or try to identify the recently inserted snippet without context. Do you have suggestions how to accomplish this? – XamlZealot Aug 21 '15 at 16:31