How do I react to PSI Tree changes?

Hi,

ok,  I managed to create PSI reference ,  my DOM modell  is purring and the next question is -  how do I react to changes?
I would like to be informed of changes in certain PSI / DOM node to invalidate cached references

Is project service good place to store cached information?  

7 comments
Comment actions Permalink

Hello, Konstantin.

For PSI tree changes you can try to use

PsiManager.addPsiTreeChangeListener(PsiTreeChangeListener listener)


Platform has an adapter for PsiTreeChangeListener -- PsiTreeChangeAdapter.

Hope, it will help you.

0
Comment actions Permalink

As I mentioned before, the correct API for caching resolve results is ResolveCache. It takes care of invalidating cached results on PSI change automatically.

0
Comment actions Permalink

OK,  slowly grokking it.  


Say, I'm implementing some DOM implementation which caches
intersting data obtained from subtags.  It stores result on ResolveCache, using underlying XML tag as key
( which is PSI element )  -   then it will be invalidated upon changes in subelements?

0
Comment actions Permalink

ResolveCache is invalidated on any change in the PSI tree anywhere.

0
Comment actions Permalink

Correct place for cache access would be implementation of PsiReferenceProvider?

0
Comment actions Permalink

No, it needs to be accessed from PsiReference.resolve(). ResolveCache has plenty of usages in the CE source code, you can look for examples there.

0
Comment actions Permalink

OK, getting it.  Key would be reference itself and values are candidates.  But in my case creating list of candidates is best
done in DOM model ( as this lst of candidates can be reused by several references) - so I would like to cache it also there

say:  talk with resolve cache from DOM Element,  initiated by reference in context of this element

Is it possible to keep a link to resolve cache in DOM element, or shall I retrieve it each time with:

   private ResolveCache getCache() {

        Project project = SharedImplUtil.getContainingFile(this.getXmlElement().getNode()).getProject();
        return ResolveCache.getInstance(project);

    }

0

Please sign in to leave a comment.