From dac1d9f98a95a35977f043c1caa10f12afcdeff9 Mon Sep 17 00:00:00 2001 From: Jessica He Date: Mon, 17 Oct 2022 09:49:20 -0400 Subject: [PATCH] Improve text formatting for experimental formatter Signed-off-by: Jessica He --- .../services/format/DOMTextFormatter.java | 65 ++++++++--- .../XMLFormatterExperimentalIndentTest.java | 35 +++--- .../XMLFormatterExperimentalTest.java | 104 +++++++++++++----- .../experimental/XMLFormatterForDTDTest.java | 30 ++--- .../XMLFormatterJoinCommentLinesTest.java | 28 ++++- .../XMLFormatterJoinContentLinesTest.java | 19 ++-- .../XMLFormatterMaxLineWithTest.java | 17 ++- .../XMLFormatterPreserveEmptyContentTest.java | 21 ++-- .../XMLFormatterPreserveSpacesTest.java | 8 +- .../XMLFormatterWhitespaceSettingTest.java | 8 +- 10 files changed, 224 insertions(+), 111 deletions(-) diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/format/DOMTextFormatter.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/format/DOMTextFormatter.java index aad84fb095..718e53dcda 100644 --- a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/format/DOMTextFormatter.java +++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/format/DOMTextFormatter.java @@ -13,6 +13,7 @@ import java.util.List; +import org.eclipse.lemminx.dom.DOMElement; import org.eclipse.lemminx.dom.DOMText; import org.eclipse.lsp4j.TextEdit; @@ -38,10 +39,13 @@ public void formatText(DOMText textNode, XMLFormattingConstraints parentConstrai FormatElementCategory formatElementCategory = parentConstraints.getFormatElementCategory(); String text = formatterDocument.getText(); int availableLineWidth = parentConstraints.getAvailableLineWidth(); + int indentLevel = parentConstraints.getIndentLevel(); + int maxLineWidth = getMaxLineWidth(); int spaceStart = -1; int spaceEnd = -1; boolean containsNewLine = false; + boolean isEmpty = true; for (int i = textNode.getStart(); i < textNode.getEnd(); i++) { char c = text.charAt(i); @@ -57,28 +61,40 @@ public void formatText(DOMText textNode, XMLFormattingConstraints parentConstrai } } else { // Text content... + isEmpty = false; spaceEnd = i; int contentStart = i; while (i + 1 < textNode.getEnd() && !Character.isWhitespace(text.charAt(i + 1))) { i++; } - int contentEnd = i; - availableLineWidth -= (contentEnd + 1 - contentStart); - - if (formatElementCategory != FormatElementCategory.PreserveSpace - && formatElementCategory != FormatElementCategory.IgnoreSpace) { + int contentEnd = i + 1; + availableLineWidth -= contentEnd - contentStart; + if (formatElementCategory != FormatElementCategory.PreserveSpace) { + if (textNode.getStart() != contentStart && (isJoinContentLines() || !containsNewLine)) { + // Decrement width for normalized space between text content (not done at + // beginning) + availableLineWidth--; + } if (availableLineWidth < 0) { if (spaceStart != -1) { - insertLineBreak(spaceStart, contentStart, edits); - availableLineWidth = getMaxLineWidth() - (contentEnd - contentStart + 1); + replaceLeftSpacesWithIndentation(indentLevel, spaceStart, contentStart, + true, edits); + availableLineWidth = maxLineWidth - (contentEnd - contentStart) + - indentLevel * getTabSize(); + containsNewLine = false; } - } else if (isJoinContentLines() || (spaceStart == textNode.getStart() || !containsNewLine)) { + } else if (isJoinContentLines() || !containsNewLine) { // Case of isJoinContent == true: join all text content with single space // Case of isJoinContent == false: normalize space only between element start // tag and start of text content or doesn't contain a new line - replaceSpacesWithOneSpace(spaceStart, spaceEnd-1, edits); + replaceSpacesWithOneSpace(spaceStart, spaceEnd - 1, edits); containsNewLine = false; - availableLineWidth --; + } else if (containsNewLine) { + replaceLeftSpacesWithIndentation(indentLevel, spaceStart, spaceEnd, + true, edits); + containsNewLine = false; + availableLineWidth = maxLineWidth - (contentEnd - contentStart) + - indentLevel * getTabSize(); } else { availableLineWidth -= spaceEnd - spaceStart; } @@ -89,8 +105,24 @@ public void formatText(DOMText textNode, XMLFormattingConstraints parentConstrai } } if (formatElementCategory != FormatElementCategory.PreserveSpace - && formatElementCategory != FormatElementCategory.IgnoreSpace) { - replaceSpacesWithOneSpace(spaceStart, spaceEnd, edits); + && formatElementCategory != FormatElementCategory.IgnoreSpace && spaceEnd + 1 != text.length()) { + DOMElement parentElement = textNode.getParentElement(); + // Don't format final spaces if text is at the end of the file + if (!containsNewLine || isEmpty && parentElement.getChildNodes().getLength() == 1 + || isJoinContentLines()) { + // Replace spaces with single space in the case of: + // 1. there is no new line + // 2. the text content is empty and is the only child in the parent element tag + // 3. isJoinContentLines + replaceSpacesWithOneSpace(spaceStart, spaceEnd, edits); + } else { + if (formatElementCategory == FormatElementCategory.NormalizeSpace + || parentElement.getLastChild() == textNode) { + // Decrement indent level if is mixed content and text content is the last child + indentLevel--; + } + replaceLeftSpacesWithIndentation(indentLevel, spaceStart, spaceEnd + 1, true, edits); + } } } @@ -102,8 +134,8 @@ private int getMaxLineWidth() { return formatterDocument.getMaxLineWidth(); } - private void insertLineBreak(int start, int end, List edits) { - formatterDocument.insertLineBreak(start, end, edits); + private int getTabSize() { + return formatterDocument.getSharedSettings().getFormattingSettings().getTabSize(); } private boolean isPreserveEmptyContent() { @@ -114,6 +146,11 @@ private void replaceSpacesWithOneSpace(int spaceStart, int spaceEnd, List edits) { + return formatterDocument.replaceLeftSpacesWithIndentation(indentLevel, from, to, addLineSeparator, edits); + } + private boolean isJoinContentLines() { return formatterDocument.getSharedSettings().getFormattingSettings().isJoinContentLines(); } diff --git a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterExperimentalIndentTest.java b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterExperimentalIndentTest.java index d75af1abe9..1b63611101 100644 --- a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterExperimentalIndentTest.java +++ b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterExperimentalIndentTest.java @@ -147,17 +147,17 @@ public void multipleRootNestedIssue634() throws BadLocationException { " \r\n" + // ""; String expected = "\r\n" + // - " test \r\n" + // + " \r\n" + // + " test\r\n" + // + " \r\n" + // "\r\n" + // "\r\n" + // - " test \r\n" + // + " \r\n" + // + " test\r\n" + // + " \r\n" + // ""; assertFormat(content, expected, // - te(1, 9, 2, 4, " "), - te(2, 8, 3, 2, " "), - te(4, 9, 4, 9, "\r\n"), - te(5, 9, 6, 4, " "), - te(6, 8, 7, 2, " ")); + te(4, 9, 4, 9, "\r\n")); assertFormat(expected, expected); } @@ -212,14 +212,8 @@ public void multipleLineContentIssue600JoinContentLinesFalse() throws BadLocatio " bar\r\n" + // " \r\n" + // ""; - String expected = "\r\n" + // - " foo\r\n" + // - " bar \r\n" + // - ""; - assertFormat(content, expected, settings, // - te(1, 5, 2, 4, " "), - te(3, 7, 4, 2, " ")); - assertFormat(expected, expected, settings); + String expected = content; + assertFormat(content, expected, settings); } // From issue: https://github.com/redhat-developer/vscode-xml/issues/600 @@ -286,15 +280,18 @@ public void xsDocumentationTextContentIssue662JoinContentFalse() throws BadLocat String expected = "\r\n" + // " \r\n" + // " \r\n" + // - " Content that spans\r\n" + // - " multiple lines. \r\n" + // + " \r\n" + // + " Content that spans\r\n" + // + " multiple lines.\r\n" + // + " \r\n" + // " \r\n" + // " \r\n" + // ""; assertFormat(content, expected, settings, // te(2, 19, 3, 11, "\r\n "), // - te(3, 29, 4, 4, " "), // - te(5, 19, 6, 0, " "), // + te(3, 29, 4, 4, "\r\n "), // + te(4, 22, 5, 4, "\r\n "), // + te(5, 19, 6, 0, "\r\n "), // te(6, 19, 7, 11, "\r\n "), // te(7, 27, 8, 11, "\r\n ")); assertFormat(expected, expected, settings); diff --git a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterExperimentalTest.java b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterExperimentalTest.java index 779e0bdfb3..f48d9e1506 100644 --- a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterExperimentalTest.java +++ b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterExperimentalTest.java @@ -89,6 +89,7 @@ public void singleEndTag() throws BadLocationException { @Test public void invalidEndTag() throws BadLocationException { + String content = "\n" + // + " Content " + + // + ""; + String expected = "\n" + // + " Content "; + SharedSettings settings = new SharedSettings(); + assertFormat(content, expected, settings, // + te(1, 78, 1, 79, "\n "), // + te(1, 150, 1, 151, "\n ")); + assertFormat(expected, expected, settings); + } + + @Test + public void testCommentLongWrapJoinContentLines() throws BadLocationException { String content = "\n" + // " Content " + // @@ -704,6 +722,7 @@ public void testCommentLongWrap() throws BadLocationException { " comment comment comment comment comment comment comment comment comment\n" + // " comment comment comment comment comment comment comment comment -->"; SharedSettings settings = new SharedSettings(); + settings.getFormattingSettings().setJoinContentLines(true); assertFormat(content, expected, settings, // te(0, 3, 1, 2, " "), // te(1, 78, 1, 79, "\n "), // @@ -713,20 +732,40 @@ public void testCommentLongWrap() throws BadLocationException { @Test public void testCommentLongTextContentWrap() throws BadLocationException { + String content = " content content content content content content content content content content content content content content content content content content \n" + + // + ""; + String expected = " content content content content content content content content content\n" + // + " content content content content content content content content content \n" + // + ""; + SharedSettings settings = new SharedSettings(); + assertFormat(content, expected, settings, // + te(0, 75, 0, 76, "\n "), // + te(0, 152, 0, 153, "\n "), + te(0, 224, 0, 225, "\n "), // + te(0, 296, 0, 297, "\n ")); + assertFormat(expected, expected, settings); + } + + @Test + public void testCommentLongTextContentWrapNewLine() throws BadLocationException { String content = "\n" + // " content content content content content content content content content content content content content content content content content content \n" + // ""; - String expected = " content content content content content content content content content\n" + // - "content content content content content content content content content \n" + // ""; SharedSettings settings = new SharedSettings(); assertFormat(content, expected, settings, // - te(0, 3, 1, 2, " "), // - te(1, 73, 1, 74, "\n"), // + te(1, 73, 1, 74, "\n "), // te(1, 150, 1, 151, "\n "), te(1, 222, 1, 223, "\n "), // te(1, 294, 1, 295, "\n ")); @@ -768,19 +807,22 @@ public void testElementContentNotNormalized() throws BadLocationException { " Content4\r" + // " Content5\r" + // ""; - String expected = " Content\r" + // - " Content2\r" + // - " Content3\r" + // - " Content4\r" + // - " Content5 "; + String expected = "\r" + // + " Content\r" + // + " Content2\r" + // + " Content3\r" + // + " Content4\r" + // + " Content5\r" + // + ""; assertFormat(content, expected, // - te(0, 3, 1, 1, " "), // - te(5, 10, 6, 0, " ")); + te(0, 3, 1, 1, "\r "), // + te(1, 8, 2, 5, "\r "), // + te(2, 13, 3, 6, "\r "), + te(3, 14, 4, 1, "\r ")); assertFormat(expected, expected); } - @Disabled @Test public void testContentFormatting2() throws BadLocationException { String content = "\r" + // @@ -793,31 +835,40 @@ public void testContentFormatting2() throws BadLocationException { String expected = "\r" + // " Content\r" + // " \r" + // - " Content2\r" + // + " Content2\r" + // " Content3\r" + // - " \r" + // + " \r" + // ""; - assertFormat(content, expected); + assertFormat(content, expected, // + te(0, 3, 1, 1, "\r "), // + te(1, 8, 2, 1, "\r "), // + te(2, 4, 3, 3, "\r "), + te(4, 12, 5, 1, "\r ")); + assertFormat(expected, expected); } - @Disabled @Test public void testContentFormattingDontMoveEndTag() throws BadLocationException { String content = "\r" + // " Content\r" + // " \r" + // " Content2\r" + // - " Content3 \r" + // + " Content3 \r" + // ""; String expected = "\r" + // " Content\r" + // " \r" + // - " Content2\r" + // + " Content2\r" + // " Content3 \r" + // ""; - assertFormat(content, expected); + assertFormat(content, expected, // + te(0, 3, 1, 1, "\r "), // + te(1, 8, 2, 1, "\r "), // + te(2, 4, 3, 3, "\r "), + te(4, 12, 4, 17, " ")); + assertFormat(expected, expected); } @Test @@ -828,7 +879,6 @@ public void testContentFormatting3() throws BadLocationException { assertFormat(content, expected); } - @Disabled @Test public void testContentFormatting6() throws BadLocationException { String content = "\r" + // @@ -836,20 +886,22 @@ public void testContentFormatting6() throws BadLocationException { " Content\r" + // ""; String expected = "\r" + // - "\r" + // - " Content\r" + // + " Content\r" + // ""; - assertFormat(content, expected); + assertFormat(content, expected, // + te(0, 3, 2, 1, "\r ")); + assertFormat(expected, expected); content = "\r\n" + // "\r\n" + // " Content\r\n" + // ""; expected = "\r\n" + // - "\r\n" + // - " Content\r\n" + // + " Content\r\n" + // ""; - assertFormat(content, expected); + assertFormat(content, expected, // + te(0, 3, 2, 1, "\r\n ")); + assertFormat(expected, expected); } private static void assertFormat(String unformatted, String actual, TextEdit... expectedEdits) diff --git a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterForDTDTest.java b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterForDTDTest.java index d3e0c5cb8d..e23045faef 100644 --- a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterForDTDTest.java +++ b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterForDTDTest.java @@ -346,17 +346,18 @@ public void testDoctypeInternalWithText() throws BadLocationException { "]>"; String expected = "\r\n" + // - "\r\n" + // - "\r\n" + // " garbageazg df\r\n" + // - " gdf\r\n" + // - "garbageazgdfg\r\n" + // + " gdf\r\n" + // + " garbageazgdfg\r\n" + // " df\r\n" + // " gd\r\n" + // " \r\n" + // "]>"; assertFormat(content, expected, settings, // te(0, 14, 1, 0, " "), // + te(2, 40, 5, 2, "\r\n "), // + te(5, 15, 6, 16, "\r\n "), // + te(6, 19, 7, 0, "\r\n "), // te(9, 4, 13, 2, "\r\n "), // te(13, 40, 15, 0, "\r\n")); assertFormat(expected, expected, settings); @@ -439,15 +440,16 @@ public void testDTDUnknownDeclNameAndText() throws BadLocationException { ""; String expected = "\r\n" + // "\r\n" + // - "\r\n" + // - " asdasd\r\n" + // - " asd\r\n" + // + "asdasd\r\n" + // + "asd\r\n" + // "\r\n" + // "\r\n" + // ""; assertFormat(content, expected, settings, // te(0, 38, 2, 2, "\r\n"), // te(2, 20, 2, 21, ""), // + te(2, 22, 4, 2, "\r\n"), // + te(4, 8, 5, 2, "\r\n"), // te(5, 5, 7, 0, "\r\n")); assertFormat(expected, expected, settings); } @@ -647,8 +649,12 @@ public void testHTMLDTD() throws BadLocationException { "\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" + // @@ -659,11 +665,9 @@ public void testHTMLDTD() throws BadLocationException { te(6, 62, 7, 12, " "), // te(7, 54, 8, 4, "\r\n"), // te(8, 10, 9, 4, "\r\n "), // - te(9, 10, 10, 4, " "), // - te(10, 7, 11, 4, " "), // + te(10, 7, 11, 4, "\r\n "), // te(11, 11, 12, 4, "\r\n "), // - te(12, 14, 13, 4, " "), // - te(13, 7, 14, 4, " "), // + te(13, 7, 14, 4, "\r\n "), // te(14, 15, 15, 4, "\r\n"), // te(16, 3, 18, 0, "\r\n")); } diff --git a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterJoinCommentLinesTest.java b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterJoinCommentLinesTest.java index fc794a1540..10893db944 100644 --- a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterJoinCommentLinesTest.java +++ b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterJoinCommentLinesTest.java @@ -90,20 +90,36 @@ public void testJoinCommentLinesNested() throws BadLocationException { @Test public void testCommentFormatSameLine() throws BadLocationException { - String content = "" + lineSeparator() + // - " Content" + lineSeparator() + // + String content = "\n" + // + " Content\n" + // " "; - String expected = " Content "; + String expected = "\n" + // + " Content\n" + // + " "; + SharedSettings settings = new SharedSettings(); + settings.getFormattingSettings().setJoinCommentLines(true); + assertFormat(content, expected, settings, // + te(0, 3, 1, 1, "\n "), // + te(2, 12, 2, 15, " "), // + te(2, 22, 2, 25, " ")); + assertFormat(expected, expected, settings); + } + @Test + public void testCommentFormatSameLineJoinContentLines() throws BadLocationException { + String content = "\n" + // + " Content\n" + // + " "; + String expected = " Content "; SharedSettings settings = new SharedSettings(); settings.getFormattingSettings().setJoinCommentLines(true); + settings.getFormattingSettings().setJoinContentLines(true); assertFormat(content, expected, settings, // te(0, 3, 1, 1, " "), // te(1, 8, 2, 0, " "), // te(2, 12, 2, 15, " "), // te(2, 22, 2, 25, " ")); assertFormat(expected, expected, settings); - } @Test @@ -112,13 +128,13 @@ public void testJoinCommentLinesLongWrap() throws BadLocationException { " Content " + // ""; - String expected = " Content "; SharedSettings settings = new SharedSettings(); settings.getFormattingSettings().setJoinCommentLines(true); assertFormat(content, expected, settings, // - te(0, 3, 1, 2, " "), // te(1, 78, 1, 79, "\n "), // te(1, 150, 1, 151, "\n ")); assertFormat(expected, expected, settings); diff --git a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterJoinContentLinesTest.java b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterJoinContentLinesTest.java index b776712a69..d03fefaff2 100644 --- a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterJoinContentLinesTest.java +++ b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterJoinContentLinesTest.java @@ -78,10 +78,12 @@ public void testJoinContentLinesFalse() throws BadLocationException { String content = "\n" + // " zz \n" + // " zz "; - String expected = " zz \n" + // - " zz "; + String expected = "\n" + // + " zz\n" + // + " zz "; assertFormat(content, expected, settings, // - te(0, 3, 1, 3, " "), + te(0, 3, 1, 3, "\n "), + te(1, 5, 2, 3, "\n "), te(2, 5, 2, 7, " ")); assertFormat(expected, expected, settings); } @@ -135,12 +137,15 @@ public void testJoinContentLinesWithSiblingElementFalse() throws BadLocationExce " zz \n" + // " \n" + // ""; - String expected = " zz \n" + // - " zz \n" + // + String expected = "\n" + // + " zz\n" + // + " zz\n" + // + " \n" + // ""; assertFormat(content, expected, settings, // - te(0, 3, 1, 3, " "), - te(2, 5, 3, 3, " "), + te(0, 3, 1, 3, "\n "), + te(1, 5, 2, 3, "\n "), + te(2, 5, 3, 3, "\n "), te(3, 6, 3, 8, " "), te(3, 12, 4, 0, "\n")); assertFormat(expected, expected, settings); diff --git a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterMaxLineWithTest.java b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterMaxLineWithTest.java index 5448a74c33..54eaf08571 100644 --- a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterMaxLineWithTest.java +++ b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterMaxLineWithTest.java @@ -31,9 +31,9 @@ public void splitText() throws BadLocationException { String content = "abcde fghi"; String expected = "abcde" + // System.lineSeparator() + // - "fghi"; + " fghi"; assertFormat(content, expected, 6, // - te(0, 8, 0, 9, System.lineSeparator())); + te(0, 8, 0, 9, System.lineSeparator() + " ")); assertFormat(expected, expected, 6); } @@ -42,9 +42,9 @@ public void splitMixedText() throws BadLocationException { String content = " efgh"; String expected = "" + // System.lineSeparator() + // - "efgh"; + " efgh"; assertFormat(content, expected, 5, // - te(0, 8, 0, 9, System.lineSeparator())); + te(0, 8, 0, 9, System.lineSeparator() + " ")); assertFormat(expected, expected, 5); } @@ -65,12 +65,11 @@ public void longText() throws BadLocationException { ""; String expected = "\r\n" + // " \r\n" + // - "vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv \r\n" - + // + " vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv \r\n" + // ""; assertFormat(content, expected, 20, // te(0, 5, 1, 1, "\r\n "), // - te(1, 7, 2, 2, "\r\n"), // + te(1, 7, 2, 2, "\r\n "), // te(2, 102, 3, 1, " ")); assertFormat(expected, expected, 20); } @@ -103,7 +102,7 @@ public void complex() throws BadLocationException { " \r\n" + // " \r\n" + // " Apache Ant (all-in-one) ffffffffffffffffff fffffffffffffffffffffffff\r\n" + // - "ggggggggggggggg\r\n" + // + " ggggggggggggggg\r\n" + // " scm:git:git.eclipse.org:/gitroot/orbit/recipes.git\r\n" + // " apache-parent/ant/org.apache.ant\r\n" + // " \r\n" + // @@ -119,7 +118,7 @@ public void complex() throws BadLocationException { te(1, 7, 1, 9, " "), // te(4, 18, 4, 30, " "), // te(4, 65, 4, 73, " "), // - te(4, 98, 4, 102, "\r\n"), // + te(4, 98, 4, 102, "\r\n "), // te(9, 18, 10, 8, " "), // te(11, 20, 14, 8, " ")); assertFormat(expected, expected, 80); diff --git a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterPreserveEmptyContentTest.java b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterPreserveEmptyContentTest.java index 709c3d2544..ec1877ed54 100644 --- a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterPreserveEmptyContentTest.java +++ b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterPreserveEmptyContentTest.java @@ -17,7 +17,6 @@ import org.eclipse.lemminx.commons.BadLocationException; import org.eclipse.lemminx.settings.SharedSettings; import org.eclipse.lsp4j.TextEdit; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; /** @@ -75,12 +74,12 @@ public void testPreserveTextContent2() throws BadLocationException { settings.getFormattingSettings().setPreserveEmptyContent(false); String content = "\n" + // - " aaa " + // - ""; - String expected = " aaa "; + " aaa "; + String expected = "\n" + // + " aaa "; assertFormat(content, expected, settings, // - te(0, 3, 1, 3, " "), // - te(1, 6, 1, 8, " ")); + te(0, 3, 1, 3, "\n "), // + te(1, 6, 1, 9, " ")); assertFormat(expected, expected, settings); } @@ -122,9 +121,10 @@ public void testDontPreserveEmptyContentTagWithSiblingContent() throws BadLocati String content = "\n" + // " zz tt "; - String expected = " zz tt "; + String expected = "\n" + // + " zz tt "; assertFormat(content, expected, settings, // - te(0, 3, 1, 3, " "), // + te(0, 3, 1, 3, "\n "), // te(1, 5, 1, 10, " "), // te(1, 13, 1, 15, " "), // te(1, 21, 1, 26, " ")); @@ -173,10 +173,11 @@ public void testDontPreserveEmptyContentTagWithSiblingWithComment() throws BadLo String content = "\n" + // " zz tt "; - String expected = " zz tt \n" + // + String expected = "\n" + // + " zz tt \n" + // ""; assertFormat(content, expected, settings, // - te(0, 3, 1, 3, " "), // + te(0, 3, 1, 3, "\n "), // te(1, 5, 1, 9, " "), // te(1, 12, 1, 14, " "), // te(1, 37, 1, 42, "\n")); diff --git a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterPreserveSpacesTest.java b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterPreserveSpacesTest.java index 306d30b6cb..1e5dba03e7 100644 --- a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterPreserveSpacesTest.java +++ b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterPreserveSpacesTest.java @@ -54,17 +54,17 @@ public void preserveSpacesWithXmlSpace2() throws BadLocationException { " \r\n" + // ""; String expected = "\r\n" + // - " c e \r\n" + // + " \r\n" + // + " c e\r\n" + // + " \r\n" + // " \r\n" + // " c e\r\n" + // " \r\n" + // ""; assertFormat(content, expected, // - te(1, 5, 2, 4, " "), // te(2, 5, 2, 7, " "), // - te(2, 14, 2, 16, " "), // - te(2, 17, 3, 2, " ")); + te(2, 14, 2, 16, " ")); assertFormat(expected, expected); } diff --git a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterWhitespaceSettingTest.java b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterWhitespaceSettingTest.java index cf585a4319..f2791306e3 100644 --- a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterWhitespaceSettingTest.java +++ b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterWhitespaceSettingTest.java @@ -173,11 +173,13 @@ public void testDontTrimFinalNewLines3() throws BadLocationException { " more text \r\n" + // " \r\n"; String expected = "\r\n" + // - " text \r\n" + // - " more text \r\n" + // + "text\r\n" + // + "more text \r\n" + // " \r\n"; assertFormat(content, expected, settings, // - te(0, 2, 0, 4, "")); + te(0, 2, 0, 4, ""), // + te(0, 9, 1, 2, "\r\n"), // + te(1, 6, 2, 2, "\r\n")); assertFormat(expected, expected, settings); }