Exemple #1
0
 @Nullable
 @Deprecated // Use JetPsiUtil.deparenthesize() or ExpressionTypingServices.deparenthesize()
 public static JetExpression deparenthesizeWithResolutionStrategy(
     @Nullable JetExpression expression,
     boolean deparenthesizeBinaryExpressionWithTypeRHS,
     @Nullable Function<JetTypeReference, Void> typeResolutionStrategy) {
   if (deparenthesizeBinaryExpressionWithTypeRHS
       && expression instanceof JetBinaryExpressionWithTypeRHS) {
     JetBinaryExpressionWithTypeRHS binaryExpression = (JetBinaryExpressionWithTypeRHS) expression;
     JetSimpleNameExpression operationSign = binaryExpression.getOperationReference();
     if (JetTokens.COLON.equals(operationSign.getReferencedNameElementType())) {
       expression = binaryExpression.getLeft();
       JetTypeReference typeReference = binaryExpression.getRight();
       if (typeResolutionStrategy != null && typeReference != null) {
         typeResolutionStrategy.apply(typeReference);
       }
     }
   } else if (expression instanceof JetPrefixExpression) {
     JetExpression baseExpression =
         getBaseExpressionIfLabeledExpression((JetPrefixExpression) expression);
     if (baseExpression != null) {
       expression = baseExpression;
     }
   } else if (expression instanceof JetExpressionWrapper) {
     expression = ((JetExpressionWrapper) expression).getBaseExpression();
   }
   if (expression instanceof JetParenthesizedExpression) {
     JetExpression innerExpression = ((JetParenthesizedExpression) expression).getExpression();
     return innerExpression != null
         ? deparenthesizeWithResolutionStrategy(
             innerExpression, deparenthesizeBinaryExpressionWithTypeRHS, typeResolutionStrategy)
         : null;
   }
   return expression;
 }
Exemple #2
0
 @Nullable
 public static JetToken getOperationToken(@NotNull JetOperationExpression expression) {
   JetSimpleNameExpression operationExpression = expression.getOperationReference();
   IElementType elementType = operationExpression.getReferencedNameElementType();
   assert elementType == null || elementType instanceof JetToken
       : "JetOperationExpression should have operation token of type JetToken: " + expression;
   return (JetToken) elementType;
 }
Exemple #3
0
  @Nullable
  public static Name getAliasName(@NotNull JetImportDirective importDirective) {
    String aliasName = importDirective.getAliasName();
    JetExpression importedReference = importDirective.getImportedReference();
    if (importedReference == null) {
      return null;
    }
    JetSimpleNameExpression referenceExpression = getLastReference(importedReference);
    if (aliasName == null) {
      aliasName = referenceExpression != null ? referenceExpression.getReferencedName() : null;
    }

    return aliasName != null && !aliasName.isEmpty() ? Name.identifier(aliasName) : null;
  }
  // TODO: Make it work for properties
  public Collection<DeclarationDescriptor> getJetCallableExtensions(
      @NotNull Condition<String> acceptedNameCondition,
      @NotNull JetSimpleNameExpression expression,
      @NotNull ResolveSession resolveSession,
      @NotNull GlobalSearchScope searchScope) {
    Collection<DeclarationDescriptor> resultDescriptors = new ArrayList<DeclarationDescriptor>();

    BindingContext context = ResolveSessionUtils.resolveToExpression(resolveSession, expression);
    JetExpression receiverExpression = expression.getReceiverExpression();

    if (receiverExpression != null) {
      JetType expressionType = context.get(BindingContext.EXPRESSION_TYPE, receiverExpression);
      JetScope scope = context.get(BindingContext.RESOLUTION_SCOPE, receiverExpression);

      if (expressionType != null && scope != null) {
        Collection<String> extensionFunctionsNames = getAllJetExtensionFunctionsNames(searchScope);

        Set<FqName> functionFQNs = new java.util.HashSet<FqName>();

        // Collect all possible extension function qualified names
        for (String name : extensionFunctionsNames) {
          if (acceptedNameCondition.value(name)) {
            Collection<PsiElement> extensionFunctions =
                getJetExtensionFunctionsByName(name, searchScope);

            for (PsiElement extensionFunction : extensionFunctions) {
              if (extensionFunction instanceof JetNamedFunction) {
                functionFQNs.add(JetPsiUtil.getFQName((JetNamedFunction) extensionFunction));
              } else if (extensionFunction instanceof PsiMethod) {
                FqName functionFQN =
                    JetFromJavaDescriptorHelper.getJetTopLevelDeclarationFQN(
                        (PsiMethod) extensionFunction);
                if (functionFQN != null) {
                  functionFQNs.add(functionFQN);
                }
              }
            }
          }
        }

        // Iterate through the function with attempt to resolve found functions
        for (FqName functionFQN : functionFQNs) {
          for (CallableDescriptor functionDescriptor :
              ExpressionTypingUtils.canFindSuitableCall(
                  functionFQN,
                  project,
                  receiverExpression,
                  expressionType,
                  scope,
                  resolveSession.getModuleConfiguration())) {

            resultDescriptors.add(functionDescriptor);
          }
        }
      }
    }

    return resultDescriptors;
  }
