use of org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement in project intellij-community by JetBrains.
the class ConditionalUtils method isReturn.
public static boolean isReturn(GrStatement statement, String value) {
if (statement == null) {
return false;
}
if (!(statement instanceof GrReturnStatement)) {
return false;
}
final GrReturnStatement returnStatement = (GrReturnStatement) statement;
final GrExpression returnValue = returnStatement.getReturnValue();
if (returnValue == null) {
return false;
}
final String returnValueText = returnValue.getText();
return value.equals(returnValueText);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement in project intellij-community by JetBrains.
the class GroovyLanguageInjectionSupport method doInject.
private static boolean doInject(@NotNull String languageId, @NotNull PsiElement psiElement, @NotNull PsiLanguageInjectionHost host) {
final PsiElement target = getTopLevelInjectionTarget(psiElement);
final PsiElement parent = target.getParent();
final Project project = psiElement.getProject();
if (parent instanceof GrReturnStatement) {
final GrControlFlowOwner owner = ControlFlowUtils.findControlFlowOwner(parent);
if (owner instanceof GrOpenBlock && owner.getParent() instanceof GrMethod) {
return JavaLanguageInjectionSupport.doInjectInJavaMethod(project, (PsiMethod) owner.getParent(), -1, host, languageId);
}
} else if (parent instanceof GrMethod) {
return JavaLanguageInjectionSupport.doInjectInJavaMethod(project, (GrMethod) parent, -1, host, languageId);
} else if (parent instanceof GrAnnotationNameValuePair) {
final PsiReference ref = parent.getReference();
if (ref != null) {
final PsiElement resolved = ref.resolve();
if (resolved instanceof PsiMethod) {
return JavaLanguageInjectionSupport.doInjectInJavaMethod(project, (PsiMethod) resolved, -1, host, languageId);
}
}
} else if (parent instanceof GrArgumentList && parent.getParent() instanceof GrMethodCall) {
final PsiMethod method = ((GrMethodCall) parent.getParent()).resolveMethod();
if (method != null) {
final int index = GrInjectionUtil.findParameterIndex(target, ((GrMethodCall) parent.getParent()));
if (index >= 0) {
return JavaLanguageInjectionSupport.doInjectInJavaMethod(project, method, index, host, languageId);
}
}
} else if (parent instanceof GrAssignmentExpression) {
final GrExpression expr = ((GrAssignmentExpression) parent).getLValue();
if (expr instanceof GrReferenceExpression) {
final PsiElement element = ((GrReferenceExpression) expr).resolve();
if (element != null) {
return doInject(languageId, element, host);
}
}
} else {
if (parent instanceof PsiVariable) {
Processor<PsiLanguageInjectionHost> fixer = getAnnotationFixer(project, languageId);
if (JavaLanguageInjectionSupport.doAddLanguageAnnotation(project, (PsiModifierListOwner) parent, host, languageId, fixer))
return true;
} else if (target instanceof PsiVariable && !(target instanceof LightElement)) {
Processor<PsiLanguageInjectionHost> fixer = getAnnotationFixer(project, languageId);
if (JavaLanguageInjectionSupport.doAddLanguageAnnotation(project, (PsiModifierListOwner) target, host, languageId, fixer))
return true;
}
}
return false;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement 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.branch.GrReturnStatement in project intellij-community by JetBrains.
the class ConditionalUtils method isReturn.
public static boolean isReturn(GrStatement statement, @NonNls String value) {
if (statement == null) {
return false;
}
if (!(statement instanceof GrReturnStatement)) {
return false;
}
final GrReturnStatement returnStatement = (GrReturnStatement) statement;
final GrExpression returnValue = returnStatement.getReturnValue();
if (returnValue == null) {
return false;
}
final String returnValueText = returnValue.getText();
return value.equals(returnValueText);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement 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);
}
}
Aggregations