find the current class

i have an action which extends AnAction class, i want to find the current class in action performed.
I tried
final PsiElement element =e.getData(DataKeys.PSI_ELEMENT); and also
final PsiElement element =(PsiElement)dataContext.getData(DataConstants.PSI_ELEMENT);
both of these gives null. How can i find the current class

Comment actions Permalink

Jetbrains have a lot of useful articles on their confluence page that are worth looking at, for instance here are a few ways to achieve what you're after -

Comment actions Permalink

For your code to work, the focus (or Cursor) has to be in the Editor. If not, you would get NULL.

Following code will give you the class name :

PsiFile psiFile = e.getData(DataKeys.PSI_FILE); System.out.println("psiFile.getName() = " + psiFile.getName());

Or if you want the actual path to the file, then use the following code :

PsiFile psiFile = e.getData(DataKeys.PSI_FILE); System.out.println("psiFile.getVirtualFile().getCanonicalPath() = " + psiFile.getVirtualFile().getCanonicalPath());

You can also get the Virtual File directly by :

VirtualFile virtualFile = e.getData(DataKeys.VIRTUAL_FILE);


Please sign in to leave a comment.