use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement in project intellij-community by JetBrains.
the class SwitchStatementGenerator method generateCaseSection.
private static void generateCaseSection(@NotNull StringBuilder builder, @NotNull ExpressionContext context, @NotNull ExpressionContext innerContext, @NotNull GrCaseSection section) {
for (GrCaseLabel label : section.getCaseLabels()) {
writeLabel(builder, context, label);
}
final GrStatement[] statements = section.getStatements();
CodeBlockGenerator generator = new CodeBlockGenerator(builder, innerContext);
for (GrStatement statement : statements) {
statement.accept(generator);
builder.append("\n");
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement in project intellij-community by JetBrains.
the class ClassItemGeneratorImpl method generateDelegateCall.
private StringBuilder generateDelegateCall(GrReflectedMethod method) {
final GrParameter[] actualParams = method.getParameterList().getParameters();
final GrParameter[] parameters = method.getBaseMethod().getParameters();
Set<String> actual = new HashSet<>(actualParams.length);
for (GrParameter param : actualParams) {
actual.add(param.getName());
}
StringBuilder builder = new StringBuilder();
if (method.isConstructor()) {
builder.append("this");
} else {
if (!PsiType.VOID.equals(context.typeProvider.getReturnType(method))) {
builder.append("return ");
}
builder.append(method.getName());
}
builder.append('(');
for (GrParameter parameter : parameters) {
if (actual.contains(parameter.getName())) {
builder.append(parameter.getName());
} else {
LOG.assertTrue(parameter.isOptional());
final GrExpression initializer = parameter.getInitializerGroovy();
LOG.assertTrue(initializer != null);
builder.append(initializer.getText());
}
builder.append(", ");
}
builder.delete(builder.length() - 2, builder.length());
//builder.removeFromTheEnd(2);
builder.append(')');
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(context.project);
final GrStatement delegateCall;
if (method.isConstructor()) {
delegateCall = factory.createConstructorInvocation(builder.toString(), method);
} else {
delegateCall = factory.createStatementFromText(builder.toString(), method);
}
final StringBuilder result = new StringBuilder();
delegateCall.accept(new CodeBlockGenerator(result, this.context.extend()));
return result;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement in project intellij-community by JetBrains.
the class GroovyExtractChooser method isLastStatementOfMethodOrClosure.
private static boolean isLastStatementOfMethodOrClosure(GrStatement[] statements) {
final GrStatement statement0 = statements[0];
PsiElement returnFrom = PsiTreeUtil.getParentOfType(statement0, GrMethod.class, GrClosableBlock.class, GroovyFile.class);
if (returnFrom instanceof GrMethod) {
returnFrom = ((GrMethod) returnFrom).getBlock();
}
LOG.assertTrue(returnFrom instanceof GrStatementOwner);
final GrStatement[] blockStatements = ((GrStatementOwner) returnFrom).getStatements();
final GrStatement lastFromBlock = ArrayUtil.getLastElement(blockStatements);
final GrStatement lastStatement = ArrayUtil.getLastElement(statements);
return statement0.getManager().areElementsEquivalent(lastFromBlock, lastStatement);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement in project intellij-community by JetBrains.
the class ExtractClosureFromClosureProcessor method preprocessUsages.
@Override
protected boolean preprocessUsages(@NotNull Ref<UsageInfo[]> refUsages) {
UsageInfo[] usagesIn = refUsages.get();
MultiMap<PsiElement, String> conflicts = new MultiMap<>();
if (!myHelper.generateDelegate()) {
for (GrStatement statement : myHelper.getStatements()) {
GroovyIntroduceParameterUtil.detectAccessibilityConflicts(statement, usagesIn, conflicts, myHelper.replaceFieldsWithGetters() != IntroduceParameterRefactoring.REPLACE_FIELDS_WITH_GETTERS_NONE, myProject);
}
}
return showConflicts(conflicts, usagesIn);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement in project intellij-community by JetBrains.
the class ExtractClosureFromMethodProcessor method performRefactoring.
@Override
protected void performRefactoring(@NotNull UsageInfo[] usages) {
final IntroduceParameterData data = new IntroduceParameterDataAdapter();
IntroduceParameterUtil.processUsages(usages, data);
final PsiMethod toSearchFor = (PsiMethod) myHelper.getToSearchFor();
final boolean methodsToProcessAreDifferent = myMethod != toSearchFor;
if (myHelper.generateDelegate()) {
GroovyIntroduceParameterUtil.generateDelegate(myMethod, data.getParameterInitializer(), myProject);
if (methodsToProcessAreDifferent) {
final GrMethod method = GroovyIntroduceParameterUtil.generateDelegate(toSearchFor, data.getParameterInitializer(), myProject);
final PsiClass containingClass = method.getContainingClass();
if (containingClass != null && containingClass.isInterface()) {
final GrOpenBlock block = method.getBlock();
if (block != null) {
block.delete();
}
}
}
}
// Changing signature of initial method
// (signature of myMethodToReplaceIn will be either changed now or have already been changed)
final FieldConflictsResolver fieldConflictsResolver = new FieldConflictsResolver(myHelper.getName(), myMethod.getBlock());
IntroduceParameterUtil.changeMethodSignatureAndResolveFieldConflicts(new UsageInfo(myMethod), usages, data);
if (methodsToProcessAreDifferent) {
IntroduceParameterUtil.changeMethodSignatureAndResolveFieldConflicts(new UsageInfo(toSearchFor), usages, data);
}
// Replacing expression occurrences
for (UsageInfo usage : usages) {
if (usage instanceof ChangedMethodCallInfo) {
PsiElement element = usage.getElement();
GroovyIntroduceParameterUtil.processChangedMethodCall(element, myHelper, myProject);
}
}
final GrStatement newStatement = ExtractUtil.replaceStatement(myDeclarationOwner, myHelper);
final Editor editor = PsiUtilBase.findEditor(newStatement);
if (editor != null) {
PsiDocumentManager.getInstance(myProject).commitDocument(editor.getDocument());
editor.getSelectionModel().removeSelection();
editor.getCaretModel().moveToOffset(newStatement.getTextRange().getEndOffset());
}
fieldConflictsResolver.fix();
}
Aggregations