use of org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement in project intellij-community by JetBrains.
the class ExpressionGenerator method isImplicitlyCastedToArray.
private static boolean isImplicitlyCastedToArray(GrListOrMap list) {
PsiElement parent = list.getParent();
GrControlFlowOwner owner = ControlFlowUtils.findControlFlowOwner(list);
if (!(owner instanceof GrOpenBlock && owner.getParent() instanceof GrMethod))
return false;
if (!(parent instanceof GrReturnStatement || ControlFlowUtils.isReturnValue(list, owner)))
return false;
PsiType type = ((GrMethod) owner.getParent()).getReturnType();
return type instanceof PsiArrayType;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement in project intellij-community by JetBrains.
the class GroovyRefactoringUtil method addBlockIntoParent.
/**
* adds block statement in parent of statement if needed. For Example:
* while (true) a=foo()
* will be replaced with
* while(true) {a=foo()}
* @param statement
* @return corresponding statement inside block if it has been created or statement itself.
* @throws com.intellij.util.IncorrectOperationException
*/
@NotNull
public static <Type extends PsiElement> Type addBlockIntoParent(@NotNull Type statement) throws IncorrectOperationException {
PsiElement parent = statement.getParent();
PsiElement child = statement;
while (!(parent instanceof GrLoopStatement) && !(parent instanceof GrIfStatement) && !(parent instanceof GrVariableDeclarationOwner) && parent != null) {
parent = parent.getParent();
child = child.getParent();
}
if (parent instanceof GrWhileStatement && child == ((GrWhileStatement) parent).getCondition() || parent instanceof GrIfStatement && child == ((GrIfStatement) parent).getCondition()) {
parent = parent.getParent();
}
assert parent != null;
if (parent instanceof GrVariableDeclarationOwner) {
return statement;
}
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(statement.getProject());
PsiElement tempStmt = statement;
while (parent != tempStmt.getParent()) {
tempStmt = tempStmt.getParent();
}
GrStatement toAdd = (GrStatement) tempStmt.copy();
GrBlockStatement blockStatement = factory.createBlockStatement();
if (parent instanceof GrLoopStatement) {
((GrLoopStatement) parent).replaceBody(blockStatement);
} else {
GrIfStatement ifStatement = (GrIfStatement) parent;
if (tempStmt == ifStatement.getThenBranch()) {
ifStatement.replaceThenBranch(blockStatement);
} else if (tempStmt == ifStatement.getElseBranch()) {
ifStatement.replaceElseBranch(blockStatement);
}
}
GrStatement result = blockStatement.getBlock().addStatementBefore(toAdd, null);
if (result instanceof GrReturnStatement) {
//noinspection ConstantConditions,unchecked
statement = (Type) ((GrReturnStatement) result).getReturnValue();
} else {
//noinspection unchecked
statement = (Type) result;
}
return statement;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement in project intellij-community by JetBrains.
the class RemoveUnnecessaryReturnIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
if (element instanceof GrReturnStatement && ((GrReturnStatement) element).getReturnValue() != null) {
GrExpression value = ((GrReturnStatement) element).getReturnValue();
((GrReturnStatement) element).replaceWithStatement(value);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.branch.GrReturnStatement 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.branch.GrReturnStatement in project intellij-community by JetBrains.
the class AddReturnTypeFix method findMethod.
@Nullable
private static GrMethod findMethod(PsiFile file, final int offset) {
final PsiElement at = file.findElementAt(offset);
if (at == null)
return null;
if (at.getParent() instanceof GrReturnStatement) {
final GrReturnStatement returnStatement = ((GrReturnStatement) at.getParent());
final PsiElement word = returnStatement.getReturnWord();
if (!word.getTextRange().contains(offset))
return null;
final GroovyPsiElement returnOwner = PsiTreeUtil.getParentOfType(returnStatement, GrClosableBlock.class, GrMethod.class);
if (returnOwner instanceof GrMethod) {
final GrTypeElement returnTypeElement = ((GrMethod) returnOwner).getReturnTypeElementGroovy();
if (returnTypeElement == null) {
return (GrMethod) returnOwner;
}
}
return null;
}
final GrMethod method = PsiTreeUtil.getParentOfType(at, GrMethod.class, false, GrTypeDefinition.class, GrClosableBlock.class);
if (method != null && GrHighlightUtil.getMethodHeaderTextRange(method).contains(offset)) {
if (method.getReturnTypeElementGroovy() == null) {
return method;
}
}
return null;
}
Aggregations