Skip to content

Commit

Permalink
Fix range formatting when root element has mixed content
Browse files Browse the repository at this point in the history
Closes #1414

Signed-off-by: David Thompson <davthomp@redhat.com>
  • Loading branch information
datho7561 committed Dec 16, 2022
1 parent 62772fd commit 990bffe
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

/**
* DOM element formatter.
*
*
* @author Angelo ZERR
*
*/
Expand Down Expand Up @@ -100,7 +100,7 @@ private int formatStartTagElement(DOMElement element, XMLFormattingConstraints p
// tag is some white spaces
// before formatting: <a> [space][space] <b> </b> example text </a>
// after formatting: <a>\n <b> </b> example text </a>
int parentStartCloseOffset = element.getParentElement().getStartTagCloseOffset() + 1;
int parentStartCloseOffset = element.getParentElement() != null ? element.getParentElement().getStartTagCloseOffset() + 1 : 0;
if ((parentStartCloseOffset != startTagOpenOffset
&& StringUtils.isWhitespace(formatterDocument.getText(), parentStartCloseOffset,
startTagOpenOffset))) {
Expand Down Expand Up @@ -227,11 +227,11 @@ private int formatAttributes(DOMElement element, XMLFormattingConstraints parent
/**
* Formats the start tag's closing bracket (>) according to
* {@code XMLFormattingOptions#isPreserveAttrLineBreaks()}
*
*
* {@code XMLFormattingOptions#isPreserveAttrLineBreaks()}: If true, must add a
* newline + indent before the closing bracket if the last attribute of the
* element and the closing bracket are in different lines.
*
*
* @param element
* @throws BadLocationException
*/
Expand Down Expand Up @@ -336,7 +336,7 @@ private int formatEndTagElement(DOMElement element, XMLFormattingConstraints par

/**
* Return the option to use to generate empty elements.
*
*
* @param element the DOM element
* @return the option to use to generate empty elements.
*/
Expand Down Expand Up @@ -368,10 +368,10 @@ private EmptyElements getEmptyElements(DOMElement element, FormatElementCategory
/**
* Return true if conditions are met to format according to the
* closingBracketNewLine setting.
*
*
* 1. splitAttribute must be set to true 2. there must be at least 2 attributes
* in the element
*
*
* @param element the DOM element
* @return true if should format according to closingBracketNewLine setting.
*/
Expand Down Expand Up @@ -408,10 +408,10 @@ private void createTextEditIfNeeded(int from, int to, String expectedContent, Li
/**
* Returns true if the DOM document have some line break in the given range
* [from, to] and false otherwise.
*
*
* @param from the from offset range.
* @param to the to offset range.
*
*
* @return true if the DOM document have some line break in the given range
* [from, to] and false otherwise.
*/
Expand All @@ -421,9 +421,9 @@ private boolean hasLineBreak(int from, int to) {

/**
* Returns the last attribute of the given DOMelement and null otherwise.
*
*
* @param element the DOM element.
*
*
* @return the last attribute of the given DOMelement and null otherwise.
*/
private static DOMAttr getLastAttribute(DOMElement element) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,21 @@ public void testSplitAttributesRangeMultipleLines() throws BadLocationException
te(3, 24, 4, 4, ""));
}

@Test
public void testMixedContentInRoot() throws BadLocationException {
String content = "|<a>\n" + //
"zz <b> </b>tt </a>\n|";

String expected = "<a> zz <b> </b>tt </a>";

assertFormat(content, expected, //
te(0, 3, 1, 0, " "), //
te(1, 2, 1, 6, " "), //
te(1, 9, 1, 11, " "), //
te(1, 17, 1, 22, " "), //
te(1, 26, 2, 0, ""));
}

private static void assertFormat(String unformatted, String actual, TextEdit... expectedEdits)
throws BadLocationException {
assertFormat(unformatted, actual, new SharedSettings(), expectedEdits);
Expand Down

0 comments on commit 990bffe

Please sign in to comment.