List<GrSignature> (@NotNull GrSignature original, PsiType[] args, int position, @NotNull PsiElement context) { GrClosureParameter[] params = original.getParameters(); List<GrClosureParameter> newParams = new ArrayList<>(params.length); List<GrClosureParameter> opts = new ArrayList<>(params.length); List<Integer> optInds = new ArrayList<>(params.length); if (position == -1) { position = params.length - args.length; } if (position < 0 || position >= params.length) { return Collections.emptyList(); } for (int i = 0; i < params.length; i++) { if (params[i].isOptional()) { opts.add(params[i]); optInds.add(i); } else { newParams.add(params[i]); } } final PsiType rtype = original.getReturnType(); final ArrayList<GrSignature> result = new ArrayList<>(); checkAndAddSignature(result, args, position, newParams, rtype, context); for (int i = 0; i < opts.size(); i++) { newParams.add(optInds.get(i), opts.get(i)); checkAndAddSignature(result, args, position, newParams, rtype, context); } return result; }