diff --git a/java/java-tests/testSrc/com/intellij/psi/impl/smartPointers/SmartPsiElementPointersTest.java b/java/java-tests/testSrc/com/intellij/psi/impl/smartPointers/SmartPsiElementPointersTest.java index 1425b9b3ab76..d87f5d48ce8e 100644 --- a/java/java-tests/testSrc/com/intellij/psi/impl/smartPointers/SmartPsiElementPointersTest.java +++ b/java/java-tests/testSrc/com/intellij/psi/impl/smartPointers/SmartPsiElementPointersTest.java @@ -877,4 +877,23 @@ public class SmartPsiElementPointersTest extends CodeInsightTestCase { private SmartPointerEx createPointer(T element) { return (SmartPointerEx)getPointerManager().createSmartPsiElementPointer(element); } + + public void testAnchorInfoHasRange() throws Exception { + PsiJavaFile file = (PsiJavaFile)createFile("a.java", "class C1{}"); + assertNotNull(((PsiFileImpl) file).getStubTree()); + PsiClass psiClass = file.getClasses()[0]; + + Segment range = createPointer(psiClass).getRange(); + assertNotNull(range); + assertEquals(psiClass.getNameIdentifier().getTextRange(), TextRange.create(range)); + + file = (PsiJavaFile)createFile("b.java", "class C2{}"); + assertNotNull(((PsiFileImpl) file).getStubTree()); + psiClass = file.getClasses()[0]; + + range = createPointer(psiClass).getPsiRange(); + assertNotNull(range); + assertEquals(psiClass.getNameIdentifier().getTextRange(), TextRange.create(range)); + } + } diff --git a/platform/core-impl/src/com/intellij/psi/impl/smartPointers/AnchorElementInfo.java b/platform/core-impl/src/com/intellij/psi/impl/smartPointers/AnchorElementInfo.java index 67144367c3e1..c3a38325f298 100644 --- a/platform/core-impl/src/com/intellij/psi/impl/smartPointers/AnchorElementInfo.java +++ b/platform/core-impl/src/com/intellij/psi/impl/smartPointers/AnchorElementInfo.java @@ -16,9 +16,9 @@ package com.intellij.psi.impl.smartPointers; import com.intellij.lang.LanguageUtil; -import com.intellij.openapi.application.ApplicationManager; -import com.intellij.openapi.editor.Document; -import com.intellij.openapi.util.*; +import com.intellij.openapi.util.ProperTextRange; +import com.intellij.openapi.util.Segment; +import com.intellij.openapi.util.TextRange; import com.intellij.psi.PsiAnchor; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiFile; @@ -102,16 +102,17 @@ class AnchorElementInfo extends SelfElementInfo { @Override public boolean pointsToTheSameElementAs(@NotNull final SmartPointerElementInfo other) { if (other instanceof AnchorElementInfo) { - AnchorElementInfo otherAnchor = (AnchorElementInfo)other; - if ((getStubId() == -1) != (otherAnchor.getStubId() == -1)) { - return ApplicationManager.getApplication().runReadAction(new Computable() { - @Override - public Boolean compute() { - return Comparing.equal(restoreElement(), other.restoreElement()); - } - }); + if (!getVirtualFile().equals(other.getVirtualFile())) return false; + + long packed1 = myStubElementTypeAndId; + long packed2 = ((AnchorElementInfo)other).myStubElementTypeAndId; + + if (packed1 != -1 && packed2 != -1) { + return packed1 == packed2; + } + if (packed1 != -1 || packed2 != -1) { + return areRestoredElementsEqual(other); } - if (myStubElementTypeAndId != otherAnchor.myStubElementTypeAndId) return false; } return super.pointsToTheSameElementAs(other); } @@ -126,9 +127,7 @@ class AnchorElementInfo extends SelfElementInfo { private void switchToTree() { PsiElement element = restoreElement(); - Document document = getDocumentToSynchronize(); - if (element != null && document != null) { - // switch to tree + if (element != null) { PsiElement anchor = AnchorElementInfoFactory.getAnchor(element); if (anchor == null) anchor = element; myType = AnchorTypeInfo.obtainInfo(anchor, myType.getFileLanguage()); @@ -146,4 +145,12 @@ class AnchorElementInfo extends SelfElementInfo { return super.getRange(); } + @Nullable + @Override + public ProperTextRange getPsiRange() { + if (getStubId() != -1) { + switchToTree(); + } + return super.getPsiRange(); + } } diff --git a/platform/core-impl/src/com/intellij/psi/impl/smartPointers/SelfElementInfo.java b/platform/core-impl/src/com/intellij/psi/impl/smartPointers/SelfElementInfo.java index 7a72f52a778b..a704c2ae27f7 100644 --- a/platform/core-impl/src/com/intellij/psi/impl/smartPointers/SelfElementInfo.java +++ b/platform/core-impl/src/com/intellij/psi/impl/smartPointers/SelfElementInfo.java @@ -232,16 +232,19 @@ public class SelfElementInfo extends SmartPointerElementInfo { public boolean pointsToTheSameElementAs(@NotNull final SmartPointerElementInfo other) { if (other instanceof SelfElementInfo) { SelfElementInfo otherInfo = (SelfElementInfo)other; + if (!getVirtualFile().equals(other.getVirtualFile()) || myType != otherInfo.myType) return false; + Segment range1 = getPsiRange(); Segment range2 = otherInfo.getPsiRange(); - return Comparing.equal(getVirtualFile(), otherInfo.getVirtualFile()) - && myType == otherInfo.myType - && range1 != null - && range2 != null + return range1 != null && range2 != null && range1.getStartOffset() == range2.getStartOffset() && range1.getEndOffset() == range2.getEndOffset() ; } + return areRestoredElementsEqual(other); + } + + protected boolean areRestoredElementsEqual(@NotNull final SmartPointerElementInfo other) { return ApplicationManager.getApplication().runReadAction(new Computable() { @Override public Boolean compute() {