some api improvements

This commit is contained in:
Maxim Medvedev
2011-06-28 10:28:15 +04:00
parent 223403303b
commit 81a54d9c6e
15 changed files with 75 additions and 118 deletions
@@ -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 extends GrStatement> T replaceThenBranch(T newBranch) throws IncorrectOperationException;
GrStatement replaceElseBranch(GrStatement newBranch) throws IncorrectOperationException;
<T extends GrStatement> T replaceElseBranch(T newBranch) throws IncorrectOperationException;
@Nullable
PsiElement getElseKeyword();
@@ -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 extends GrCondition> T replaceBody (T statement) throws IncorrectOperationException;
GrStatement getBody();
}
@@ -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();
}
@@ -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();
}
@@ -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();
}
@@ -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;
@@ -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;
@@ -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<PsiCodeBlock>(newBody));
return newBody;
}
public static <T extends GrCondition> 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;
}
}
@@ -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 extends GrCondition> T replaceBody(T newBody) throws IncorrectOperationException {
return PsiImplUtil.replaceBody(newBody, getBody(), getNode(), getProject());
}
public PsiElement getRParenth() {
@@ -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 extends GrStatement> 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 extends GrStatement> T replaceElseBranch(T newBranch) throws IncorrectOperationException {
return PsiImplUtil.replaceBody(newBranch, getElseBranch(), getNode(), getProject());
}
public PsiElement getElseKeyword() {
@@ -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 extends GrCondition> T replaceBody(T newBody) throws IncorrectOperationException {
return PsiImplUtil.replaceBody(newBody, getBody(), getNode(), getProject());
}
public PsiElement getRParenth() {
@@ -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();
}
@@ -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() {
@@ -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) {
@@ -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) {