diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/format/DOMCommentFormatter.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/format/DOMCommentFormatter.java index 4ace76927..35ae26b8f 100644 --- a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/format/DOMCommentFormatter.java +++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/format/DOMCommentFormatter.java @@ -60,10 +60,9 @@ public void formatComment(DOMComment commentNode, XMLFormattingConstraints paren preservedNewLines + 1, edits); availableLineWidth = getMaxLineWidth() - getTabSize(); } else if (addLineSeparator && startRange < start) { - // remove spaces and indent - replaceLeftSpacesWithIndentation(parentConstraints.getIndentLevel(), leftWhitespaceOffset, start, + replaceLeftSpacesWithIndentation(indentLevel, leftWhitespaceOffset, start, addLineSeparator, edits); - availableLineWidth = getMaxLineWidth() - getTabSize(); + availableLineWidth = getMaxLineWidth() - getTabSize() * indentLevel; } int spaceStart = -1; int spaceEnd = -1; @@ -85,21 +84,21 @@ public void formatComment(DOMComment commentNode, XMLFormattingConstraints paren return; } int contentStart = i; - while (i < commentNode.getEnd() + 1 && !Character.isWhitespace(text.charAt(i + 1))) { + while (i + 1 < commentNode.getEnd() && !Character.isWhitespace(text.charAt(i + 1))) { i++; } int contentEnd = i; availableLineWidth -= (contentEnd + 1 - contentStart); if (availableLineWidth <= 0 && spaceStart != -1) { // Add new line when the comment extends over the maximum line width - replaceLeftSpacesWithIndentation(parentConstraints.getIndentLevel(), spaceStart, contentStart, + replaceLeftSpacesWithIndentation(indentLevel, spaceStart, contentStart, true, edits); - int indentSpaces = (getTabSize() * parentConstraints.getIndentLevel()); + int indentSpaces = (getTabSize() * indentLevel); availableLineWidth = getMaxLineWidth() - indentSpaces - (contentEnd + 1 - contentStart); } else if (isJoinCommentLines()) { replaceSpacesWithOneSpace(spaceStart, spaceEnd - 1, edits); availableLineWidth--; - } else { + } else if (spaceStart != -1) { availableLineWidth -= spaceEnd - spaceStart; } spaceStart = -1; diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/format/DOMDocTypeFormatter.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/format/DOMDocTypeFormatter.java index af4891a8f..6c067d9a5 100644 --- a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/format/DOMDocTypeFormatter.java +++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/format/DOMDocTypeFormatter.java @@ -43,6 +43,7 @@ public void formatDocType(DOMDocumentType docType, XMLFormattingConstraints pare if (isDTD) { formatDTD(docType, parentConstraints, start, end, edits); } else { + replaceLeftSpacesWithIndentation(parentConstraints.getIndentLevel(), docType.getParentNode().getStart(), docType.getStart(), true, edits); List parameters = docType.getParameters(); if (!parameters.isEmpty()) { for (DTDDeclParameter parameter : parameters) { @@ -85,6 +86,10 @@ public void formatDocType(DOMDocumentType docType, XMLFormattingConstraints pare int endDocType = internalSubset.getEnd() - 1; String lineDelimiter = formatterDocument.getLineDelimiter(); replaceLeftSpacesWith(startDocType, endDocType, lineDelimiter, edits); + // Remove space between end brackets + // Exmaple Before: ]|> + removeLeftSpaces(internalSubset.getEnd(), docType.getEnd()-1, edits); } } @@ -108,6 +113,7 @@ private void formatDTD(DOMDocumentType docType, XMLFormattingConstraints parentC default: // unknown, so just leave alone for now but make sure to update // available line width + formatterDocument.format(child, parentConstraints, start, end, edits); int width = updateLineWidthWithLastLine(child, parentConstraints.getAvailableLineWidth()); parentConstraints.setAvailableLineWidth(width); } 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 aa5bf92d2..23ffc9b6d 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 @@ -44,7 +44,7 @@ public void formatElement(DOMElement element, XMLFormattingConstraints parentCon // Format start tag element with proper indentation int indentLevel = parentConstraints.getIndentLevel(); - int nb = formatStartTagElement(element, parentConstraints, emptyElements, end, edits); + int nb = formatStartTagElement(element, parentConstraints, emptyElements, start, end, edits); if (emptyElements == EmptyElements.ignore) { // Format children of the element @@ -66,7 +66,7 @@ public void formatElement(DOMElement element, XMLFormattingConstraints parentCon } private int formatStartTagElement(DOMElement element, XMLFormattingConstraints parentConstraints, - EmptyElements emptyElements, int end, List edits) { + EmptyElements emptyElements, int start, int end, List edits) { if (!element.hasStartTag()) { // ex : end) { + if (end != -1 && startTagOpenOffset > end || start != -1 && startTagCloseOffset != -1 && startTagCloseOffset < start) { return 0; } switch (formatElementCategory) { @@ -89,10 +90,10 @@ private int formatStartTagElement(DOMElement element, XMLFormattingConstraints p // before formatting: [space][space] example text // after formatting: \n example text int parentStartCloseOffset = element.getParentElement().getStartTagCloseOffset() + 1; - if (parentStartCloseOffset != startTagOffset + if (parentStartCloseOffset != startTagOpenOffset && StringUtils.isWhitespace(formatterDocument.getText(), parentStartCloseOffset, - startTagOffset)) { - int nbSpaces = replaceLeftSpacesWithIndentation(indentLevel, parentStartCloseOffset, startTagOffset, + startTagOpenOffset)) { + int nbSpaces = replaceLeftSpacesWithIndentation(indentLevel, parentStartCloseOffset, startTagOpenOffset, !addLineSeparator, edits); width = element.getTagName() != null ? nbSpaces + element.getTagName().length() + 1 : nbSpaces; if (!addLineSeparator) { @@ -104,14 +105,14 @@ private int formatStartTagElement(DOMElement element, XMLFormattingConstraints p // If preserve new lines int preservedNewLines = getPreservedNewlines(); int currentNewLineCount = XMLFormatterDocumentNew.getExistingNewLineCount(formatterDocument.getText(), - startTagOffset, + startTagOpenOffset, formatterDocument.getLineDelimiter()); if (currentNewLineCount > preservedNewLines) { - replaceLeftSpacesWithIndentationWithMultiNewLines(indentLevel, 0, startTagOffset, + replaceLeftSpacesWithIndentationWithMultiNewLines(indentLevel, 0, startTagOpenOffset, preservedNewLines + 1, edits); } else { // remove spaces and indent - int nbSpaces = replaceLeftSpacesWithIndentation(indentLevel, 0, startTagOffset, !addLineSeparator, + int nbSpaces = replaceLeftSpacesWithIndentation(indentLevel, 0, startTagOpenOffset, !addLineSeparator, edits); width = element.getTagName() != null ? nbSpaces + element.getTagName().length() + 1 : nbSpaces; if (!addLineSeparator) { @@ -282,6 +283,7 @@ private int formatEndTagElement(DOMElement element, XMLFormattingConstraints par int indentLevel = parentConstraints.getIndentLevel(); FormatElementCategory formatElementCategory = constraints.getFormatElementCategory(); int endTagOffset = element.getEndTagOpenOffset(); + int startTagCloseOffset = element.getStartTagCloseOffset(); switch (formatElementCategory) { case PreserveSpace: @@ -294,7 +296,7 @@ private int formatEndTagElement(DOMElement element, XMLFormattingConstraints par if ((element.getLastChild().isElement() || element.getLastChild().isComment()) && Character.isWhitespace(formatterDocument.getText().charAt(endTagOffset - 1)) && !isPreserveEmptyContent()) { - replaceLeftSpacesWithIndentation(indentLevel, element.getStartTagCloseOffset(), endTagOffset, true, + replaceLeftSpacesWithIndentation(indentLevel, startTagCloseOffset, endTagOffset, true, edits); } break; @@ -305,11 +307,11 @@ private int formatEndTagElement(DOMElement element, XMLFormattingConstraints par endTagOffset, formatterDocument.getLineDelimiter()); if (currentNewLineCount > preservedNewLines) { - replaceLeftSpacesWithIndentationWithMultiNewLines(indentLevel, element.getStartTagCloseOffset(), + replaceLeftSpacesWithIndentationWithMultiNewLines(indentLevel, startTagCloseOffset, endTagOffset, preservedNewLines + 1, edits); } else { // remove spaces and indent - replaceLeftSpacesWithIndentation(indentLevel, element.getStartTagCloseOffset(), endTagOffset, true, + replaceLeftSpacesWithIndentation(indentLevel, startTagCloseOffset, endTagOffset, true, edits); break; } diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/format/DOMProcessingInstructionFormatter.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/format/DOMProcessingInstructionFormatter.java index da139c162..5d04c1bad 100644 --- a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/format/DOMProcessingInstructionFormatter.java +++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/format/DOMProcessingInstructionFormatter.java @@ -14,6 +14,7 @@ import java.util.List; import org.eclipse.lemminx.dom.DOMAttr; +import org.eclipse.lemminx.dom.DOMElement; import org.eclipse.lemminx.dom.DOMProcessingInstruction; import org.eclipse.lsp4j.TextEdit; @@ -38,6 +39,16 @@ public DOMProcessingInstructionFormatter(XMLFormatterDocumentNew formatterDocume public void formatProcessingInstruction(DOMProcessingInstruction processingInstruction, XMLFormattingConstraints parentConstraints, List edits) { int prevOffset = processingInstruction.getStartContent(); + DOMElement parentElement = processingInstruction.getParentElement(); + + // If the processing instruction is contained within a parent element + // Ex: | --> add a new line and indent here accordingly + if (parentElement != null) { + int indentLevel = parentConstraints.getIndentLevel(); + int parentStartCloseOffset = parentElement.getStartTagCloseOffset() + 1; + replaceLeftSpacesWithIndentation(indentLevel, parentStartCloseOffset, processingInstruction.getStart(), + true, edits); + } // 1. format attributes : attributes must be in a same line separate with only // one space if (processingInstruction.hasAttributes()) { @@ -67,4 +78,8 @@ private void replaceLeftSpacesWith(int leftLimit, int to, String replacement, Li formatterDocument.replaceLeftSpacesWith(leftLimit, to, replacement, edits); } + private int replaceLeftSpacesWithIndentation(int indentLevel, int from, int to, boolean addLineSeparator, + List edits) { + return formatterDocument.replaceLeftSpacesWithIndentation(indentLevel, from, to, addLineSeparator, edits); + } } 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 1fad128c8..aad84fb09 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 @@ -59,7 +59,7 @@ public void formatText(DOMText textNode, XMLFormattingConstraints parentConstrai // Text content... spaceEnd = i; int contentStart = i; - while (i < textNode.getEnd() + 1 && !Character.isWhitespace(text.charAt(i + 1))) { + while (i + 1 < textNode.getEnd() && !Character.isWhitespace(text.charAt(i + 1))) { i++; } int contentEnd = i; diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/format/XMLFormatterDocumentNew.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/format/XMLFormatterDocumentNew.java index 0cd03850d..570c9a778 100644 --- a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/format/XMLFormatterDocumentNew.java +++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/format/XMLFormatterDocumentNew.java @@ -171,7 +171,7 @@ public List format(DOMDocument document, int start, int end) int endDocument = xml.length() - 1; if (endDocument >= 0) { char c = xml.charAt(endDocument); - if (c != '\n') { + if (c != '\n' && (end == -1 || endDocument < end)) { try { Position pos = textDocument.positionAt(endDocument); pos.setCharacter(pos.getCharacter() + 1); @@ -269,7 +269,7 @@ private void formatSiblings(List edits, DOMNode domNode, XMLFormatting } } - private void format(DOMNode child, XMLFormattingConstraints parentConstraints, int start, int end, + public void format(DOMNode child, XMLFormattingConstraints parentConstraints, int start, int end, List edits) { switch (child.getNodeType()) { @@ -555,7 +555,7 @@ public FormatElementCategory getFormatElementCategory(DOMElement element, boolean hasText = false; boolean onlySpaces = true; for (DOMNode child : element.getChildren()) { - if (child.isElement() || child.isComment()) { + if (child.isElement() || child.isComment() || child.isProcessingInstruction()) { hasElement = true; } else if (child.isText()) { onlySpaces = ((Text) child).isElementContentWhitespace(); @@ -702,7 +702,7 @@ private void trimFinalNewlines(boolean insertFinalNewline, List edits) public static int getExistingNewLineCount(String text, int offset, String delimiter) { boolean delimiterHasTwoCharacters = delimiter.length() == 2; int newLineCounter = 0; - for (int i = offset; i > 0; i--) { + for (int i = offset; i > 1; i--) { String c; if (!Character.isWhitespace(text.charAt(i - 1))) { if (!delimiterHasTwoCharacters) { 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 d8bdda1e6..779e0bdfb 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 @@ -566,24 +566,29 @@ public void testProlog4WithUnknownVariable() throws BadLocationException { assertFormat(expected, expected); } - @Disabled @Test public void testPI() throws BadLocationException { String content = ""; String expected = "" + lineSeparator() + // " " + lineSeparator() + // ""; - assertFormat(content, expected); + assertFormat(content, expected, // + te(0, 3, 0, 3, lineSeparator() + " "), // + te(0, 23, 0, 24, ""), // + te(0, 26, 0, 26, lineSeparator())); + assertFormat(expected, expected); } - @Disabled @Test public void testPINoContent() throws BadLocationException { String content = ""; String expected = "" + lineSeparator() + // - " " + lineSeparator() + // + " " + lineSeparator() + // ""; - assertFormat(content, expected); + assertFormat(content, expected, // + te(0, 3, 0, 3, lineSeparator() + " "), // + te(0, 10, 0, 10, lineSeparator())); + assertFormat(expected, expected); } @Disabled @@ -591,9 +596,15 @@ public void testPINoContent() throws BadLocationException { public void testDefinedPIWithVariables() throws BadLocationException { String content = ""; String expected = "" + lineSeparator() + // - " " + lineSeparator() + // + " " + lineSeparator() + // ""; - assertFormat(content, expected); + assertFormat(content, expected, // + te(0, 3, 0, 3, lineSeparator() + " "), // + te(0, 19, 0, 22, " "), // + te(0, 41, 0, 46, " "), // + te(0, 51, 0, 54, ""), // + te(0, 66, 0, 66, lineSeparator())); + assertFormat(expected, expected); } @Disabled @@ -601,9 +612,16 @@ public void testDefinedPIWithVariables() throws BadLocationException { public void testDefinedPIWithJustAttributeNames() throws BadLocationException { String content = ""; String expected = "" + lineSeparator() + // - " " + lineSeparator() + // + " " + lineSeparator() + // ""; - assertFormat(content, expected); + assertFormat(content, expected, // + te(0, 3, 0, 3, lineSeparator() + " "), // + te(0, 19, 0, 23, " "), // + te(0, 27, 0, 32, " "), // + te(0, 36, 0, 38, ""), // + te(0, 39, 0, 46, " "), // + te(0, 55, 0, 55, lineSeparator())); + assertFormat(expected, expected); } @Disabled @@ -611,7 +629,7 @@ public void testDefinedPIWithJustAttributeNames() throws BadLocationException { public void testPIWithVariables() throws BadLocationException { String content = ""; String expected = "" + lineSeparator() + // - " " + lineSeparator() + // + " " + lineSeparator() + // ""; assertFormat(content, expected); } @@ -741,7 +759,6 @@ public void testCommentWithRange() throws BadLocationException { // ---------- Tests for Text formatting - @Disabled @Test public void testElementContentNotNormalized() throws BadLocationException { String content = "\r" + // @@ -751,15 +768,16 @@ public void testElementContentNotNormalized() throws BadLocationException { " Content4\r" + // " Content5\r" + // ""; - String expected = "\r" + // - " Content\r" + // + String expected = " Content\r" + // " Content2\r" + // " Content3\r" + // " Content4\r" + // - " Content5\r" + // - ""; + " Content5 "; - assertFormat(content, expected); + assertFormat(content, expected, // + te(0, 3, 1, 1, " "), // + te(5, 10, 6, 0, " ")); + assertFormat(expected, expected); } @Disabled @@ -834,146 +852,6 @@ public void testContentFormatting6() throws BadLocationException { assertFormat(content, expected); } - @Disabled - @Test - public void testTrimTrailingWhitespaceText() throws BadLocationException { - SharedSettings settings = new SharedSettings(); - settings.getFormattingSettings().setTrimTrailingWhitespace(true); - String content = " \n" + // - "text \n" + // - " text text text \n" + // - " text\n" + // - " "; - String expected = "\n" + // - "text\n" + // - " text text text\n" + // - " text\n" + // - ""; - assertFormat(content, expected, settings); - } - - @Disabled - @Test - public void testTrimTrailingWhitespaceNewlines() throws BadLocationException { - SharedSettings settings = new SharedSettings(); - settings.getFormattingSettings().setTrimTrailingWhitespace(true); - String content = " \n" + // - " \n" + // - " "; - String expected = ""; - assertFormat(content, expected, settings); - } - - @Disabled - @Test - public void testTrimTrailingWhitespaceTextAndNewlines() throws BadLocationException { - SharedSettings settings = new SharedSettings(); - settings.getFormattingSettings().setTrimTrailingWhitespace(true); - String content = " \n" + // - " \n" + // - "text \n" + // - " text text text \n" + // - " \n" + // - " text\n" + // - " \n" + // - " "; - String expected = "\n" + // - "\n" + // - "text\n" + // - " text text text\n" + // - "\n" + // - " text\n" + // - "\n" + // - ""; - assertFormat(content, expected, settings); - } - - @Disabled - @Test - public void testDontInsertFinalNewLineWithRange() throws BadLocationException { - SharedSettings settings = new SharedSettings(); - settings.getFormattingSettings().setInsertFinalNewline(true); - String content = "
\r\n" + // - " |\r\n" + // - "
"; - String expected = "
\r\n" + // - " \r\n" + // - "
"; - assertFormat(content, expected, settings); - } - - @Disabled - @Test - public void testInsertFinalNewLineWithRange2() throws BadLocationException { - SharedSettings settings = new SharedSettings(); - settings.getFormattingSettings().setInsertFinalNewline(true); - String content = "
\r\n" + // - " |\r\n" + // - "
|"; - String expected = "
\r\n" + // - " \r\n" + // - "
\r\n"; - assertFormat(content, expected, settings); - } - - @Disabled - @Test - public void testInsertFinalNewLineWithRange3() throws BadLocationException { - SharedSettings settings = new SharedSettings(); - settings.getFormattingSettings().setInsertFinalNewline(true); - String content = "
\r\n" + // - " |\r\n" + // - "\r\n" + "|" + "\r\n" + // - "

\r\n" + // - "
"; - String expected = "
\r\n" + // - " \r\n" + // - "\r\n" + // - "

" + "\r\n" + // - "
"; - assertFormat(content, expected, settings); - } - - @Test - public void testDontTrimFinalNewLines() throws BadLocationException { - SharedSettings settings = new SharedSettings(); - settings.getFormattingSettings().setTrimFinalNewlines(false); - String content = "\r\n\r\n\r\n"; - String expected = "\r\n\r\n\r\n"; - - assertFormat(content, expected, settings, // - te(0, 2, 0, 4, "")); - assertFormat(expected, expected, settings); - } - - @Disabled - @Test - public void testDontTrimFinalNewLines2() throws BadLocationException { - SharedSettings settings = new SharedSettings(); - settings.getFormattingSettings().setTrimFinalNewlines(false); - String content = "\r\n" + // - " \r\n\r\n"; - String expected = "\r\n" + // - " \r\n\r\n"; - assertFormat(content, expected, settings); - } - - @Disabled - @Test - public void testDontTrimFinalNewLines3() throws BadLocationException { - SharedSettings settings = new SharedSettings(); - settings.getFormattingSettings().setTrimFinalNewlines(false); - String content = "\r\n" + // - " text \r\n" + // - " more text \r\n" + // - " \r\n"; - String expected = "\r\n" + // - " text \r\n" + // - " more text \r\n" + // - " \r\n"; - assertFormat(content, expected, settings); - } - private static void assertFormat(String unformatted, String actual, TextEdit... expectedEdits) throws BadLocationException { assertFormat(unformatted, actual, new SharedSettings(), 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 de1ba1fd8..d3e0c5cb8 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 @@ -12,6 +12,9 @@ package org.eclipse.lemminx.services.format.experimental; import static org.eclipse.lemminx.XMLAssert.te; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.lang.annotation.Documented; import org.eclipse.lemminx.AbstractCacheBasedTest; import org.eclipse.lemminx.XMLAssert; @@ -27,9 +30,10 @@ */ public class XMLFormatterForDTDTest extends AbstractCacheBasedTest { - @Disabled @Test public void testDoctypeNoInternalSubset() throws BadLocationException { + SharedSettings settings = new SharedSettings(); + settings.getFormattingSettings().setPreservedNewlines(1); String content = "\r\n" + // @@ -52,7 +56,11 @@ public void testDoctypeNoInternalSubset() throws BadLocationException { "\r\n" + // " Don't forget me this weekend\r\n" + // ""; - assertFormat(content, expected); + assertFormat(content, expected, settings, // + te(0, 9, 0, 13, " "), // + te(0, 17, 2, 0, ""), // + te(8, 29, 10, 2, "\r\n\r\n ")); + assertFormat(expected, expected, settings); } @Test @@ -88,7 +96,6 @@ public void testDoctypeNoInternalSubsetNoNewlines() throws BadLocationException assertFormat(expected, expected); } - @Disabled @Test public void testDoctypeInternalSubset() throws BadLocationException { String content = "Jani\r\n" + // - "\r\n" + // " Reminder\r\n" + // " Don't forget me this weekend\r\n" + // ""; - assertFormat(content, expected); + assertFormat(content, expected, // + te(0, 14, 3, 0, " "), // + te(3, 1, 3, 9, "\r\n "), // + te(4, 25, 4, 25, "\r\n "), // + te(4, 50, 7, 2, "\r\n "), // + te(14, 19, 16, 2, "\r\n ")); + assertFormat(expected, expected); } - @Disabled @Test public void testDoctypeInternalSubsetNoNewlines() throws BadLocationException { SharedSettings settings = new SharedSettings(); @@ -167,7 +178,14 @@ public void testDoctypeInternalSubsetNoNewlines() throws BadLocationException { " Reminder\r\n" + // " Don't forget me this weekend\r\n" + // ""; - assertFormat(content, expected, settings); + assertFormat(content, expected, settings, // + te(0, 14, 3, 0, " "), // + te(3, 1, 3, 9, "\r\n "), // + te(4, 25, 4, 25, "\r\n "), // + te(4, 50, 7, 2, "\r\n "), // + te(11, 15, 14, 2, "\r\n "), // + te(14, 19, 16, 2, "\r\n ")); + assertFormat(expected, expected, settings); } @Test @@ -206,7 +224,6 @@ public void testDoctypeInternalDeclSpacesBetweenParameters() throws BadLocationE assertFormat(expected, expected); } - @Disabled @Test public void testDoctypeInternalWithAttlist() throws BadLocationException { String content = "\r\n" + // " \r\n" + // "]>\r\n" + // - "\r\n" + // "\r\n" + // - "\r\n" + // " Fred\r\n" + // ""; - assertFormat(content, expected); + assertFormat(content, expected, // + te(0, 14, 1, 0, " "), // + te(2, 40, 4, 2, "\r\n "), // + te(4, 25, 6, 2, "\r\n "), // + te(6, 39, 8, 0, "\r\n"), // + te(8, 2, 10, 0, "\r\n"), // + te(10, 6, 12, 2, "\r\n ")); + assertFormat(expected, expected); } @Test @@ -266,7 +288,6 @@ public void testDoctypeInternalAllDecls() throws BadLocationException { assertFormat(expected, expected); } - @Disabled @Test public void testDoctypeInternalWithComments() throws BadLocationException { String content = "\r\n" + // " \r\n" + // "]>"; - assertFormat(content, expected); + assertFormat(content, expected, // + te(0, 14, 1, 0, " "), // + te(1, 1, 2, 2, "\r\n "), // + te(2, 18, 4, 2, "\r\n "), // + te(4, 40, 7, 2, "\r\n "), // + te(7, 39, 9, 2, "\r\n "), // + te(9, 18, 11, 2, "\r\n "), // + te(13, 2, 14, 0, "")); + assertFormat(expected, expected); } - @Disabled @Test public void testDoctypeInternalWithText() throws BadLocationException { + SharedSettings settings = new SharedSettings(); + settings.getFormattingSettings().setPreservedNewlines(0); String content = "\r\n" + // @@ -316,6 +346,8 @@ public void testDoctypeInternalWithText() throws BadLocationException { "]>"; String expected = "\r\n" + // + "\r\n" + // + "\r\n" + // " garbageazg df\r\n" + // " gdf\r\n" + // "garbageazgdfg\r\n" + // @@ -323,7 +355,11 @@ public void testDoctypeInternalWithText() throws BadLocationException { " gd\r\n" + // " \r\n" + // "]>"; - assertFormat(content, expected); + assertFormat(content, expected, settings, // + te(0, 14, 1, 0, " "), // + te(9, 4, 13, 2, "\r\n "), // + te(13, 40, 15, 0, "\r\n")); + assertFormat(expected, expected, settings); } @Test @@ -340,10 +376,9 @@ public void testDTDMultiParameterAttlist() throws BadLocationException { te(1, 15, 1, 16, "\r\n "), // te(1, 35, 1, 36, "\r\n "), // te(1, 62, 1, 63, "\r\n ")); - assertDTDFormat(expected, expected); + assertDTDFormat(expected, expected, settings); } - @Disabled @Test public void testDTDIndentation() throws BadLocationException { SharedSettings settings = new SharedSettings(); @@ -359,10 +394,14 @@ public void testDTDIndentation() throws BadLocationException { "\r\n" + // "\r\n" + // ""; - assertFormat(content, expected, settings); + assertFormat(content, expected, settings, // + te(0, 0, 0, 2, ""), // + te(0, 40, 2, 3, "\r\n"), // + te(2, 40, 4, 6, "\r\n"), // + te(4, 73, 6, 6, "\r\n")); + assertDTDFormat(expected, expected, settings); } - @Disabled @Test public void testDTDNotEndBrackets() throws BadLocationException { SharedSettings settings = new SharedSettings(); @@ -370,23 +409,25 @@ public void testDTDNotEndBrackets() throws BadLocationException { String content = "\r\n" + // - "\r\n" + // "\r\n" + // " \r\n" + // "\r\n" + // @@ -397,16 +438,20 @@ public void testDTDUnknownDeclNameAndText() throws BadLocationException { "\r\n" + // ""; String expected = "\r\n" + // - "\r\n" + // - "asdasd\r\n" + // + "\r\n" + // + "\r\n" + // + " asdasd\r\n" + // " asd\r\n" + // "\r\n" + // "\r\n" + // ""; - assertFormat(content, expected, settings); + assertFormat(content, expected, settings, // + te(0, 38, 2, 2, "\r\n"), // + te(2, 20, 2, 21, ""), // + te(5, 5, 7, 0, "\r\n")); + assertFormat(expected, expected, settings); } - @Disabled @Test public void testAllDoctypeParameters() throws BadLocationException { String content = "\r\n" + // @@ -422,7 +467,6 @@ public void testAllDoctypeParameters() throws BadLocationException { " \r\n" + // "]\r\n" + // "\r\n" + // - "\r\n" + // ">\r\n" + // "\r\n" + // " sdsd\r\n" + // @@ -445,14 +489,19 @@ public void testAllDoctypeParameters() throws BadLocationException { "]>\r\n" + // "\r\n" + // " sdsd\r\n" + // - "\r\n" + // " \r\n" + // - "\r\n" + // " er\r\n" + // " dd\r\n" + // " \r\n" + // ""; - assertFormat(content, expected); + assertFormat(content, expected, // + te(1, 125, 2, 8, "\r\n "), // + te(4, 29, 7, 10, "\r\n "), // + te(10, 1, 12, 0, ""), // + te(14, 35, 16, 2, "\r\n "), // + te(16, 11, 18, 4, "\r\n ")); + assertFormat(expected, expected); + } @Disabled @@ -461,19 +510,21 @@ public void testDTDElementContentWithAsterisk() throws BadLocationException { SharedSettings settings = new SharedSettings(); String content = ""; String expected = ""; - assertFormat(content, expected, settings); + assertFormat(content, expected, settings, false); } - @Disabled @Test public void testDoctypeSingleLineFormat() throws BadLocationException { String content = "]>\r\n" + // ""; - String expected = "\r\n" + // + String expected = "\r\n" + // " \r\n" + // "]>"; - assertFormat(content, expected); + assertFormat(content, expected, // + te(0, 35, 0, 35, "\r\n "), // + te(0, 76, 0, 76, "\r\n"), // + te(0, 78, 1, 0, "")); + assertFormat(expected, expected); } @Test @@ -507,7 +558,6 @@ public void testDoctypeInvalidParameterUnclosed() throws BadLocationException { assertFormat(expected, expected); } - @Disabled @Test public void testUnclosedSystemId() throws BadLocationException { String content = "\r\n" + // @@ -520,12 +570,12 @@ public void testUnclosedSystemId() throws BadLocationException { "\r\n" + // " \r\n" + // "]>\r\n" + // - "\r\n" + // ""; - assertFormat(content, expected); + assertFormat(content, expected, // + te(3, 2, 5, 0, "\r\n")); + assertFormat(expected, expected); } - @Disabled @Test public void testUnclosedPublicId() throws BadLocationException { String content = "\r\n" + // @@ -538,12 +588,12 @@ public void testUnclosedPublicId() throws BadLocationException { "\r\n" + // " \r\n" + // "]>\r\n" + // - "\r\n" + // ""; - assertFormat(content, expected); + assertFormat(content, expected, // + te(3, 2, 5, 0, "\r\n")); + assertFormat(expected, expected); } - @Disabled @Test public void testCommentAfterMissingClosingBracket() throws BadLocationException { String content = "\r\n" + // "]>\r\n" + // - "\r\n" + // ""; - assertFormat(content, expected); + assertFormat(content, expected, // + te(1, 85, 3, 2, "\r\n "), // + te(4, 2, 6, 0, "\r\n")); + assertFormat(expected, expected); } - @Disabled @Test public void testHTMLDTD() throws BadLocationException { SharedSettings settings = new SharedSettings(); @@ -591,25 +642,32 @@ public void testHTMLDTD() throws BadLocationException { 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" + // "\r\n" + // "%HTML4.dtd;"; - assertFormat(content, expected, settings); + assertFormat(content, expected, settings, // + te(4, 19, 6, 4, "\r\n"), // + 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(11, 11, 12, 4, "\r\n "), // + te(12, 14, 13, 4, " "), // + te(13, 7, 14, 4, " "), // + te(14, 15, 15, 4, "\r\n"), // + te(16, 3, 18, 0, "\r\n")); } - @Disabled @Test public void testXMLInDTDFile() throws BadLocationException { SharedSettings settings = new SharedSettings(); @@ -623,12 +681,18 @@ public void testXMLInDTDFile() throws BadLocationException { ""; String expected = "\r\n" + // "\r\n" + // - "\r\n" + // - "\r\n" + // - "\r\n" + // - "\r\n" + // + " \r\n" + // + " \r\n" + // + " \r\n" + // + " \r\n" + // ""; - assertFormat(content, expected, settings); + assertFormat(content, expected, settings, // + te(1, 22, 2, 4, "\r\n "), // + te(2, 26, 2, 27, ""), // + te(2, 28, 3, 8, "\r\n "), // + te(3, 45, 4, 8, "\r\n "), // + te(4, 45, 5, 4, "\r\n ")); + assertFormat(expected, expected, settings); } @Disabled @@ -637,7 +701,6 @@ public void testBadDTDFile() throws BadLocationException { SharedSettings settings = new SharedSettings(); String content = "\r\n" + // ""; - assertDTDFormat(content, expected, settings); - } - - private static void assertDTDFormat(String unformatted, String actual, TextEdit... expectedEdits) - throws BadLocationException { - assertDTDFormat(unformatted, actual, new SharedSettings(), expectedEdits); + assertDTDFormat(content, expected, settings, // + te(0, 13, 0, 14, "\r\n "), // + te(0, 33, 0, 34, "\r\n "), // + te(1, 1, 3, 0, "\r\n")); + assertDTDFormat(expected, expected, settings); } private static void assertDTDFormat(String unformatted, String expected, SharedSettings sharedSettings, @@ -690,6 +751,11 @@ private static void assertFormat(String unformatted, String expected, SharedSett assertFormat(unformatted, expected, sharedSettings, "test.xml", expectedEdits); } + private static void assertFormat(String unformatted, String expected, SharedSettings sharedSettings, + Boolean considerRangeFormat, TextEdit... expectedEdits) throws BadLocationException { + assertFormat(unformatted, expected, sharedSettings, "test.xml", considerRangeFormat, expectedEdits); + } + private static void assertFormat(String unformatted, String expected, SharedSettings sharedSettings, String uri, TextEdit... expectedEdits) throws BadLocationException { assertFormat(unformatted, expected, sharedSettings, uri, true, expectedEdits); diff --git a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterInsertFinalNewLineTest.java b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterInsertFinalNewLineTest.java index ff18836bc..6f989fc64 100644 --- a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterInsertFinalNewLineTest.java +++ b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterInsertFinalNewLineTest.java @@ -19,7 +19,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; /** @@ -55,19 +54,21 @@ public void testDontInsertFinalNewLine2() throws BadLocationException { settings.getFormattingSettings().setInsertFinalNewline(true); String content = "\r\n"; String expected = "\r\n"; - assertFormat(content, expected, settings, te(0, 2, 0, 4, "")); + assertFormat(content, expected, settings, // + te(0, 2, 0, 4, "")); assertFormat(expected, expected, settings); } - @Disabled @Test public void testDontInsertFinalNewLine3() throws BadLocationException { SharedSettings settings = new SharedSettings(); settings.getFormattingSettings().setTrimFinalNewlines(false); - settings.getFormattingSettings().setInsertFinalNewline(true); + settings.getFormattingSettings().setInsertFinalNewline(false); String content = "\r\n" + " "; String expected = "\r\n" + " "; - assertFormat(content, expected, settings); + assertFormat(content, expected, settings, // + te(0, 2, 0, 4, "")); + assertFormat(expected, expected, settings); } @Test 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 2b6773b8f..cf585a431 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 @@ -53,7 +53,7 @@ public void testTrimTrailingWhitespaceNewlines() throws BadLocationException { String content = " \n" + // " \n" + // " "; - String expected = ""; + String expected = " "; assertFormat(content, expected, settings); } @@ -81,7 +81,6 @@ public void testTrimTrailingWhitespaceTextAndNewlines() throws BadLocationExcept assertFormat(content, expected, settings); } - @Disabled @Test public void testDontInsertFinalNewLineWithRange() throws BadLocationException { SharedSettings settings = new SharedSettings(); @@ -92,10 +91,13 @@ public void testDontInsertFinalNewLineWithRange() throws BadLocationException { String expected = "
\r\n" + // " \r\n" + // "
"; - assertFormat(content, expected, settings); + assertFormat(content, expected, settings, // + te(1, 6, 1, 8, " "), // + te(1, 11, 1, 12, ""), // + te(1, 13, 1, 14, ""), // + te(1, 19, 1, 19, " ")); } - @Disabled @Test public void testInsertFinalNewLineWithRange2() throws BadLocationException { SharedSettings settings = new SharedSettings(); @@ -106,10 +108,15 @@ public void testInsertFinalNewLineWithRange2() throws BadLocationException { String expected = "
\r\n" + // " \r\n" + // "
\r\n"; - assertFormat(content, expected, settings); + assertFormat(content, expected, settings, // + te(1, 6, 1, 8, " "), // + te(1, 11, 1, 12, ""), // + te(1, 13, 1, 14, ""), // + te(1, 19, 1, 19, " "), // + te(1, 21, 2, 1, "\r\n"), // + te(2, 7, 2, 7, "\r\n")); } - @Disabled @Test public void testInsertFinalNewLineWithRange3() throws BadLocationException { SharedSettings settings = new SharedSettings(); @@ -122,9 +129,14 @@ public void testInsertFinalNewLineWithRange3() throws BadLocationException { String expected = "
\r\n" + // " \r\n" + // "\r\n" + // + "\r\n" + // "

" + "\r\n" + // "
"; - assertFormat(content, expected, settings); + assertFormat(content, expected, settings, // + te(1, 6, 1, 8, " "), // + te(1, 11, 1, 12, ""), // + te(1, 13, 1, 14, ""), // + te(1, 19, 1, 19, " ")); } @Test diff --git a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterWithRangeTest.java b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterWithRangeTest.java index d478f7768..aeee3e224 100644 --- a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterWithRangeTest.java +++ b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterWithRangeTest.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; /** @@ -105,17 +104,15 @@ public void rangeChildrenPartialSelection() throws BadLocationException { String expected = "\n" + // " \n" + // - " License Name\n" + // + " License Name\n" + // " abcdefghijklmnop\n" + // " repo\n" + // " \n" + // ""; assertFormat(content, expected, // - te(1, 11, 2, 2, "\n "), // te(2, 27, 3, 14, "\n "), // te(3, 41, 4, 14, "\n ")); - assertFormat(expected, expected); } @Test @@ -261,7 +258,6 @@ public void rangeSelectWithinText() throws BadLocationException { assertFormat(content, expected); } - @Disabled @Test public void rangeSelectEntityNoIndent() throws BadLocationException { String content = "\r\n" + // @@ -270,7 +266,7 @@ public void rangeSelectEntityNoIndent() throws BadLocationException { "]>"; String expected = "\r\n" + // "\r\n" + // + "\r\n" + // "]>"; assertFormat(content, expected); }