/// <summary>
        /// Attempts to locate the reference name in the specified schema.
        /// </summary>
        /// <param name="name">The reference to look up.</param>
        /// <param name="schemaCompletionData">The schema completion data to use to
        /// find the reference.</param>
        /// <param name="elementName">The element to determine what sort of reference it is
        /// (e.g. group, attribute, element).</param>
        /// <returns><see langword="null"/> if no match can be found.</returns>
        XmlSchemaObject FindSchemaObjectReference(string name, XmlSchemaCompletionData schemaCompletionData, string elementName)
        {
            QualifiedName           qualifiedName       = schemaCompletionData.CreateQualifiedName(name);
            XmlSchemaCompletionData qualifiedNameSchema = FindSchema(qualifiedName.Namespace);

            if (qualifiedNameSchema != null)
            {
                schemaCompletionData = qualifiedNameSchema;
            }
            switch (elementName)
            {
            case "element":
                return(schemaCompletionData.FindElement(qualifiedName));

            case "attribute":
                return(schemaCompletionData.FindAttribute(qualifiedName.Name));

            case "group":
                return(schemaCompletionData.FindGroup(qualifiedName.Name));

            case "attributeGroup":
                return(schemaCompletionData.FindAttributeGroup(qualifiedName.Name));
            }
            return(null);
        }
        /// <summary>

        /// Gets the XmlSchemaObject that defines the currently selected xml element or attribute.

        /// </summary>

        /// <param name="currentSchemaCompletionData">This is the schema completion data for the schema currently being
        /// displayed. This can be null if the document is not a schema.</param>

        public XmlSchemaObject GetSchemaObjectSelected(XmlSchemaCompletionData currentSchemaCompletionData)

        {
            // Find element under cursor.

            XmlElementPath path = GetElementPath();

            //attribute name under cursor, if valid

            string     attributeName = null;
            XAttribute xatt          = Tracker.Engine.Nodes.Peek(0) as XAttribute;

            if (xatt != null)
            {
                XName xattName = xatt.Name;
                if (Tracker.Engine.CurrentState is XmlNameState)
                {
                    xattName = GetCompleteName();
                }
                attributeName = xattName.FullName;
            }


            // Find schema definition object.

            XmlSchemaCompletionData schemaCompletionData = FindSchema(path);

            XmlSchemaObject schemaObject = null;

            if (schemaCompletionData != null)
            {
                XmlSchemaElement element = schemaCompletionData.FindElement(path);

                schemaObject = element;

                if (element != null)
                {
                    if (!string.IsNullOrEmpty(attributeName))
                    {
                        XmlSchemaAttribute attribute = schemaCompletionData.FindAttribute(element, attributeName);

                        if (attribute != null)
                        {
                            if (currentSchemaCompletionData != null)
                            {
                                schemaObject = GetSchemaObjectReferenced(currentSchemaCompletionData, element, attribute);
                            }
                            else
                            {
                                schemaObject = attribute;
                            }
                        }
                    }

                    return(schemaObject);
                }
            }

            return(null);
        }