use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.
the class GrIntroduceClosureParameterProcessor method generateDelegateClosure.
private GrClosableBlock generateDelegateClosure(GrClosableBlock originalClosure, GrVariable anchor, String newName) {
GrClosableBlock result;
if (originalClosure.hasParametersSection()) {
result = myFactory.createClosureFromText("{->}", anchor);
final GrParameterList parameterList = (GrParameterList) originalClosure.getParameterList().copy();
result.getParameterList().replace(parameterList);
} else {
result = myFactory.createClosureFromText("{}", anchor);
}
StringBuilder call = new StringBuilder();
call.append(newName).append('(');
final GrParameter[] parameters = result.getParameters();
for (int i = 0; i < parameters.length; i++) {
if (!mySettings.parametersToRemove().contains(i)) {
call.append(parameters[i].getName()).append(", ");
}
}
call.append(myParameterInitializer.getText());
call.append(")");
final GrStatement statement = myFactory.createStatementFromText(call.toString());
result.addStatementBefore(statement, null);
return result;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.
the class GrVariableInplaceRenamer method renameSynthetic.
@Override
protected void renameSynthetic(String newName) {
PsiNamedElement elementToRename = getVariable();
if (elementToRename instanceof ClosureSyntheticParameter && !"it".equals(newName)) {
final GrClosableBlock closure = ((ClosureSyntheticParameter) elementToRename).getClosure();
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(myProject);
final PsiType type = ((ClosureSyntheticParameter) elementToRename).getTypeGroovy();
final GrParameter newParam = factory.createParameter(newName, TypesUtil.unboxPrimitiveTypeWrapper(type));
final GrParameter added = closure.addParameter(newParam);
JavaCodeStyleManager.getInstance(added.getProject()).shortenClassReferences(added);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.
the class GroovyIntroduceParameterUtil method findParametersToRemove.
public static TObjectIntHashMap<GrParameter> findParametersToRemove(IntroduceParameterInfo helper) {
final TObjectIntHashMap<GrParameter> result = new TObjectIntHashMap<>();
final TextRange range = ExtractUtil.getRangeOfRefactoring(helper);
GrParameter[] parameters = helper.getToReplaceIn().getParameters();
for (int i = 0; i < parameters.length; i++) {
GrParameter parameter = parameters[i];
if (shouldRemove(parameter, range.getStartOffset(), range.getEndOffset())) {
result.put(parameter, i);
}
}
return result;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.
the class GroovyIntroduceParameterUtil method generateDelegate.
public static GrMethod generateDelegate(PsiMethod prototype, IntroduceParameterData.ExpressionWrapper initializer, Project project) {
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
GrMethod result;
if (prototype instanceof GrMethod) {
result = (GrMethod) prototype.copy();
} else {
StringBuilder builder = new StringBuilder();
builder.append(prototype.getModifierList().getText()).append(' ');
if (prototype.getReturnTypeElement() != null) {
builder.append(prototype.getReturnTypeElement().getText());
}
builder.append(' ').append(prototype.getName());
builder.append(prototype.getParameterList().getText());
builder.append("{}");
result = factory.createMethodFromText(builder.toString());
}
StringBuilder call = new StringBuilder();
call.append("def foo(){\n");
final GrParameter[] parameters = result.getParameters();
call.append(prototype.getName());
if (initializer.getExpression() instanceof GrClosableBlock) {
if (parameters.length > 0) {
call.append('(');
for (GrParameter parameter : parameters) {
call.append(parameter.getName()).append(", ");
}
call.replace(call.length() - 2, call.length(), ")");
}
call.append(initializer.getText());
} else {
call.append('(');
for (GrParameter parameter : parameters) {
call.append(parameter.getName()).append(", ");
}
call.append(initializer.getText());
call.append(")");
}
call.append("\n}");
final GrOpenBlock block = factory.createMethodFromText(call.toString()).getBlock();
result.getBlock().replace(block);
final PsiElement parent = prototype.getParent();
final GrMethod method = (GrMethod) parent.addBefore(result, prototype);
JavaCodeStyleManager.getInstance(method.getProject()).shortenClassReferences(method);
return method;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter in project intellij-community by JetBrains.
the class GroovyIntroduceParameterMethodUsagesProcessor method addParameter.
@NotNull
public static GrParameter addParameter(@NotNull GrParametersOwner parametersOwner, @Nullable MethodJavaDocHelper javaDocHelper, @NotNull PsiType forcedType, @NotNull String parameterName, boolean isFinal, @NotNull Project project) {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
final String typeText = forcedType.equalsToText(CommonClassNames.JAVA_LANG_OBJECT) || forcedType == PsiType.NULL || PsiType.VOID.equals(forcedType) ? null : forcedType.getCanonicalText();
GrParameter parameter = factory.createParameter(parameterName, typeText, parametersOwner);
parameter.getModifierList().setModifierProperty(PsiModifier.FINAL, isFinal);
final PsiParameter anchorParameter = getAnchorParameter(parametersOwner);
final GrParameterList parameterList = parametersOwner.getParameterList();
parameter = (GrParameter) parameterList.addAfter(parameter, anchorParameter);
JavaCodeStyleManager.getInstance(project).shortenClassReferences(parameter);
if (javaDocHelper != null) {
final PsiDocTag tagForAnchorParameter = javaDocHelper.getTagForParameter(anchorParameter);
javaDocHelper.addParameterAfter(parameterName, tagForAnchorParameter);
}
return parameter;
}
Aggregations