Skip to content

Commit

Permalink
Implement trim trailing whitespaces formatting setting
Browse files Browse the repository at this point in the history
Signed-off-by: David Kwon <dakwon@redhat.com>
  • Loading branch information
dkwon17 authored and angelozerr committed Jun 17, 2020
1 parent 7973e91 commit 981cddd
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class XMLFormattingOptions extends FormattingOptions {
public static final int DEFAULT_TAB_SIZE = 2;
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;

// All possible keys
private static final String SPLIT_ATTRIBUTES = "splitAttributes";
Expand All @@ -38,6 +39,7 @@ public class XMLFormattingOptions extends FormattingOptions {
private static final String JOIN_CONTENT_LINES = "joinContentLines";
private static final String PRESERVED_NEWLINES = "preservedNewlines";
private static final String TRIM_FINAL_NEWLINES = "trimFinalNewlines";
private static final String TRIM_TRAILING_WHITESPACE = "trimTrailingWhitespace";
private static final String ENFORCE_QUOTE_STYLE = "enforceQuoteStyle";
private static final String PRESERVE_ATTR_LINE_BREAKS = "preserveAttributeLineBreaks";

Expand Down Expand Up @@ -296,6 +298,15 @@ public boolean isTrimFinalNewlines() {
return (value == null) ? true: value;
}

public void setTrimTrailingWhitespace(boolean newValue) {
this.putBoolean(TRIM_TRAILING_WHITESPACE, newValue);
}

public boolean isTrimTrailingWhitespace() {
final Boolean value = this.getBoolean(TRIM_TRAILING_WHITESPACE);
return (value == null) ? DEFAULT_TRIM_TRAILING_SPACES: value;
}

public void setEnforceQuoteStyle(EnforceQuoteStyle enforce) {
this.putString(XMLFormattingOptions.ENFORCE_QUOTE_STYLE, enforce.name());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ public XMLBuilder addContent(String text) {
* <code>delimiter</code>
*
* @param text the proposed text to add
* @param isWhitespaceContent whether or not the text contains whitespace content
* @param isWhitespaceContent whether or not the text contains only whitespace content
* @param hasSiblings whether or not the corresponding text node has siblings
* @param delimiter line delimiter
* @return this XMLBuilder with <code>text</code> added depending on
Expand All @@ -274,6 +274,9 @@ public XMLBuilder addContent(String text, boolean isWhitespaceContent, boolean h
} else if (hasSiblings) {
text = text.trim();
}
if (isTrimTrailingWhitespace()) {
text = trimTrailingSpacesEachLine(text);
}
xml.append(text);
} else if (!hasSiblings && isPreserveEmptyContent()) {
xml.append(text);
Expand Down Expand Up @@ -327,7 +330,7 @@ public String toString() {
}

/**
* Trims the trailing newlines for the current xml StringBuilder
* Trims the trailing newlines for the current XML StringBuilder
*/
public void trimFinalNewlines() {
int i = xml.length() - 1;
Expand All @@ -336,6 +339,32 @@ public void trimFinalNewlines() {
}
}

/**
* Returns <code>str</code> with the trailing spaces from each line
* removed
*
* @param str the String
* @return <code>str</code> with the trailing spaces from each line
* removed
*/
private static String trimTrailingSpacesEachLine(String str) {
StringBuilder sb = new StringBuilder(str);
int i = str.length() - 1;
boolean removeSpaces = true;
while (i >= 0) {
char curr = sb.charAt(i);
if (curr == '\n' || curr == '\r') {
removeSpaces = true;
} else if (removeSpaces && Character.isWhitespace(curr)) {
sb.deleteCharAt(i);
} else {
removeSpaces = false;
}
i--;
}
return sb.toString();
}

public XMLBuilder startCDATA() {
xml.append("<![CDATA[");
return this;
Expand Down Expand Up @@ -472,6 +501,10 @@ private boolean isPreserveEmptyContent() {
return sharedSettings.getFormattingSettings().isPreserveEmptyContent();
}

private boolean isTrimTrailingWhitespace() {
return sharedSettings.getFormattingSettings().isTrimTrailingWhitespace();
}

private int getPreservedNewlines() {
return sharedSettings.getFormattingSettings().getPreservedNewlines();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public void rangeSelectEntityNoIndent() throws BadLocationException {
String content = "<?xml version='1.0' standalone='no'?>\r\n" + //
"<!DOCTYPE root-element [\r\n" + //
"|<!ENTITY local \"LOCALLY DECLARED ENTITY\">|\r\n" + //
"]>";
"]>";
String expected = "<?xml version='1.0' standalone='no'?>\r\n" + //
"<!DOCTYPE root-element [\r\n" + //
" <!ENTITY local \"LOCALLY DECLARED ENTITY\">\r\n" + //
Expand All @@ -301,15 +301,15 @@ public void rangeSelectEntityWithIndent() throws BadLocationException {
String content = "<?xml version='1.0' standalone='no'?>\r\n" + //
"<!DOCTYPE root-element [\r\n" + //
" |<!ENTITY local \"LOCALLY DECLARED ENTITY\">|\r\n" + //
"]>";
"]>";
String expected = "<?xml version='1.0' standalone='no'?>\r\n" + //
"<!DOCTYPE root-element [\r\n" + //
" <!ENTITY local \"LOCALLY DECLARED ENTITY\">\r\n" + //
"]>";
format(content, expected);
}


@Test
public void testProlog() throws BadLocationException {
String content = "<?xml version= \"1.0\" encoding=\"UTF-8\" ?>\r\n";
Expand Down Expand Up @@ -2101,6 +2101,57 @@ public void testNoSpacesOnNewLine() throws BadLocationException {
format(content, expected);
}

@Test
public void testTrimTrailingWhitespaceText() throws BadLocationException {
SharedSettings settings = new SharedSettings();
settings.getFormattingSettings().setTrimTrailingWhitespace(true);
String content = "<a> \n" +
"text \n" +
" text text text \n" +
" text\n" +
"</a> ";
String expected = "<a>\n" +
"text\n" +
" text text text\n" +
" text\n" +
"</a>";
format(content, expected, settings);
}

@Test
public void testTrimTrailingWhitespaceNewlines() throws BadLocationException {
SharedSettings settings = new SharedSettings();
settings.getFormattingSettings().setTrimTrailingWhitespace(true);
String content = "<a> \n" +
" \n" +
"</a> ";
String expected = "<a></a>";
format(content, expected, settings);
}

@Test
public void testTrimTrailingWhitespaceTextAndNewlines() throws BadLocationException {
SharedSettings settings = new SharedSettings();
settings.getFormattingSettings().setTrimTrailingWhitespace(true);
String content = "<a> \n" +
" \n" +
"text \n" +
" text text text \n" +
" \n" +
" text\n" +
" \n" +
"</a> ";
String expected = "<a>\n" +
"\n" +
"text\n" +
" text text text\n" +
"\n" +
" text\n" +
"\n" +
"</a>";
format(content, expected, settings);
}

@Test
public void testTrimFinalNewlinesDefault() throws BadLocationException {
String content = "<a ></a>\r\n";
Expand Down Expand Up @@ -2628,7 +2679,7 @@ public void preserveAttributeLineBreaksCollapseEmptyElement() throws BadLocation
"<b attr=\"value\" attr=\"value\"\n" + //
"attr=\"value\" attr=\"value\"\n" + //
"attr=\"value\" attr=\"value\">\n" + //
"</b>\n" +
"</b>\n" +
"</a>";
String expected = "<a>\n" +
" <b attr=\"value\" attr=\"value\"\n" + //
Expand Down

0 comments on commit 981cddd

Please sign in to comment.