private static XmlTag getComplexOrSimpleTypeDefinition(PsiElement element, PsiElement originalElement) {
    XmlElementDescriptor descriptor = element.getUserData(DESCRIPTOR_KEY);

    XmlTag contextTag = null;

    XmlAttribute contextAttribute;

    if (descriptor == null &&
        originalElement != null &&
        (contextAttribute = PsiTreeUtil.getParentOfType(originalElement, XmlAttribute.class)) != null) {
      final XmlAttributeDescriptor attributeDescriptor = contextAttribute.getDescriptor();

      if (attributeDescriptor instanceof XmlAttributeDescriptorImpl) {
        final XmlElementDescriptorImpl elementDescriptor = (XmlElementDescriptorImpl)XmlUtil.findXmlDescriptorByType(
          (XmlTag)attributeDescriptor.getDeclaration(),
          contextAttribute.getParent()
        );

        TypeDescriptor type = elementDescriptor != null ? elementDescriptor.getType(contextAttribute) : null;

        if (type instanceof ComplexTypeDescriptor) {
          return ((ComplexTypeDescriptor)type).getDeclaration();
        }
      }
    }

    if (descriptor == null &&
        originalElement != null &&
        (contextTag = PsiTreeUtil.getParentOfType(originalElement, XmlTag.class)) != null) {
      descriptor = contextTag.getDescriptor();
    }

    if (descriptor instanceof XmlElementDescriptorImpl) {
      TypeDescriptor type = ((XmlElementDescriptorImpl)descriptor).getType(contextTag);

      if (type instanceof ComplexTypeDescriptor) {
        return ((ComplexTypeDescriptor)type).getDeclaration();
      }
    }

    return null;
  }
private static XmlTag getComplexOrSimpleTypeDefinition(PsiElement element, PsiElement originalElement)