Exemple #5
0
  @Nullable
  public static Name getConventionName(@NotNull JetSimpleNameExpression simpleNameExpression) {
    if (simpleNameExpression.getIdentifier() != null) {
      return simpleNameExpression.getReferencedNameAsName();
    }

    PsiElement firstChild = simpleNameExpression.getFirstChild();
    if (firstChild != null) {
      IElementType elementType = firstChild.getNode().getElementType();
      if (elementType instanceof JetToken) {
        JetToken jetToken = (JetToken) elementType;
        return OperatorConventions.getNameForOperationSymbol(jetToken);
      }
    }

    return null;
  }
Exemple #6
0
  @Nullable
  private static FqName getFQName(@Nullable JetExpression expression) {
    if (expression == null) {
      return null;
    }

    if (expression instanceof JetDotQualifiedExpression) {
      JetDotQualifiedExpression dotQualifiedExpression = (JetDotQualifiedExpression) expression;
      FqName parentFqn = getFQName(dotQualifiedExpression.getReceiverExpression());
      Name child = getName(dotQualifiedExpression.getSelectorExpression());

      return parentFqn != null && child != null ? parentFqn.child(child) : null;
    } else if (expression instanceof JetSimpleNameExpression) {
      JetSimpleNameExpression simpleNameExpression = (JetSimpleNameExpression) expression;
      return FqName.topLevel(simpleNameExpression.getReferencedNameAsName());
    } else {
      throw new IllegalArgumentException(
          "Can't construct fqn for: " + expression.getClass().toString());
    }
  }
Exemple #7
0
  public static boolean isSelectorInQualified(@NotNull JetSimpleNameExpression nameExpression) {
    PsiElement nameExpressionParent = nameExpression.getParent();

    if (nameExpressionParent instanceof JetUserType) {
      assert ((JetUserType) nameExpressionParent).getReferenceExpression() == nameExpression;
      return ((JetUserType) nameExpressionParent).getQualifier() != null;
    }

    JetExpression selector = nameExpression;
    if (nameExpressionParent instanceof JetCallExpression
        && ((JetCallExpression) nameExpressionParent).getCalleeExpression() == nameExpression) {
      selector = (JetCallExpression) nameExpressionParent;
    }

    PsiElement selectorParent = selector.getParent();
    return selectorParent instanceof JetQualifiedExpression
        && (((JetQualifiedExpression) selectorParent).getSelectorExpression() == selector);
  }
  @NotNull
  public Collection<FunctionDescriptor> getTopLevelFunctionDescriptorsByName(
      @NotNull String name,
      @NotNull JetSimpleNameExpression expression,
      @NotNull ResolveSession resolveSession,
      @NotNull GlobalSearchScope scope) {
    // name parameter can differ from expression.getReferenceName() when expression contains
    // completion suffix
    Name referenceName =
        expression.getIdentifier() == null
            ? JetPsiUtil.getConventionName(expression)
            : Name.identifier(name);
    if (referenceName == null || referenceName.toString().isEmpty()) {
      return Collections.emptyList();
    }

    BindingContext context = ResolveSessionUtils.resolveToExpression(resolveSession, expression);
    JetScope jetScope = context.get(BindingContext.RESOLUTION_SCOPE, expression);

    if (jetScope == null) {
      return Collections.emptyList();
    }

    Set<FunctionDescriptor> result = Sets.newHashSet();

    Collection<PsiMethod> topLevelFunctionPrototypes =
        JetFromJavaDescriptorHelper.getTopLevelFunctionPrototypesByName(
            referenceName.getName(), project, scope);
    for (PsiMethod method : topLevelFunctionPrototypes) {
      FqName functionFQN = JetFromJavaDescriptorHelper.getJetTopLevelDeclarationFQN(method);
      if (functionFQN != null) {
        JetImportDirective importDirective =
            JetPsiFactory.createImportDirective(project, new ImportPath(functionFQN, false));
        Collection<? extends DeclarationDescriptor> declarationDescriptors =
            new QualifiedExpressionResolver()
                .analyseImportReference(
                    importDirective,
                    jetScope,
                    new BindingTraceContext(),
                    resolveSession.getModuleConfiguration());
        for (DeclarationDescriptor declarationDescriptor : declarationDescriptors) {
          if (declarationDescriptor instanceof FunctionDescriptor) {
            result.add((FunctionDescriptor) declarationDescriptor);
          }
        }
      }
    }

    Set<FqName> affectedPackages = Sets.newHashSet();
    Collection<JetNamedFunction> jetNamedFunctions =
        JetShortFunctionNameIndex.getInstance().get(referenceName.getName(), project, scope);
    for (JetNamedFunction jetNamedFunction : jetNamedFunctions) {
      PsiFile containingFile = jetNamedFunction.getContainingFile();
      if (containingFile instanceof JetFile) {
        JetFile jetFile = (JetFile) containingFile;
        String packageName = jetFile.getPackageName();
        if (packageName != null) {
          affectedPackages.add(new FqName(packageName));
        }
      }
    }

    for (FqName affectedPackage : affectedPackages) {
      NamespaceDescriptor packageDescriptor =
          resolveSession.getPackageDescriptorByFqName(affectedPackage);
      assert packageDescriptor != null
          : "There's a function in stub index with invalid package: " + affectedPackage;
      JetScope memberScope = packageDescriptor.getMemberScope();
      result.addAll(memberScope.getFunctions(referenceName));
    }

    return result;
  }
Exemple #9
0
 public static boolean isBackingFieldReference(@NotNull JetSimpleNameExpression expression) {
   return expression.getReferencedNameElementType() == JetTokens.FIELD_IDENTIFIER;
 }