Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new formatting setting xml.format.splitAttributesIndentSize #952

Merged
merged 1 commit into from
Dec 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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";
Expand All @@ -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.
*
*
* <ul>
* <li>{@link #expand} : expand empty elements. With this option the following
* XML:
*
*
* <pre>
* {@code
* <example />
* }
* </pre>
*
*
* will be formatted to :
*
*
* <pre>
* {@code
* <example><example>
* }
* </pre>
*
*
* </li>
* <li>{@link #collapse} : collapse empty elements. With this option the
* following XML:
*
*
* <pre>
* {@code
* <example></example>
* }
* </pre>
*
*
* will be formatted to :
*
*
* <pre>
* {@code
* <example />
* }
* </pre>
*
*
* </li>
* <li>{@link #ignore} : keeps the original XML content for empty elements.
* </li>
* </ul>
*
*
*/
public static enum EmptyElements {
expand, collapse, ignore;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -284,7 +287,7 @@ public EmptyElements getEmptyElements() {

/**
* Returns the value of trimFinalNewlines.
*
*
* If the trimFinalNewlines does not exist, defaults to true.
*/
@Override
Expand All @@ -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;
}

Expand All @@ -328,7 +331,7 @@ public void setPreserveAttrLineBreaks(final boolean preserveAttrLineBreaks) {

/**
* Returns the value of preserveAttrLineBreaks
*
*
* @return the value of preserveAttrLineBreaks
*/
public boolean isPreserveAttrLineBreaks() {
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<IFormatterParticipant> formatterParticipants;

Expand Down Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -605,12 +605,12 @@ public void testCDATANotClosed() throws BadLocationException {
@Test
public void testCDATAWithRange() throws BadLocationException {
String content = "<foo>\r\n" + //
" <![CDATA[ |<bar>|\r\n" + //
" <![CDATA[ |<bar>|\r\n" + //
" </bar>\r\n" + //
" ]]>\r\n" + //
"</foo>";
String expected = "<foo>\r\n" + //
" <![CDATA[ <bar>\r\n" + //
" <![CDATA[ <bar>\r\n" + //
" </bar>\r\n" + //
" ]]>\r\n" + //
"</foo>";
Expand Down Expand Up @@ -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 = "<root a='a' b='b' c='c'/>\n";
String expected = "<root\n" + //
"a='a'\n" + //
"b='b'\n" + //
"c='c' />";
assertFormat(content, expected, settings);
}

@Test
public void splitAttributesIndentSizeNegative() throws BadLocationException {
SharedSettings settings = new SharedSettings();
settings.getFormattingSettings().setSplitAttributes(true);
settings.getFormattingSettings().setSplitAttributesIndentSize(-1);

String content = "<root a='a' b='b' c='c'/>\n";
String expected = "<root\n" + //
"a='a'\n" + //
"b='b'\n" + //
"c='c' />";
assertFormat(content, expected, settings);
}

@Test
public void splitAttributesIndentSize1() throws BadLocationException {
SharedSettings settings = new SharedSettings();
settings.getFormattingSettings().setSplitAttributes(true);
settings.getFormattingSettings().setSplitAttributesIndentSize(1);

String content = "<root a='a' b='b' c='c'/>\n";
String expected = "<root\n" + //
" a='a'\n" + //
" b='b'\n" + //
" c='c' />";
assertFormat(content, expected, settings);
}

@Test
public void splitAttributesIndentSizeDefault() throws BadLocationException {
SharedSettings settings = new SharedSettings();
settings.getFormattingSettings().setSplitAttributes(true);

String content = "<root a='a' b='b' c='c'/>\n";
String expected = "<root\n" + //
" a='a'\n" + //
" b='b'\n" + //
" c='c' />";
assertFormat(content, expected, settings);
}

@Test
public void testTemplate() throws BadLocationException {
String content = "";
Expand Down