Accessing loaded solution a projects from Roslyn from a standard VSPackage

已答复 Accessing loaded solution a projects from Roslyn from a standard VSPackage

  • 2012年2月7日 6:27
     
      包含代码

    I am currently spinning up on Roslyn and want to directly access the solution and projects from the the default loaded workspace. So I have created a default package in c# and I use the loading of a menu command to trigger spinning up the Workspace discovery service. I store the instance of the service as a field on package implementation itself. From there I set the field _activeWorkspace with an instance of the IWorkspaceDiscoveryService. See LoadWorkspace below.

    private void LoadWorkspace()
            {
                _activeWorkSpaces = null;
    
                var compoentModel = GetService(typeof(SComponentModel)) as IComponentModel;
    
                if (compoentModel == null) return;
    
                _activeWorkSpaces = compoentModel.GetService<Roslyn.Services.IWorkspaceDiscoveryService>();
    
            }

    Once this has completed I have a full instance of the service. I am able to access the default workspace. My issue is when I access the solution it does not map to the active solution. I have recompiled and reattached the services, but for some reason it simply will not find the active solution. My assumption is that I dont have a active reference to the loaded solutions workspace.

    Any recommendations on how I can access the active workspace without diving into the active document and pulling the IText?

    Thanks

          

全部回复

  • 2012年2月7日 20:52
    所有者:
     
     

    Hi Stephen - I am not an expert on IWorkspaceDiscoveryService. But as far as I am aware 'workspaceDiscoveryService.PrimaryWorkspace.CurrentSolution' should yield the solution that is currently loaded in VS. Are you saying that this doesn't yield the solution that you are expecting? What solution does it yield i.e. what projects are you seeing under 'workspaceDiscoveryService.PrimaryWorkspace.CurrentSolution.Projects'?

    Another workspace that you may encounter is the 'MiscellaneousFilesWorkspace'. This is the workspace that gets created for loose code files i.e. code files that are open in VS but not part of any projects in the currently loaded solution.


    Shyam Namboodiripad | Software Development Engineer in Test | Roslyn Compilers Team

  • 2012年2月7日 23:08
     
     

    Yup so when I pull up CurrentSolution I do indeed get a implementation of ISolution but it has no projects and the solution id is just "Solution". It is acting like it can't see the solution so it creates a default. I can ship you the source code its rather simple by design this is a proving ground service I am working on.

    Thanks

    Stephen

  • 2012年2月8日 19:25
    所有者:
     
     

    Hi Stephen,

    Are you running your extension in the "Roslyn" root suffix, or the normal VS SDK "Exp" root suffix.  We've only replace the language services and integrated with the project system in the "Roslyn" root suffix.  If you want your extension to work outside of that, you'll have to create your own Workspace, and populate it by enumerating the items in the IVsSolution.

    This is a short term issue, as eventually Roslyn will completely replace the existing language services and the workspace will be populated as you expect.

    -- Kevin Pilch-Bisson kevinpi@microsoft.com


  • 2012年2月8日 20:56
    所有者:
     
     

    Kevin is correct - currently extensions that target the Roslyn language services need to run under the "Roslyn" rootsuffix. The following threads may be of interest (they have some more details around the Roslyn rootsuffix and around workspaces inside VS) -

    How do you use a VS plugin written using Roslyn project?
    Is it possible to get an IWorkspace using the Services API?


    Shyam Namboodiripad | Software Development Engineer in Test | Roslyn Compilers Team

  • 2012年2月9日 7:34
     
     

    Yup I am indeed running in the stock Exp root suffix. This makes alot of sense and now I understand why it magically isn't there. So I have just another quick question to close out this thread. You mention the fact that I can spin through the items in IVsSolution and populate my own Workspace. So basically I looked through the various methods to stand up IWorkspace and I dont see any real options for creating the workspace unless I load directly from the solution file. Can you provide a pointer in that direction. And if that is the case and I start spinning through the items in IVsSolution how do I read those into the workspace. Am I basically loading the individual projects in directly from the file or playing the active document game and setting an active window and then reading the text buffer?

    Let me know what is a good approach and thanks again for all the help on this.

    Stephen

  • 2012年2月11日 5:27
    所有者:
     
     已答复

    To be honest, I haven't really tries this approach, and I don't know what issues you will run into. I can Thi k of a few options, but all of them have drawbacks:

    • Just use the Roslyn root suffix - requires your users to use it as well.
    • Each time your command is invoked, just construct a simple workspace (or even just a Compilation) using the current document and a few well known references. This is simple, but the semantic information won't be very accurate.
    • Find out the path to the current IVsSolution and call Workspace.LoadSolution on it.  You'll also need to track changes to text documents and update the workspace to include them.  This is a significant amount of work to get right.
    • Like above, but instead of calling LoadSolution, just constructing a solution by enumerating the items in the IVsSolution. This requires the same tracking as above, but you will probably not be able to get all the info you need (compiler options, etc).

    which one you want depends on exactly what you are trying to do.


    -- Kevin Pilch-Bisson kevinpi@microsoft.com

  • 2012年2月11日 15:38
     
     

    Thanks this is what I thought and this puts me where I needed to be. In a nut shell there are two core activities I am working on that require me to interact with solution explorer. I am currenty working up the examples I need to finish some prototypes I use with my development teams. Once I get my examples done Ill do a write up on how I used it externally and post the results back on this thread simply as another reference for other people that are trying to use similar solutions.

    Thanks everyone for the quick turn around on this it really helped me from banging my head against the wall.

    Stephen

  • 2012年2月11日 22:53
     
     
    I do this by having a separate process that runs Roslyn which I keep up to date with the changes (same as your third option... and I probably don't get it 100% right). Here's the code: https://bitbucket.org/perelman/roslyn_process. Perhaps keeping it in an entirely separate process is overkill and I should actually make it run as a separate thread within the same program. At the very least, it's nice to be able to edit and restart my code without restarting Visual Studio.

    I posted over on the Visual Studio extensibility forum asking how to handle actually packaging an extension that uses an external program. If you know a sane way to do so please reply to http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/e1bc2709-fb9c-4122-82a4-2e4690b4a9f9.
  • 2012年7月19日 23:12
     
     
    Did you remove your project from bitbucket?  I'm trying to do the same thing that it sounds like you are doing.  Would you be willing to share the code with me?  I'm trying to write a VSPackage that analyzes code using Roslyn CTP, but I need to be able to keep the Roslyn language services in sync with the VS 2010 language services at least until Microsoft switches over completely.  Did you find a better solution?