Beispiel #1
0
    private static PsiReference[] createReferencesForNamedArgument(
        @NotNull PsiElement element,
        GrNamedArgument namedArgument,
        @NotNull ProcessingContext context) {
      String labelName = namedArgument.getLabelName();
      if (labelName == null) return PsiReference.EMPTY_ARRAY;

      if (!GroovyMethodInfo.getAllSupportedNamedArguments().contains(labelName)) {
        // Optimization: avoid unnecessary resolve.
        return PsiReference.EMPTY_ARRAY;
      }

      PsiElement call = PsiUtil.getCallByNamedParameter(namedArgument);

      if (!(call instanceof GrMethodCall)) return PsiReference.EMPTY_ARRAY;

      GrExpression invokedExpression = ((GrMethodCall) call).getInvokedExpression();
      if (!(invokedExpression instanceof GrReferenceExpression)) return PsiReference.EMPTY_ARRAY;

      for (GroovyResolveResult result :
          ((GrReferenceExpression) invokedExpression).multiResolve(false)) {
        PsiElement eMethod = result.getElement();
        if (!(eMethod instanceof PsiMethod)) continue;

        PsiMethod method = (PsiMethod) eMethod;

        for (GroovyMethodInfo info : GroovyMethodInfo.getInfos(method)) {
          Object referenceProvider = info.getNamedArgReferenceProvider(labelName);
          if (referenceProvider != null) {
            PsiReference[] refs;

            if (referenceProvider instanceof GroovyNamedArgumentReferenceProvider) {
              refs =
                  ((GroovyNamedArgumentReferenceProvider) referenceProvider)
                      .createRef(element, namedArgument, result, context);
            } else {
              refs =
                  ((PsiReferenceProvider) referenceProvider)
                      .getReferencesByElement(element, context);
            }

            if (refs.length > 0) {
              return refs;
            }
          }
        }
      }

      return PsiReference.EMPTY_ARRAY;
    }
 @Override
 public void visitNamedArgument(GrNamedArgument argument) {
   GrArgumentLabel label = argument.getLabel();
   if (label != null) {
     PsiElement pparent = argument.getParent().getParent();
     if (pparent instanceof GrCall && resolvesToDefaultConstructor(((GrCall) pparent))) {
       final GroovyResolveResult resolveResult = label.advancedResolve();
       PsiElement resolved = resolveResult.getElement();
       PsiType type;
       if (resolved instanceof PsiField) {
         type = ((PsiField) resolved).getType();
       } else if (resolved instanceof PsiMethod
           && GroovyPropertyUtils.isSimplePropertySetter((PsiMethod) resolved)) {
         type = ((PsiMethod) resolved).getParameterList().getParameters()[0].getType();
       } else {
         type = null;
       }
       type = resolveResult.getSubstitutor().substitute(type);
       if (type != null) {
         myResult = createSimpleSubTypeResult(type);
       }
     }
   }
 }
 private static boolean namedArgumentListsAreEquivalent(
     GrNamedArgument[] namedArgs1, GrNamedArgument[] namedArgs2) {
   if (namedArgs1.length != namedArgs2.length) {
     return false;
   }
   for (GrNamedArgument arg1 : namedArgs1) {
     final GrArgumentLabel label1 = arg1.getLabel();
     if (label1 == null) {
       return false;
     }
     final String name1 = label1.getName();
     boolean found = false;
     final GrExpression expression1 = arg1.getExpression();
     for (GrNamedArgument arg2 : namedArgs2) {
       final GrArgumentLabel label2 = arg2.getLabel();
       if (label2 == null) {
         return false;
       }
       final String name2 = label2.getName();
       final GrExpression expression2 = arg2.getExpression();
       if (name1 == null) {
         if (name2 == null
             && expressionsAreEquivalent(
                 ((GrExpression) label1.getNameElement()), (GrExpression) label2.getNameElement())
             && expressionsAreEquivalent(expression1, expression2)) {
           found = true;
           break;
         }
       } else if (name1.equals(name2) && expressionsAreEquivalent(expression1, expression2)) {
         found = true;
         break;
       }
     }
     if (!found) {
       return false;
     }
   }
   return true;
 }
  @NotNull
  private Collection<? extends MavenRemoteRepository> findMavenRemoteRepositories(
      @Nullable GrClosableBlock repositoriesBlock) {
    Set<MavenRemoteRepository> myRemoteRepositories = ContainerUtil.newHashSet();
    for (GrMethodCall repo :
        PsiTreeUtil.getChildrenOfTypeAsList(repositoriesBlock, GrMethodCall.class)) {

      final String expressionText = repo.getInvokedExpression().getText();
      if ("mavenCentral".equals(expressionText)) {
        myRemoteRepositories.add(mavenCentralRemoteRepository);
      } else if ("mavenRepo".equals(expressionText)) {
        for (GrNamedArgument namedArgument : repo.getNamedArguments()) {
          if ("url".equals(namedArgument.getLabelName())) {
            URI urlArgumentValue = resolveUriFromSimpleExpression(namedArgument.getExpression());
            if (urlArgumentValue != null) {
              String textUri = urlArgumentValue.toString();
              myRemoteRepositories.add(
                  new MavenRemoteRepository(textUri, null, textUri, null, null, null));
            }
            break;
          }
        }
      } else if ("maven".equals(expressionText) && repo.getClosureArguments().length > 0) {
        List<GrApplicationStatement> applicationStatementList =
            PsiTreeUtil.getChildrenOfTypeAsList(
                repo.getClosureArguments()[0], GrApplicationStatement.class);
        if (!applicationStatementList.isEmpty()) {
          GrApplicationStatement statement = applicationStatementList.get(0);
          if (statement == null) continue;
          GrExpression expression = statement.getInvokedExpression();

          if ("url".equals(expression.getText())) {
            URI urlArgumentValue =
                resolveUriFromSimpleExpression(statement.getExpressionArguments()[0]);
            if (urlArgumentValue != null) {
              String textUri = urlArgumentValue.toString();
              myRemoteRepositories.add(
                  new MavenRemoteRepository(textUri, null, textUri, null, null, null));
            }
          }
        }

        List<GrAssignmentExpression> assignmentExpressionList =
            PsiTreeUtil.getChildrenOfTypeAsList(
                repo.getClosureArguments()[0], GrAssignmentExpression.class);
        if (!assignmentExpressionList.isEmpty()) {
          GrAssignmentExpression statement = assignmentExpressionList.get(0);
          if (statement == null) continue;
          GrExpression expression = statement.getLValue();

          if ("url".equals(expression.getText())) {
            URI urlArgumentValue = resolveUriFromSimpleExpression(statement.getRValue());
            if (urlArgumentValue != null) {
              String textUri = urlArgumentValue.toString();
              myRemoteRepositories.add(
                  new MavenRemoteRepository(textUri, null, textUri, null, null, null));
            }
          }
        }
      }
    }

    return myRemoteRepositories;
  }
  public List<Block> generateSubBlocks() {

    // For binary expressions
    PsiElement blockPsi = myNode.getPsi();

    if (blockPsi instanceof GrBinaryExpression
        && !(blockPsi.getParent() instanceof GrBinaryExpression)) {
      return generateForBinaryExpr();
    }

    // For multiline strings
    if ((myNode.getElementType() == mSTRING_LITERAL || myNode.getElementType() == mGSTRING_LITERAL)
        && myBlock.getTextRange().equals(myNode.getTextRange())) {
      String text = myNode.getText();
      if (text.length() > 6) {
        if (text.substring(0, 3).equals("'''") && text.substring(text.length() - 3).equals("'''")
            || text.substring(0, 3).equals("\"\"\"")
                & text.substring(text.length() - 3).equals("\"\"\"")) {
          return generateForMultiLineString();
        }
      }
    }

    if (myNode.getElementType() == mGSTRING_BEGIN
        && myBlock.getTextRange().equals(myNode.getTextRange())) {
      String text = myNode.getText();
      if (text.length() > 3) {
        if (text.substring(0, 3).equals("\"\"\"")) {
          return generateForMultiLineGStringBegin();
        }
      }
    }

    // for gstrings
    if (myNode.getElementType() == GSTRING) {
      final ArrayList<Block> subBlocks = new ArrayList<Block>();
      ASTNode[] children = getGroovyChildren(myNode);
      for (ASTNode childNode : children) {
        if (childNode.getTextRange().getLength() > 0) {
          final Indent indent = GroovyIndentProcessor.getChildIndent(myBlock, childNode);
          subBlocks.add(
              new GroovyBlock(
                  childNode, indent, myWrap, mySettings, myGroovySettings, myAlignmentProvider));
        }
      }
      return subBlocks;
    }

    // chained properties, calls, indexing, etc
    if (NESTED.contains(myNode.getElementType())
        && blockPsi.getParent() != null
        && !NESTED.contains(blockPsi.getParent().getNode().getElementType())) {
      final List<Block> subBlocks = new ArrayList<Block>();
      AlignmentProvider.Aligner dotsAligner =
          mySettings.ALIGN_MULTILINE_CHAINED_METHODS
              ? myAlignmentProvider.createAligner(true)
              : null;
      addNestedChildren(myNode.getPsi(), subBlocks, dotsAligner, true);
      return subBlocks;
    }

    if (blockPsi instanceof GrListOrMap
        && ((GrListOrMap) blockPsi).isMap()
        && myGroovySettings.ALIGN_NAMED_ARGS_IN_MAP) {
      AlignmentProvider.Aligner labels = myAlignmentProvider.createAligner(false);
      AlignmentProvider.Aligner exprs = myAlignmentProvider.createAligner(true);
      GrNamedArgument[] namedArgs = ((GrListOrMap) blockPsi).getNamedArguments();
      for (GrNamedArgument arg : namedArgs) {
        GrArgumentLabel label = arg.getLabel();
        if (label != null) labels.append(label);

        PsiElement colon = arg.getColon();
        if (colon == null) colon = arg.getExpression();
        if (colon != null) exprs.append(colon);
      }
    }

    // For Parameter lists
    if (isListLikeClause(blockPsi)) {
      final ArrayList<Block> subBlocks = new ArrayList<Block>();
      List<ASTNode> astNodes = visibleChildren(myNode);

      if (mustAlign(blockPsi, astNodes)) {
        final AlignmentProvider.Aligner aligner = myAlignmentProvider.createAligner(false);
        for (ASTNode node : astNodes) {
          if (!isKeyword(node)) aligner.append(node.getPsi());
        }
      }
      for (ASTNode childNode : astNodes) {
        final Indent indent = GroovyIndentProcessor.getChildIndent(myBlock, childNode);
        subBlocks.add(
            new GroovyBlock(
                childNode, indent, myWrap, mySettings, myGroovySettings, myAlignmentProvider));
      }
      return subBlocks;
    }

    boolean classLevel = blockPsi instanceof GrTypeDefinitionBody;
    if (blockPsi instanceof GrCodeBlock || blockPsi instanceof GroovyFile || classLevel) {
      List<ASTNode> children = visibleChildren(myNode);
      calculateAlignments(children, classLevel);
      final ArrayList<Block> subBlocks = new ArrayList<Block>();

      if (classLevel && myAlignment != null) {
        final AlignmentProvider.Aligner aligner = myAlignmentProvider.createAligner(true);
        for (ASTNode child : children) {
          aligner.append(child.getPsi());
        }
      }
      for (ASTNode childNode : children) {
        final Indent indent = GroovyIndentProcessor.getChildIndent(myBlock, childNode);
        subBlocks.add(
            new GroovyBlock(
                childNode, indent, myWrap, mySettings, myGroovySettings, myAlignmentProvider));
      }
      return subBlocks;
    }

    // For other cases
    final ArrayList<Block> subBlocks = new ArrayList<Block>();
    for (ASTNode childNode : visibleChildren(myNode)) {
      final Indent indent = GroovyIndentProcessor.getChildIndent(myBlock, childNode);
      subBlocks.add(
          new GroovyBlock(
              childNode, indent, myWrap, mySettings, myGroovySettings, myAlignmentProvider));
    }
    return subBlocks;
  }