Skip to content

Commit e4292ef

Browse files
JessicaJHeeangelozerr
authored andcommitted
Support maxLineWidth with invalid attribute values
Signed-off-by: Jessica He <jhe@redhat.com>
1 parent 299e648 commit e4292ef

10 files changed

Lines changed: 94 additions & 9 deletions

File tree

org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/xsi/participants/XSIFormatterParticipant.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public boolean formatAttributeValue(DOMAttr attr, XMLFormatterDocumentNew format
166166
XSISchemaLocationSplit split = XSISchemaLocationSplit.getSplit(formattingOptions);
167167

168168
if (split == XSISchemaLocationSplit.none || !XSISchemaModel.isXSISchemaLocationAttr(attr.getName(), attr)) {
169-
if (formatterDocument.isMaxLineWidthSupported()) {
169+
if (formatterDocument.isMaxLineWidthSupported() && attr.getValue() != null) {
170170
parentConstraints
171171
.setAvailableLineWidth(parentConstraints.getAvailableLineWidth() - attr.getValue().length());
172172
}
@@ -226,7 +226,7 @@ public boolean formatAttributeValue(DOMAttr attr, XMLFormatterDocumentNew format
226226
locationNum++;
227227
}
228228
}
229-
if (formatterDocument.isMaxLineWidthSupported()) {
229+
if (formatterDocument.isMaxLineWidthSupported() && attr.getValue() != null) {
230230
parentConstraints
231231
.setAvailableLineWidth(formatterDocument.getMaxLineWidth() - (attrValueStart - indentSpaceOffset)
232232
- attr.getValue().length());

org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/format/DOMAttributeFormatter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,10 @@ public void formatAttribute(DOMAttr attr, int prevOffset, boolean singleAttribut
9595
if (isMaxLineWidthSupported() && parentConstraints.getAvailableLineWidth() < 0
9696
&& !isSplitAttributes()) {
9797
replaceLeftSpacesWithIndentation(indentLevel + 1, from, to, true, edits);
98+
int attrValuelength = attr.getValue() != null ? attr.getValue().length() : 0;
9899
parentConstraints.setAvailableLineWidth(
99100
getMaxLineWidth() - getTabSize() * (indentLevel + 1) - attributeNamelength
100-
- attr.getValue().length());
101+
- attrValuelength);
101102
} else {
102103
// remove extra whitespaces between previous attribute
103104
// attr0='name'[space][space][space]attr1='name' -->

org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/format/DOMElementFormatter.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ private int formatStartTagElement(DOMElement element, XMLFormattingConstraints p
103103
if ((parentStartCloseOffset != startTagOpenOffset
104104
&& StringUtils.isWhitespace(formatterDocument.getText(), parentStartCloseOffset,
105105
startTagOpenOffset))
106-
|| (parentConstraints.getAvailableLineWidth() - width) < 0) {
106+
|| ((parentConstraints.getAvailableLineWidth() - width) < 0 && isMaxLineWidthSupported())) {
107107
replaceLeftSpacesWithIndentationPreservedNewLines(parentStartCloseOffset, startTagOpenOffset,
108108
indentLevel, edits);
109109
parentConstraints.setAvailableLineWidth(getMaxLineWidth());
@@ -307,7 +307,7 @@ private int formatEndTagElement(DOMElement element, XMLFormattingConstraints par
307307
// before formatting: <a> example text <b> </b> [space][space]</a>
308308
// after formatting: <a> example text <b> </b>\n</a>
309309
DOMNode lastChild = element.getLastChild();
310-
if (lastChild != null
310+
if (lastChild != null
311311
&& (lastChild.isElement() || lastChild.isComment())
312312
&& Character.isWhitespace(formatterDocument.getText().charAt(endTagOpenOffset - 1))
313313
&& !isPreserveEmptyContent()) {
@@ -480,4 +480,8 @@ private int getMaxLineWidth() {
480480
private int getTabSize() {
481481
return formatterDocument.getSharedSettings().getFormattingSettings().getTabSize();
482482
}
483+
484+
private boolean isMaxLineWidthSupported() {
485+
return formatterDocument.isMaxLineWidthSupported();
486+
}
483487
}

org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/services/format/DOMTextFormatter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public void formatText(DOMText textNode, XMLFormattingConstraints parentConstrai
4040
String text = formatterDocument.getText();
4141
int availableLineWidth = parentConstraints.getAvailableLineWidth();
4242
int indentLevel = parentConstraints.getIndentLevel();
43-
int maxLineWidth = getMaxLineWidth();
4443

4544
int spaceStart = -1;
4645
int spaceEnd = -1;
@@ -71,6 +70,7 @@ public void formatText(DOMText textNode, XMLFormattingConstraints parentConstrai
7170
}
7271
int contentEnd = i + 1;
7372
if (isMaxLineWidthSupported()) {
73+
int maxLineWidth = getMaxLineWidth();
7474
availableLineWidth -= contentEnd - contentStart;
7575
if (textNode.getStart() != contentStart && availableLineWidth >= 0
7676
&& (isJoinContentLines() || !containsNewLine)) {
@@ -112,7 +112,7 @@ public void formatText(DOMText textNode, XMLFormattingConstraints parentConstrai
112112
if (formatElementCategory != FormatElementCategory.IgnoreSpace && spaceEnd + 1 != text.length()) {
113113
DOMElement parentElement = textNode.getParentElement();
114114
// Don't format final spaces if text is at the end of the file
115-
if ((!containsNewLine || isJoinContentLines()) && availableLineWidth > 0) {
115+
if ((!containsNewLine || isJoinContentLines()) && (!isMaxLineWidthSupported() || availableLineWidth >= 0)) {
116116
// Replace spaces with single space in the case of:
117117
// 1. there is no new line
118118
// 2. isJoinContentLines

org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/settings/XMLFormattingOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ private void initializeDefaultSettings() {
157157
this.setJoinContentLines(false);
158158
this.setEnabled(true);
159159
this.setExperimental(false);
160-
this.setMaxLineWidth(80);
160+
this.setMaxLineWidth(0);
161161
this.setSpaceBeforeEmptyCloseTag(true);
162162
this.setPreserveEmptyContent(false);
163163
this.setPreservedNewlines(DEFAULT_PRESERVER_NEW_LINES);

org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/xsi/XSIFormatterExperimentalTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public void xsiSchemaLocationSplitNoneWithDefaultMaxLineWidth() throws BadLocati
7979
// None
8080
settings = createSettings();
8181
XSISchemaLocationSplit.setSplit(XSISchemaLocationSplit.none, settings.getFormattingSettings());
82+
settings.getFormattingSettings().setMaxLineWidth(80);
8283
assertFormat(content, expected, settings,
8384
te(1, 58, 1, 59, "\r\n "), //
8485
te(1, 112, 1, 113, "\r\n "), //

org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterExperimentalTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,7 @@ public void testCommentLongWrapJoinContentLines() throws BadLocationException {
724724
" comment comment comment comment comment comment comment comment --></a>";
725725
SharedSettings settings = new SharedSettings();
726726
settings.getFormattingSettings().setJoinContentLines(true);
727+
settings.getFormattingSettings().setMaxLineWidth(80);
727728
assertFormat(content, expected, settings, //
728729
te(0, 3, 1, 2, " "), //
729730
te(1, 78, 1, 79, "\n "), //
@@ -743,6 +744,7 @@ public void testCommentLongTextContentWrap() throws BadLocationException {
743744
" comment comment comment comment comment comment comment -->\n" + //
744745
"</a>";
745746
SharedSettings settings = new SharedSettings();
747+
settings.getFormattingSettings().setMaxLineWidth(80);
746748
assertFormat(content, expected, settings, //
747749
te(0, 75, 0, 76, "\n "), //
748750
te(0, 152, 0, 153, "\n "),
@@ -765,6 +767,7 @@ public void testCommentLongTextContentWrapNewLine() throws BadLocationException
765767
" comment comment comment comment comment comment comment -->\n" + //
766768
"</a>";
767769
SharedSettings settings = new SharedSettings();
770+
settings.getFormattingSettings().setMaxLineWidth(80);
768771
assertFormat(content, expected, settings, //
769772
te(1, 73, 1, 74, "\n "), //
770773
te(1, 150, 1, 151, "\n "),

org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterJoinCommentLinesTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ public void testJoinCommentLinesLongWrap() throws BadLocationException {
134134
" comment comment comment comment comment comment comment comment --></a>";
135135
SharedSettings settings = new SharedSettings();
136136
settings.getFormattingSettings().setJoinCommentLines(true);
137+
settings.getFormattingSettings().setMaxLineWidth(80);
137138
assertFormat(content, expected, settings, //
138139
te(1, 78, 1, 79, "\n "), //
139140
te(1, 150, 1, 151, "\n "));
@@ -154,6 +155,7 @@ public void testJoinCommentLinesLongWrapSingleWord() throws BadLocationException
154155
"</a>";
155156
SharedSettings settings = new SharedSettings();
156157
settings.getFormattingSettings().setJoinCommentLines(true);
158+
settings.getFormattingSettings().setMaxLineWidth(80);
157159
assertFormat(content, expected, settings, //
158160
te(0, 3, 1, 0, "\n "), //
159161
te(1, 62, 1, 63, "\n "), //

org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterMaxLineWithTest.java

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ public void splitWithAttributeNested() throws BadLocationException {
194194
" c=\"test\"> </b>" + System.lineSeparator() + //
195195
"</a>";
196196
assertFormat(content, expected, settings, //
197-
198197
te(0, 2, 0, 3, System.lineSeparator() + " "), //
199198
te(0, 13, 0, 14, System.lineSeparator() + " "), //
200199
te(0, 16, 0, 17, System.lineSeparator() + " "), //
@@ -218,6 +217,80 @@ public void splitWithAttributeKeepSameLine() throws BadLocationException {
218217
assertFormat(expected, expected, settings);
219218
}
220219

220+
@Test
221+
public void splitWithAttributeInvalid() throws BadLocationException {
222+
SharedSettings settings = new SharedSettings();
223+
settings.getFormattingSettings().setTabSize(4);
224+
settings.getFormattingSettings().setMaxLineWidth(10);
225+
settings.getFormattingSettings().setJoinContentLines(true);
226+
String content = "<a bb=> </a>";
227+
String expected = "<a bb=> </a>";
228+
assertFormat(content, expected, settings, //
229+
te(0, 7, 0, 11, " "));
230+
assertFormat(expected, expected, settings);
231+
}
232+
233+
@Test
234+
public void splitWithAttributeInvalidSingleQuote() throws BadLocationException {
235+
SharedSettings settings = new SharedSettings();
236+
settings.getFormattingSettings().setTabSize(4);
237+
settings.getFormattingSettings().setMaxLineWidth(10);
238+
settings.getFormattingSettings().setJoinContentLines(true);
239+
String content = "<a bb=\"> </a>";
240+
String expected = "<a" + System.lineSeparator() + //
241+
" bb=\"> </a>";
242+
assertFormat(content, expected, settings, //
243+
te(0, 2, 0, 3, System.lineSeparator() + " "));
244+
assertFormat(expected, expected, settings);
245+
}
246+
247+
@Test
248+
public void splitWithAttributeInvalidSpace() throws BadLocationException {
249+
SharedSettings settings = new SharedSettings();
250+
settings.getFormattingSettings().setTabSize(4);
251+
settings.getFormattingSettings().setMaxLineWidth(10);
252+
settings.getFormattingSettings().setJoinContentLines(true);
253+
String content = "<a bb = > </a>";
254+
String expected = "<a bb=> </a>";
255+
assertFormat(content, expected, settings, //
256+
te(0, 5, 0, 6, ""), //
257+
te(0, 7, 0, 8, ""), //
258+
te(0, 9, 0, 13, " "));
259+
assertFormat(expected, expected, settings);
260+
}
261+
262+
@Test
263+
public void splitWithAttributeInvalidSpaceSingleQuote() throws BadLocationException {
264+
SharedSettings settings = new SharedSettings();
265+
settings.getFormattingSettings().setTabSize(4);
266+
settings.getFormattingSettings().setMaxLineWidth(10);
267+
settings.getFormattingSettings().setJoinContentLines(true);
268+
String content = "<a bb = \" > </a>";
269+
String expected = "<a" + System.lineSeparator() + //
270+
" bb=\" > </a>";
271+
assertFormat(content, expected, settings, //
272+
te(0, 2, 0, 3, System.lineSeparator() + " "), //
273+
te(0, 5, 0, 6, ""), //
274+
te(0, 7, 0, 8, ""));
275+
assertFormat(expected, expected, settings);
276+
}
277+
278+
@Test
279+
public void splitWithAttributeInvalidSpaceQuoted() throws BadLocationException {
280+
SharedSettings settings = new SharedSettings();
281+
settings.getFormattingSettings().setTabSize(4);
282+
settings.getFormattingSettings().setMaxLineWidth(10);
283+
settings.getFormattingSettings().setJoinContentLines(true);
284+
String content = "<a bb = \" \" > </a>";
285+
String expected = "<a bb=\" \"> </a>";
286+
assertFormat(content, expected, settings, //
287+
te(0, 5, 0, 6, ""), //
288+
te(0, 7, 0, 8, ""), //
289+
te(0, 11, 0, 12, ""), //
290+
te(0, 13, 0, 17, " "));
291+
assertFormat(expected, expected, settings);
292+
}
293+
221294
@Test
222295
public void longText() throws BadLocationException {
223296
SharedSettings settings = new SharedSettings();

org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/services/format/experimental/XMLFormatterPreserveSpacesTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ public void preserveSpacesWithXsdStringWithMaxLineWidth() throws Exception {
132132

133133
SharedSettings settings = new SharedSettings();
134134
settings.getFormattingSettings().setGrammarAwareFormatting(true);
135+
settings.getFormattingSettings().setMaxLineWidth(80);
135136
assertFormat(content, expected, settings, //
136137
te(1, 50, 1, 51, "\r\n "), //
137138
te(1, 104, 1, 105, "\r\n "), //

0 commit comments

Comments
 (0)