Skip to content

Commit

Permalink
splitAttributes keeps first attribute on same line
Browse files Browse the repository at this point in the history
Fixes redhat-developer/vscode-xml#59

Signed-off-by: Nikolas Komonen <[email protected]>
  • Loading branch information
NikolasKomonen committed Oct 12, 2018
1 parent 0d63a2c commit 929b17d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public boolean hasAttribute(String name) {
* @return true if there are attributes and null otherwise.
*/
public boolean hasAttributes() {
return attributeNodes != null;
return attributeNodes != null && attributeNodes.size() != 0;
}

public void setAttribute(String name, String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private void format(Node node, int level, int end, XMLBuilder xml) {
if (value == null) {
continue;
}
xml.addPrologAttribute(name, value, level);
xml.addSingleAttribute(name, value);
}
}
xml.endPrologOrPI();
Expand Down Expand Up @@ -166,11 +166,18 @@ private void format(Node node, int level, int end, XMLBuilder xml) {
xml.startElement(tag, false);
if (node.hasAttributes()) {
// generate attributes
int attributeIndex = 0;
for (Attr attr : node.getAttributeNodes()) {
String attributeName = attr.getName();
xml.addAttribute(attributeName, attr.getValue(), attributeIndex, level, tag);
attributeIndex++;
List<Attr> attributes = node.getAttributeNodes();
if(attributes.size() == 1) {
Attr singleAttribute = attributes.get(0);
xml.addSingleAttribute(singleAttribute.getName(),singleAttribute.getValue());
}
else {
int attributeIndex = 0;
for (Attr attr : attributes) {
String attributeName = attr.getName();
xml.addAttribute(attributeName, attr.getValue(), attributeIndex, level, tag);
attributeIndex++;
}
}
}
boolean hasElements = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public XMLBuilder endElement() {
return this;
}

public XMLBuilder addPrologAttribute(String name, String value, int level) {
public XMLBuilder addSingleAttribute(String name, String value) {
xml.append(" ");
xml.append(name);
xml.append("=\"");
Expand All @@ -86,7 +86,7 @@ public XMLBuilder addPrologAttribute(String name, String value, int level) {
}

public XMLBuilder addAttribute(String name, String value, int index, int level, String tagName) {
int spaces = 1;
int spaces = 1; //Accounts for '<' if isSplitAttributes() or space for single attribute
if (isSplitAttributes()) {
linefeed();
indent(level);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,43 @@ public void testPI() throws BadLocationException {
format(content, expected);
}

@Test
public void testSplitAttributesSingle() throws BadLocationException {
String content = "<a k1=\"v1\"></a>";
String expected = "<a k1=\"v1\"></a>";
XMLFormattingOptions formattingOptions = createDefaultFormattingOptions();
formattingOptions.setSplitAttributes(true);
format(content, expected, formattingOptions);
}

@Test
public void testSplitAttributes() throws BadLocationException {
String content = "<a k1=\"v1\" k2=\"v2\"></a>";
String expected = "<a" + lineSeparator() + //
" k1=\"v1\"" + lineSeparator() +
String expected =
"<a" + lineSeparator() +
" k1=\"v1\"" + lineSeparator() + //
" k2=\"v2\"></a>";
XMLFormattingOptions formattingOptions = createDefaultFormattingOptions();
formattingOptions.setSplitAttributes(true);
format(content, expected, formattingOptions);
}

@Test
public void testSplitAttributesNested() throws BadLocationException {
String content = "<a k1=\"v1\" k2=\"v2\"><b aa=\"ok\" bb = \"oo\"></b></a>";
String expected =
"<a" + lineSeparator() +
" k1=\"v1\"" + lineSeparator() + //
" k2=\"v2\">" + lineSeparator() +
" <b" + lineSeparator() +
" aa=\"ok\"" + lineSeparator() + //
" bb=\"oo\"></b>" + lineSeparator() +
"</a>";
XMLFormattingOptions formattingOptions = createDefaultFormattingOptions();
formattingOptions.setSplitAttributes(true);
format(content, expected, formattingOptions);
}

@Test
public void testSplitAttributesProlog() throws BadLocationException {
String content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
Expand Down

0 comments on commit 929b17d

Please sign in to comment.