0

I want to write an extension for Visual Studio 2019 that is somewhat similar to the built-in Class View. So I want a Tool Window that shows a list (tree) of classes that are present under current solution.

I can think of two things:

  1. I need to scan projects and find classes
  2. Every time a source file gets edited, I need to know about this immediately to add a newly written class or to remove existing.

Please point me the direction to search in. I guess I should use .NET Compiler Platform SDK, but cannot figure out how. There seems to be no interfaces with events or such.

Dmitry Arestov
  • 1,427
  • 12
  • 24
  • Does this [thread](https://stackoverflow.com/a/56184238/16363677) give you some references? Also, this answers: [hashs](https://stackoverflow.com/a/13014886/16363677), [MD5](https://stackoverflow.com/a/9640068/16363677) may be helpful. – Tianyu Oct 27 '21 at 03:08
  • Not quite. That thread is about scanning every project and file in a solution, which I can use for initial building of the list. But then, it would be ridiculous to repeat all that steps when the user edits the code even a bit. – Dmitry Arestov Oct 27 '21 at 05:52
  • Is there a high-level Roslyn API like 'ClassAdded' event? Or should I watch the project files manually and rescan them on change? – Dmitry Arestov Oct 27 '21 at 05:54

1 Answers1

0

Try subscribing to the RunningDocTableEvents. This deals with all open files (the only ones which should change). There is a BeforeSave and an AfterSave event you can listen for and then rescan the appropriate file.

 var runningDocumentTable = (IVsRunningDocumentTable)GetService(typeof(SVsRunningDocumentTable));

 runningDocumentTable.AdviseRunningDocTableEvents(_viewModel, out _viewCookie);

Your _viewModel must implement the IVsRunningDocTableEvents3 interface (any events not needed should just return S_OK. For example:

  #region IVsRunningDocTableEvents3 Members

  public int OnAfterAttributeChange(uint docCookie, uint grfAttribs)
    {
        return VSConstants.S_OK;
    }

  ...  

    public int OnAfterSave(uint docCookie)
    {
        // do something here
        return VSConstants.S_OK;
    }