use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap in project intellij-community by JetBrains.
the class ArgumentListGenerator method generateMultiArg.
private boolean generateMultiArg(GrClosureSignatureUtil.ArgInfo<PsiElement> arg, GrClosureParameter param, PsiSubstitutor substitutor, Project project, GroovyPsiElement context) {
final PsiType type = param.getType();
if (type instanceof PsiEllipsisType) {
for (PsiElement element : arg.args) {
LOG.assertTrue(element instanceof GrExpression);
((GrExpression) element).accept(myExpressionGenerator);
myBuilder.append(", ");
}
if (!arg.args.isEmpty()) {
myBuilder.delete(myBuilder.length() - 2, myBuilder.length());
return true;
} else {
return false;
}
} else if (type instanceof PsiArrayType) {
myBuilder.append("new ");
if (arg.args.isEmpty()) {
TypeWriter.writeType(myBuilder, ((PsiArrayType) type).getComponentType(), context);
myBuilder.append("[0]");
} else {
TypeWriter.writeTypeForNew(myBuilder, type, context);
myBuilder.append("{");
for (PsiElement element : arg.args) {
LOG.assertTrue(element instanceof GrExpression);
((GrExpression) element).accept(myExpressionGenerator);
myBuilder.append(", ");
}
if (!arg.args.isEmpty())
myBuilder.delete(myBuilder.length() - 2, myBuilder.length());
//if (arg.args.size() > 0) myBuilder.removeFromTheEnd(2);
myBuilder.append('}');
}
} else {
final GrExpression listOrMap = GroovyRefactoringUtil.generateArgFromMultiArg(substitutor, arg.args, type, project);
LOG.assertTrue(listOrMap instanceof GrListOrMap);
listOrMap.accept(myExpressionGenerator);
}
return true;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap in project intellij-community by JetBrains.
the class GroovyStaticTypeCheckVisitor method processTupleAssignment.
@Override
protected void processTupleAssignment(@NotNull GrTupleExpression tupleExpression, @NotNull GrExpression initializer) {
if (initializer instanceof GrListOrMap && !((GrListOrMap) initializer).isMap()) {
final GrListOrMap initializerList = (GrListOrMap) initializer;
final GrExpression[] vars = tupleExpression.getExpressions();
final GrExpression[] expressions = initializerList.getInitializers();
if (vars.length > expressions.length) {
registerError(initializer, GroovyBundle.message("incorrect.number.of.values", vars.length, expressions.length), LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR);
} else {
for (int i = 0; i < vars.length; i++) {
processAssignmentWithinMultipleAssignment(vars[i], expressions[i], tupleExpression);
}
}
} else {
registerError(initializer, GroovyBundle.message("multiple.assignments.without.list.expr"), LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap in project intellij-community by JetBrains.
the class GrChangeSignatureUsageProcessor method processMethodUsage.
private static void processMethodUsage(PsiElement element, JavaChangeInfo changeInfo, boolean toChangeArguments, boolean toCatchExceptions, GrClosureSignatureUtil.ArgInfo<PsiElement>[] map, PsiSubstitutor substitutor) {
if (map == null)
return;
if (changeInfo.isNameChanged()) {
if (element instanceof GrReferenceElement) {
element = ((GrReferenceElement) element).handleElementRename(changeInfo.getNewName());
}
}
if (toChangeArguments) {
JavaParameterInfo[] parameters = changeInfo.getNewParameters();
GrArgumentList argumentList = PsiUtil.getArgumentsList(element);
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(element.getProject());
if (argumentList == null) {
if (element instanceof GrEnumConstant) {
argumentList = factory.createArgumentList();
argumentList = (GrArgumentList) element.add(argumentList);
} else {
return;
}
}
Set<PsiElement> argsToDelete = new HashSet<>(map.length * 2);
for (GrClosureSignatureUtil.ArgInfo<PsiElement> argInfo : map) {
argsToDelete.addAll(argInfo.args);
}
GrExpression[] values = new GrExpression[parameters.length];
for (int i = 0; i < parameters.length; i++) {
JavaParameterInfo parameter = parameters[i];
int index = parameter.getOldIndex();
if (index >= 0) {
argsToDelete.removeAll(map[index].args);
} else {
values[i] = createDefaultValue(factory, changeInfo, parameter, argumentList, substitutor);
}
}
for (PsiElement arg : argsToDelete) {
arg.delete();
}
boolean skipOptionals = false;
//PsiTreeUtil.getChildOfAnyType(argumentList, GrExpression.class, GrNamedArgument.class);
PsiElement anchor = null;
for (int i = 0; i < parameters.length; i++) {
JavaParameterInfo parameter = parameters[i];
int index = parameter.getOldIndex();
if (index >= 0) {
GrClosureSignatureUtil.ArgInfo<PsiElement> argInfo = map[index];
List<PsiElement> arguments = argInfo.args;
if (argInfo.isMultiArg) {
//arguments for Map and varArg
if ((i != 0 || !(!arguments.isEmpty() && arguments.iterator().next() instanceof GrNamedArgument)) && (i != parameters.length - 1 || !parameter.isVarargType())) {
final PsiType type = parameter.createType(changeInfo.getMethod().getParameterList(), argumentList.getManager());
final GrExpression arg = GroovyRefactoringUtil.generateArgFromMultiArg(substitutor, arguments, type, element.getProject());
for (PsiElement argument : arguments) {
argument.delete();
}
anchor = argumentList.addAfter(arg, anchor);
JavaCodeStyleManager.getInstance(anchor.getProject()).shortenClassReferences(anchor);
}
} else {
//arguments for simple parameters
if (arguments.size() == 1) {
//arg exists
PsiElement arg = arguments.iterator().next();
if (i == parameters.length - 1 && parameter.isVarargType()) {
if (arg instanceof GrSafeCastExpression) {
PsiElement expr = ((GrSafeCastExpression) arg).getOperand();
if (expr instanceof GrListOrMap && !((GrListOrMap) expr).isMap()) {
final PsiElement copy = expr.copy();
PsiElement[] newVarargs = ((GrListOrMap) copy).getInitializers();
for (PsiElement vararg : newVarargs) {
anchor = argumentList.addAfter(vararg, anchor);
}
arg.delete();
continue;
}
}
}
PsiElement curArg = getNextOfType(argumentList, anchor, GrExpression.class);
if (curArg == arg) {
anchor = arg;
} else {
final PsiElement copy = arg.copy();
anchor = argumentList.addAfter(copy, anchor);
arg.delete();
}
} else {
//arg is skipped. Parameter is optional
skipOptionals = true;
}
}
} else {
if (skipOptionals && isParameterOptional(parameter))
continue;
if (forceOptional(parameter)) {
skipOptionals = true;
continue;
}
try {
final GrExpression value = values[i];
if (i > 0 && (value == null || anchor == null)) {
PsiElement comma = Factory.createSingleLeafElement(GroovyTokenTypes.mCOMMA, ",", 0, 1, SharedImplUtil.findCharTableByTree(argumentList.getNode()), argumentList.getManager()).getPsi();
if (anchor == null)
anchor = argumentList.getLeftParen();
anchor = argumentList.addAfter(comma, anchor);
}
if (value != null) {
anchor = argumentList.addAfter(value, anchor);
}
} catch (IncorrectOperationException e) {
LOG.error(e.getMessage());
}
}
}
for (PsiElement arg : argsToDelete) {
arg.delete();
}
GrCall call = GroovyRefactoringUtil.getCallExpressionByMethodReference(element);
if (argumentList.getText().trim().isEmpty() && (call == null || !PsiImplUtil.hasClosureArguments(call))) {
argumentList = argumentList.replaceWithArgumentList(factory.createArgumentList());
}
CodeStyleManager.getInstance(argumentList.getProject()).reformat(argumentList);
}
if (toCatchExceptions) {
final ThrownExceptionInfo[] exceptionInfos = changeInfo.getNewExceptions();
PsiClassType[] exceptions = getExceptions(exceptionInfos, element, element.getManager());
fixExceptions(element, exceptions);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap in project intellij-community by JetBrains.
the class GppClosureParameterTypeProvider method getOverriddenMethodVariants.
@NotNull
public static List<Pair<PsiMethod, PsiSubstitutor>> getOverriddenMethodVariants(GrNamedArgument namedArgument) {
final GrArgumentLabel label = namedArgument.getLabel();
if (label == null) {
return Collections.emptyList();
}
final String methodName = label.getName();
if (methodName == null) {
return Collections.emptyList();
}
final PsiElement map = namedArgument.getParent();
if (map instanceof GrListOrMap && ((GrListOrMap) map).isMap()) {
for (PsiType expected : GroovyExpectedTypesProvider.getDefaultExpectedTypes((GrExpression) map)) {
if (expected instanceof PsiClassType) {
final List<Pair<PsiMethod, PsiSubstitutor>> pairs = getMethodsToOverrideImplementInInheritor((PsiClassType) expected, false);
return ContainerUtil.findAll(pairs, pair -> methodName.equals(pair.first.getName()));
}
}
}
return Collections.emptyList();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap in project intellij-community by JetBrains.
the class GppTypeConverter method isConvertible.
@Override
public Boolean isConvertible(@NotNull PsiType lType, @NotNull PsiType rType, @NotNull GroovyPsiElement context) {
if (context instanceof GrListOrMap && context.getReference() instanceof LiteralConstructorReference && ((LiteralConstructorReference) context.getReference()).getConstructedClassType() != null)
return null;
if (rType instanceof GrTupleType) {
final GrTupleType tupleType = (GrTupleType) rType;
final PsiType expectedComponent = PsiUtil.extractIterableTypeParameter(lType, false);
if (expectedComponent != null && isMethodCallConversion(context)) {
PsiType[] parameters = tupleType.getParameters();
if (parameters.length == 1) {
PsiType tupleComponent = parameters[0];
if (tupleComponent != null && TypesUtil.isAssignable(expectedComponent, tupleComponent, context) && hasDefaultConstructor(lType)) {
return true;
}
}
}
}
return null;
}
Aggregations