microsoft / VSExtensibility

A repo for upcoming changes to extensibility in Visual Studio, the new extensibility model, and language server protocol.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Any way to listen/subscribe to Solution events? Similar to ITextViewOpenClosedListener would there be a ISolutionOpenClosedListener

luislhg opened this issue · comments

Hello, is there any way to listen and react when a solution or project is opened?

I only found ITextViewOpenClosedListener, but I'd really need to listen for whenever a solution or project has been opened/loaded in Visual Studio.

@luislhg - apologies on the delay. What you're seeking is possible, but we don't yet have the documentation or samples to highlight it yet. We're working on adding the gaps in documentation with respect to project/solution APIs, and will have that available in the coming weeks. Stay tuned!

Hi @luislhg, you could consider using Project Query APIs that cover certain solution/project level events. Here is the documentation to help get acquainted - Project query conceptual overview - Visual Studio (Windows) | Microsoft Learn

We have a couple of APIs that can be used, please find examples below:

1. Using SubscribeAsync

var solutions = await querySpace
     .Solutions
     .ExecuteQueryAsync();

 var singleSolution = solutions.FirstOrDefault();
 var observer = new SubscriptionObserver();
 var subscribe = singleSolution
     .Projects
     .With(p => p.Name)
     .SubscribeAsync(observer, cancellationToken);

class SubscriptionObserver: IObserver<IQueryResults<IProjectSnapshot>>
{
    public void OnCompleted()
    {}

    public void OnError(Exception error)
    {}

    public void OnNext(IQueryResults<IProjectSnapshot> value)
    {}
}

2. Using TrackUpdatesAsync

var observer = new TrackerObserver();
var track= singleSolution
.Projects
.With(p => p.Name)
.TrackUpdatesAsync(observer, cancellationToken);

private class TrackerObserver : IObserver<IQueryTrackUpdates<IProjectSnapshot>>
{
    public void OnCompleted()
    {}

    public void OnError(Exception error)
    {}

    public void OnNext(IQueryTrackUpdates<IProjectSnapshot> value)
    {}
}

The 'observer' implements IObserver and receives notifications of the updates. The main difference between the 2 APIs is that SubscribeAsync provides the complete result set each time and it is up to the client to compute the diff if needed. The TrackUpdatesAsync API would return the results as "added" the first time but for the subsequent times, it would give you a diff from the previous results.