예제 #1
0
  private static List<GrStatement> generateVarDeclarations(
      List<VariableInfo> varInfos, Project project, @Nullable GrExpression initializer) {
    List<GrStatement> result = new ArrayList<GrStatement>();
    if (varInfos.size() == 0) return result;

    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
    boolean distinctDeclaration = haveDifferentTypes(varInfos);

    if (distinctDeclaration) {
      for (VariableInfo info : varInfos) {
        result.add(
            factory.createVariableDeclaration(
                ArrayUtil.EMPTY_STRING_ARRAY, null, info.getType(), info.getName()));
      }
    } else {
      String[] names = new String[varInfos.size()];
      for (int i = 0, mustAddLength = varInfos.size(); i < mustAddLength; i++) {
        names[i] = varInfos.get(i).getName();
      }
      result.add(
          factory.createVariableDeclaration(
              ArrayUtil.EMPTY_STRING_ARRAY, initializer, varInfos.get(0).getType(), names));
    }
    return result;
  }
예제 #2
0
 private static boolean haveDifferentTypes(List<VariableInfo> varInfos) {
   if (varInfos.size() < 2) return true;
   Set<String> diffTypes = new HashSet<String>();
   for (VariableInfo info : varInfos) {
     final PsiType t = info.getType();
     diffTypes.add(t == null ? null : TypesUtil.unboxPrimitiveTypeWrapper(t).getCanonicalText());
   }
   return diffTypes.size() > 1;
 }
예제 #3
0
  private static GrStatement[] createTupleDeclaration(
      final VariableInfo[] infos, GrMethodCallExpression callExpression, final Project project) {
    GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);

    StringBuilder tuple = new StringBuilder();
    tuple.append("def (");
    for (VariableInfo info : infos) {
      final PsiType type = info.getType();
      if (type != null) {
        final PsiType unboxed = TypesUtil.unboxPrimitiveTypeWrapper(type);
        tuple.append(unboxed.getCanonicalText());
        tuple.append(' ');
      }
      tuple.append(info.getName());
      tuple.append(",");
    }
    StringUtil.trimEnd(tuple, ",");
    tuple.append(")=");
    tuple.append(callExpression.getText());

    return new GrStatement[] {factory.createStatementFromText(tuple)};
  }