lemminx icon indicating copy to clipboard operation
lemminx copied to clipboard

CodeAction to generate missing element declarations in XSD files

Open datho7561 opened this issue 3 years ago • 0 comments

Given a schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="bun">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="cheese" minOccurs="0" maxOccurs="unbounded" />
                <xs:element ref="lettuce" minOccurs="0" maxOccurs="unbounded" />
                <xs:element ref="meat" minOccurs="0" maxOccurs="unbounded" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="cheese">
        <xs:complexType>
            <xs:attribute name="type">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:enumeration value="gouda" />
                    </xs:restriction>
                </xs:simpleType>
            </xs:attribute>
        </xs:complexType>
    </xs:element>
</xs:schema>

The elements lettuce and meat are stated to be children of the element bun, but they are not defined in the XSD, so they are marked with the xsd(src-resolve) error.

It would be nice to have a code action that generates the declaration, as a new child of xs:schema, and places the cursor within the declaration to complete the details. eg the user places the cursor on "lettuce" and activates the CodeAction, resulting in:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="bun">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="cheese" minOccurs="0" maxOccurs="unbounded" />
                <xs:element ref="lettuce" minOccurs="0" maxOccurs="unbounded" />
                <xs:element ref="meat" minOccurs="0" maxOccurs="unbounded" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="cheese">
        <xs:complexType>
            <xs:attribute name="type">
                <xs:simpleType>
                    <xs:restriction base="xs:string">
                        <xs:enumeration value="gouda" />
                    </xs:restriction>
                </xs:simpleType>
            </xs:attribute>
        </xs:complexType>
    </xs:element>
  <xs:element name="lettuce">
    |
  </xs:element>
</xs:schema>

datho7561 avatar Jun 22 '22 15:06 datho7561