All PsiElements recreated when the file is modified?

Say I have a following XML file:

  
    
      Something
    
  
  
    
      Other
    
  
]]>


In that file one of the XmlTags would have some userData set. Now if I modify the "Other" text by e.g. adding one new letter thus changing it to "Otther", it would seem that the userData stored in the XmlTag is lost. This happens when the same tag is searched again from the file.

That leads me to believe that the whole structure of the file has been recreated because of that (rather minor) modification and the old XmlTag object has been discarded and replaced with a new one. Is that true?

To put the same question differently, does any modification of a PsiFile cause the whole structure of that PsiFile to be recreated from the scratch?

This would seem to make it very inconvenient to store userData in PsiElements that describe the structure of a Java class or an XML file. Also it probably doesn't make sense to store references to any of those elements as any change to the file would basically make those references outdated, i.e. they would reference to a PsiElement that isn't used anymore.

Is all this true or is there something I don't understand?

- Markus

3 comments
Comment actions Permalink

PsiElements indeed can be recreated. This depends on how effectively parser
is able to survive on change. Incremental reparse doesn't work all the time
so one should not rely on this.

For caching purposes one could look on CachedValuesManager.createCachedValue().
The concept is quite simple: CachedValueProvider provides means for certain
value calculation along with items (PsiElements for instance) to depend on.
As one of the dependencies is outdated or invalidated CachedValuesManager
automatically recalculates the value.

There's one usage sample in OpenAPI sources: MethodSignatureUtil.MethodSignaturesProvider.

-


Maxim Shafirov
http://www.jetbrains.com
"Develop with pleasure!"

Say I have a following XML file:

> 
> 
> Something
> 
> 
> 
> 
> Other
> 
> 
> ]]>

In that file one of the XmlTags would have some userData set. Now if I
modify the "Other" text by e.g. adding one new letter thus changing it
to "Otther", it would seem that the userData stored in the XmlTag is
lost. This happens when the same tag is searched again from the file.

That leads me to believe that the whole structure of the file has been
recreated because of that (rather minor) modification and the old
XmlTag object has been discarded and replaced with a new one. Is that
true?

To put the same question differently, does any modification of a
PsiFile cause the whole structure of that PsiFile to be recreated from
the scratch?

This would seem to make it very inconvenient to store userData in
PsiElements that describe the structure of a Java class or an XML
file. Also it probably doesn't make sense to store references to any
of those elements as any change to the file would basically make those
references outdated, i.e. they would reference to a PsiElement that
isn't used anymore.

Is all this true or is there something I don't understand?

- Markus



0
Comment actions Permalink

Thank you for the answer!

0
Comment actions Permalink

What are the criteria for a "dependency"? Should it implement ModificationTracker?

0

Please sign in to leave a comment.