Skip to content

Commit

Permalink
Enable experimental formatter tests
Browse files Browse the repository at this point in the history
Signed-off-by: Jessica He <[email protected]>
  • Loading branch information
JessicaJHee authored and angelozerr committed Oct 18, 2022
1 parent 84e8197 commit 3416b1b
Show file tree
Hide file tree
Showing 11 changed files with 244 additions and 269 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,9 @@ public void formatComment(DOMComment commentNode, XMLFormattingConstraints paren
preservedNewLines + 1, edits);
availableLineWidth = getMaxLineWidth() - getTabSize();
} else if (addLineSeparator && startRange < start) {
// remove spaces and indent
replaceLeftSpacesWithIndentation(parentConstraints.getIndentLevel(), leftWhitespaceOffset, start,
replaceLeftSpacesWithIndentation(indentLevel, leftWhitespaceOffset, start,
addLineSeparator, edits);
availableLineWidth = getMaxLineWidth() - getTabSize();
availableLineWidth = getMaxLineWidth() - getTabSize() * indentLevel;
}
int spaceStart = -1;
int spaceEnd = -1;
Expand All @@ -85,21 +84,21 @@ public void formatComment(DOMComment commentNode, XMLFormattingConstraints paren
return;
}
int contentStart = i;
while (i < commentNode.getEnd() + 1 && !Character.isWhitespace(text.charAt(i + 1))) {
while (i + 1 < commentNode.getEnd() && !Character.isWhitespace(text.charAt(i + 1))) {
i++;
}
int contentEnd = i;
availableLineWidth -= (contentEnd + 1 - contentStart);
if (availableLineWidth <= 0 && spaceStart != -1) {
// Add new line when the comment extends over the maximum line width
replaceLeftSpacesWithIndentation(parentConstraints.getIndentLevel(), spaceStart, contentStart,
replaceLeftSpacesWithIndentation(indentLevel, spaceStart, contentStart,
true, edits);
int indentSpaces = (getTabSize() * parentConstraints.getIndentLevel());
int indentSpaces = (getTabSize() * indentLevel);
availableLineWidth = getMaxLineWidth() - indentSpaces - (contentEnd + 1 - contentStart);
} else if (isJoinCommentLines()) {
replaceSpacesWithOneSpace(spaceStart, spaceEnd - 1, edits);
availableLineWidth--;
} else {
} else if (spaceStart != -1) {
availableLineWidth -= spaceEnd - spaceStart;
}
spaceStart = -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public void formatDocType(DOMDocumentType docType, XMLFormattingConstraints pare
if (isDTD) {
formatDTD(docType, parentConstraints, start, end, edits);
} else {
replaceLeftSpacesWithIndentation(parentConstraints.getIndentLevel(), docType.getParentNode().getStart(), docType.getStart(), true, edits);
List<DTDDeclParameter> parameters = docType.getParameters();
if (!parameters.isEmpty()) {
for (DTDDeclParameter parameter : parameters) {
Expand Down Expand Up @@ -85,6 +86,10 @@ public void formatDocType(DOMDocumentType docType, XMLFormattingConstraints pare
int endDocType = internalSubset.getEnd() - 1;
String lineDelimiter = formatterDocument.getLineDelimiter();
replaceLeftSpacesWith(startDocType, endDocType, lineDelimiter, edits);
// Remove space between end brackets
// Exmaple Before: <!DOCTYPE person [...
// <!ENTITY AUTHOR \"John Doe\">]|>
removeLeftSpaces(internalSubset.getEnd(), docType.getEnd()-1, edits);
}
}

Expand All @@ -108,6 +113,7 @@ private void formatDTD(DOMDocumentType docType, XMLFormattingConstraints parentC
default:
// unknown, so just leave alone for now but make sure to update
// available line width
formatterDocument.format(child, parentConstraints, start, end, edits);
int width = updateLineWidthWithLastLine(child, parentConstraints.getAvailableLineWidth());
parentConstraints.setAvailableLineWidth(width);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void formatElement(DOMElement element, XMLFormattingConstraints parentCon

// Format start tag element with proper indentation
int indentLevel = parentConstraints.getIndentLevel();
int nb = formatStartTagElement(element, parentConstraints, emptyElements, end, edits);
int nb = formatStartTagElement(element, parentConstraints, emptyElements, start, end, edits);

if (emptyElements == EmptyElements.ignore) {
// Format children of the element
Expand All @@ -66,17 +66,18 @@ public void formatElement(DOMElement element, XMLFormattingConstraints parentCon
}

private int formatStartTagElement(DOMElement element, XMLFormattingConstraints parentConstraints,
EmptyElements emptyElements, int end, List<TextEdit> edits) {
EmptyElements emptyElements, int start, int end, List<TextEdit> edits) {
if (!element.hasStartTag()) {
// ex : </
return element.getEnd() - element.getStart();
}
int width = 0;
int indentLevel = parentConstraints.getIndentLevel();
FormatElementCategory formatElementCategory = parentConstraints.getFormatElementCategory();
int startTagOffset = element.getStartTagOpenOffset();
int startTagOpenOffset = element.getStartTagOpenOffset();
int startTagCloseOffset = element.getStartTagCloseOffset();
boolean addLineSeparator = element.getParentElement() == null && element.getPreviousSibling() == null;
if (end != -1 && startTagOffset > end) {
if (end != -1 && startTagOpenOffset > end || start != -1 && startTagCloseOffset != -1 && startTagCloseOffset < start) {
return 0;
}
switch (formatElementCategory) {
Expand All @@ -89,10 +90,10 @@ private int formatStartTagElement(DOMElement element, XMLFormattingConstraints p
// before formatting: <a> [space][space] <b> </b> example text </a>
// after formatting: <a>\n <b> </b> example text </a>
int parentStartCloseOffset = element.getParentElement().getStartTagCloseOffset() + 1;
if (parentStartCloseOffset != startTagOffset
if (parentStartCloseOffset != startTagOpenOffset
&& StringUtils.isWhitespace(formatterDocument.getText(), parentStartCloseOffset,
startTagOffset)) {
int nbSpaces = replaceLeftSpacesWithIndentation(indentLevel, parentStartCloseOffset, startTagOffset,
startTagOpenOffset)) {
int nbSpaces = replaceLeftSpacesWithIndentation(indentLevel, parentStartCloseOffset, startTagOpenOffset,
!addLineSeparator, edits);
width = element.getTagName() != null ? nbSpaces + element.getTagName().length() + 1 : nbSpaces;
if (!addLineSeparator) {
Expand All @@ -104,14 +105,14 @@ private int formatStartTagElement(DOMElement element, XMLFormattingConstraints p
// If preserve new lines
int preservedNewLines = getPreservedNewlines();
int currentNewLineCount = XMLFormatterDocumentNew.getExistingNewLineCount(formatterDocument.getText(),
startTagOffset,
startTagOpenOffset,
formatterDocument.getLineDelimiter());
if (currentNewLineCount > preservedNewLines) {
replaceLeftSpacesWithIndentationWithMultiNewLines(indentLevel, 0, startTagOffset,
replaceLeftSpacesWithIndentationWithMultiNewLines(indentLevel, 0, startTagOpenOffset,
preservedNewLines + 1, edits);
} else {
// remove spaces and indent
int nbSpaces = replaceLeftSpacesWithIndentation(indentLevel, 0, startTagOffset, !addLineSeparator,
int nbSpaces = replaceLeftSpacesWithIndentation(indentLevel, 0, startTagOpenOffset, !addLineSeparator,
edits);
width = element.getTagName() != null ? nbSpaces + element.getTagName().length() + 1 : nbSpaces;
if (!addLineSeparator) {
Expand Down Expand Up @@ -282,6 +283,7 @@ private int formatEndTagElement(DOMElement element, XMLFormattingConstraints par
int indentLevel = parentConstraints.getIndentLevel();
FormatElementCategory formatElementCategory = constraints.getFormatElementCategory();
int endTagOffset = element.getEndTagOpenOffset();
int startTagCloseOffset = element.getStartTagCloseOffset();

switch (formatElementCategory) {
case PreserveSpace:
Expand All @@ -294,7 +296,7 @@ private int formatEndTagElement(DOMElement element, XMLFormattingConstraints par
if ((element.getLastChild().isElement() || element.getLastChild().isComment())
&& Character.isWhitespace(formatterDocument.getText().charAt(endTagOffset - 1))
&& !isPreserveEmptyContent()) {
replaceLeftSpacesWithIndentation(indentLevel, element.getStartTagCloseOffset(), endTagOffset, true,
replaceLeftSpacesWithIndentation(indentLevel, startTagCloseOffset, endTagOffset, true,
edits);
}
break;
Expand All @@ -305,11 +307,11 @@ private int formatEndTagElement(DOMElement element, XMLFormattingConstraints par
endTagOffset,
formatterDocument.getLineDelimiter());
if (currentNewLineCount > preservedNewLines) {
replaceLeftSpacesWithIndentationWithMultiNewLines(indentLevel, element.getStartTagCloseOffset(),
replaceLeftSpacesWithIndentationWithMultiNewLines(indentLevel, startTagCloseOffset,
endTagOffset, preservedNewLines + 1, edits);
} else {
// remove spaces and indent
replaceLeftSpacesWithIndentation(indentLevel, element.getStartTagCloseOffset(), endTagOffset, true,
replaceLeftSpacesWithIndentation(indentLevel, startTagCloseOffset, endTagOffset, true,
edits);
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.List;

import org.eclipse.lemminx.dom.DOMAttr;
import org.eclipse.lemminx.dom.DOMElement;
import org.eclipse.lemminx.dom.DOMProcessingInstruction;
import org.eclipse.lsp4j.TextEdit;

Expand All @@ -38,6 +39,16 @@ public DOMProcessingInstructionFormatter(XMLFormatterDocumentNew formatterDocume
public void formatProcessingInstruction(DOMProcessingInstruction processingInstruction,
XMLFormattingConstraints parentConstraints, List<TextEdit> edits) {
int prevOffset = processingInstruction.getStartContent();
DOMElement parentElement = processingInstruction.getParentElement();

// If the processing instruction is contained within a parent element
// Ex: <a>|<?m2e?></a> --> add a new line and indent here accordingly
if (parentElement != null) {
int indentLevel = parentConstraints.getIndentLevel();
int parentStartCloseOffset = parentElement.getStartTagCloseOffset() + 1;
replaceLeftSpacesWithIndentation(indentLevel, parentStartCloseOffset, processingInstruction.getStart(),
true, edits);
}
// 1. format attributes : attributes must be in a same line separate with only
// one space
if (processingInstruction.hasAttributes()) {
Expand Down Expand Up @@ -67,4 +78,8 @@ private void replaceLeftSpacesWith(int leftLimit, int to, String replacement, Li
formatterDocument.replaceLeftSpacesWith(leftLimit, to, replacement, edits);
}

private int replaceLeftSpacesWithIndentation(int indentLevel, int from, int to, boolean addLineSeparator,
List<TextEdit> edits) {
return formatterDocument.replaceLeftSpacesWithIndentation(indentLevel, from, to, addLineSeparator, edits);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void formatText(DOMText textNode, XMLFormattingConstraints parentConstrai
// Text content...
spaceEnd = i;
int contentStart = i;
while (i < textNode.getEnd() + 1 && !Character.isWhitespace(text.charAt(i + 1))) {
while (i + 1 < textNode.getEnd() && !Character.isWhitespace(text.charAt(i + 1))) {
i++;
}
int contentEnd = i;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public List<? extends TextEdit> format(DOMDocument document, int start, int end)
int endDocument = xml.length() - 1;
if (endDocument >= 0) {
char c = xml.charAt(endDocument);
if (c != '\n') {
if (c != '\n' && (end == -1 || endDocument < end)) {
try {
Position pos = textDocument.positionAt(endDocument);
pos.setCharacter(pos.getCharacter() + 1);
Expand Down Expand Up @@ -269,7 +269,7 @@ private void formatSiblings(List<TextEdit> edits, DOMNode domNode, XMLFormatting
}
}

private void format(DOMNode child, XMLFormattingConstraints parentConstraints, int start, int end,
public void format(DOMNode child, XMLFormattingConstraints parentConstraints, int start, int end,
List<TextEdit> edits) {

switch (child.getNodeType()) {
Expand Down Expand Up @@ -555,7 +555,7 @@ public FormatElementCategory getFormatElementCategory(DOMElement element,
boolean hasText = false;
boolean onlySpaces = true;
for (DOMNode child : element.getChildren()) {
if (child.isElement() || child.isComment()) {
if (child.isElement() || child.isComment() || child.isProcessingInstruction()) {
hasElement = true;
} else if (child.isText()) {
onlySpaces = ((Text) child).isElementContentWhitespace();
Expand Down Expand Up @@ -702,7 +702,7 @@ private void trimFinalNewlines(boolean insertFinalNewline, List<TextEdit> edits)
public static int getExistingNewLineCount(String text, int offset, String delimiter) {
boolean delimiterHasTwoCharacters = delimiter.length() == 2;
int newLineCounter = 0;
for (int i = offset; i > 0; i--) {
for (int i = offset; i > 1; i--) {
String c;
if (!Character.isWhitespace(text.charAt(i - 1))) {
if (!delimiterHasTwoCharacters) {
Expand Down
Loading

0 comments on commit 3416b1b

Please sign in to comment.