Search code examples
visual-studio-extensionsvsixvs-extensibilitylanguage-server-protocol

How do I find the open folder in a VSIX extension


I'd like to write a VSIX LSP extension. I'd like this to work in the simplest possible way - that seems to be using the "Open Folder" feature to open a folder of code, and do my thing.

To start the LSP server, I need to know the directory of the opened folder. How do I know whether Visual Studio is in "open folder" mode (if it's not, the LSP should just not be started), and how do I know the path to that folder (so I can start the LSP server)?

I found https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualstudio.shell.interop.ivssolutionevents7?view=visualstudiosdk-2017 which seems promising in that I can register for when some some specific folder is opened - an event that tells me the "open folder" functionality has been used would probably be perfect - if folder is opened, start the LSP for that folder.


Solution

  • The following code will get you 3 information:

    // get solution reference from a service provider (package, etc.)
    var solution = (IVsSolution)ServiceProvider.GetService(typeof(SVsSolution));
    
    solution.GetSolutionInfo(out string dir, out string file, out string ops);
    // dir will contain the solution's directory path (folder in the open folder case)
    
    solution.GetProperty((int)__VSPROPID.VSPROPID_IsSolutionOpen, out object open);
    bool isOpen = (bool)open; // is the solution open?
    
    // __VSPROPID7 needs Microsoft.VisualStudio.Shell.Interop.15.0.DesignTime.dll
    solution.GetProperty((int)__VSPROPID7.VSPROPID_IsInOpenFolderMode, out object folderMode);
    bool isInFolderMode = (bool)folderMode; // is the solution in folder mode?