예제 #1
0
  @Nullable
  private KtNamedDeclaration[] getVariables(Expression[] params, ExpressionContext context) {
    if (params.length != 0) return null;

    Project project = context.getProject();
    PsiDocumentManager.getInstance(project).commitAllDocuments();

    PsiFile psiFile =
        PsiDocumentManager.getInstance(project).getPsiFile(context.getEditor().getDocument());
    if (!(psiFile instanceof KtFile)) return null;

    KtExpression contextExpression = findContextExpression(psiFile, context.getStartOffset());
    if (contextExpression == null) return null;

    ResolutionFacade resolutionFacade = ResolutionUtils.getResolutionFacade(contextExpression);

    BindingContext bindingContext =
        resolutionFacade.analyze(contextExpression, BodyResolveMode.FULL);
    LexicalScope scope =
        ScopeUtils.getResolutionScope(contextExpression, bindingContext, resolutionFacade);

    IterableTypesDetector detector =
        resolutionFacade.getIdeService(IterableTypesDetection.class).createDetector(scope);

    DataFlowInfo dataFlowInfo =
        BindingContextUtilsKt.getDataFlowInfo(bindingContext, contextExpression);

    List<VariableDescriptor> filteredDescriptors = new ArrayList<VariableDescriptor>();
    for (DeclarationDescriptor declarationDescriptor : getAllVariables(scope)) {
      if (declarationDescriptor instanceof VariableDescriptor) {
        VariableDescriptor variableDescriptor = (VariableDescriptor) declarationDescriptor;

        if (variableDescriptor.getExtensionReceiverParameter() != null
            && ExtensionUtils.substituteExtensionIfCallableWithImplicitReceiver(
                    variableDescriptor, scope, bindingContext, dataFlowInfo)
                .isEmpty()) {
          continue;
        }

        if (isSuitable(variableDescriptor, project, detector)) {
          filteredDescriptors.add(variableDescriptor);
        }
      }
    }

    List<KtNamedDeclaration> declarations = new ArrayList<KtNamedDeclaration>();
    for (DeclarationDescriptor declarationDescriptor : filteredDescriptors) {
      PsiElement declaration =
          DescriptorToSourceUtils.descriptorToDeclaration(declarationDescriptor);
      assert declaration == null || declaration instanceof PsiNamedElement;

      if (declaration instanceof KtProperty || declaration instanceof KtParameter) {
        declarations.add((KtNamedDeclaration) declaration);
      }
    }

    return declarations.toArray(new KtNamedDeclaration[declarations.size()]);
  }
예제 #2
0
  @Nullable
  private static PsiNamedElement[] getSupertypes(Expression[] params, ExpressionContext context) {
    if (params.length != 0) return null;

    Project project = context.getProject();
    PsiDocumentManager.getInstance(project).commitAllDocuments();

    PsiFile psiFile =
        PsiDocumentManager.getInstance(project).getPsiFile(context.getEditor().getDocument());
    if (!(psiFile instanceof JetFile)) return null;

    JetExpression expression =
        PsiTreeUtil.getParentOfType(
            psiFile.findElementAt(context.getStartOffset()), JetExpression.class);
    if (expression == null) return null;

    BindingContext bc = ResolutionUtils.analyze(expression, BodyResolveMode.FULL);
    JetScope scope = bc.get(BindingContext.RESOLUTION_SCOPE, expression);
    if (scope == null) return null;

    List<PsiNamedElement> result = new ArrayList<PsiNamedElement>();

    for (DeclarationDescriptor descriptor :
        scope.getDescriptors(
            DescriptorKindFilter.NON_SINGLETON_CLASSIFIERS,
            JetScope.Companion.getALL_NAME_FILTER())) {
      if (!(descriptor instanceof ClassDescriptor)) continue;
      ClassDescriptor classDescriptor = (ClassDescriptor) descriptor;
      if (!classDescriptor.getModality().isOverridable()) continue;
      ClassKind kind = classDescriptor.getKind();
      if (kind == ClassKind.INTERFACE || kind == ClassKind.CLASS) {
        PsiElement declaration = DescriptorToSourceUtils.descriptorToDeclaration(descriptor);
        if (declaration != null) {
          result.add((PsiNamedElement) declaration);
        }
      }
    }

    return result.toArray(new PsiNamedElement[result.size()]);
  }