UserData lifecycle

There are various caches that are mediated through UserDataHolder.  Especially frequently the PyFile is tagged with some data.  I'm wondering what's the lifecycle of these caches especially with respect to PyFile.  When are these caches cleared? Is there a way to force clear these caches? Do stubs inherit the caches of the corresponding PyFile objects?


There is no automatic mechanism that would clear the userdata of a PSI element. Normally it lives as long as the element itself, which for a PsiFile can be the entire IDE usage session.

If you need to store a value in UserData that depends on other things (such as the contents of the file itself), you can use CachedValuesManager.createCachedValue() and specify the dependencies for the value.

There is no mechanism to copy UserData between PSI elements and the corresponding stubs.


Hello. I have got a question.

PsiLanguageInjectionHost has a putUserData(key, value) method. If I use it to put a value, and then use FileContentUtil.reparseFiles(PsiLanguageInjectionHost, new List<>(), true), it happens that the value, which I put, disappears.


host.putUserData(Constants.KEY, value);


FileContentUtil.reparseFiles(project, new List<>(), true);

assert host.getUserData(Constants.KEY) == null;




Please sign in to leave a comment.