Need to perform an action whenever an import is added (java/scala)
已回答
Hi,
I'm looking for an ImportAddedListener or something of the sort for java & scala.
My use-case is that I need to perform an action (similar to adding a maven dependency to pom.xml) whenever an import is added to a java or scala.
Is there something I can use?
Thanks in advance!
请先登录再写评论。
You can try implementing PsiTreeChangeListener (http://www.jetbrains.org/intellij/sdk/docs/basics/architectural_overview/psi_files.html#how-do-i-get-notified-when-psi-files-change) and determine whether change was "adding import" but it will be hard to implement it correctly, see javadoc on PsiTreeChangeEvent.
Thanks!
That’s one nasty javadoc :(
No other avenue you think I can pursue? I thought maybe to decorate a few of the import relates IntentAction classes and work from there (hacky and problematic but might be better than that javadoc warning)
Also I'm thinking of maybe attacking this in a few different directions and I'd really appreciate your thoughts/inputs:
1. Paste related- Catch cases where someone pastes an import into a Java/Scala file. Considering registering a "ContentChangedListener".
2. Import auto complete related hooks. I've been able to identify 3 relevant cases and I'm not sure yet about the hooks to get notified about them.
I know this doesn't catch the case where someone will manually type a fully qualified import but I'm willing to not handle this for now.
Do you think this is a relevant approach?
If so any chance you know which hooks I can use to work after these actions happen?
Thanks!
Import related actions:
Auto complete of an import
Auto complete of a class
Resolving a class
There are many ways an import statement could be inserted/modified/deleted in sources - including by updating from version control system, or external changes by some tool. etc etc.
So any attempt to "catch" changes outside of underlying Document/PSI will always be incomplete.
Thank you for all your help.
For others what I did was something like this:
To get the manual addition of an import working I did:
Which means we have a token which belongs to a statement and there isn't a next sibling (before it's finished the next sibling exists and is an error sibling).
I'm now completely stuck on a scala ScImportStmt thing (getting the file where the imported class was declared in) but I should probably open a new thread about it. Thanks!