Skip to content

Commit

Permalink
Fixes indentation issue for schema based attributes
Browse files Browse the repository at this point in the history
Fixes eclipse-lemminx#188

The issue was that when adding attributes it  needed to know before hand how many attributes
would be added to know which add attribute method should be called.
.isRequired() was messing this up and was moved outside to determine the total variables being added
before the add attribute methods were called.

Signed-off-by: Nikolas Komonen <[email protected]>
  • Loading branch information
NikolasKomonen committed Nov 5, 2018
1 parent 2d79d8a commit 93d2802
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,23 +134,28 @@ public String generate(Collection<CMAttributeDeclaration> attributes, String tag

private int generate(Collection<CMAttributeDeclaration> attributes, int level, int snippetIndex, XMLBuilder xml, String tagName) {
int attributeIndex = 0;
int attributesSize = attributes.size();
for (CMAttributeDeclaration attributeDeclaration : attributes) {
if (attributeDeclaration.isRequired()) {
String value = attributeDeclaration.getDefaultValue();
if (canSupportSnippets) {
snippetIndex++;
value = ("$" + snippetIndex);
}
if(attributesSize != 1) {
xml.addAttributes(attributeDeclaration.getName(), value, attributeIndex, level, tagName);
}
else {
xml.addSingleAttribute(attributeDeclaration.getName(), value);
}

attributeIndex++;

ArrayList<CMAttributeDeclaration> requiredAttributes = new ArrayList<CMAttributeDeclaration>();
for (CMAttributeDeclaration att: attributes) {
if(att.isRequired()) {
requiredAttributes.add(att);
}
}
int attributesSize = requiredAttributes.size();
for (CMAttributeDeclaration attributeDeclaration : requiredAttributes) {
String value = attributeDeclaration.getDefaultValue();
if (canSupportSnippets) {
snippetIndex++;
value = ("$" + snippetIndex);
}
if(attributesSize != 1) {
xml.addAttributes(attributeDeclaration.getName(), value, attributeIndex, level, tagName);
}
else {
xml.addSingleAttribute(attributeDeclaration.getName(), value);
}

attributeIndex++;
}
return snippetIndex;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,15 @@ public void completionOnAttributeValue2() throws BadLocationException {
c("debit", "debit"), c("cash", "cash"));
}

@Test
public void schemaLocationWithElementAndAttributeCompletion() throws BadLocationException {
String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" + //
"<invoice xmlns=\"http://simpleAttribute\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\r\n"
+ " xsi:schemaLocation=\"http://simpleAttribute xsd/simpleAttribute.xsd \">\r\n" + //
" <pro|</invoice>";
XMLAssert.testCompletionFor(xml, null, "src/test/resources/simpleAttribute.xml", null, c("product", "<product description=\"\" />"));
}

@Test
public void completionWithoutStartBracket() throws BadLocationException {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + //
Expand Down
27 changes: 27 additions & 0 deletions org.eclipse.lsp4xml/src/test/resources/xsd/simpleAttribute.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema
elementFormDefault="qualified"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element
name="invoice"
type="invoiceType"></xsd:element>
<xsd:complexType name="invoiceType">
<xsd:annotation>
<xsd:documentation>An invoice type...</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:element
name="product"
type="productType"></xsd:element>
</xsd:sequence>
</xsd:complexType>


<xsd:complexType name="productType">
<xsd:attribute
name="description"
type="xsd:string"
use="required"></xsd:attribute>
</xsd:complexType>

</xsd:schema>

0 comments on commit 93d2802

Please sign in to comment.