Java 类com.intellij.psi.filters.classes.AssignableFromContextFilter 实例源码

项目:intellij-ce-playground    文件:JavaCompletionContributor.java   
@Nullable
public static ElementFilter getReferenceFilter(PsiElement position) {
  // Completion after extends in interface, type parameter and implements in class
  final PsiClass containingClass = PsiTreeUtil.getParentOfType(position, PsiClass.class, false, PsiCodeBlock.class, PsiMethod.class, PsiExpressionList.class, PsiVariable.class, PsiAnnotation.class);
  if (containingClass != null && psiElement().afterLeaf(PsiKeyword.EXTENDS, PsiKeyword.IMPLEMENTS, ",", "&").accepts(position)) {
    return new AndFilter(ElementClassFilter.CLASS, new NotFilter(new AssignableFromContextFilter()));
  }

  if (ANNOTATION_NAME.accepts(position)) {
    return new AnnotationTypeFilter();
  }

  if (JavaKeywordCompletion.DECLARATION_START.getValue().accepts(position) ||
      JavaKeywordCompletion.isInsideParameterList(position) ||
      isInsideAnnotationName(position)) {
    return new OrFilter(ElementClassFilter.CLASS, ElementClassFilter.PACKAGE_FILTER);
  }

  if (psiElement().afterLeaf(PsiKeyword.INSTANCEOF).accepts(position)) {
    return new ElementExtractorFilter(ElementClassFilter.CLASS);
  }

  if (JavaKeywordCompletion.VARIABLE_AFTER_FINAL.accepts(position)) {
    return ElementClassFilter.CLASS;
  }

  if (isCatchFinallyPosition(position) ||
      JavaKeywordCompletion.START_SWITCH.accepts(position) ||
      JavaKeywordCompletion.isInstanceofPlace(position) ||
      JavaKeywordCompletion.isAfterPrimitiveOrArrayType(position)) {
    return null;
  }

  if (JavaKeywordCompletion.START_FOR.accepts(position)) {
    return new OrFilter(ElementClassFilter.CLASS, ElementClassFilter.VARIABLE);
  }

  if (JavaSmartCompletionContributor.AFTER_NEW.accepts(position)) {
    return ElementClassFilter.CLASS;
  }

  if (psiElement().inside(PsiReferenceParameterList.class).accepts(position)) {
    return ElementClassFilter.CLASS;
  }

  if (psiElement().inside(PsiAnnotationParameterList.class).accepts(position)) {
    return createAnnotationFilter(position);
  }

  PsiVariable var = PsiTreeUtil.getParentOfType(position, PsiVariable.class, false, PsiClass.class);
  if (var != null && PsiTreeUtil.isAncestor(var.getInitializer(), position, false)) {
    return new ExcludeDeclaredFilter(new ClassFilter(PsiVariable.class));
  }

  if (SWITCH_LABEL.accepts(position)) {
    return new ClassFilter(PsiField.class) {
      @Override
      public boolean isAcceptable(Object element, PsiElement context) {
        return element instanceof PsiEnumConstant;
      }
    };
  }

  return TrueFilter.INSTANCE;
}
项目:tools-idea    文件:JavaCompletionContributor.java   
@Nullable
public static ElementFilter getReferenceFilter(PsiElement position) {
  // Completion after extends in interface, type parameter and implements in class
  final PsiClass containingClass = PsiTreeUtil.getParentOfType(position, PsiClass.class, false, PsiCodeBlock.class, PsiMethod.class, PsiExpressionList.class, PsiVariable.class, PsiAnnotation.class);
  if (containingClass != null && psiElement().afterLeaf(PsiKeyword.EXTENDS, PsiKeyword.IMPLEMENTS, ",", "&").accepts(position)) {
    return new AndFilter(ElementClassFilter.CLASS, new NotFilter(new AssignableFromContextFilter()));
  }

  if (ANNOTATION_NAME.accepts(position)) {
    return new AnnotationTypeFilter();
  }

  if (JavaCompletionData.DECLARATION_START.accepts(position) ||
      JavaCompletionData.isInsideParameterList(position) ||
      psiElement().inside(psiElement(PsiJavaCodeReferenceElement.class).withParent(psiAnnotation())).accepts(position)) {
    return new OrFilter(ElementClassFilter.CLASS, ElementClassFilter.PACKAGE_FILTER);
  }

  if (psiElement().afterLeaf(PsiKeyword.INSTANCEOF).accepts(position)) {
    return new ElementExtractorFilter(ElementClassFilter.CLASS);
  }

  if (JavaCompletionData.VARIABLE_AFTER_FINAL.accepts(position)) {
    return ElementClassFilter.CLASS;
  }

  if (JavaCompletionData.AFTER_TRY_BLOCK.isAcceptable(position, position) ||
      JavaCompletionData.START_SWITCH.accepts(position) ||
      JavaCompletionData.isInstanceofPlace(position) ||
      JavaCompletionData.isAfterPrimitiveOrArrayType(position)) {
    return null;
  }

  if (JavaCompletionData.START_FOR.accepts(position)) {
    return new OrFilter(ElementClassFilter.CLASS, ElementClassFilter.VARIABLE);
  }

  if (JavaSmartCompletionContributor.AFTER_NEW.accepts(position)) {
    return ElementClassFilter.CLASS;
  }

  if (psiElement().inside(PsiReferenceParameterList.class).accepts(position)) {
    return ElementClassFilter.CLASS;
  }

  if (psiElement().inside(PsiAnnotationParameterList.class).accepts(position)) {
    return createAnnotationFilter(position);
  }

  if (psiElement().afterLeaf("=").inside(PsiVariable.class).accepts(position)) {
    return new OrFilter(
      new ClassFilter(PsiVariable.class, false),
      new ExcludeDeclaredFilter(new ClassFilter(PsiVariable.class)));
  }

  if (SWITCH_LABEL.accepts(position)) {
    return new ClassFilter(PsiField.class) {
      @Override
      public boolean isAcceptable(Object element, PsiElement context) {
        return element instanceof PsiEnumConstant;
      }
    };
  }

  return TrueFilter.INSTANCE;
}