diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/xsi/participants/XSIFormatterParticipant.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/xsi/participants/XSIFormatterParticipant.java
index 53cc93df0..b9e27e4b7 100644
--- a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/xsi/participants/XSIFormatterParticipant.java
+++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/xsi/participants/XSIFormatterParticipant.java
@@ -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;
}
diff --git a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/settings/XMLFormatterMaxLineWithTest.java b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/settings/XMLFormatterMaxLineWithTest.java
index 8cc2ac040..b638424a1 100644
--- a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/settings/XMLFormatterMaxLineWithTest.java
+++ b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/settings/XMLFormatterMaxLineWithTest.java
@@ -605,9 +605,39 @@ public void commentFormattingLineBreakingEarlyAtMaxLineWidth() throws BadLocatio
"-->\r\n" + //
"";
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 = "";
+ String expected = "";
+ 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 = "";
+ String expected = "";
+ 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,
@@ -615,4 +645,4 @@ private static void assertFormat(String unformatted, String expected, SharedSett
throws BadLocationException {
XMLAssert.assertFormat(null, unformatted, expected, sharedSettings, "test.xml", Boolean.FALSE, expectedEdits);
}
-}
+}
\ No newline at end of file