How to Get Project Path and File Path in a Remote Environment for a Plugin
Answered
In local mode, the following method can correctly retrieve the file path. However, in remote mode, it only returns a path in the form of `\filename`:
file.getPresentableUrl();
In local mode, the following method can also correctly retrieve the main project path. However, in remote mode, it returns a local mapping instead of the correct project path:
ProjectUtil.guessProjectDir(project);
Could you please advise on how to correctly retrieve the project path and file path in a remote mode when the plugin is installed on the Client?
Please sign in to leave a comment.
Hi,
Regarding the main project path, there is no such concept. A project can consist of multiple modules located in different locations. Please describe your use case - why do you need a project path?
Regarding file path - why do you retrieve it via getPresentableName? What does file.getPath return in both cases?
Thanks for your reply.
I need to retrieve the project path because I want the plugin to call another program, and this program takes the project path as a parameter. Alternatively, it doesn’t necessarily have to be called the project path—it could just be the root folder path of the folder opened in the IDE.
Regarding retrieving the path, I referred to the implementation of `CopyPathsAction`. In this implementation, the `getPaths` method uses the `getPresentableUrl` method. However, I’ve also looked into the implementation of `getPresentableUrl`, which ultimately relies on `getPath`. What I actually need is just the return value of `getPath`.
The problem arises in remote mode because the specific implementation class of `VirtualFile` is `ThinClientNodeVirtualFile`. In this class, the implementation of `getPath` is defined in `LightVirtualFileBase`. Here, the return value of `getPath` depends on `getParent`, but the `parent` is defined as `null`, which means it will always return `/filename`.
I would like to emphasize that what you see in the project view doesn't have to be a single opened directory. A project can contain multiple modules, which consist of roots located in various directories. You shouldn't assume that the project is just a single directory. I understand that in your case, you can simplify the use case, if there is a strict convention of locating a project in a single directory, but I wanted to make it clear.
Anyway, this question is more about getting a path of a file, as far as I understand. I asked my colleagues responsible for remote development features. Please be patient.
Hello Xuxin3101 !
Could you please provide a bit more detail about the place where you're trying to get a project path? Is it an action, a startup activity, an extension or something else?
For instance, if we're talking about
CopyPathsAction
, it is executed on the backend, so the actualVirtualFile
with the correct parent will be used. However, based on your description, it seems your code is being called on the frontend, right?