Code inspection related with multiple files/multiple places in the same file
I'm working on a plugin for Android Studio and I want to implement a code inspection that will register problems based on the status of multiple files/multiple places in the same file. For example, I want to flag a warning for using a Java API call only when a specific permission is defined in the manifest file, which requires that if I remove the permission in the manifest file, the warning that was flagged before for that API should be dismissed immediately as well.
Because this is similar to some existing code inspections such as "unused variable" detection, it seems like a doable task, but I just don't know what's the right way to do it and what API to use.
I have searched the forum but didn't find similar questions. Could anyone shed some light on how to implement this? Thanks a lot in advance!
Tianshi
Please sign in to leave a comment.
Hi Tianshil,
IDEA restarts highlighting for all visible editors on every change (with some delay to allow fast typing). So if you would check permissions before java api check, then you should achieve what you want.
Anna
Thanks a lot, Anna!
I want to check whether I get it right: Do you mean that the element visitor functions will be called to inspect all visible files on every change? Will the file that's being changed be inspected first? Or the order is not related to which file is being changed?
BTW, is there an API to track the change that was just made? For example, returning which line/Java element was just added/deleted, or just returning which file was just changed?
Tianshi
The visitors would be called on every change and as they are called in parallel, you should not rely on any order. E.g. if you have a method, then you may check if it is used and if the usage was deleted a moment ago, you won't find the usage anymore and you would be able to highlight as unused.
There is com.intellij.psi.PsiTreeChangeListener, it provides all details about the change (added/removed child/children, etc), but you should not use it in the inspections.
Hope it helps,
Anna
Thanks, that was very helpful.