use of org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner in project intellij-community by JetBrains.
the class GrIntroduceVariableHandler method addVariable.
private static GrVariable addVariable(@NotNull GrIntroduceContext context, @NotNull GroovyIntroduceVariableSettings settings) {
GrStatement anchor = findAnchor(context, settings.replaceAllOccurrences());
PsiElement parent = anchor.getParent();
assert parent instanceof GrStatementOwner;
GrVariableDeclaration generated = generateDeclaration(context, settings);
GrStatement declaration = ((GrStatementOwner) parent).addStatementBefore(generated, anchor);
declaration = (GrStatement) JavaCodeStyleManager.getInstance(context.getProject()).shortenClassReferences(declaration);
return ((GrVariableDeclaration) declaration).getVariables()[0];
}
use of org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner in project intellij-community by JetBrains.
the class GrIntroduceClosureParameterProcessor method insertDeclaration.
private GrVariableDeclaration insertDeclaration(GrVariable original, GrVariableDeclaration declaration) {
if (original instanceof GrField) {
final PsiClass containingClass = ((GrField) original).getContainingClass();
LOG.assertTrue(containingClass != null);
return (GrVariableDeclaration) containingClass.addBefore(declaration, original.getParent());
}
final GrStatementOwner block;
if (original instanceof PsiParameter) {
final PsiElement container = original.getParent().getParent();
if (container instanceof GrMethod) {
block = ((GrMethod) container).getBlock();
} else if (container instanceof GrClosableBlock) {
block = (GrCodeBlock) container;
} else if (container instanceof GrForStatement) {
final GrStatement body = ((GrForStatement) container).getBody();
if (body instanceof GrBlockStatement) {
block = ((GrBlockStatement) body).getBlock();
} else {
GrBlockStatement blockStatement = myFactory.createBlockStatement();
LOG.assertTrue(blockStatement != null);
if (body != null) {
blockStatement.getBlock().addStatementBefore((GrStatement) body.copy(), null);
blockStatement = (GrBlockStatement) body.replace(blockStatement);
} else {
blockStatement = (GrBlockStatement) container.add(blockStatement);
}
block = blockStatement.getBlock();
}
} else {
throw new IncorrectOperationException();
}
LOG.assertTrue(block != null);
return (GrVariableDeclaration) block.addStatementBefore(declaration, null);
}
PsiElement parent = original.getParent();
LOG.assertTrue(parent instanceof GrVariableDeclaration);
final PsiElement pparent = parent.getParent();
if (pparent instanceof GrIfStatement) {
if (((GrIfStatement) pparent).getThenBranch() == parent) {
block = ((GrIfStatement) pparent).replaceThenBranch(myFactory.createBlockStatement()).getBlock();
} else {
block = ((GrIfStatement) pparent).replaceElseBranch(myFactory.createBlockStatement()).getBlock();
}
parent = block.addStatementBefore(((GrVariableDeclaration) parent), null);
} else if (pparent instanceof GrLoopStatement) {
block = ((GrLoopStatement) pparent).replaceBody(myFactory.createBlockStatement()).getBlock();
parent = block.addStatementBefore(((GrVariableDeclaration) parent), null);
} else {
LOG.assertTrue(pparent instanceof GrStatementOwner);
block = (GrStatementOwner) pparent;
}
return (GrVariableDeclaration) block.addStatementBefore(declaration, (GrStatement) parent);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner in project intellij-community by JetBrains.
the class GrIntroduceLocalVariableProcessor method insertVariableDefinition.
@NotNull
private GrVariable insertVariableDefinition(@NotNull GrVariableDeclaration declaration, @NotNull GrStatement anchor, @Nullable PsiElement expression) throws IncorrectOperationException {
GrLabeledStatement labeledStatement = expression != null && expression.getParent() instanceof GrLabeledStatement ? (GrLabeledStatement) expression.getParent() : null;
boolean expressionMustBeDeleted = expression != null && PsiUtil.isExpressionStatement(expression) && !isSingleGStringInjectionExpr(expression);
boolean anchorEqualsExpression = anchor == expression || labeledStatement == anchor;
String usedLabel = labeledStatement != null ? labeledStatement.getName() : null;
if (expressionMustBeDeleted && !anchorEqualsExpression) {
expression.delete();
}
boolean isInsideControlStatement = isControlStatementBranch(anchor);
if (isInsideControlStatement) {
anchor = insertBraces(anchor);
}
LOG.assertTrue(myOccurrences.length > 0);
GrStatementOwner block = (GrStatementOwner) anchor.getParent();
if (usedLabel != null && expressionMustBeDeleted && anchorEqualsExpression) {
GrLabeledStatement definitionWithLabel = (GrLabeledStatement) GroovyPsiElementFactory.getInstance(anchor.getProject()).createStatementFromText(usedLabel + ": foo()");
GrLabeledStatement inserted = insertStatement(definitionWithLabel, anchor, block, true);
declaration = inserted.getStatement().replaceWithStatement(declaration);
} else {
declaration = insertStatement(declaration, anchor, block, expressionMustBeDeleted && anchorEqualsExpression);
}
final GrVariable variable = declaration.getVariables()[0];
JavaCodeStyleManager.getInstance(declaration.getProject()).shortenClassReferences(declaration);
PsiElement markerPlace = expressionMustBeDeleted ? variable : isInsideControlStatement ? declaration.getParent() : expression;
refreshPositionMarker(markerPlace);
return variable;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner in project intellij-community by JetBrains.
the class ExtractClosureFromClosureProcessor method performRefactoring.
@Override
protected void performRefactoring(@NotNull UsageInfo[] usages) {
GrIntroduceClosureParameterProcessor.processExternalUsages(usages, myHelper, generateClosure(myHelper));
GrIntroduceClosureParameterProcessor.processClosure(usages, myHelper);
GrStatementOwner declarationOwner = GroovyRefactoringUtil.getDeclarationOwner(myHelper.getStatements()[0]);
ExtractUtil.replaceStatement(declarationOwner, myHelper);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.util.GrStatementOwner in project intellij-community by JetBrains.
the class GrIntroduceFieldProcessor method initializeInMethod.
void initializeInMethod(@NotNull GrVariable field, @NotNull List<PsiElement> replaced) {
final PsiElement _scope = myContext.getScope();
final PsiElement scope = _scope instanceof GroovyScriptClass ? ((GroovyScriptClass) _scope).getContainingFile() : _scope;
final PsiElement place = replaced.get(0);
final GrMember member = GrIntroduceFieldHandler.getContainer(place, scope);
GrStatementOwner container = member instanceof GrMethod ? ((GrMethod) member).getBlock() : member instanceof GrClassInitializer ? ((GrClassInitializer) member).getBlock() : place.getContainingFile() instanceof GroovyFile ? ((GroovyFile) place.getContainingFile()) : null;
assert container != null;
final PsiElement anchor;
if (mySettings.removeLocalVar()) {
GrVariable variable = myLocalVariable;
anchor = PsiTreeUtil.getParentOfType(variable, GrStatement.class);
} else {
anchor = GrIntroduceHandlerBase.findAnchor(replaced.toArray(new PsiElement[replaced.size()]), container);
GrIntroduceHandlerBase.assertStatement(anchor, myContext.getScope());
}
initializeInMethodInner(field, container, (GrStatement) anchor, replaced.get(0));
}
Aggregations