From 200a7aa8e39e42ba0809c86f89adeb0e0706178b Mon Sep 17 00:00:00 2001 From: David Thompson Date: Tue, 22 Dec 2020 16:32:55 -0500 Subject: [PATCH] Add new formatting setting `xml.format.splitAttributesIndentSize` Adds a new setting which controls the level of indentation of attributes with respect to their parent element when `xml.format.splitAttributes` is enabled. Based off of a comment by Cong Wang (@I322871) on the vscode-xml Gitter Signed-off-by: David Thompson --- .../settings/XMLFormattingOptions.java | 52 +++++++++++----- .../org/eclipse/lemminx/utils/XMLBuilder.java | 5 +- .../lemminx/services/XMLFormatterTest.java | 59 ++++++++++++++++++- 3 files changed, 96 insertions(+), 20 deletions(-) diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/settings/XMLFormattingOptions.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/settings/XMLFormattingOptions.java index 766b10270..e94d4b219 100644 --- a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/settings/XMLFormattingOptions.java +++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/settings/XMLFormattingOptions.java @@ -17,7 +17,7 @@ /** * This class is the root of all formatting settings. It is necessary to update * this class for any new additions. - * + * * All defaults should be set here to eventually be overridden if needed. */ public class XMLFormattingOptions extends FormattingOptions { @@ -28,6 +28,7 @@ public class XMLFormattingOptions extends FormattingOptions { public static final EnforceQuoteStyle DEFAULT_ENFORCE_QUOTE_STYLE = EnforceQuoteStyle.ignore; public static final boolean DEFAULT_PRESERVE_ATTR_LINE_BREAKS = false; public static final boolean DEFAULT_TRIM_TRAILING_SPACES = false; + public static final int DEFAULT_SPLIT_ATTRIBUTES_INDENT_SIZE = 2; // All possible keys private static final String SPLIT_ATTRIBUTES = "splitAttributes"; @@ -43,51 +44,52 @@ public class XMLFormattingOptions extends FormattingOptions { private static final String ENFORCE_QUOTE_STYLE = "enforceQuoteStyle"; private static final String PRESERVE_ATTR_LINE_BREAKS = "preserveAttributeLineBreaks"; private static final String PRESERVE_EMPTY_CONTENT = "preserveEmptyContent"; + private static final String SPLIT_ATTRIBUTES_INDENT_SIZE = "splitAttributesIndentSize"; /** * Options for formatting empty elements. - * + * * - * + * */ public static enum EmptyElements { expand, collapse, ignore; @@ -125,6 +127,7 @@ private void initializeDefaultSettings() { this.setPreserveEmptyContent(false); this.setPreservedNewlines(DEFAULT_PRESERVER_NEW_LINES); this.setEmptyElement(EmptyElements.ignore); + this.setSplitAttributesIndentSize(DEFAULT_SPLIT_ATTRIBUTES_INDENT_SIZE); } public XMLFormattingOptions(int tabSize, boolean insertSpaces, boolean initializeDefaultSettings) { @@ -284,7 +287,7 @@ public EmptyElements getEmptyElements() { /** * Returns the value of trimFinalNewlines. - * + * * If the trimFinalNewlines does not exist, defaults to true. */ @Override @@ -309,13 +312,13 @@ public void setEnforceQuoteStyle(EnforceQuoteStyle enforce) { public EnforceQuoteStyle getEnforceQuoteStyle() { String value = this.getString(XMLFormattingOptions.ENFORCE_QUOTE_STYLE); EnforceQuoteStyle enforceStyle = null; - + try { enforceStyle = value == null ? null : EnforceQuoteStyle.valueOf(value); } catch (IllegalArgumentException e) { return DEFAULT_ENFORCE_QUOTE_STYLE; } - + return enforceStyle == null ? DEFAULT_ENFORCE_QUOTE_STYLE : enforceStyle; } @@ -328,7 +331,7 @@ public void setPreserveAttrLineBreaks(final boolean preserveAttrLineBreaks) { /** * Returns the value of preserveAttrLineBreaks - * + * * @return the value of preserveAttrLineBreaks */ public boolean isPreserveAttrLineBreaks() { @@ -345,6 +348,25 @@ public boolean isPreserveAttrLineBreaks() { } } + /** + * Sets the value of splitAttributesIndentSize + * + * @param splitAttributesIndentSize the new value for splitAttributesIndentSize + */ + public void setSplitAttributesIndentSize(int splitAttributesIndentSize) { + this.putNumber(SPLIT_ATTRIBUTES_INDENT_SIZE, Integer.valueOf(splitAttributesIndentSize)); + } + + /** + * Returns the value of splitAttributesIndentSize or zero if it was set to a negative value + * + * @return the value of splitAttributesIndentSize or zero if it was set to a negative value + */ + public int getSplitAttributesIndentSize() { + int splitAttributesIndentSize = getNumber(SPLIT_ATTRIBUTES_INDENT_SIZE).intValue(); + return splitAttributesIndentSize < 0 ? 0 : splitAttributesIndentSize; + } + public XMLFormattingOptions merge(FormattingOptions formattingOptions) { formattingOptions.entrySet().stream().forEach(entry -> { this.put(entry.getKey(), entry.getValue()); 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 265cb2c20..11303fee6 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 @@ -35,7 +35,6 @@ public class XMLBuilder { private final String lineDelimiter; private final StringBuilder xml; private final String whitespacesIndent; - private final int splitAttributesIndent = 2; private final Collection formatterParticipants; @@ -174,7 +173,7 @@ public XMLBuilder addPrologAttribute(DOMAttr attr) { public XMLBuilder addAttribute(String name, String value, int level, boolean surroundWithQuotes) { if (isSplitAttributes()) { linefeed(); - indent(level + splitAttributesIndent); + indent(level + sharedSettings.getFormattingSettings().getSplitAttributesIndentSize()); } else { appendSpace(); } @@ -190,7 +189,7 @@ public XMLBuilder addAttribute(DOMAttr attr, int level) { private XMLBuilder addAttribute(DOMAttr attr, int level, boolean surroundWithQuotes) { if (isSplitAttributes()) { linefeed(); - indent(level + splitAttributesIndent); + indent(level + sharedSettings.getFormattingSettings().getSplitAttributesIndentSize()); } else { appendSpace(); } diff --git a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/XMLFormatterTest.java b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/XMLFormatterTest.java index 941782d59..f09c26552 100644 --- a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/XMLFormatterTest.java +++ b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/XMLFormatterTest.java @@ -605,12 +605,12 @@ public void testCDATANotClosed() throws BadLocationException { @Test public void testCDATAWithRange() throws BadLocationException { String content = "\r\n" + // - " |\r\n" + // + " |\r\n" + // " \r\n" + // " ]]>\r\n" + // ""; String expected = "\r\n" + // - " \r\n" + // + " \r\n" + // " \r\n" + // " ]]>\r\n" + // ""; @@ -2850,6 +2850,61 @@ public void preserveAttributeLineBreaksRangeFormattingWithEndTag3() throws BadLo assertFormat(content, expected, settings); } + @Test + public void splitAttributesIndentSize0() throws BadLocationException { + SharedSettings settings = new SharedSettings(); + settings.getFormattingSettings().setSplitAttributes(true); + settings.getFormattingSettings().setSplitAttributesIndentSize(0); + + String content = "\n"; + String expected = ""; + assertFormat(content, expected, settings); + } + + @Test + public void splitAttributesIndentSizeNegative() throws BadLocationException { + SharedSettings settings = new SharedSettings(); + settings.getFormattingSettings().setSplitAttributes(true); + settings.getFormattingSettings().setSplitAttributesIndentSize(-1); + + String content = "\n"; + String expected = ""; + assertFormat(content, expected, settings); + } + + @Test + public void splitAttributesIndentSize1() throws BadLocationException { + SharedSettings settings = new SharedSettings(); + settings.getFormattingSettings().setSplitAttributes(true); + settings.getFormattingSettings().setSplitAttributesIndentSize(1); + + String content = "\n"; + String expected = ""; + assertFormat(content, expected, settings); + } + + @Test + public void splitAttributesIndentSizeDefault() throws BadLocationException { + SharedSettings settings = new SharedSettings(); + settings.getFormattingSettings().setSplitAttributes(true); + + String content = "\n"; + String expected = ""; + assertFormat(content, expected, settings); + } + @Test public void testTemplate() throws BadLocationException { String content = "";