more general replace() implementation
authorDmitry Jemerov <yole@jetbrains.com>
Fri, 12 Feb 2010 16:20:20 +0000 (12 19:20 +0300)
committerDmitry Jemerov <yole@jetbrains.com>
Mon, 15 Feb 2010 18:25:12 +0000 (15 21:25 +0300)
platform/lang-impl/src/com/intellij/extapi/psi/ASTDelegatePsiElement.java

index 0474126..5dbb539 100644 (file)
@@ -29,7 +29,6 @@ import com.intellij.psi.PsiFile;
 import com.intellij.psi.PsiInvalidElementAccessException;
 import com.intellij.psi.impl.CheckUtil;
 import com.intellij.psi.impl.PsiManagerEx;
-import com.intellij.psi.impl.source.PsiFileImpl;
 import com.intellij.psi.impl.source.SourceTreeToPsiMap;
 import com.intellij.psi.impl.source.codeStyle.CodeEditUtil;
 import com.intellij.psi.impl.source.tree.ChangeUtil;
@@ -304,16 +303,17 @@ public abstract class ASTDelegatePsiElement extends PsiElementBase {
 
   @Override
   public PsiElement replace(@NotNull final PsiElement newElement) throws IncorrectOperationException {
+    CheckUtil.checkWritable(this);
+    TreeElement elementCopy = ChangeUtil.copyToElement(newElement);
     if (getParent() instanceof ASTDelegatePsiElement) {
       final ASTDelegatePsiElement parentElement = (ASTDelegatePsiElement)getParent();
-      CheckUtil.checkWritable(this);
-      TreeElement elementCopy = ChangeUtil.copyToElement(newElement);
       parentElement.replaceChildInternal(this, elementCopy);
-      elementCopy = ChangeUtil.decodeInformation(elementCopy);
-      return SourceTreeToPsiMap.treeElementToPsi(elementCopy);
     }
-
-    return super.replace(newElement);
+    else {
+      CodeEditUtil.replaceChild(getParent().getNode(), getNode(), elementCopy);
+    }
+    elementCopy = ChangeUtil.decodeInformation(elementCopy);
+    return SourceTreeToPsiMap.treeElementToPsi(elementCopy);
   }
 
   public void replaceChildInternal(final PsiElement child, final TreeElement newElement) {