use of com.intellij.refactoring.changeSignature.ParameterInfoImpl in project intellij-community by JetBrains.
the class ChangeMethodSignatureFromUsageFix method findNewParamsPlace.
protected boolean findNewParamsPlace(PsiExpression[] expressions, PsiMethod targetMethod, PsiSubstitutor substitutor, StringBuilder buf, HashSet<ParameterInfoImpl> newParams, PsiParameter[] parameters, List<ParameterInfoImpl> result) {
// find which parameters to introduce and where
Set<String> existingNames = new HashSet<>();
for (PsiParameter parameter : parameters) {
existingNames.add(parameter.getName());
}
int ei = 0;
int pi = 0;
PsiParameter varargParam = targetMethod.isVarArgs() ? parameters[parameters.length - 1] : null;
while (ei < expressions.length || pi < parameters.length) {
if (buf.length() > 0)
buf.append(", ");
PsiExpression expression = ei < expressions.length ? expressions[ei] : null;
PsiParameter parameter = pi < parameters.length ? parameters[pi] : null;
PsiType paramType = parameter == null ? null : substitutor.substitute(parameter.getType());
boolean parameterAssignable = paramType != null && (expression == null || TypeConversionUtil.areTypesAssignmentCompatible(paramType, expression));
if (parameterAssignable) {
final PsiType type = parameter.getType();
result.add(new ParameterInfoImpl(pi, parameter.getName(), type));
buf.append(escapePresentableType(type));
pi++;
ei++;
} else if (isArgumentInVarargPosition(expressions, ei, varargParam, substitutor)) {
if (pi == parameters.length - 1) {
assert varargParam != null;
final PsiType type = varargParam.getType();
result.add(new ParameterInfoImpl(pi, varargParam.getName(), type));
buf.append(escapePresentableType(type));
}
pi++;
ei++;
} else if (expression != null) {
if (varargParam != null && pi >= parameters.length)
return false;
if (PsiPolyExpressionUtil.isPolyExpression(expression))
return false;
PsiType exprType = RefactoringUtil.getTypeByExpression(expression);
if (exprType == null)
return false;
if (exprType instanceof PsiDisjunctionType) {
exprType = ((PsiDisjunctionType) exprType).getLeastUpperBound();
}
JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(expression.getProject());
String name = suggestUniqueParameterName(codeStyleManager, expression, exprType, existingNames);
final ParameterInfoImpl newParameterInfo = new ParameterInfoImpl(-1, name, exprType, expression.getText().replace('\n', ' '));
result.add(newParameterInfo);
newParams.add(newParameterInfo);
buf.append("<b>").append(escapePresentableType(exprType)).append("</b>");
ei++;
}
}
if (result.size() != expressions.length && varargParam == null)
return false;
return true;
}
use of com.intellij.refactoring.changeSignature.ParameterInfoImpl in project intellij-community by JetBrains.
the class ChangeMethodSignatureFromUsageFix method formatTypesList.
@Nullable
private static String formatTypesList(ParameterInfoImpl[] infos, PsiElement context) {
if (infos == null)
return null;
StringBuilder result = new StringBuilder();
try {
for (ParameterInfoImpl info : infos) {
PsiType type = info.createType(context);
if (type == null)
return null;
if (result.length() != 0)
result.append(", ");
result.append(type.getPresentableText());
}
return result.toString();
} catch (IncorrectOperationException e) {
return null;
}
}
use of com.intellij.refactoring.changeSignature.ParameterInfoImpl in project intellij-community by JetBrains.
the class ChangeMethodSignatureFromUsageFix method getShortText.
@Nullable
private String getShortText() {
final StringBuilder buf = new StringBuilder();
final HashSet<ParameterInfoImpl> newParams = new HashSet<>();
final HashSet<ParameterInfoImpl> removedParams = new HashSet<>();
final HashSet<ParameterInfoImpl> changedParams = new HashSet<>();
getNewParametersInfo(myExpressions, myTargetMethod, mySubstitutor, buf, newParams, removedParams, changedParams);
final String targetMethodName = myTargetMethod.getName();
if (myTargetMethod.getContainingClass().findMethodsByName(targetMethodName, true).length == 1) {
if (newParams.size() == 1) {
final ParameterInfoImpl p = newParams.iterator().next();
return QuickFixBundle.message("add.parameter.from.usage.text", p.getTypeText(), (ArrayUtil.find(myNewParametersInfo, p) + 1), targetMethodName);
}
if (removedParams.size() == 1) {
final ParameterInfoImpl p = removedParams.iterator().next();
return QuickFixBundle.message("remove.parameter.from.usage.text", (p.getOldIndex() + 1), targetMethodName);
}
if (changedParams.size() == 1) {
final ParameterInfoImpl p = changedParams.iterator().next();
return QuickFixBundle.message("change.parameter.from.usage.text", (p.getOldIndex() + 1), targetMethodName, myTargetMethod.getParameterList().getParameters()[p.getOldIndex()].getType().getPresentableText(), p.getTypeText());
}
}
return "<html> Change signature of " + targetMethodName + "(" + buf.toString() + ")</html>";
}
use of com.intellij.refactoring.changeSignature.ParameterInfoImpl in project intellij-community by JetBrains.
the class MethodParameterFix method getNewParametersInfo.
@NotNull
private ParameterInfoImpl[] getNewParametersInfo(PsiMethod method) throws IncorrectOperationException {
List<ParameterInfoImpl> result = new ArrayList<>();
PsiParameter[] parameters = method.getParameterList().getParameters();
PsiElementFactory factory = JavaPsiFacade.getInstance(method.getProject()).getElementFactory();
JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(method.getProject());
SuggestedNameInfo nameInfo = codeStyleManager.suggestVariableName(VariableKind.PARAMETER, null, null, myParameterType);
PsiParameter newParameter = factory.createParameter(nameInfo.names[0], myParameterType);
if (method.getContainingClass().isInterface()) {
PsiUtil.setModifierProperty(newParameter, PsiModifier.FINAL, false);
}
for (int i = 0; i < parameters.length; i++) {
PsiParameter parameter = parameters[i];
if (i == myIndex) {
newParameter.setName(parameter.getName());
parameter = newParameter;
}
result.add(new ParameterInfoImpl(i, parameter.getName(), parameter.getType()));
}
if (parameters.length == myIndex) {
result.add(new ParameterInfoImpl(-1, newParameter.getName(), newParameter.getType()));
}
return result.toArray(new ParameterInfoImpl[result.size()]);
}
use of com.intellij.refactoring.changeSignature.ParameterInfoImpl in project intellij-community by JetBrains.
the class ChangeMethodSignatureFromUsageReverseOrderFix method isAvailable.
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (myTargetMethod.isValid() && myExpressions.length > myTargetMethod.getParameterList().getParametersCount()) {
if (super.isAvailable(project, editor, file)) {
final ArrayList<ParameterInfoImpl> result = new ArrayList<>();
if (super.findNewParamsPlace(myExpressions, myTargetMethod, mySubstitutor, new StringBuilder(), new HashSet<>(), myTargetMethod.getParameterList().getParameters(), result)) {
if (myNewParametersInfo.length != result.size())
return true;
for (int i = 0, size = result.size(); i < size; i++) {
ParameterInfoImpl info = result.get(i);
info.setName(myNewParametersInfo[i].getName());
if (!myNewParametersInfo[i].equals(info))
return true;
}
}
}
}
return false;
}
Aggregations