use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression in project intellij-community by JetBrains.
the class GrConcatenationInjector method findLanguageParams.
private static BaseInjection findLanguageParams(PsiElement place) {
PsiElement parent = place.getParent();
if (parent instanceof GrAssignmentExpression && ((GrAssignmentExpression) parent).getRValue() == place) {
final GrExpression lvalue = ((GrAssignmentExpression) parent).getLValue();
if (lvalue instanceof GrReferenceExpression) {
final PsiElement resolved = ((GrReferenceExpression) lvalue).resolve();
if (resolved instanceof PsiModifierListOwner) {
return getLanguageParams((PsiModifierListOwner) resolved);
}
}
} else if (parent instanceof GrVariable) {
return getLanguageParams((PsiModifierListOwner) parent);
} else if (parent instanceof GrArgumentList) {
final PsiElement pparent = parent.getParent();
if (pparent instanceof GrCall) {
final GrCall call = (GrCall) pparent;
final GroovyResolveResult result = call.advancedResolve();
if (result.getElement() != null) {
final Map<GrExpression, Pair<PsiParameter, PsiType>> map = GrClosureSignatureUtil.mapArgumentsToParameters(result, place, false, false, call.getNamedArguments(), call.getExpressionArguments(), call.getClosureArguments());
if (map != null) {
final Pair<PsiParameter, PsiType> pair = map.get(place);
return getLanguageParams(pair.first);
}
}
}
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression in project intellij-community by JetBrains.
the class GrCastFix method findExpressionToCast.
private static GrExpression findExpressionToCast(ProblemDescriptor descriptor) {
final PsiElement element = descriptor.getPsiElement();
final PsiElement parent = element.getParent();
if (parent instanceof GrVariable) {
return ((GrVariable) parent).getInitializerGroovy();
} else if (parent instanceof GrAssignmentExpression) {
return ((GrAssignmentExpression) parent).getRValue();
} else if (parent instanceof GrThrowStatement) {
return ((GrThrowStatement) parent).getException();
} else if (parent instanceof GrReturnStatement) {
return ((GrReturnStatement) parent).getReturnValue();
} else if (element instanceof GrExpression) {
return (GrExpression) element;
}
PsiFile file = element.getContainingFile();
VirtualFile virtualFile = file.getVirtualFile();
String url = virtualFile == null ? "" : virtualFile.getPresentableUrl();
LOG.error("can't find expression to cast at position " + element.getTextRange(), new Attachment(url, file.getText()));
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression in project intellij-community by JetBrains.
the class ExpandBooleanIntention method processIntention.
@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final GrStatement containingStatement = (GrStatement) element;
if (ExpandBooleanPredicate.isBooleanAssignment(containingStatement)) {
final GrAssignmentExpression assignmentExpression = (GrAssignmentExpression) containingStatement;
final GrExpression rhs = assignmentExpression.getRValue();
assert rhs != null;
final String rhsText = rhs.getText();
final GrExpression lhs = assignmentExpression.getLValue();
final String lhsText = lhs.getText();
@NonNls final String statement = "if(" + rhsText + "){\n" + lhsText + " = true\n}else{\n" + lhsText + " = false\n}";
PsiImplUtil.replaceStatement(statement, containingStatement);
} else if (ExpandBooleanPredicate.isBooleanReturn(containingStatement)) {
final GrReturnStatement returnStatement = (GrReturnStatement) containingStatement;
final GrExpression returnValue = returnStatement.getReturnValue();
final String valueText = returnValue.getText();
@NonNls final String statement = "if(" + valueText + "){\nreturn true\n}else{\nreturn false\n}";
PsiImplUtil.replaceStatement(statement, containingStatement);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression in project intellij-community by JetBrains.
the class ReplaceIfWithTernaryIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
final GrIfStatement ifStatement = (GrIfStatement) element.getParent();
final PsiElement thenBranch = skipBlock(ifStatement.getThenBranch());
final PsiElement elseBranch = skipBlock(ifStatement.getElseBranch());
if (thenBranch instanceof GrAssignmentExpression && elseBranch instanceof GrAssignmentExpression) {
final GrAssignmentExpression assignment = (GrAssignmentExpression) GroovyPsiElementFactory.getInstance(project).createStatementFromText("a = b ? c : d");
assignment.getLValue().replaceWithExpression(((GrAssignmentExpression) thenBranch).getLValue(), true);
final GrConditionalExpression conditional = (GrConditionalExpression) assignment.getRValue();
replaceConditional(conditional, ifStatement.getCondition(), ((GrAssignmentExpression) thenBranch).getRValue(), ((GrAssignmentExpression) elseBranch).getRValue());
ifStatement.replaceWithStatement(assignment);
}
if (thenBranch instanceof GrReturnStatement && elseBranch instanceof GrReturnStatement) {
final GrReturnStatement returnSt = (GrReturnStatement) GroovyPsiElementFactory.getInstance(project).createStatementFromText("return a ? b : c");
final GrConditionalExpression conditional = (GrConditionalExpression) returnSt.getReturnValue();
replaceConditional(conditional, ifStatement.getCondition(), ((GrReturnStatement) thenBranch).getReturnValue(), ((GrReturnStatement) elseBranch).getReturnValue());
ifStatement.replaceWithStatement(returnSt);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrAssignmentExpression in project intellij-community by JetBrains.
the class GroovyShellLanguageConsoleView method processCode.
protected void processCode() {
GroovyShellCodeFragment groovyFile = getGroovyFile();
for (GrTopStatement statement : groovyFile.getTopStatements()) {
if (statement instanceof GrImportStatement) {
groovyFile.addImportsFromString(importToString((GrImportStatement) statement));
} else if (statement instanceof GrMethod) {
groovyFile.addVariable(((GrMethod) statement).getName(), generateClosure((GrMethod) statement));
} else if (statement instanceof GrAssignmentExpression) {
GrAssignmentExpression assignment = (GrAssignmentExpression) statement;
GrExpression left = assignment.getLValue();
if (left instanceof GrReferenceExpression && !((GrReferenceExpression) left).isQualified()) {
groovyFile.addVariable(((GrReferenceExpression) left).getReferenceName(), assignment.getRValue());
}
} else if (statement instanceof GrTypeDefinition) {
groovyFile.addTypeDefinition(prepareTypeDefinition((GrTypeDefinition) statement));
}
}
PsiType scriptType = groovyFile.getInferredScriptReturnType();
if (scriptType != null) {
groovyFile.addVariable("_", scriptType);
}
}
Aggregations