From 4f77751e8be88b68d726a39cb9d49690dd624310 Mon Sep 17 00:00:00 2001 From: Maxim Shafirov Date: Thu, 20 Jan 2005 00:22:28 +0300 Subject: [PATCH] Write interface added to ASTNode. Implementations delegate to ChangeUtil. --- openapi/src/com/intellij/lang/ASTNode.java | 6 +++++ .../psi/impl/source/tree/ChangeUtil.java | 12 +++++----- .../impl/source/tree/CompositeElement.java | 20 ++++++++++++++++ .../psi/impl/source/tree/LeafElement.java | 24 ++++++++++++++++++- 4 files changed, 55 insertions(+), 7 deletions(-) diff --git a/openapi/src/com/intellij/lang/ASTNode.java b/openapi/src/com/intellij/lang/ASTNode.java index 9277ba5bc859..94c76cb7ab12 100644 --- a/openapi/src/com/intellij/lang/ASTNode.java +++ b/openapi/src/com/intellij/lang/ASTNode.java @@ -30,5 +30,11 @@ public interface ASTNode extends UserDataHolder { ASTNode[] getChildren(TokenSet filter); + void addChild(ASTNode child, ASTNode anchorBefore); + void removeChild(ASTNode child); + void replaceChild(ASTNode oldChild, ASTNode newChild); + void replaceAllChildrenToChildrenOf(ASTNode anotherParent); + void addChildren(ASTNode firstChild, ASTNode lastChild, ASTNode anchorBefore); + Object clone(); } diff --git a/source/com/intellij/psi/impl/source/tree/ChangeUtil.java b/source/com/intellij/psi/impl/source/tree/ChangeUtil.java index 94fdab858a15..fd909732640b 100644 --- a/source/com/intellij/psi/impl/source/tree/ChangeUtil.java +++ b/source/com/intellij/psi/impl/source/tree/ChangeUtil.java @@ -1,18 +1,17 @@ package com.intellij.psi.impl.source.tree; +import com.intellij.lang.ASTNode; import com.intellij.lexer.JavaLexer; import com.intellij.lexer.Lexer; import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.editor.Document; import com.intellij.openapi.util.Key; import com.intellij.openapi.vfs.VirtualFile; -import com.intellij.openapi.editor.Document; import com.intellij.pom.java.LanguageLevel; import com.intellij.psi.*; -import com.intellij.psi.impl.DebugUtil; +import com.intellij.psi.impl.PsiDocumentManagerImpl; import com.intellij.psi.impl.PsiManagerImpl; import com.intellij.psi.impl.PsiTreeChangeEventImpl; -import com.intellij.psi.impl.PsiDocumentManagerImpl; -import com.intellij.psi.impl.compiled.ClsFileImpl; import com.intellij.psi.impl.cache.RepositoryManager; import com.intellij.psi.impl.light.LightClassReference; import com.intellij.psi.impl.light.LightTypeElement; @@ -22,11 +21,12 @@ import com.intellij.psi.impl.source.codeStyle.CodeStyleManagerEx; import com.intellij.psi.impl.source.parsing.*; import com.intellij.util.CharTable; import com.intellij.util.IncorrectOperationException; -import com.intellij.lang.ASTNode; public class ChangeUtil implements Constants { private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.source.tree.ChangeUtil"); + private ChangeUtil() {} + public static void addChild(final CompositeElement parent, final TreeElement child, TreeElement anchorBefore) { LOG.assertTrue(anchorBefore == null || anchorBefore.getTreeParent() == parent); @@ -93,7 +93,7 @@ public class ChangeUtil implements Constants { } } - public static void registerLeafsInCharTab(CharTable newCharTab, ASTNode child, CharTable oldCharTab) { + private static void registerLeafsInCharTab(CharTable newCharTab, ASTNode child, CharTable oldCharTab) { if(newCharTab == oldCharTab) return; while(child != null){ CharTable charTable = child.getUserData(CharTable.CHAR_TABLE_KEY); diff --git a/source/com/intellij/psi/impl/source/tree/CompositeElement.java b/source/com/intellij/psi/impl/source/tree/CompositeElement.java index 19e9f01208cd..37266cd68413 100644 --- a/source/com/intellij/psi/impl/source/tree/CompositeElement.java +++ b/source/com/intellij/psi/impl/source/tree/CompositeElement.java @@ -306,4 +306,24 @@ public class CompositeElement extends TreeElement implements Cloneable{ public ASTNode getLastChildNode() { return lastChild; } + + public void addChild(ASTNode child, ASTNode anchorBefore) { + ChangeUtil.addChild(this, (TreeElement)child, (TreeElement)anchorBefore); + } + + public void removeChild(ASTNode child) { + ChangeUtil.removeChild(this, (TreeElement)child); + } + + public void replaceChild(ASTNode oldChild, ASTNode newChild) { + ChangeUtil.replaceChild(this, (TreeElement)oldChild, (TreeElement)newChild); + } + + public void replaceAllChildrenToChildrenOf(ASTNode anotherParent) { + ChangeUtil.replaceAllChildren(this, (CompositeElement)anotherParent); + } + + public void addChildren(ASTNode firstChild, ASTNode lastChild, ASTNode anchorBefore) { + ChangeUtil.addChildren(this, (TreeElement)firstChild, lastChild, (TreeElement)anchorBefore); + } } diff --git a/source/com/intellij/psi/impl/source/tree/LeafElement.java b/source/com/intellij/psi/impl/source/tree/LeafElement.java index a93e8fd988db..373806e49749 100644 --- a/source/com/intellij/psi/impl/source/tree/LeafElement.java +++ b/source/com/intellij/psi/impl/source/tree/LeafElement.java @@ -3,8 +3,10 @@ package com.intellij.psi.impl.source.tree; import com.intellij.psi.tree.IElementType; import com.intellij.psi.tree.TokenSet; import com.intellij.util.CharTable; +import com.intellij.util.IncorrectOperationException; import com.intellij.util.text.StringSearcher; import com.intellij.lang.ASTNode; +import sun.plugin.dom.exception.InvalidStateException; public abstract class LeafElement extends TreeElement { private volatile int myState = 0; // 16 bit for type, 15 bit for state and 1 bit for parentFlag @@ -127,5 +129,25 @@ public abstract class LeafElement extends TreeElement { public ASTNode[] getChildren(TokenSet filter) { return TreeElement.EMPTY_ARRAY; - } + } + + public void addChild(ASTNode child, ASTNode anchorBefore) { + throw new InvalidStateException("Leaf elements cannot have children."); + } + + public void removeChild(ASTNode child) { + throw new InvalidStateException("Leaf elements cannot have children."); + } + + public void replaceChild(ASTNode oldChild, ASTNode newChild) { + throw new InvalidStateException("Leaf elements cannot have children."); + } + + public void replaceAllChildrenToChildrenOf(ASTNode anotherParent) { + throw new InvalidStateException("Leaf elements cannot have children."); + } + + public void addChildren(ASTNode firstChild, ASTNode lastChild, ASTNode anchorBefore) { + throw new InvalidStateException("Leaf elements cannot have children."); + } }