Skip to content

Commit

Permalink
Fix bug in comment formatting with xml.format.maxLineWidth
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 4, 2023
1 parent 3a25dad commit 84a5962
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 11 deletions.
Expand Up @@ -54,12 +54,18 @@ public void formatComment(DOMComment commentNode, XMLFormattingConstraints paren
}
int spaceStart = -1;
int spaceEnd = -1;
availableLineWidth -= 4; // count for '<-!--'
availableLineWidth -= 4; // count for '<!--'
int whiteSpaceOffset = -1;

for (int i = commentNode.getStartContent(); i < commentNode.getEndContent(); i++) {
char c = text.charAt(i);
if (Character.isWhitespace(c)) {
// Whitespaces
if (isLineSeparator(c) && !isJoinCommentLines()) {
// Reset avaliable line width when there is new line
availableLineWidth = maxLineWidth;
}
whiteSpaceOffset = i;

if (spaceStart == -1) {
spaceStart = i;
} else {
Expand All @@ -75,22 +81,25 @@ public void formatComment(DOMComment commentNode, XMLFormattingConstraints paren
while (i + 1 < commentNode.getEnd() && !Character.isWhitespace(text.charAt(i + 1))) {
i++;
}
int contentEnd = i;
int contentEnd = i + 1;
if (isMaxLineWidthSupported()) {
availableLineWidth -= (contentEnd + 1 - contentStart);
if (availableLineWidth <= 0 && spaceStart != -1) {
// Adjust availableLineWidth for whitespaces before comment content
if (commentNode.getStartContent() != contentStart && isJoinCommentLines()
&& availableLineWidth >= 0) {
availableLineWidth--;
} else {
availableLineWidth -= spaceEnd - whiteSpaceOffset;
}
availableLineWidth -= (contentEnd - contentStart);
if (availableLineWidth < 0 && spaceStart != -1) {
// Add new line when the comment extends over the maximum line width
replaceLeftSpacesWithIndentation(indentLevel, spaceStart, contentStart,
true, edits);
int indentSpaces = tabSize * indentLevel;
availableLineWidth = maxLineWidth - indentSpaces - (contentEnd + 1 - contentStart);
availableLineWidth = maxLineWidth - indentSpaces - (contentEnd - contentStart);
spaceStart = -1;
spaceEnd = -1;
continue;
} else if (isJoinCommentLines()) {
availableLineWidth--;
} else if (spaceStart != -1) {
availableLineWidth -= spaceEnd - spaceStart;
}
}
if (isJoinCommentLines()) {
Expand Down Expand Up @@ -139,4 +148,9 @@ private void replaceLeftSpacesWithIndentationPreservedNewLines(int spaceStart, i
formatterDocument.replaceLeftSpacesWithIndentationPreservedNewLines(spaceStart, spaceEnd, indentLevel,
edits);
}

private static boolean isLineSeparator(char c) {
return c == '\r' || c == '\n';
}

}
Expand Up @@ -13,7 +13,6 @@

import java.util.List;

import org.eclipse.lemminx.dom.DOMElement;
import org.eclipse.lemminx.dom.DOMText;
import org.eclipse.lsp4j.TextEdit;

Expand Down
Expand Up @@ -557,6 +557,59 @@ public void mixedTextNoJoinContentLines() throws BadLocationException {
assertFormat(expected, expected, settings);
}

// https://github.com/redhat-developer/vscode-xml/issues/851
@Test
public void commentFormattingLineBreakingEarly() throws BadLocationException {
SharedSettings settings = new SharedSettings();
settings.getFormattingSettings().setMaxLineWidth(100); // set to default
String content = "<!--\r\n" + //
"/*******************************************************************************\r\n" + //
" * Lorem ipsum dolor sit amet, consectetur\r\n" + //
" * © Copyright adipiscing elit. In eget magna ornare,\r\n" + //
" *\r\n" + //
" * pharetra sapienvitae, iaculis purus. Sed et dignissim lacus.\r\n" + //
" * Morbi condimentum nisi eget sem laoreet placerat.\r\n" + //
" * Pellentesque diam elit, vehicula et auctor a, euismod a enim.\r\n" + //
" *******************************************************************************/\r\n" + //
"-->\r\n" + //
"</foo>";
String expected = content;
assertFormat(content, expected, settings);
}

@Test
public void commentFormattingLineBreakingEarlyAtMaxLineWidth() throws BadLocationException {
SharedSettings settings = new SharedSettings();
settings.getFormattingSettings().setMaxLineWidth(100); // set to default
String content = "<!--\r\n" + //
"/*******************************************************************************\r\n" + //
" * Lorem ipsum dolor sit amet, consectetur © Copyright adipiscing elit. In eget magna ornare, tester\r\n"
+ // line width = 100
" *\r\n" + //
" * pharetra sapienvitae, iaculis purus. Sed et dignissim lacus. Morbi condimentum nisi eget sem laoreet placerat.\r\n"
+ //
" * Pellentesque diam elit, vehicula et auctor a, euismod a enim.\r\n" + //
" *******************************************************************************/\r\n" + //
"-->\r\n" + //
"</foo>";
String expected = "<!--\r\n" + //
"/*******************************************************************************\r\n" + //
" * Lorem ipsum dolor sit amet, consectetur © Copyright adipiscing elit. In eget magna ornare, tester\r\n"
+ //
" *\r\n" + //
" * pharetra sapienvitae, iaculis purus. Sed et dignissim lacus. Morbi condimentum nisi eget sem\r\n" + //
"laoreet placerat.\r\n"
+ //
" * Pellentesque diam elit, vehicula et auctor a, euismod a enim.\r\n" + //
" *******************************************************************************/\r\n" + //
"-->\r\n" + //
"</foo>";
assertFormat(content, expected, settings, //
te(4, 95, 4,96, "\r\n"));
assertFormat(expected, expected, settings);

}

private static void assertFormat(String unformatted, String expected, SharedSettings sharedSettings,
TextEdit... expectedEdits)
throws BadLocationException {
Expand Down

0 comments on commit 84a5962

Please sign in to comment.