Skip to content

Commit

Permalink
Fix max line width wrapping with attributes
Browse files Browse the repository at this point in the history
Signed-off-by: Jessica He <jhe@redhat.com>
  • Loading branch information
JessicaJHee authored and datho7561 committed Jan 9, 2023
1 parent 1663001 commit 6c4bd39
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
Expand Up @@ -216,20 +216,22 @@ public boolean formatAttributeValue(DOMAttr attr, XMLFormatterDocument formatter
indentSpaceOffset = (attrValueStart + 1) - attr.getNodeAttrName().getStart()
+ (parentConstraints.getIndentLevel() + 1) * tabSize;
}
int attrValuelength = attrValueStart - indentSpaceOffset + attr.getValue().length();
// Insert newline and indent where required based on setting
if (locationNum % lineFeed == 0) {
formatterDocument.replaceLeftSpacesWithIndentationWithOffsetSpaces(indentSpaceOffset,
attrValueStart, attrValueStart + i + 1, true, edits);
availableLineWidth = formatterDocument.getMaxLineWidth() - attrValuelength;
} else {
formatterDocument.replaceLeftSpacesWithOneSpace(indentSpaceOffset, attrValueStart + i + 1, edits);
availableLineWidth = availableLineWidth - attrValuelength;
}
locationNum++;
}
}
if (formatterDocument.isMaxLineWidthSupported() && attr.getValue() != null) {
parentConstraints
.setAvailableLineWidth(formatterDocument.getMaxLineWidth() - (attrValueStart - indentSpaceOffset)
- attr.getValue().length());
.setAvailableLineWidth(availableLineWidth);
}
return true;
}
Expand Down
Expand Up @@ -605,14 +605,44 @@ public void commentFormattingLineBreakingEarlyAtMaxLineWidth() throws BadLocatio
"-->\r\n" + //
"</foo>";
assertFormat(content, expected, settings, //
te(4, 95, 4,96, "\r\n"));
te(4, 95, 4, 96, "\r\n"));
assertFormat(expected, expected, settings);
}

// https://github.com/eclipse/lemminx/issues/1439
@Test
public void firstLineNoBreakAtMaxLineWidth() throws BadLocationException {
SharedSettings settings = new SharedSettings();
settings.getFormattingSettings().setMaxLineWidth(160);
String content = "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\"></project>";
String expected = "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
+ System.lineSeparator() + //
" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\"></project>";
assertFormat(content, expected, settings, //
te(0, 104, 0, 105, System.lineSeparator() + " "));
assertFormat(expected, expected, settings);
}

// https://github.com/eclipse/lemminx/issues/1439
@Test
public void firstLineNoBreakAtMaxLineWidthDefaultWidth() throws BadLocationException {
SharedSettings settings = new SharedSettings();
settings.getFormattingSettings().setMaxLineWidth(100); // default max line width
String content = "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\"></project>";
String expected = "<project xmlns=\"http://maven.apache.org/POM/4.0.0\""
+ System.lineSeparator() + //
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
+ System.lineSeparator() + //
" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\"></project>";
assertFormat(content, expected, settings, //
te(0, 50, 0, 51, System.lineSeparator() + " "), //
te(0, 104, 0, 105, System.lineSeparator() + " "));
assertFormat(expected, expected, settings);
}

private static void assertFormat(String unformatted, String expected, SharedSettings sharedSettings,
TextEdit... expectedEdits)
throws BadLocationException {
XMLAssert.assertFormat(null, unformatted, expected, sharedSettings, "test.xml", Boolean.FALSE, expectedEdits);
}
}
}

0 comments on commit 6c4bd39

Please sign in to comment.