do not keep strong reference to cached psi element

This commit is contained in:
Egor.Ushakov
2015-03-23 20:15:34 +03:00
parent 4f83f03fae
commit 736da7bfbc
@@ -32,6 +32,7 @@ import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.lang.ref.WeakReference;
import java.util.List;
/**
@@ -59,7 +60,7 @@ public abstract class SourcePosition implements Navigatable{
@NotNull private final PsiFile myFile;
private long myModificationStamp = -1L;
private PsiElement myPsiElement;
private WeakReference<PsiElement> myPsiElementRef;
private Integer myLine;
private Integer myOffset;
@@ -120,7 +121,7 @@ public abstract class SourcePosition implements Navigatable{
myModificationStamp = myFile.getModificationStamp();
myLine = null;
myOffset = null;
myPsiElement = null;
myPsiElementRef = null;
}
}
@@ -128,7 +129,7 @@ public abstract class SourcePosition implements Navigatable{
if (myModificationStamp != myFile.getModificationStamp()) {
return true;
}
final PsiElement psiElement = myPsiElement;
final PsiElement psiElement = myPsiElementRef != null ? myPsiElementRef.get() : null;
return psiElement != null && !ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {
@Override
public Boolean compute() {
@@ -158,10 +159,13 @@ public abstract class SourcePosition implements Navigatable{
@Override
public PsiElement getElementAt() {
updateData();
if (myPsiElement == null) {
myPsiElement = calcPsiElement();
PsiElement element = myPsiElementRef != null ? myPsiElementRef.get() : null;
if (element == null) {
element = calcPsiElement();
myPsiElementRef = new WeakReference<PsiElement>(element);
return element;
}
return myPsiElement;
return element;
}
protected int calcLine() {