Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix max line width wrapping with attributes #1442

Merged
merged 1 commit into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);
}
}
}