Why am I getting an incomplete file path for diff view action (Diff.EditorPopupMenu)?

Answered

Hello. I created a Diff.EditorPopupMenu action, and I would like to get the file path of the current file I'm looking at while using the threeside diff view. For example, if I'm viewing a threeside diff for src/Foo.java, I expect getPath() to return src/Foo.java. However, when I run my action, I get /Foo.java instead. 

Why is this happening, and how can I get the full path? Below is a sample of what my code looks like:

public void actionPerformed(@NotNull AnActionEvent e) {
SimpleThreesideDiffViewer currentThreeSideDiffViewer;

try {
currentThreeSideDiffViewer =
(SimpleThreesideDiffViewer) e.getDataContext().getData(DiffDataKeys.DIFF_VIEWER);
} catch (ClassCastException cce) {
return;
}

if (currentThreeSideDiffViewer == null) return;

// I read the current file here
VirtualFile file = e.getDataContext().getData(CommonDataKeys.VIRTUAL_FILE));

// I get incorrect output below - file path is incomplete
String path = file.getPath();
}

 

Thanks in advance

6 comments
Comment actions Permalink

>Why is this happening
Because these VirtualFiles are not real files (aka "file.isLocalFileSystem == false"). So, most of the time, their path isn't needed for anything.

>And how can I get the full path?
Which IDE version do you check? Some cases of this issue were fixed in 2020.2+.

1
Comment actions Permalink

Hi Aleksey Pivovarov, thanks so much for your help.

I'm currently IntelliJ version 2020.1.2 (CE version). I may now consider updating to a more recent version (2020.2+) if this resolves the issue. If that's the case, would the method I showed above, therefore, show the full path? Or would I have to use a workaround?

0
Comment actions Permalink

This depends on how this window was created. For conflicts from VCS integration it should return absolute path.

A possible workaround might be to use `com.intellij.codeInsight.daemon.OutsidersPsiFileSupport#getOriginalFilePath` or 'com.intellij.diff.DiffVcsDataKeys#REVISION_INFO'.
These will always return a valid path (no more "/placeholder.txt"), but might not be specified for some diff views.

What do you need this path for?

1
Comment actions Permalink

Thanks Aleksey Pivovarov.

I will give these a try when I get the chance. Currently, the Diff.EditorPopupMenu action I created can be run on a diff view window that is created by IntelliJ (in other words, I do not create any new windows). I'm trying to create a plugin that displays some information on the diff view for a file currently being looked at. (For example, when viewing a diff for Foo.java, show information for that file on the window).

One feature I'm working on is to display git commit data relevant to the current file shown on the diff view, so I was hoping to use the file path as an extra parameter for the git log command (that I would run using git4idea).

0
Comment actions Permalink

In this case you might want to look at REVISION_INFO mentioned above. It is set for contents loaded from vcs and contains corresponding revision number.
For example: this key is used by "Annotate (with Git Blame Info)" action in diff views.

If you decide to rely on "getPath", you might encounter weird cases when it is not *tightly* related to visible content.
Ex: "Compare (File) with Clipboard" action might return path of the file for the "from clipboard" side.

0
Comment actions Permalink

That's great and thanks once again! The information you provided will really help. I'll be sure to post here again if I encounter any issues with the method you mentioned above.

0

Please sign in to leave a comment.