Beispiel #1
0
  @Nullable
  public static JetElement getEnclosingElementForLocalDeclaration(
      @Nullable JetNamedDeclaration declaration) {
    if (declaration instanceof JetTypeParameter) {
      declaration = PsiTreeUtil.getParentOfType(declaration, JetNamedDeclaration.class);
    } else if (declaration instanceof JetParameter) {
      PsiElement parent = declaration.getParent();
      if (parent != null && parent.getParent() instanceof JetNamedFunction) {
        declaration = (JetNamedFunction) parent.getParent();
      }
    }

    //noinspection unchecked
    JetElement container =
        PsiTreeUtil.getParentOfType(
            declaration,
            JetBlockExpression.class,
            JetClassInitializer.class,
            JetProperty.class,
            JetFunction.class,
            JetParameter.class);
    if (container == null) return null;

    return (container instanceof JetClassInitializer)
        ? ((JetClassInitializer) container).getBody()
        : container;
  }
Beispiel #2
0
  @Nullable
  public static FqName getFQName(@NotNull JetNamedDeclaration namedDeclaration) {
    Name name = namedDeclaration.getNameAsName();
    if (name == null) {
      return null;
    }

    PsiElement parent = namedDeclaration.getParent();
    if (parent instanceof JetClassBody) {
      // One nesting to JetClassBody doesn't affect to qualified name
      parent = parent.getParent();
    }

    FqName firstPart = null;
    if (parent instanceof JetFile) {
      firstPart = getFQName((JetFile) parent);
    } else if (parent instanceof JetNamedFunction || parent instanceof JetClass) {
      firstPart = getFQName((JetNamedDeclaration) parent);
    } else if (namedDeclaration instanceof JetParameter) {
      JetClass constructorClass = getClassIfParameterIsProperty((JetParameter) namedDeclaration);
      if (constructorClass != null) {
        firstPart = getFQName(constructorClass);
      }
    } else if (parent instanceof JetObjectDeclaration) {
      if (parent.getParent() instanceof JetClassObject) {
        JetClassOrObject classOrObject =
            PsiTreeUtil.getParentOfType(parent, JetClassOrObject.class);
        if (classOrObject != null) {
          firstPart = getFQName(classOrObject);
        }
      } else {
        firstPart = getFQName((JetNamedDeclaration) parent);
      }
    }

    if (firstPart == null) {
      return null;
    }

    return firstPart.child(name);
  }