find the current class

Hi,
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

0
2 comments
Avatar
Permanently deleted user

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 - http://confluence.jetbrains.com/display/IDEADEV/IntelliJ+IDEA+Architectural+Overview#IntelliJIDEAArchitecturalOverview-PsiFiles

0

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);

0

Please sign in to leave a comment.