use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression in project intellij-community by JetBrains.
the class ExpandBooleanPredicate method isBooleanAssignment.
public static boolean isBooleanAssignment(GrStatement expression) {
if (!(expression instanceof GrAssignmentExpression)) {
return false;
}
final GrAssignmentExpression assignment = (GrAssignmentExpression) expression;
final GrExpression rhs = assignment.getRValue();
if (rhs == null) {
return false;
}
if (rhs instanceof GrLiteral) {
return false;
}
final PsiType assignmentType = rhs.getType();
if (assignmentType == null) {
return false;
}
return assignmentType.equals(PsiType.BOOLEAN) || assignmentType.equalsToText("java.lang.Boolean");
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression in project intellij-community by JetBrains.
the class GroovyBlock method getChildAttributes.
@Override
@NotNull
public ChildAttributes getChildAttributes(final int newChildIndex) {
ASTNode astNode = getNode();
final PsiElement psiParent = astNode.getPsi();
if (psiParent instanceof GroovyFileBase) {
return new ChildAttributes(Indent.getNoneIndent(), null);
}
if (psiParent instanceof GrSwitchStatement) {
List<Block> subBlocks = getSubBlocks();
if (newChildIndex > 0) {
Block block = subBlocks.get(newChildIndex - 1);
if (block instanceof GroovyBlock) {
PsiElement anchorPsi = ((GroovyBlock) block).getNode().getPsi();
if (anchorPsi instanceof GrCaseSection) {
for (GrStatement statement : ((GrCaseSection) anchorPsi).getStatements()) {
if (statement instanceof GrBreakStatement || statement instanceof GrContinueStatement || statement instanceof GrReturnStatement || statement instanceof GrThrowStatement) {
final Indent indent = GroovyIndentProcessor.getSwitchCaseIndent(myContext.getSettings());
return new ChildAttributes(indent, null);
}
}
int indentSize = myContext.getSettings().getIndentOptions().INDENT_SIZE;
final int spaces = myContext.getSettings().INDENT_CASE_FROM_SWITCH ? 2 * indentSize : indentSize;
return new ChildAttributes(Indent.getSpaceIndent(spaces), null);
}
}
}
}
if (psiParent instanceof GrCaseLabel) {
return new ChildAttributes(GroovyIndentProcessor.getSwitchCaseIndent(getContext().getSettings()), null);
}
if (psiParent instanceof GrCaseSection) {
return getSwitchIndent((GrCaseSection) psiParent, newChildIndex);
}
if (TokenSets.BLOCK_SET.contains(astNode.getElementType()) || GroovyElementTypes.SWITCH_STATEMENT.equals(astNode.getElementType())) {
return new ChildAttributes(Indent.getNormalIndent(), null);
}
if (GroovyElementTypes.CASE_SECTION.equals(astNode.getElementType())) {
return new ChildAttributes(Indent.getNormalIndent(), null);
}
if (psiParent instanceof GrBinaryExpression || psiParent instanceof GrConditionalExpression || psiParent instanceof GrCommandArgumentList || psiParent instanceof GrArgumentList || psiParent instanceof GrParameterList || psiParent instanceof GrListOrMap || psiParent instanceof GrAnnotationArgumentList || psiParent instanceof GrVariable || psiParent instanceof GrAssignmentExpression) {
return new ChildAttributes(Indent.getContinuationWithoutFirstIndent(), null);
}
if (psiParent instanceof GrDocComment || psiParent instanceof GrDocTag) {
return new ChildAttributes(Indent.getSpaceIndent(GroovyIndentProcessor.GDOC_COMMENT_INDENT), null);
}
if (psiParent instanceof GrIfStatement || psiParent instanceof GrLoopStatement) {
return new ChildAttributes(Indent.getNormalIndent(), null);
}
if (psiParent instanceof GrLabeledStatement && newChildIndex == 2) {
final Indent indent = getContext().getGroovySettings().INDENT_LABEL_BLOCKS ? Indent.getLabelIndent() : Indent.getNoneIndent();
return new ChildAttributes(indent, null);
}
return new ChildAttributes(Indent.getNoneIndent(), null);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression in project intellij-community by JetBrains.
the class ReplaceIfWithTernaryIntention method getElementPredicate.
@NotNull
@Override
protected PsiElementPredicate getElementPredicate() {
return new PsiElementPredicate() {
@Override
public boolean satisfiedBy(PsiElement e) {
if (!e.getNode().getElementType().equals(GroovyTokenTypes.kIF))
return false;
if (!(e.getParent() instanceof GrIfStatement))
return false;
final GrIfStatement ifStatement = (GrIfStatement) e.getParent();
final PsiElement thenBranch = skipBlock(ifStatement.getThenBranch());
final PsiElement elseBranch = skipBlock(ifStatement.getElseBranch());
if (thenBranch instanceof GrAssignmentExpression && elseBranch instanceof GrAssignmentExpression && ((GrAssignmentExpression) thenBranch).getRValue() != null && ((GrAssignmentExpression) elseBranch).getRValue() != null) {
final GrExpression lvalue1 = ((GrAssignmentExpression) thenBranch).getLValue();
final GrExpression lvalue2 = ((GrAssignmentExpression) elseBranch).getLValue();
return EquivalenceChecker.expressionsAreEquivalent(lvalue1, lvalue2);
}
if (thenBranch instanceof GrReturnStatement && elseBranch instanceof GrReturnStatement && ((GrReturnStatement) thenBranch).getReturnValue() != null && ((GrReturnStatement) elseBranch).getReturnValue() != null) {
return true;
}
return false;
}
};
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression in project intellij-community by JetBrains.
the class GrVariableJoinLinesHandler method tryJoinStatements.
@Override
public int tryJoinStatements(@NotNull GrStatement first, @NotNull GrStatement second) {
if (first instanceof GrVariableDeclaration && !((GrVariableDeclaration) first).isTuple() && second instanceof GrAssignmentExpression) {
final GrExpression lvalue = ((GrAssignmentExpression) second).getLValue();
final GrExpression rValue = ((GrAssignmentExpression) second).getRValue();
if (lvalue instanceof GrReferenceExpression && rValue != null) {
final PsiElement resolved = ((GrReferenceExpression) lvalue).resolve();
if (ArrayUtil.contains(resolved, ((GrVariableDeclaration) first).getVariables())) {
assert resolved instanceof GrVariable;
if (((GrVariable) resolved).getInitializerGroovy() == null) {
((GrVariable) resolved).setInitializerGroovy(rValue);
second.delete();
GrExpression newInitializer = ((GrVariable) resolved).getInitializerGroovy();
assert newInitializer != null;
return newInitializer.getTextRange().getEndOffset();
}
}
}
}
return CANNOT_JOIN;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression in project intellij-community by JetBrains.
the class RenameAliasImportedMethodProcessor method renameElement.
@Override
public void renameElement(PsiElement psiElement, String newName, UsageInfo[] usages, @Nullable RefactoringElementListener listener) throws IncorrectOperationException {
boolean isGetter = GroovyPropertyUtils.isSimplePropertyGetter((PsiMethod) psiElement);
boolean isSetter = GroovyPropertyUtils.isSimplePropertySetter((PsiMethod) psiElement);
List<UsageInfo> methodAccess = new ArrayList<>(usages.length);
List<UsageInfo> propertyAccess = new ArrayList<>(usages.length);
for (UsageInfo usage : usages) {
final PsiElement element = usage.getElement();
if (element instanceof GrReferenceExpression && ((GrReferenceExpression) element).advancedResolve().isInvokedOnProperty()) {
propertyAccess.add(usage);
} else {
methodAccess.add(usage);
}
}
super.renameElement(psiElement, newName, methodAccess.toArray(new UsageInfo[methodAccess.size()]), listener);
final String propertyName;
if (isGetter) {
propertyName = GroovyPropertyUtils.getPropertyNameByGetterName(newName, true);
} else if (isSetter) {
propertyName = GroovyPropertyUtils.getPropertyNameBySetterName(newName);
} else {
propertyName = null;
}
if (propertyName == null) {
//it means accessor is renamed to not-accessor and we should replace all property-access-refs with method-access-refs
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(psiElement.getProject());
for (UsageInfo info : propertyAccess) {
final PsiElement element = info.getElement();
if (element instanceof GrReferenceExpression) {
final PsiElement qualifier = ((GrReferenceExpression) element).getQualifier();
String qualifierPrefix = qualifier == null ? "" : qualifier.getText() + ".";
if (isGetter) {
final GrExpression call = factory.createExpressionFromText(qualifierPrefix + newName + "()");
((GrReferenceExpression) element).replaceWithExpression(call, true);
} else {
final PsiElement parent = element.getParent();
assert parent instanceof GrAssignmentExpression;
final GrExpression rValue = ((GrAssignmentExpression) parent).getRValue();
final GrExpression call = factory.createExpressionFromText(qualifierPrefix + newName + "(" + (rValue == null ? "" : rValue.getText()) + ")");
((GrAssignmentExpression) parent).replaceWithExpression(call, true);
}
}
}
} else {
for (UsageInfo usage : propertyAccess) {
final PsiReference ref = usage.getReference();
if (ref != null) {
ref.handleElementRename(propertyName);
}
}
}
}
Aggregations