From f79623990b90ac48df312051786b7c4f449e4091 Mon Sep 17 00:00:00 2001 From: David Kwon Date: Thu, 4 Jul 2019 17:28:48 -0400 Subject: [PATCH] Remove indentation when formatting only whitespace Signed-off-by: David Kwon --- .../lsp4xml/services/XMLFormatter.java | 1 - .../org/eclipse/lsp4xml/utils/XMLBuilder.java | 1 - .../lsp4xml/services/XMLFormatterTest.java | 77 ++++++++++++------- 3 files changed, 50 insertions(+), 29 deletions(-) diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/services/XMLFormatter.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/services/XMLFormatter.java index 03e0f7059..21d4bee32 100644 --- a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/services/XMLFormatter.java +++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/services/XMLFormatter.java @@ -311,7 +311,6 @@ private void format(DOMNode node) throws BadLocationException { if (node.hasChildNodes()) { // element has body - // startElementClosed = true; this.indentLevel++; for (DOMNode child : node.getChildren()) { boolean textElement = !child.isText(); diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/utils/XMLBuilder.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/utils/XMLBuilder.java index 1f72159af..c13542820 100644 --- a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/utils/XMLBuilder.java +++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/utils/XMLBuilder.java @@ -242,7 +242,6 @@ else if(hasSiblings) { int newLineCount = StringUtils.getNumberOfNewLines(text, isWhitespaceContent, delimiter, preservedNewLines); for (int i = 0; i < newLineCount - 1; i++) { // - 1 because the node after will insert a delimiter xml.append(delimiter); - this.indent(level); } } diff --git a/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/services/XMLFormatterTest.java b/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/services/XMLFormatterTest.java index f17f36b01..575d09e0a 100644 --- a/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/services/XMLFormatterTest.java +++ b/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/services/XMLFormatterTest.java @@ -1137,11 +1137,11 @@ public void testContentFormatting6() throws BadLocationException { "\r\n" + "\r\n" + " Fred\r\n" + - " \r\n" + + "\r\n" + " Jani\r\n" + - " \r\n" + + "\r\n" + " Reminder\r\n" + - " \r\n" + + "\r\n" + " Don't forget me this weekend\r\n" + ""; format(content, expected, formattingOptions); @@ -1209,10 +1209,10 @@ public void testDoctypeNoInternalSubsetNoNewlines() throws BadLocationException "]>\r\n" + "\r\n" + " Fred\r\n" + - " \r\n" + - " \r\n" + + "\r\n" + + "\r\n" + " Jani\r\n" + - " \r\n" + + "\r\n" + " Reminder\r\n" + " Don't forget me this weekend\r\n" + ""; @@ -1321,7 +1321,7 @@ public void testDoctypeNoInternalSubsetNoNewlines() throws BadLocationException "]>\r\n" + "\r\n" + "\r\n" + - " \r\n" + + "\r\n" + " Fred\r\n" + ""; format(content, expected, formattingOptions); @@ -1536,9 +1536,9 @@ public void testAllDoctypeParameters() throws BadLocationException { "]>\r\n" + "\r\n" + " sdsd\r\n" + - " \r\n" + + "\r\n" + " \r\n" + - " \r\n" + + "\r\n" + " er\r\n" + " dd\r\n" + " \r\n" + @@ -2050,8 +2050,8 @@ public void testPreserveNewlines() throws BadLocationException { String expected = "\r\n" + " \r\n" + - " \r\n" + - " \r\n" + + "\r\n" + + "\r\n" + ""; format(content, expected, formattingOptions); } @@ -2070,9 +2070,9 @@ public void testPreserveNewlines3Max() throws BadLocationException { String expected = "\r\n" + " \r\n" + - " \r\n" + - " \r\n" + - " \r\n" + + "\r\n" + + "\r\n" + + "\r\n" + ""; format(content, expected, formattingOptions); } @@ -2091,8 +2091,8 @@ public void testPreserveNewlines2() throws BadLocationException { String expected = "\r\n" + " \r\n" + - " \r\n" + - " \r\n" + + "\r\n" + + "\r\n" + ""; format(content, expected, formattingOptions); } @@ -2114,11 +2114,11 @@ public void testPreserveNewlinesBothSides() throws BadLocationException { ""; String expected = "\r\n" + - " \r\n" + - " \r\n" + + "\r\n" + + "\r\n" + " \r\n" + - " \r\n" + - " \r\n" + + "\r\n" + + "\r\n" + ""; format(content, expected, formattingOptions); } @@ -2146,14 +2146,14 @@ public void testPreserveNewlinesBothSidesMultipleTags() throws BadLocationExcept ""; String expected = "\r\n" + - " \r\n" + - " \r\n" + + "\r\n" + + "\r\n" + " \r\n" + - " \r\n" + - " \r\n" + + "\r\n" + + "\r\n" + " \r\n" + - " \r\n" + - " \r\n" + + "\r\n" + + "\r\n" + ""; format(content, expected, formattingOptions); } @@ -2169,7 +2169,7 @@ public void testPreserveNewlinesSingleLine() throws BadLocationException { String expected = "\r\n" + " \r\n" + - " \r\n" + + "\r\n" + ""; format(content, expected, formattingOptions); } @@ -2188,6 +2188,29 @@ public void testPreserveNewlines4() throws BadLocationException { format(content, expected, formattingOptions); } + @Test + public void testNoSpacesOnNewLine() throws BadLocationException { + XMLFormattingOptions formattingOptions = createDefaultFormattingOptions(); + String content = + "\r\n" + + " \r\n" + + "\r\n" + + "\r\n" + + " \r\n" + + "\r\n" + + "\r\n" + + " \r\n" + + "\r\n" + + "\r\n" + + ""; + String expected = + "\r\n" + + " \r\n" + + "\r\n" + + "\r\n" + + ""; + format(content, expected, formattingOptions); + } @Test