From be6d79d113336e7744ed31080e6cea6ad3e284f0 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/DOMElementFormatter.java | 38 ++-- .../services/format/DOMTextFormatter.java | 70 ++++++-- .../format/XMLFormattingConstraints.java | 10 ++ .../xsi/XSIFormatterExperimentalTest.java | 40 +++-- .../XMLFormatterExperimentalIndentTest.java | 46 +++-- .../XMLFormatterExperimentalTest.java | 104 ++++++++--- .../experimental/XMLFormatterForDTDTest.java | 30 ++-- .../XMLFormatterJoinCommentLinesTest.java | 28 ++- .../XMLFormatterJoinContentLinesTest.java | 24 ++- .../XMLFormatterMaxLineWithTest.java | 165 +++++++++++++++++- ...matterPreserveAttributeLineBreaksTest.java | 6 +- .../XMLFormatterPreserveEmptyContentTest.java | 26 +-- .../XMLFormatterPreserveSpacesTest.java | 8 +- .../XMLFormatterWhitespaceSettingTest.java | 8 +- 14 files changed, 455 insertions(+), 148 deletions(-) diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/format/DOMElementFormatter.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/format/DOMElementFormatter.java index 23ffc9b6d5..e1c45efc60 100644 --- a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/format/DOMElementFormatter.java +++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/format/DOMElementFormatter.java @@ -46,6 +46,12 @@ public void formatElement(DOMElement element, XMLFormattingConstraints parentCon int indentLevel = parentConstraints.getIndentLevel(); int nb = formatStartTagElement(element, parentConstraints, emptyElements, start, end, edits); + // Set indent level for text in mixed content + int mixedIndentLevel = parentConstraints.getMixedContentIndentLevel(); + if (mixedIndentLevel == 0 && parentConstraints.getFormatElementCategory() == FormatElementCategory.MixedContent){ + parentConstraints.setMixedContentIndentLevel(indentLevel); + } + if (emptyElements == EmptyElements.ignore) { // Format children of the element XMLFormattingConstraints constraints = new XMLFormattingConstraints(); @@ -54,14 +60,14 @@ public void formatElement(DOMElement element, XMLFormattingConstraints parentCon constraints.setIndentLevel(indentLevel + 1); } constraints.setFormatElementCategory(getFormatElementCategory(element, parentConstraints)); - constraints.setAvailableLineWidth(getMaxLineWidth() - nb); formatChildren(element, constraints, start, end, edits); // Format end tag element with proper indentation if (element.hasEndTag()) { - formatEndTagElement(element, parentConstraints, constraints, edits); + nb = formatEndTagElement(element, parentConstraints, constraints, edits); } + parentConstraints.setAvailableLineWidth(constraints.getAvailableLineWidth() - nb); } } @@ -92,7 +98,7 @@ private int formatStartTagElement(DOMElement element, XMLFormattingConstraints p int parentStartCloseOffset = element.getParentElement().getStartTagCloseOffset() + 1; if (parentStartCloseOffset != startTagOpenOffset && StringUtils.isWhitespace(formatterDocument.getText(), parentStartCloseOffset, - startTagOpenOffset)) { + startTagOpenOffset)) { int nbSpaces = replaceLeftSpacesWithIndentation(indentLevel, parentStartCloseOffset, startTagOpenOffset, !addLineSeparator, edits); width = element.getTagName() != null ? nbSpaces + element.getTagName().length() + 1 : nbSpaces; @@ -100,12 +106,13 @@ private int formatStartTagElement(DOMElement element, XMLFormattingConstraints p width -= formatterDocument.getLineDelimiter().length(); } } + width = element.getTagName() != null ? element.getTagName().length() + 1 : 0; break; case IgnoreSpace: // If preserve new lines int preservedNewLines = getPreservedNewlines(); int currentNewLineCount = XMLFormatterDocumentNew.getExistingNewLineCount(formatterDocument.getText(), - startTagOpenOffset, + startTagOpenOffset, formatterDocument.getLineDelimiter()); if (currentNewLineCount > preservedNewLines) { replaceLeftSpacesWithIndentationWithMultiNewLines(indentLevel, 0, startTagOpenOffset, @@ -119,8 +126,10 @@ private int formatStartTagElement(DOMElement element, XMLFormattingConstraints p width -= formatterDocument.getLineDelimiter().length(); } } + parentConstraints.setAvailableLineWidth(getMaxLineWidth()); + break; case NormalizeSpace: - width++; + width = element.getTagName() != null ? element.getTagName().length() + 1 : 0; break; } parentConstraints.setAvailableLineWidth(parentConstraints.getAvailableLineWidth() - width); @@ -128,6 +137,7 @@ private int formatStartTagElement(DOMElement element, XMLFormattingConstraints p formatAttributes(element, parentConstraints, edits); boolean formatted = false; + width = 0; switch (emptyElements) { case expand: { if (element.isSelfClosed()) { @@ -181,6 +191,7 @@ private int formatStartTagElement(DOMElement element, XMLFormattingConstraints p break; } default: + width++; } if (!formatted) { @@ -189,6 +200,7 @@ private int formatStartTagElement(DOMElement element, XMLFormattingConstraints p } } } + parentConstraints.setAvailableLineWidth(parentConstraints.getAvailableLineWidth() - width); return width; } @@ -285,6 +297,9 @@ private int formatEndTagElement(DOMElement element, XMLFormattingConstraints par int endTagOffset = element.getEndTagOpenOffset(); int startTagCloseOffset = element.getStartTagCloseOffset(); + int width = 0; + int nbSpaces = 0; + switch (formatElementCategory) { case PreserveSpace: // Preserve existing spaces @@ -296,26 +311,28 @@ private int formatEndTagElement(DOMElement element, XMLFormattingConstraints par if ((element.getLastChild().isElement() || element.getLastChild().isComment()) && Character.isWhitespace(formatterDocument.getText().charAt(endTagOffset - 1)) && !isPreserveEmptyContent()) { - replaceLeftSpacesWithIndentation(indentLevel, startTagCloseOffset, endTagOffset, true, + nbSpaces = replaceLeftSpacesWithIndentation(indentLevel, startTagCloseOffset, endTagOffset, true, edits); } + width = element.getTagName() != null ? nbSpaces + element.getTagName().length() + 2 : nbSpaces; break; case IgnoreSpace: // If preserve new lines int preservedNewLines = getPreservedNewlines(); int currentNewLineCount = XMLFormatterDocumentNew.getExistingNewLineCount(formatterDocument.getText(), - endTagOffset, - formatterDocument.getLineDelimiter()); + endTagOffset, formatterDocument.getLineDelimiter()); if (currentNewLineCount > preservedNewLines) { replaceLeftSpacesWithIndentationWithMultiNewLines(indentLevel, startTagCloseOffset, endTagOffset, preservedNewLines + 1, edits); } else { // remove spaces and indent - replaceLeftSpacesWithIndentation(indentLevel, startTagCloseOffset, endTagOffset, true, + nbSpaces = replaceLeftSpacesWithIndentation(indentLevel, startTagCloseOffset, endTagOffset, true, edits); + width = element.getTagName() != null ? nbSpaces + element.getTagName().length() + 2 : nbSpaces; break; } case NormalizeSpace: + width = element.getTagName() != null ? element.getTagName().length() + 2 : 0; break; } // 2) remove some spaces between the end tag and and close bracket @@ -324,8 +341,9 @@ private int formatEndTagElement(DOMElement element, XMLFormattingConstraints par if (element.isEndTagClosed()) { int endTagCloseOffset = element.getEndTagCloseOffset(); removeLeftSpaces(element.getEndTagOpenOffset(), endTagCloseOffset, edits); + width++; } - return 0; + return width; } /** 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..f7941c6171 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,43 @@ 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 && availableLineWidth >= 0 + && (isJoinContentLines() || !containsNewLine)) { + // Decrement width for normalized space between text content (not done at + // beginning) + availableLineWidth--; + } if (availableLineWidth < 0) { + int mixedContentIndentLevel = parentConstraints.getMixedContentIndentLevel() == 0 ? indentLevel + : parentConstraints.getMixedContentIndentLevel(); if (spaceStart != -1) { - insertLineBreak(spaceStart, contentStart, edits); - availableLineWidth = getMaxLineWidth() - (contentEnd - contentStart + 1); + replaceLeftSpacesWithIndentation(mixedContentIndentLevel, spaceStart, contentStart, + true, edits); + availableLineWidth = maxLineWidth - (contentEnd - contentStart) + - mixedContentIndentLevel * 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; + } else if (containsNewLine) { + replaceLeftSpacesWithIndentation(indentLevel, spaceStart, spaceEnd, + true, edits); containsNewLine = false; - availableLineWidth --; + availableLineWidth = maxLineWidth - (contentEnd - contentStart) + - indentLevel * getTabSize(); } else { availableLineWidth -= spaceEnd - spaceStart; } @@ -89,8 +108,26 @@ 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 || isJoinContentLines()) { + // Replace spaces with single space in the case of: + // 1. there is no new line + // 2. isJoinContentLines + replaceSpacesWithOneSpace(spaceStart, spaceEnd, edits); + if (spaceStart != -1) { + availableLineWidth--; + parentConstraints.setAvailableLineWidth(availableLineWidth); + } + } 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 +139,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 +151,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/main/java/org/eclipse/lemminx/services/format/XMLFormattingConstraints.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/format/XMLFormattingConstraints.java index 17ad8f33b7..2ced2afc37 100644 --- a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/format/XMLFormattingConstraints.java +++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/format/XMLFormattingConstraints.java @@ -23,6 +23,7 @@ public class XMLFormattingConstraints { private int availableLineWidth = 0; private int indentLevel = 0; + private int mixedContentIndentLevel = 0; /** * Initializes the values in this formatting constraint with values from @@ -34,6 +35,7 @@ public void copyConstraints(XMLFormattingConstraints constraints) { setFormatElementCategory(constraints.getFormatElementCategory()); setAvailableLineWidth(constraints.getAvailableLineWidth()); setIndentLevel(constraints.getIndentLevel()); + setMixedContentIndentLevel(constraints.getMixedContentIndentLevel()); } public FormatElementCategory getFormatElementCategory() { @@ -60,4 +62,12 @@ public void setIndentLevel(int indentLevel) { this.indentLevel = indentLevel; } + public int getMixedContentIndentLevel() { + return mixedContentIndentLevel; + } + + public void setMixedContentIndentLevel(int mixedContentIndentLevel) { + this.mixedContentIndentLevel = mixedContentIndentLevel; + } + } diff --git a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/xsi/XSIFormatterExperimentalTest.java b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/xsi/XSIFormatterExperimentalTest.java index d9e879f151..f6ca043b40 100644 --- a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/xsi/XSIFormatterExperimentalTest.java +++ b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/xsi/XSIFormatterExperimentalTest.java @@ -206,11 +206,13 @@ public void xsiSchemaLocationSplitOnPairWithSplitAttribute() throws BadLocationE " xmlns:util=\"http://www.springframework.org/schema/util\"\r\n" + // " xsi:schemaLocation=\"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd\r\n" + // - " http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd\"> "; + " http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd\">\r\n" + + // + ""; assertFormat(content, expected, settings, te(5, 24, 6, 8, ""), // te(6, 112, 7, 8, "\r\n "), // - te(7, 111, 9, 0, " ")); + te(7, 111, 9, 0, "\r\n")); assertFormat(expected, expected, settings); } @@ -348,7 +350,9 @@ public void xsiSchemaLocationSplitOnElementWithTabs() throws BadLocationExceptio + // " http://www.springframework.org/schema/util\r\n" + // - " http://www.springframework.org/schema/util/spring-util.xsd\"> "; + " http://www.springframework.org/schema/util/spring-util.xsd\">\r\n" + + // + ""; assertFormat(content, expected, settings, te(1, 6, 2, 4, " "), // te(2, 57, 3, 4, " "), // @@ -356,7 +360,7 @@ public void xsiSchemaLocationSplitOnElementWithTabs() throws BadLocationExceptio te(4, 51, 4, 52, "\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t "), // te(4, 112, 5, 8, "\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t "), // te(5, 50, 5, 51, "\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t "), // - te(5, 111, 7, 0, " ")); + te(5, 111, 7, 0, "\r\n")); assertFormat(expected, expected, settings); } @@ -387,7 +391,8 @@ public void xsiSchemaLocationSplitOnElementWithTabsWithSplitAttribute() throws B " xsi:schemaLocation=\"http://www.springframework.org/schema/beans\r\n" + // " http://www.springframework.org/schema/beans/spring-beans.xsd\r\n" + // " http://www.springframework.org/schema/util\r\n" + // - " http://www.springframework.org/schema/util/spring-util.xsd\"> "; + " http://www.springframework.org/schema/util/spring-util.xsd\">\r\n" + // + ""; assertFormat(content, expected, settings, te(1, 6, 2, 4, "\r\n\t\t"), // te(2, 55, 3, 4, "\r\n\t\t"), // @@ -397,7 +402,7 @@ public void xsiSchemaLocationSplitOnElementWithTabsWithSplitAttribute() throws B te(6, 51, 6, 52, "\r\n\t\t\t\t\t\t\t"), // te(6, 112, 7, 8, "\r\n\t\t\t\t\t\t\t"), // te(7, 50, 7, 51, "\r\n\t\t\t\t\t\t\t"), // - te(7, 111, 9, 0, " ")); + te(7, 111, 9, 0, "\r\n")); assertFormat(expected, expected, settings); } @@ -429,7 +434,9 @@ public void xsiSchemaLocationSplitOnElementWithTabsWithPreserveLineBreaksOnSome( + // " http://www.springframework.org/schema/util\r\n" + // - " http://www.springframework.org/schema/util/spring-util.xsd\"> "; + " http://www.springframework.org/schema/util/spring-util.xsd\">\r\n" + + // + ""; assertFormat(content, expected, settings, te(1, 6, 2, 4, "\r\n\t"), // te(2, 109, 3, 4, "\r\n\t"), // @@ -437,7 +444,7 @@ public void xsiSchemaLocationSplitOnElementWithTabsWithPreserveLineBreaksOnSome( te(4, 51, 4, 52, "\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"), // te(4, 112, 5, 8, "\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"), // te(5, 50, 5, 51, "\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"), // - te(5, 111, 7, 0, " ")); + te(5, 111, 7, 0, "\r\n")); assertFormat(expected, expected, settings); } @@ -470,7 +477,9 @@ public void xsiSchemaLocationSplitOnElementWithTabsWithPreserveLineBreaksWithDif + // " http://www.springframework.org/schema/util\r\n" + // - " http://www.springframework.org/schema/util/spring-util.xsd\"> "; + " http://www.springframework.org/schema/util/spring-util.xsd\">\r\n" + + // + ""; assertFormat(content, expected, settings, te(1, 6, 2, 6, "\r\n\t"), // te(2, 111, 3, 6, "\r\n\t"), // @@ -478,7 +487,7 @@ public void xsiSchemaLocationSplitOnElementWithTabsWithPreserveLineBreaksWithDif te(4, 51, 4, 52, "\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"), // te(4, 112, 5, 8, "\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"), // te(5, 50, 5, 51, "\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"), // - te(5, 111, 7, 0, " ")); + te(5, 111, 7, 0, "\r\n")); assertFormat(expected, expected, settings); } @@ -511,7 +520,9 @@ public void xsiSchemaLocationSplitOnElementWithTabsWithPreserveLineBreaksWithMul + // " http://www.springframework.org/schema/util\r\n" + // - " http://www.springframework.org/schema/util/spring-util.xsd\"> "; + " http://www.springframework.org/schema/util/spring-util.xsd\">\r\n" + + // + ""; assertFormat(content, expected, settings, te(1, 6, 2, 8, "\r\n\t"), // te(2, 113, 3, 8, "\r\n\t"), // @@ -519,7 +530,7 @@ public void xsiSchemaLocationSplitOnElementWithTabsWithPreserveLineBreaksWithMul te(4, 51, 4, 52, "\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"), // te(4, 112, 5, 8, "\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"), // te(5, 50, 5, 51, "\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"), // - te(5, 111, 7, 0, " ")); + te(5, 111, 7, 0, "\r\n")); assertFormat(expected, expected, settings); } @@ -550,7 +561,8 @@ public void xsiSchemaLocationSplitOnElementWithTabsWithPreserveLineBreaks() thro " xsi:schemaLocation=\"http://www.springframework.org/schema/beans\r\n" + // " http://www.springframework.org/schema/beans/spring-beans.xsd\r\n" + // " http://www.springframework.org/schema/util\r\n" + // - " http://www.springframework.org/schema/util/spring-util.xsd\"> "; + " http://www.springframework.org/schema/util/spring-util.xsd\">\r\n" + // + ""; assertFormat(content, expected, settings, te(1, 6, 2, 4, "\r\n\t"), // te(2, 55, 3, 4, "\r\n\t"), // @@ -560,7 +572,7 @@ public void xsiSchemaLocationSplitOnElementWithTabsWithPreserveLineBreaks() thro te(6, 51, 6, 52, "\r\n\t\t\t\t\t\t"), // te(6, 112, 7, 8, "\r\n\t\t\t\t\t\t"), // te(7, 50, 7, 51, "\r\n\t\t\t\t\t\t"), // - te(7, 111, 9, 0, " ")); + te(7, 111, 9, 0, "\r\n")); assertFormat(expected, expected, settings); } 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..a0511bf862 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 @@ -53,19 +53,18 @@ public void oneElementsInSameLine() throws BadLocationException { public void oneElementsInDifferentLine() throws BadLocationException { String content = "\r\n" + // ""; - String expected = " "; - assertFormat(content, expected, // - te(0, 3, 1, 0, " ")); - assertFormat(expected, expected); + String expected = content; + assertFormat(content, expected); } @Test public void oneElementsInDifferentLineWithSpace() throws BadLocationException { String content = "\r\n" + // " "; - String expected = " "; + String expected = "\r\n" + // + ""; assertFormat(content, expected, // - te(0, 3, 1, 2, " ")); + te(0, 3, 1, 2, "\r\n")); assertFormat(expected, expected); } @@ -147,17 +146,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 +211,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 +279,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..209e3bebaa 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); } @@ -96,9 +98,10 @@ public void testJoinContentLinesFalseEmptyContent() throws BadLocationException " \n" + // " " + // ""; - String expected = " "; + String expected = "\n" + // + ""; assertFormat(content, expected, settings, // - te(0, 3, 2, 5, " ")); + te(0, 3, 2, 5, "\n")); assertFormat(expected, expected, settings); } @@ -135,12 +138,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..fb4580785f 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,12 +118,160 @@ 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); } + // https://github.com/eclipse/lemminx/issues/594 + @Test + public void mixedText() throws BadLocationException { + String content = "All DocBook V5.0 elements are in the namespace http://docbook.org/ns/docbook. XML Extensible Markup\r\n" + + // + " Language namespaces are used to distinguish between different element sets. In the last few years, almost all new\r\n" + + // + " XML grammars have used their own namespace. It is easy to create compound documents that contain elements from different XML\r\n" + + // + " vocabularies. DocBook V5.0 is\r\n" + // + "\r\n" + // + "\r\n" + // + " following this\r\n" + // + " design/rule.\r\n" + // + "\r\n" + // + " Using\r\n" + // + " namespaces in your documents is very easy. Consider this simple article marked up in DocBook V4.5:"; + String expected = "All DocBook V5.0 elements are in the namespace http://docbook.org/ns/docbook. XML Extensible Markup\r\n" + + // + " Language namespaces are used to distinguish between different element sets. In the last few years, almost all\r\n" + + // + " new XML grammars have used their own namespace. It is easy to create compound documents that contain elements from different XML\r\n" + + // + " vocabularies. DocBook V5.0 is following this design/rule. Using\r\n" + + // + " namespaces in your documents is very easy. Consider this simple article marked up in DocBook V4.5:"; + assertFormat(content, expected, 130, // + te(1, 127, 1, 128, "\r\n "), // + te(1, 131, 2, 2, " "), // + te(3, 31, 6, 2, " "), // + te(6, 37, 7, 2, " "), // + te(7, 56, 9, 2, " ")); + assertFormat(expected, expected, 130); + } + + // https://github.com/eclipse/lemminx/issues/594 + @Test + public void mixedTextIsChild() throws BadLocationException { + String content = "\r\n" + // + "All DocBook V5.0 elements are in the namespace http://docbook.org/ns/docbook. XML Extensible Markup\r\n" + + // + " Language namespaces are used to distinguish between different element sets. In the last few years, almost all new\r\n" + + // + " XML grammars have used their own namespace. It is easy to create compound documents that contain elements from different XML\r\n" + + // + " vocabularies. DocBook V5.0 is\r\n" + // + "\r\n" + // + "\r\n" + // + " following this\r\n" + // + " design/rule.\r\n" + // + "\r\n" + // + " Using\r\n" + // + " namespaces in your documents is very easy. Consider this simple article marked up in DocBook V4.5:" + + // + " All DocBook V5.0 elements are in the namespace http://docbook.org/ns/docbook. XML Extensible Markup\r\n" + + // + " Language namespaces are used to distinguish between different element sets. In the last few years, almost all\r\n" + + // + " new XML grammars have used their own namespace. It is easy to create compound documents that contain elements from different XML\r\n" + + // + " vocabularies. DocBook V5.0 is following this design/rule. Using\r\n" + + // + " namespaces in your documents is very easy. Consider this simple article marked up in DocBook V4.5:"; + String expected = "\r\n" + // + " All DocBook V5.0 elements are in the namespace http://docbook.org/ns/docbook. XML Extensible\r\n" + + // + " Markup Language namespaces are used to distinguish between different element sets. In the last few years,\r\n" + + // + " almost all new XML grammars have used their own namespace. It is easy to create compound documents that contain elements from\r\n" + + // + " different XML vocabularies. DocBook V5.0 is following this design/rule.\r\n" + + // + " Using namespaces in your documents is very easy. Consider this simple article marked up in DocBook V4.5:\r\n" + + // + "\r\n" + // + "All DocBook V5.0 elements are in the namespace http://docbook.org/ns/docbook. XML Extensible Markup\r\n" + + // + " Language namespaces are used to distinguish between different element sets. In the last few years, almost all\r\n" + + // + " new XML grammars have used their own namespace. It is easy to create compound documents that contain elements from different XML\r\n" + + // + " vocabularies. DocBook V5.0 is following this design/rule. Using\r\n" + + // + " namespaces in your documents is very easy. Consider this simple article marked up in DocBook V4.5:"; + assertFormat(content, expected, 130, // + te(0, 8, 1, 0, "\r\n "), // + te(1, 123, 1, 124, "\r\n "), // + te(1, 130, 2, 2, " "), // + te(2, 116, 2, 117, "\r\n "), // + te(2, 131, 3, 2, " "), + te(3, 112, 3, 113, "\r\n "), // + te(3, 126, 4, 2, " "), // + te(4, 31, 7, 2, " "), // + te(7, 37, 8, 2, " "), // + te(8, 56, 10, 2, "\r\n "), + te(10, 7, 11, 2, " "), // + te(11, 107, 11, 107, "\r\n"), // + te(11, 116, 11, 117, "\r\n")); + assertFormat(expected, expected, 130); + } + + // https://github.com/eclipse/lemminx/issues/594 + @Test + public void mixedTextNoJoinContentLines() throws BadLocationException { + String content = "All DocBook V5.0 elements are in the namespace http://docbook.org/ns/docbook. XML Extensible Markup\r\n" + + // + " Language namespaces are used to distinguish between different element sets. In the last few years, almost all new\r\n" + + // + " XML grammars have used their own namespace. It is easy to create compound documents that contain elements from different XML\r\n" + + // + " vocabularies. DocBook V5.0 is\r\n" + // + "\r\n" + // + "\r\n" + // + " following this\r\n" + // + " design/rule.\r\n" + // + "\r\n" + // + " Using\r\n" + // + " namespaces in your documents is very easy. Consider this simple article marked up in DocBook V4.5:"; + String expected = "All DocBook V5.0 elements are in the namespace http://docbook.org/ns/docbook. XML Extensible Markup\r\n" + + // + " Language namespaces are used to distinguish between different element sets. In the last few years, almost all\r\n" + + // + " new\r\n" + // + " XML grammars have used their own namespace. It is easy to create compound documents that contain elements from different XML\r\n" + + // + " vocabularies. DocBook V5.0 is\r\n" + // + " following this\r\n" + // + " design/rule.\r\n" + // + " Using\r\n" + // + " namespaces in your documents is very easy. Consider this simple article marked up in DocBook V4.5:"; + SharedSettings settings = new SharedSettings(); + + assertFormat(content, expected, 130, settings, // + te(1, 127, 1, 128, "\r\n "), // + te(3, 31, 6, 2, "\r\n "), // + te(7, 56, 9, 2, "\r\n ")); + assertFormat(expected, expected, 130, settings); + } + + private static void assertFormat(String unformatted, String expected, int maxLineWidth, + SharedSettings sharedSettings, TextEdit... expectedEdits) + throws BadLocationException { + sharedSettings.getFormattingSettings().setMaxLineWidth(maxLineWidth); + // Force to "experimental" formatter + sharedSettings.getFormattingSettings().setExperimental(true); + XMLAssert.assertFormat(null, unformatted, expected, sharedSettings, "test.xml", Boolean.FALSE, expectedEdits); + } + private static void assertFormat(String unformatted, String expected, int maxLineWidth, TextEdit... expectedEdits) throws BadLocationException { SharedSettings sharedSettings = new SharedSettings(); diff --git a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterPreserveAttributeLineBreaksTest.java b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterPreserveAttributeLineBreaksTest.java index c90a1a1705..6ed2f2ee34 100644 --- a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterPreserveAttributeLineBreaksTest.java +++ b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterPreserveAttributeLineBreaksTest.java @@ -68,14 +68,14 @@ public void preserveAttributeLineBreaks() throws BadLocationException { String expected = "\n" + // " \n" + // + " attr=\"value\" attr=\"value\">\n" + // + "\n" + // ""; assertFormat(content, expected, settings, // te(0, 3, 1, 0, "\n "), // te(1, 28, 2, 0, "\n "), // - te(2, 25, 3, 0, "\n "), // - te(3, 26, 4, 0, " ")); + te(2, 25, 3, 0, "\n ")); assertFormat(expected, expected, settings); } 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..a30ebae68e 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; /** @@ -49,9 +48,10 @@ public void testDontPreserveEmptyContentTag() throws BadLocationException { String content = "\n" + // " " + // ""; - String expected = " "; + String expected = "\n" + // + ""; assertFormat(content, expected, settings, // - te(0, 3, 1, 5, " ")); + te(0, 3, 1, 5, "\n")); assertFormat(expected, expected, settings); } @@ -75,12 +75,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 +122,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 +174,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); }