diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/XMLFormatter.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/XMLFormatter.java index 7948bdf8a..ad81583e0 100644 --- a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/XMLFormatter.java +++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/XMLFormatter.java @@ -658,7 +658,7 @@ private static void addPrologToXMLBuilder(DOMNode node, XMLBuilder xml) { DOMProcessingInstruction processingInstruction = (DOMProcessingInstruction) node; xml.startPrologOrPI(processingInstruction.getTarget()); if (node.hasAttributes()) { - addAttributes(node, xml); + addPrologAttributes(node, xml); } xml.endPrologOrPI(); } @@ -666,15 +666,14 @@ private static void addPrologToXMLBuilder(DOMNode node, XMLBuilder xml) { /** * Will add all attributes, to the given builder, on a single line */ - private static void addAttributes(DOMNode node, XMLBuilder xmlBuilder) { + private static void addPrologAttributes(DOMNode node, XMLBuilder xmlBuilder) { List attrs = node.getAttributeNodes(); if (attrs == null) { return; } for (DOMAttr attr : attrs) { - xmlBuilder.addAttributesOnSingleLine(attr, true); + xmlBuilder.addSingleAttribute(attr, true); } - xmlBuilder.appendSpace(); } } diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/utils/XMLBuilder.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/utils/XMLBuilder.java index 8b92f09c5..513999ca3 100644 --- a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/utils/XMLBuilder.java +++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/utils/XMLBuilder.java @@ -92,7 +92,7 @@ public XMLBuilder closeStartElement() { public XMLBuilder selfCloseElement() { if (formattingOptions.isSpaceBeforeEmptyCloseTag()) { - xml.append(" "); + appendSpace(); } xml.append("/>"); return this; @@ -107,32 +107,37 @@ public XMLBuilder addSingleAttribute(String name, String value) { * * It will not perform any linefeeds and only basic indentation. * - * @param name attribute name - * @param value attribute value - * @return + * @param name attribute name + * @param value attribute value + * @param surroundWithQuotes true if quotes should be added around originalValue + * @return this XML Builder */ public XMLBuilder addSingleAttribute(String name, String value, boolean surroundWithQuotes) { - xml.append(" "); + appendSpace(); addAttributeContents(name, true, value, surroundWithQuotes); - return this; } - public XMLBuilder addAttributesOnSingleLine(DOMAttr attr, boolean surroundWithQuotes) { - xml.append(" "); + /** + * Used when only one attribute is being added to a node. + * + * It will not perform any linefeeds and only basic indentation. + * + * @param attr attribute + * @param surroundWithQuotes true if value should be added with quotes, false + * otherwise + * @return this XML Builder + */ + public XMLBuilder addSingleAttribute(DOMAttr attr, boolean surroundWithQuotes) { + appendSpace(); addAttributeContents(attr.getName(), attr.hasDelimiter(), attr.getValue(), surroundWithQuotes); - return this; } - public XMLBuilder addAttribute(String name, String value, int level) { - return addAttribute(name, value, level, false); - } - /** * Used when you are knowingly adding multiple attributes. * - * It will do linefeeds and indentation. + * Does linefeeds and indentation. * * @param name * @param value @@ -144,7 +149,7 @@ public XMLBuilder addAttribute(String name, String value, int level, boolean sur linefeed(); indent(level + splitAttributesIndent); } else { - xml.append(" "); + appendSpace(); } addAttributeContents(name, true, value, surroundWithQuotes); @@ -160,24 +165,25 @@ public XMLBuilder addAttribute(DOMAttr attr, int level, boolean surroundWithQuot linefeed(); indent(level + splitAttributesIndent); } else { - xml.append(" "); + appendSpace(); } addAttributeContents(attr.getName(), attr.hasDelimiter(), attr.getOriginalValue(), surroundWithQuotes); return this; } - /** - * * Builds the attribute {name, '=', and value}. - * - * Never puts quotes around unquoted values unless indicated to by - * 'surroundWithQuotes' - * - * @param name name of the attribute - * @param equalsSign true if equals sign exists, false otherwise - * @param originalValue value of the attribute - * @param surroundWithQuotes true if value should be added with quotes, false otherwise - */ + /** + * Builds the attribute {name, '=', and value}. + * + * Never puts quotes around unquoted values unless indicated to by + * 'surroundWithQuotes' + * + * @param name name of the attribute + * @param equalsSign true if equals sign exists, false otherwise + * @param originalValue value of the attribute + * @param surroundWithQuotes true if quotes should be added around originalValue, + * false otherwise + */ private void addAttributeContents(String name, boolean equalsSign, String originalValue, boolean surroundWithQuotes) { if (name != null) { @@ -278,7 +284,7 @@ public XMLBuilder indent(int level) { for (int i = 0; i < level; i++) { if (isInsertSpaces()) { for (int j = 0; j < getTabSize(); j++) { - xml.append(" "); + appendSpace(); } } else { xml.append("\t"); @@ -294,9 +300,9 @@ public XMLBuilder startPrologOrPI(String tagName) { } public XMLBuilder addContentPI(String content) { - xml.append(" "); + appendSpace(); xml.append(content); - xml.append(" "); + appendSpace(); return this; } @@ -340,7 +346,7 @@ public XMLBuilder endCDATA() { public XMLBuilder startComment(DOMComment comment) { if (comment.isCommentSameLineEndTag()) { - xml.append(" "); + appendSpace(); } xml.append("