How to access project of an opened editor in PhpStorm if multiple projects are opened?
In my PhpStorm (172.4155.41) plugin, I have an access to a virtual file instance (as well as to its PSI file and its editor instance). Once I have open more than one project, by choosing "Open in current window" and "Add to currently opened projects", the plugin starts getting an incorrect project of the editor.
For example, if I open an editor of any file of second project in the projects tree, and I call
editor.getProject()
method, the first opened project is returned. Also, even though I have 2 projects opened, when I call
ProjectManager.getOpenProjects()
an array with only the first opened project is returned.
What am I doing wrong and how to correctly get an editor's project when multiple projects are opened in PhpStorm?
Please sign in to leave a comment.
"Add to currently opened projects" has a limitation. It doesn't actually open the second project but simply adds its files to the first one. Thus there's only one real project is opened. As a consequence, there's only one instance of Project is returned from ProjectManager.getOpenProjects().
Could you please describe your problem. Perhaps there's another way to solve it.
Thanks for your response!
I am launching an external executable when certain files are being opened/edited, and need to set the working directory when launching it. The working directory has to be the root path of the file's project.
For example, a user has 2 opened projects: /users/a/project1 and /users/a/project2. When the user opens a file /users/a/project2/src/a.txt from project2, I need to pass the '/users/a/project2' path to the external executable.
Is there any way to solve and get project's root of an opened Editor/VirtualFile?
You can try to find a directory which has .idea as a child.
Alternatively, you can find a module for a file and then take its roots.
I didn't try it myself, but it should work. Feel free to ask any further questions in any case.
Thanks! Tried both and both solutions seem to work for me.