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

Set experimental formatter to default #1381

Merged
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 @@ -19,7 +19,7 @@
import org.eclipse.lemminx.extensions.xsi.XSISchemaModel;
import org.eclipse.lemminx.extensions.xsi.settings.XSISchemaLocationSplit;
import org.eclipse.lemminx.services.extensions.format.IFormatterParticipant;
import org.eclipse.lemminx.services.format.XMLFormatterDocumentNew;
import org.eclipse.lemminx.services.format.XMLFormatterDocument;
import org.eclipse.lemminx.services.format.XMLFormattingConstraints;
import org.eclipse.lemminx.settings.XMLFormattingOptions;
import org.eclipse.lemminx.utils.StringUtils;
Expand Down Expand Up @@ -86,10 +86,12 @@ public boolean formatAttributeValue(String name, String valueWithoutQuote, Chara
}
List<String> locations = getLocations(valueWithoutQuote);
String indent = "";
boolean insertSpaces = formattingOptions.isInsertSpaces();
int tabSize = formattingOptions.getTabSize();
for (int i = 0; i < locations.size(); i++) {
if (i % lineFeed == 0) {
if (i == 0) {
indent = getCurrentLineIndent(xml, formattingOptions);
indent = getCurrentLineIndent(xml, insertSpaces, tabSize);
} else {
xml.linefeed();
xml.append(indent);
Expand Down Expand Up @@ -127,9 +129,7 @@ private static List<String> getLocations(String value) {
return locations;
}

public String getCurrentLineIndent(XMLBuilder xml, XMLFormattingOptions formattingOptions) {
boolean insertSpaces = formattingOptions.isInsertSpaces();
int tabSize = formattingOptions.getTabSize();
public String getCurrentLineIndent(XMLBuilder xml, boolean insertSpaces, int tabSize) {
int nbChars = 0;
for (int i = xml.length() - 1; i >= 0; i--) {
if (xml.charAt(i) == '\r' || xml.charAt(i) == '\n') {
Expand Down Expand Up @@ -160,7 +160,7 @@ public String getCurrentLineIndent(XMLBuilder xml, XMLFormattingOptions formatti
}

@Override
public boolean formatAttributeValue(DOMAttr attr, XMLFormatterDocumentNew formatterDocument,
public boolean formatAttributeValue(DOMAttr attr, XMLFormatterDocument formatterDocument,
XMLFormattingConstraints parentConstraints, XMLFormattingOptions formattingOptions, List<TextEdit> edits) {

XSISchemaLocationSplit split = XSISchemaLocationSplit.getSplit(formattingOptions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.services.extensions.XMLExtensionsRegistry;
import org.eclipse.lemminx.services.extensions.format.IFormatterParticipant;
import org.eclipse.lemminx.services.format.XMLFormatterDocumentOld;
import org.eclipse.lemminx.services.format.XMLFormatterDocument;
import org.eclipse.lemminx.services.format.XMLFormatterDocumentNew;
import org.eclipse.lemminx.settings.SharedSettings;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.TextEdit;
Expand All @@ -41,8 +41,8 @@ public XMLFormatter(XMLExtensionsRegistry extensionsRegistry) {
}

/**
* Returns a List containing a single TextEdit, containing the newly formatted
* changes of the document.
* Returns a List containing multiple TextEdits to remove, add,
* update spaces / indent.
*
* @param textDocument document to perform formatting on
* @param range specified range in which formatting will be done
Expand All @@ -51,12 +51,12 @@ public XMLFormatter(XMLExtensionsRegistry extensionsRegistry) {
*/
public List<? extends TextEdit> format(DOMDocument xmlDocument, Range range, SharedSettings sharedSettings) {
try {
if (sharedSettings.getFormattingSettings().isExperimental()) {
XMLFormatterDocumentNew formatterDocument = new XMLFormatterDocumentNew(xmlDocument, range,
sharedSettings, getFormatterParticipants());
if (sharedSettings.getFormattingSettings().isLegacy()) {
XMLFormatterDocumentOld formatterDocument = new XMLFormatterDocumentOld(xmlDocument.getTextDocument(),
range, sharedSettings, getFormatterParticipants());
return formatterDocument.format();
}
XMLFormatterDocument formatterDocument = new XMLFormatterDocument(xmlDocument.getTextDocument(), range,
XMLFormatterDocument formatterDocument = new XMLFormatterDocument(xmlDocument, range,
sharedSettings, getFormatterParticipants());
return formatterDocument.format();
} catch (BadLocationException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.eclipse.lemminx.dom.DOMElement;
import org.eclipse.lemminx.extensions.contentmodel.model.CMDocument;
import org.eclipse.lemminx.services.format.FormatElementCategory;
import org.eclipse.lemminx.services.format.XMLFormatterDocumentNew;
import org.eclipse.lemminx.services.format.XMLFormatterDocument;
import org.eclipse.lemminx.services.format.XMLFormattingConstraints;
import org.eclipse.lemminx.settings.SharedSettings;
import org.eclipse.lemminx.settings.XMLFormattingOptions;
Expand Down Expand Up @@ -66,7 +66,7 @@ default boolean formatAttributeValue(String name, String valueWithoutQuote, Char
* @param edits the text edit list
* @return true if the given attribute can be formatted and false otherwise.
*/
default boolean formatAttributeValue(DOMAttr attr, XMLFormatterDocumentNew formatterDocument,
default boolean formatAttributeValue(DOMAttr attr, XMLFormatterDocument formatterDocument,
XMLFormattingConstraints parentConstraints, XMLFormattingOptions formattingOptions, List<TextEdit> edits) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
*/
public class DOMAttributeFormatter {

private final XMLFormatterDocumentNew formatterDocument;
private final XMLFormatterDocument formatterDocument;

public DOMAttributeFormatter(XMLFormatterDocumentNew formatterDocument) {
public DOMAttributeFormatter(XMLFormatterDocument formatterDocument) {
this.formatterDocument = formatterDocument;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
* DOM CDATA section formatter.
*/
public class DOMCDATAFormatter {
private final XMLFormatterDocumentNew formatterDocument;
private final XMLFormatterDocument formatterDocument;

public DOMCDATAFormatter(XMLFormatterDocumentNew formatterDocument) {
public DOMCDATAFormatter(XMLFormatterDocument formatterDocument) {
this.formatterDocument = formatterDocument;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
*/
public class DOMCommentFormatter {

private final XMLFormatterDocumentNew formatterDocument;
private final XMLFormatterDocument formatterDocument;

public DOMCommentFormatter(XMLFormatterDocumentNew formatterDocument) {
public DOMCommentFormatter(XMLFormatterDocument formatterDocument) {
this.formatterDocument = formatterDocument;
}

Expand All @@ -45,9 +45,12 @@ public void formatComment(DOMComment commentNode, XMLFormattingConstraints paren
}

int indentLevel = parentConstraints.getIndentLevel();
int tabSize = getTabSize();
int maxLineWidth = getMaxLineWidth();

if (formatterDocument.hasLineBreak(leftWhitespaceOffset, start) && startRange < start) {
replaceLeftSpacesWithIndentationPreservedNewLines(0, start, indentLevel, edits);
availableLineWidth = getMaxLineWidth() - getTabSize() * indentLevel;
availableLineWidth = maxLineWidth - tabSize * indentLevel;
}
int spaceStart = -1;
int spaceEnd = -1;
Expand Down Expand Up @@ -79,8 +82,8 @@ public void formatComment(DOMComment commentNode, XMLFormattingConstraints paren
// Add new line when the comment extends over the maximum line width
replaceLeftSpacesWithIndentation(indentLevel, spaceStart, contentStart,
true, edits);
int indentSpaces = getTabSize() * indentLevel;
availableLineWidth = getMaxLineWidth() - indentSpaces - (contentEnd + 1 - contentStart);
int indentSpaces = tabSize * indentLevel;
availableLineWidth = maxLineWidth - indentSpaces - (contentEnd + 1 - contentStart);
spaceStart = -1;
spaceEnd = -1;
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
*/
public class DOMDocTypeFormatter {

private final XMLFormatterDocumentNew formatterDocument;
private final XMLFormatterDocument formatterDocument;

public DOMDocTypeFormatter(XMLFormatterDocumentNew formatterDocument) {
public DOMDocTypeFormatter(XMLFormatterDocument formatterDocument) {
this.formatterDocument = formatterDocument;
}

Expand Down Expand Up @@ -136,7 +136,7 @@ private void formatDTDNodeDecl(DTDDeclNode nodeDecl, XMLFormattingConstraints pa
int nodeDeclStart = nodeDecl.getStart();
int indentLevel = parentConstraints.getIndentLevel();
int preservedNewLines = getPreservedNewlines();
int currentNewLineCount = XMLFormatterDocumentNew.getExistingNewLineCount(formatterDocument.getText(),
int currentNewLineCount = XMLFormatterDocument.getExistingNewLineCount(formatterDocument.getText(),
nodeDeclStart, formatterDocument.getLineDelimiter());
if (currentNewLineCount > preservedNewLines) {
// Reduce to number of new lines to the new line number specified by
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@
*/
public class DOMElementFormatter {

private final XMLFormatterDocumentNew formatterDocument;
private final XMLFormatterDocument formatterDocument;

private final DOMAttributeFormatter attributeFormatter;

public DOMElementFormatter(XMLFormatterDocumentNew formatterDocument, DOMAttributeFormatter attributeFormatter) {
public DOMElementFormatter(XMLFormatterDocument formatterDocument, DOMAttributeFormatter attributeFormatter) {
this.formatterDocument = formatterDocument;
this.attributeFormatter = attributeFormatter;
}

public void formatElement(DOMElement element, XMLFormattingConstraints parentConstraints, int start, int end,
List<TextEdit> edits) {
EmptyElements emptyElements = getEmptyElements(element,
formatterDocument.getFormatElementCategory(element, parentConstraints));
FormatElementCategory formatElementCategory = getFormatElementCategory(element, parentConstraints);
EmptyElements emptyElements = getEmptyElements(element, formatElementCategory);

// Format start tag element with proper indentation
int indentLevel = parentConstraints.getIndentLevel();
Expand All @@ -63,7 +63,7 @@ public void formatElement(DOMElement element, XMLFormattingConstraints parentCon
if ((element.isClosed())) {
constraints.setIndentLevel(indentLevel + 1);
}
constraints.setFormatElementCategory(getFormatElementCategory(element, parentConstraints));
constraints.setFormatElementCategory(formatElementCategory);

formatChildren(element, constraints, start, end, edits);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
*/
public class DOMProcessingInstructionFormatter {

private final XMLFormatterDocumentNew formatterDocument;
private final XMLFormatterDocument formatterDocument;

private final DOMAttributeFormatter attributeFormatter;

public DOMProcessingInstructionFormatter(XMLFormatterDocumentNew formatterDocument,
public DOMProcessingInstructionFormatter(XMLFormatterDocument formatterDocument,
DOMAttributeFormatter attributeFormatter) {
this.formatterDocument = formatterDocument;
this.attributeFormatter = attributeFormatter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
*/
public class DOMTextFormatter {

private final XMLFormatterDocumentNew formatterDocument;
private final XMLFormatterDocument formatterDocument;

public DOMTextFormatter(XMLFormatterDocumentNew formatterDocument) {
public DOMTextFormatter(XMLFormatterDocument formatterDocument) {
this.formatterDocument = formatterDocument;
}

Expand Down Expand Up @@ -139,7 +139,6 @@ public void formatText(DOMText textNode, XMLFormattingConstraints parentConstrai
}
}
if (formatElementCategory != FormatElementCategory.IgnoreSpace && spaceEnd + 1 != text.length()) {
DOMElement parentElement = textNode.getParentElement();
// Don't format final spaces if text is at the end of the file
if ((!containsNewLine || isJoinContentLines() || isMixedContent)
&& (!isMaxLineWidthSupported() || availableLineWidth >= 0)) {
Expand All @@ -161,8 +160,7 @@ public void formatText(DOMText textNode, XMLFormattingConstraints parentConstrai
edits);
availableLineWidth = getMaxLineWidth() - (textEnd - textStart) - mixedContentIndentLevel * getTabSize();
} else {
if (formatElementCategory == FormatElementCategory.NormalizeSpace
|| parentElement.getLastChild() == textNode) {
if (formatElementCategory == FormatElementCategory.NormalizeSpace) {
// Decrement indent level if is mixed content and text content is the last child
indentLevel--;
}
Expand Down
Loading