diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/api/statements/GrIfStatement.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/api/statements/GrIfStatement.java index 44dd216fa9d6..4db5b039c54c 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/api/statements/GrIfStatement.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/api/statements/GrIfStatement.java @@ -16,10 +16,10 @@ package org.jetbrains.plugins.groovy.lang.psi.api.statements; -import org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement; -import org.jetbrains.annotations.Nullable; -import com.intellij.util.IncorrectOperationException; import com.intellij.psi.PsiElement; +import com.intellij.util.IncorrectOperationException; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement; import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression; /** @@ -36,9 +36,9 @@ public interface GrIfStatement extends GrStatement, GrControlStatement { @Nullable GrStatement getElseBranch(); - GrStatement replaceThenBranch(GrStatement newBranch) throws IncorrectOperationException; + T replaceThenBranch(T newBranch) throws IncorrectOperationException; - GrStatement replaceElseBranch(GrStatement newBranch) throws IncorrectOperationException; + T replaceElseBranch(T newBranch) throws IncorrectOperationException; @Nullable PsiElement getElseKeyword(); diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/api/statements/GrLoopStatement.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/api/statements/GrLoopStatement.java index 52d7f358a4f8..2e241fac0875 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/api/statements/GrLoopStatement.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/api/statements/GrLoopStatement.java @@ -23,6 +23,6 @@ import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrCondition; * @author ilyas */ public interface GrLoopStatement extends GrStatement { - GrCondition replaceBody (GrCondition statement) throws IncorrectOperationException; + T replaceBody (T statement) throws IncorrectOperationException; GrStatement getBody(); } diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/api/statements/GrVariable.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/api/statements/GrVariable.java index 2db88f9ab5c6..dcf2ddc6ab78 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/api/statements/GrVariable.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/api/statements/GrVariable.java @@ -22,6 +22,7 @@ import com.intellij.util.ArrayFactory; import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.Nullable; import org.jetbrains.plugins.groovy.lang.psi.GrNamedElement; +import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList; import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression; import org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement; @@ -51,4 +52,7 @@ public interface GrVariable extends PsiVariable, GrNamedElement { @Nullable PsiType getDeclaredType(); + + @Nullable + GrModifierList getModifierList(); } diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/api/statements/GrWhileStatement.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/api/statements/GrWhileStatement.java index 55152cbec034..527c7d28bfb0 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/api/statements/GrWhileStatement.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/api/statements/GrWhileStatement.java @@ -16,10 +16,10 @@ package org.jetbrains.plugins.groovy.lang.psi.api.statements; +import com.intellij.psi.PsiElement; +import org.jetbrains.annotations.Nullable; import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrCondition; import org.jetbrains.plugins.groovy.lang.psi.api.formatter.GrControlStatement; -import org.jetbrains.annotations.Nullable; -import com.intellij.psi.PsiElement; /** * @autor: Dmitry.Krasilschikov @@ -27,14 +27,16 @@ import com.intellij.psi.PsiElement; */ public interface GrWhileStatement extends GrStatement, GrControlStatement, GrLoopStatement { - public GrCondition getCondition(); - - public GrStatement getBody(); + @Nullable + GrCondition getCondition(); @Nullable - public PsiElement getRParenth(); + GrStatement getBody(); - @Nullable - public PsiElement getLParenth(); + @Nullable + PsiElement getRParenth(); + + @Nullable + PsiElement getLParenth(); } diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/api/statements/params/GrParameter.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/api/statements/params/GrParameter.java index 063a4279a974..460574557b32 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/api/statements/params/GrParameter.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/api/statements/params/GrParameter.java @@ -16,15 +16,15 @@ package org.jetbrains.plugins.groovy.lang.psi.api.statements.params; -import com.intellij.psi.PsiModifierList; +import com.intellij.psi.PsiParameter; import com.intellij.util.ArrayFactory; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrCondition; -import org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement; +import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList; import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable; import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression; -import com.intellij.psi.PsiParameter; +import org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement; /** * @author: Dmitry.Krasilschikov @@ -46,7 +46,7 @@ public interface GrParameter extends PsiParameter, GrVariable, GrCondition { GrExpression getDefaultInitializer(); @NotNull - PsiModifierList getModifierList(); + GrModifierList getModifierList(); boolean isOptional(); } diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/api/util/GrStatementOwner.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/api/util/GrStatementOwner.java index ebe539bda523..758b2360d651 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/api/util/GrStatementOwner.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/api/util/GrStatementOwner.java @@ -16,9 +16,10 @@ package org.jetbrains.plugins.groovy.lang.psi.api.util; -import com.intellij.util.IncorrectOperationException; import com.intellij.psi.PsiElement; +import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement; import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement; @@ -27,7 +28,7 @@ import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement; */ public interface GrStatementOwner extends GroovyPsiElement { - GrStatement addStatementBefore(@NotNull GrStatement statement, GrStatement anchor) throws IncorrectOperationException; + GrStatement addStatementBefore(@NotNull GrStatement statement, @Nullable GrStatement anchor) throws IncorrectOperationException; void removeElements(PsiElement[] elements) throws IncorrectOperationException; diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/GroovyPsiElementFactoryImpl.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/GroovyPsiElementFactoryImpl.java index b7206c80def0..a0621b1f89e3 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/GroovyPsiElementFactoryImpl.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/GroovyPsiElementFactoryImpl.java @@ -254,11 +254,12 @@ public class GroovyPsiElementFactoryImpl extends GroovyPsiElementFactory { } public GrClosableBlock createClosureFromText(String closureText) throws IncorrectOperationException { - PsiFile psiFile = createDummyFile(closureText); - ASTNode node = psiFile.getFirstChild().getNode(); - if (node.getElementType() != GroovyElementTypes.CLOSABLE_BLOCK) - throw new IncorrectOperationException("Invalid all text"); - return (GrClosableBlock) node.getPsi(); + GroovyFile psiFile = createDummyFile("def foo = " + closureText); + final GrStatement st = psiFile.getStatements()[0]; + LOG.assertTrue(st instanceof GrVariableDeclaration, closureText); + final GrExpression initializer = ((GrVariableDeclaration)st).getVariables()[0].getInitializerGroovy(); + LOG.assertTrue(initializer instanceof GrClosableBlock, closureText); + return ((GrClosableBlock)initializer); } private GroovyFileImpl createDummyFile(String text, boolean physical) { @@ -298,9 +299,7 @@ public class GroovyPsiElementFactoryImpl extends GroovyPsiElementFactory { public GrCodeReferenceElement createTypeOrPackageReference(String qName) { final GroovyFileBase file = createDummyFile("def " + qName + " i"); - LOG - .assertTrue(file.getTopStatements().length == 1 && (GrVariableDeclaration)file.getTopStatements()[0] instanceof GrVariableDeclaration, - qName); + LOG.assertTrue(file.getTopStatements().length == 1 && (GrVariableDeclaration)file.getTopStatements()[0] instanceof GrVariableDeclaration, qName); GrVariableDeclaration varDecl = (GrVariableDeclaration) file.getTopStatements()[0]; final GrClassTypeElement typeElement = (GrClassTypeElement) varDecl.getTypeElementGroovy(); assert typeElement != null; diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/PsiImplUtil.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/PsiImplUtil.java index 437ecfbdf737..f783ce405d2d 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/PsiImplUtil.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/PsiImplUtil.java @@ -19,6 +19,7 @@ package org.jetbrains.plugins.groovy.lang.psi.impl; import com.intellij.lang.ASTNode; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.module.Module; +import com.intellij.openapi.project.Project; import com.intellij.openapi.roots.OrderEntry; import com.intellij.openapi.roots.ProjectFileIndex; import com.intellij.openapi.roots.ProjectRootManager; @@ -34,6 +35,7 @@ import com.intellij.psi.util.MethodSignatureUtil; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.reference.SoftReference; import com.intellij.util.ArrayUtil; +import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes; @@ -42,6 +44,7 @@ import org.jetbrains.plugins.groovy.lang.psi.GrNamedElement; import org.jetbrains.plugins.groovy.lang.psi.GrQualifiedReference; import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory; import org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult; +import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrCondition; import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement; import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable; import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration; @@ -385,10 +388,6 @@ public class PsiImplUtil { method.hasModifierProperty(PsiModifier.STATIC); } - public static PsiType getNominalType(final GrExpression expr) { - return expr.getType(); - } - public static void deleteStatementTail(PsiElement container, @NotNull PsiElement statement) { PsiElement next = statement.getNextSibling(); while (next != null) { @@ -449,6 +448,7 @@ public class PsiImplUtil { return AstBufferUtil.getTextSkippingTokens(node, TokenSets.WHITE_SPACES_OR_COMMENTS); } + @Nullable public static PsiCodeBlock getOrCreatePsiCodeBlock(GrOpenBlock block) { if (block == null) return null; @@ -459,4 +459,17 @@ public class PsiImplUtil { block.putUserData(PSI_CODE_BLOCK, new SoftReference(newBody)); return newBody; } + + public static T replaceBody(T newBody, GrStatement body, ASTNode node, Project project) { + if (body == null || newBody == null) { + throw new IncorrectOperationException(); + } + ASTNode oldBodyNode = body.getNode(); + if (oldBodyNode.getTreePrev() != null && mNLS.equals(oldBodyNode.getTreePrev().getElementType())) { + ASTNode whiteNode = GroovyPsiElementFactory.getInstance(project).createWhiteSpace().getNode(); + node.replaceChild(oldBodyNode.getTreePrev(), whiteNode); + } + node.replaceChild(oldBodyNode, newBody.getNode()); + return newBody; + } } diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/GrForStatementImpl.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/GrForStatementImpl.java index bcf67bf428a0..e945782cadc7 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/GrForStatementImpl.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/GrForStatementImpl.java @@ -26,7 +26,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes; import org.jetbrains.plugins.groovy.lang.psi.GroovyElementVisitor; -import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory; import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrCondition; import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrForStatement; import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement; @@ -34,6 +33,7 @@ import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable; import org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForClause; import org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrForInClause; import org.jetbrains.plugins.groovy.lang.psi.impl.GroovyPsiElementImpl; +import org.jetbrains.plugins.groovy.lang.psi.impl.PsiImplUtil; import org.jetbrains.plugins.groovy.lang.resolve.ResolveUtil; /** @@ -78,23 +78,8 @@ public class GrForStatementImpl extends GroovyPsiElementImpl implements GrForSta return true; } - public GrCondition replaceBody(GrCondition newBody) throws IncorrectOperationException { - if (getBody() == null || - newBody == null) { - throw new IncorrectOperationException(); - } - ASTNode oldBodyNode = getBody().getNode(); - if (oldBodyNode.getTreePrev() != null && - GroovyTokenTypes.mNLS.equals(oldBodyNode.getTreePrev().getElementType())) { - ASTNode whiteNode = GroovyPsiElementFactory.getInstance(getProject()).createWhiteSpace().getNode(); - getNode().replaceChild(oldBodyNode.getTreePrev(), whiteNode); - } - this.getNode().replaceChild(oldBodyNode, newBody.getNode()); - ASTNode newNode = newBody.getNode(); - if (!(newNode.getPsi() instanceof GrCondition)) { - throw new IncorrectOperationException(); - } - return (GrCondition) newNode.getPsi(); + public T replaceBody(T newBody) throws IncorrectOperationException { + return PsiImplUtil.replaceBody(newBody, getBody(), getNode(), getProject()); } public PsiElement getRParenth() { diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/GrIfStatementImpl.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/GrIfStatementImpl.java index 7d608f4b8e8e..4e0456c8156b 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/GrIfStatementImpl.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/GrIfStatementImpl.java @@ -23,12 +23,11 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes; import org.jetbrains.plugins.groovy.lang.psi.GroovyElementVisitor; -import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory; -import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrCondition; import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrIfStatement; import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement; import org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression; import org.jetbrains.plugins.groovy.lang.psi.impl.GroovyPsiElementImpl; +import org.jetbrains.plugins.groovy.lang.psi.impl.PsiImplUtil; /** * @autor: ilyas @@ -78,43 +77,12 @@ public class GrIfStatementImpl extends GroovyPsiElementImpl implements GrIfState return null; } - public GrStatement replaceThenBranch(GrStatement newBranch) throws IncorrectOperationException { - final GrStatement thenBranch = getThenBranch(); - if (thenBranch == null || - newBranch == null) { - throw new IncorrectOperationException(); - } - ASTNode oldBodyNode = thenBranch.getNode(); - if (oldBodyNode.getTreePrev() != null && - GroovyTokenTypes.mNLS.equals(oldBodyNode.getTreePrev().getElementType())) { - ASTNode whiteNode = GroovyPsiElementFactory.getInstance(getProject()).createWhiteSpace().getNode(); - getNode().replaceChild(oldBodyNode.getTreePrev(), whiteNode); - } - this.getNode().replaceChild(oldBodyNode, newBranch.getNode()); - ASTNode newNode = newBranch.getNode(); - if (!(newNode.getPsi() instanceof GrCondition)) { - throw new IncorrectOperationException(); - } - return (GrStatement) newNode.getPsi(); + public T replaceThenBranch(T newBranch) throws IncorrectOperationException { + return PsiImplUtil.replaceBody(newBranch, getThenBranch(), getNode(), getProject()); } - public GrStatement replaceElseBranch(GrStatement newBranch) throws IncorrectOperationException { - final GrStatement elseBranch = getElseBranch(); - if (elseBranch == null || newBranch == null) { - throw new IncorrectOperationException(); - } - ASTNode oldBodyNode = elseBranch.getNode(); - if (oldBodyNode.getTreePrev() != null && - GroovyTokenTypes.mNLS.equals(oldBodyNode.getTreePrev().getElementType())) { - ASTNode whiteNode = GroovyPsiElementFactory.getInstance(getProject()).createWhiteSpace().getNode(); - getNode().replaceChild(oldBodyNode.getTreePrev(), whiteNode); - } - this.getNode().replaceChild(oldBodyNode, newBranch.getNode()); - ASTNode newNode = newBranch.getNode(); - if (!(newNode.getPsi() instanceof GrCondition)) { - throw new IncorrectOperationException(); - } - return (GrStatement) newNode.getPsi(); + public T replaceElseBranch(T newBranch) throws IncorrectOperationException { + return PsiImplUtil.replaceBody(newBranch, getElseBranch(), getNode(), getProject()); } public PsiElement getElseKeyword() { diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/GrWhileStatementImpl.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/GrWhileStatementImpl.java index 5bf932faa6ea..4ed7e275a08f 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/GrWhileStatementImpl.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/GrWhileStatementImpl.java @@ -17,17 +17,17 @@ package org.jetbrains.plugins.groovy.lang.psi.impl.statements; import com.intellij.lang.ASTNode; -import com.intellij.util.IncorrectOperationException; import com.intellij.psi.PsiElement; +import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes; -import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory; import org.jetbrains.plugins.groovy.lang.psi.GroovyElementVisitor; -import org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement; import org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrCondition; import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement; import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrWhileStatement; import org.jetbrains.plugins.groovy.lang.psi.impl.GroovyPsiElementImpl; +import org.jetbrains.plugins.groovy.lang.psi.impl.PsiImplUtil; /** * @autor: ilyas @@ -45,6 +45,7 @@ public class GrWhileStatementImpl extends GroovyPsiElementImpl implements GrWhil return "WHILE statement"; } + @Nullable public GrCondition getCondition() { PsiElement lParenth = getLParenth(); @@ -56,6 +57,7 @@ public class GrWhileStatementImpl extends GroovyPsiElementImpl implements GrWhil return null; } + @Nullable public GrStatement getBody() { GrStatement[] statements = findChildrenByClass(GrStatement.class); @@ -65,23 +67,8 @@ public class GrWhileStatementImpl extends GroovyPsiElementImpl implements GrWhil return null; } - public GrCondition replaceBody(GrCondition newBody) throws IncorrectOperationException { - if (getBody() == null || - newBody == null) { - throw new IncorrectOperationException(); - } - ASTNode oldBodyNode = getBody().getNode(); - if (oldBodyNode.getTreePrev() != null && - GroovyTokenTypes.mNLS.equals(oldBodyNode.getTreePrev().getElementType())) { - ASTNode whiteNode = GroovyPsiElementFactory.getInstance(getProject()).createWhiteSpace().getNode(); - getNode().replaceChild(oldBodyNode.getTreePrev(), whiteNode); - } - getNode().replaceChild(oldBodyNode, newBody.getNode()); - ASTNode newNode = newBody.getNode(); - if (!(newNode.getPsi() instanceof GrCondition)) { - throw new IncorrectOperationException(); - } - return (GrCondition) newNode.getPsi(); + public T replaceBody(T newBody) throws IncorrectOperationException { + return PsiImplUtil.replaceBody(newBody, getBody(), getNode(), getProject()); } public PsiElement getRParenth() { diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/blocks/GrBlockImpl.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/blocks/GrBlockImpl.java index 312a8920e2fc..d0892f4d99bd 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/blocks/GrBlockImpl.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/blocks/GrBlockImpl.java @@ -144,7 +144,7 @@ public abstract class GrBlockImpl extends LazyParseablePsiElement implements GrC return result.toArray(new GrStatement[result.size()]); } - public GrStatement addStatementBefore(@NotNull GrStatement element, GrStatement anchor) throws IncorrectOperationException { + public GrStatement addStatementBefore(@NotNull GrStatement element, @Nullable GrStatement anchor) throws IncorrectOperationException { if (anchor == null && getRBrace() == null) { throw new IncorrectOperationException(); } diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/clauses/GrCaseSectionImpl.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/clauses/GrCaseSectionImpl.java index 7c5e86806d17..f55361a82edd 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/clauses/GrCaseSectionImpl.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/clauses/GrCaseSectionImpl.java @@ -22,6 +22,7 @@ import com.intellij.psi.ResolveState; import com.intellij.psi.scope.PsiScopeProcessor; import com.intellij.util.IncorrectOperationException; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import org.jetbrains.plugins.groovy.lang.lexer.GroovyTokenTypes; import org.jetbrains.plugins.groovy.lang.psi.GroovyElementVisitor; import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement; @@ -73,21 +74,18 @@ public class GrCaseSectionImpl extends GroovyPsiElementImpl implements GrCaseSec return findChildrenByClass(GrStatement.class); } - public GrStatement addStatementBefore(@NotNull GrStatement element, GrStatement anchor) throws IncorrectOperationException { - - if (anchor == null || !this.equals(anchor.getParent())) { - throw new IncorrectOperationException(); - } + public GrStatement addStatementBefore(@NotNull GrStatement element, @Nullable GrStatement anchor) throws IncorrectOperationException { ASTNode elemNode = element.copy().getNode(); assert elemNode != null; - final ASTNode anchorNode = anchor.getNode(); + final ASTNode anchorNode = anchor != null ? anchor.getNode() : null; getNode().addChild(elemNode, anchorNode); if (mayUseNewLinesAsSeparators()) { getNode().addLeaf(GroovyTokenTypes.mNLS, "\n", anchorNode); - } else { + } + else { getNode().addLeaf(GroovyTokenTypes.mSEMI, ";", anchorNode); } - return (GrStatement) elemNode.getPsi(); + return (GrStatement)elemNode.getPsi(); } private boolean mayUseNewLinesAsSeparators() { diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/expressions/GrExpressionImpl.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/expressions/GrExpressionImpl.java index a1651a5369f6..9437fb1ff845 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/expressions/GrExpressionImpl.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/expressions/GrExpressionImpl.java @@ -24,7 +24,7 @@ public abstract class GrExpressionImpl extends GroovyPsiElementImpl implements G @Nullable public PsiType getNominalType() { - return PsiImplUtil.getNominalType(this); + return getType(); } public GrExpression replaceWithExpression(@NotNull GrExpression newExpr, boolean removeUnnecessaryParentheses) { diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/expressions/path/GrCallExpressionImpl.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/expressions/path/GrCallExpressionImpl.java index 8c99506452fa..5d5141539823 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/expressions/path/GrCallExpressionImpl.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/expressions/path/GrCallExpressionImpl.java @@ -34,7 +34,7 @@ public abstract class GrCallExpressionImpl extends GrCallImpl implements GrCallE @Nullable public PsiType getNominalType() { - return PsiImplUtil.getNominalType(this); + return getType(); } public GrExpression replaceWithExpression(@NotNull GrExpression newExpr, boolean removeUnnecessaryParentheses) {