How can I register a QuickFix from a PsiTreeChangeListener?

Answered

Hi,

I have a class which implements PsiTreeChangeListener and does some actions based on the events. 

Some events have ambigous data which I'd like to input the user and have them decide. Basically I'd like to annotation a specific import statement (which I have because it's the PsiElement I just got).

 

I thought I need to register a quickfix but I can't find how to do that from a PsiTreeChangeListener (tried injecting a ProblemsHolder or an AnnotationHolder but I get an `unsatisfied dependency`).

 

Any help would be appreciated,

Ittai 

7 comments
Comment actions Permalink

PsiTreeChangeListener is not the right place to do this:

/**
* Listener for receiving notifications about all changes in the PSI tree of a project.<p></p>
*
* Try to avoid processing PSI events at all cost! See {@link PsiTreeChangeEvent} documentation for more details.
*
* @see PsiManager#addPsiTreeChangeListener(PsiTreeChangeListener)
* @see PsiManager#removePsiTreeChangeListener(PsiTreeChangeListener)
*/

You should be implementing IntentionAction which is designed for what you are trying to do.

 

0
Comment actions Permalink

Thanks for your response. I chose PsiTreeChangeListener following advice I got from Jetbrains support on this channel due to other requirements.

Still I'd like to understand a bit better what you mean- AFAIU the key part is not the IntentionAction but rather the part that adds it (or the QuickFix) which can be an Annotator or an Inspection. Am I mistaken?

My problem is that I need to be invoked only when the import expression is added or changed (Inspection for example is called many many times).

0
Comment actions Permalink

You can register your fix via Intention, Annotator or Inspection (depending on configurability/options etc.). All variants will update whenever the file is changed and their applicability changes.

0
Comment actions Permalink

Thanks Yann!

Is there a way for me to have an Intention, Annotator or inspection which are only triggered from a listener (directly, message bus, other mechanism)?

0
Comment actions Permalink

No, this is not the way they're designed to work

0
Comment actions Permalink

Fair enough. Thank you.

I'll try to elaborate on the problem I'm trying to solve (if you'd rather I open a new thread dedicated to it I'll do it):

Whenever an import is added or changed in java/scala files I want to do some action (it has an expensive lookup with external service and if true then expensive operation of running an external binary). For this I'm using the listener (I was unable to use the inspection approach with PsiCache) and it works well when no user interaction is needed. Sometimes the expensive lookup doesn't return 0 or 1 results but several and I would like to ask the user what to do.

The most obvious one is to register a fix but that seems like something I can't do.

What do you think I should do?

 

Thanks in advance!

0
Comment actions Permalink

You could store "was there a relevant change in file" from listener in file's user data and use that in quickfix/inspection to quickly skip the expensive operations.

0

Please sign in to leave a comment.