How to get package name of the file on which an action has been performed?

Answered

I have an Action class that is triggered on rt click in the project tree or in the editor. I want to extract the java module and java package name for .java files from the AnActionEvent in this case. I cant seem to figure out how.

I have tired,

actionEvent.getDataContext() but that doesnt have the package name and module name

0
3 comments

Use LangDataKeys.PSI_FILE to obtain PsiFile, when it’s a Java file you can obtain package name via com.intellij.psi.PsiJavaFile#getPackageName. PlatformCoreDataKeys#MODULE for Module.

0

Trying to get the base package for my project i.e. “com.android.example”. I've tried the above but it seems to just give the full directory for wherever I am for example: “com.android.example.account.a”. 

I'm trying to get just the base package as I need it for my own template for specific imports in Fragments. I am also trying to use `actionEvent.getDataContext()`. 

Thanks. 

0

Sorry, your post is a bit unclear. What is “base package”? Please always share your code.

0

Please sign in to leave a comment.