Correct PsiAnchor for cls elements
authorMaxim Shafirov <max@jetbrains.com>
Fri, 25 Dec 2009 10:50:14 +0000 (13:50 +0300)
committerMaxim Shafirov <max@jetbrains.com>
Fri, 25 Dec 2009 10:50:14 +0000 (13:50 +0300)
java/java-impl/src/com/intellij/psi/impl/compiled/ClsRepositoryPsiElement.java
platform/lang-impl/src/com/intellij/psi/PsiAnchor.java

index 6d48380dedd0b3fc66f4360cb92ba14d6e29b6d7..ef1ac2797bcdf0e24d642a7a6ecc9fa13816977f 100644 (file)
  */
 package com.intellij.psi.impl.compiled;
 
-import com.intellij.psi.PsiElement;
-import com.intellij.psi.PsiFile;
-import com.intellij.psi.PsiInvalidElementAccessException;
-import com.intellij.psi.PsiManager;
+import com.intellij.psi.*;
+import com.intellij.psi.stubs.IStubElementType;
 import com.intellij.psi.stubs.PsiFileStub;
 import com.intellij.psi.stubs.StubElement;
 import com.intellij.util.ArrayUtil;
@@ -26,13 +24,17 @@ import org.jetbrains.annotations.NotNull;
 
 import java.util.List;
 
-public abstract class ClsRepositoryPsiElement<T extends StubElement> extends ClsElementImpl {
+public abstract class ClsRepositoryPsiElement<T extends StubElement> extends ClsElementImpl implements StubBasedPsiElement<T> {
   private final T myStub;
 
   protected ClsRepositoryPsiElement(final T stub) {
     myStub = stub;
   }
 
+  public IStubElementType getElementType() {
+    return myStub.getStubType();
+  }
+
   public PsiElement getParent() {
     return myStub.getParentStub().getPsi();
   }
index 81ee7d95cd512f223d8047a1ad8575a11b20199b..24121c111b39611e45104bb69dedb8420f46eaf2 100644 (file)
@@ -26,6 +26,7 @@ import com.intellij.openapi.util.NullableComputable;
 import com.intellij.openapi.util.TextRange;
 import com.intellij.psi.impl.light.LightElement;
 import com.intellij.psi.impl.source.PsiFileImpl;
+import com.intellij.psi.impl.source.PsiFileWithStubSupport;
 import com.intellij.psi.stubs.StubBase;
 import com.intellij.psi.stubs.StubElement;
 import com.intellij.psi.stubs.StubTree;
@@ -46,7 +47,7 @@ public abstract class PsiAnchor {
   public abstract int getEndOffset();
 
   public static PsiAnchor create(final PsiElement element) {
-    if (element instanceof PsiCompiledElement || element instanceof PsiFile) {
+    if (element instanceof PsiFile) {
       return new HardReference(element);
     }
 
@@ -55,7 +56,7 @@ public abstract class PsiAnchor {
       return new HardReference(element);
     }
 
-    if (element instanceof StubBasedPsiElement && element.isPhysical() && ((PsiFileImpl)file).getContentElementType() instanceof IStubFileElementType) {
+    if (element instanceof StubBasedPsiElement && element.isPhysical() && (element instanceof PsiCompiledElement || ((PsiFileImpl)file).getContentElementType() instanceof IStubFileElementType)) {
       final StubBasedPsiElement elt = (StubBasedPsiElement)element;
       if (elt.getStub() != null || elt.getElementType().shouldCreateStub(element.getNode())) {
         return new StubIndexReference(file, calcStubIndex((StubBasedPsiElement)element));
@@ -225,12 +226,17 @@ public abstract class PsiAnchor {
     public PsiElement retrieve() {
       return ApplicationManager.getApplication().runReadAction(new NullableComputable<PsiElement>() {
         public PsiElement compute() {
-          PsiFileImpl fileImpl = (PsiFileImpl)myFile;
+          PsiFileWithStubSupport fileImpl = (PsiFileWithStubSupport)myFile;
           StubTree tree = fileImpl.getStubTree();
 
           boolean foreign = (tree == null);
           if (foreign) {
-            tree = fileImpl.calcStubTree();
+            if (fileImpl instanceof PsiFileImpl) {
+              tree = ((PsiFileImpl)fileImpl).calcStubTree();
+            }
+            else {
+              return null;
+            }
           }
 
           StubElement stub = tree.getPlainList().get(myIndex);