Skip to content

Commit

Permalink
Set experimental formatter to default
Browse files Browse the repository at this point in the history
Signed-off-by: Jessica He <jhe@redhat.com>
  • Loading branch information
JessicaJHee committed Nov 28, 2022
1 parent 267551a commit 2c7f337
Show file tree
Hide file tree
Showing 34 changed files with 4,680 additions and 4,703 deletions.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -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,
if (sharedSettings.getFormattingSettings().isLegacy()) {
XMLFormatterDocument formatterDocument = new XMLFormatterDocument(xmlDocument.getTextDocument(), range,
sharedSettings, getFormatterParticipants());
return formatterDocument.format();
}
XMLFormatterDocument formatterDocument = new XMLFormatterDocument(xmlDocument.getTextDocument(), range,
XMLFormatterDocumentNew formatterDocument = new XMLFormatterDocumentNew(xmlDocument, range,
sharedSettings, getFormatterParticipants());
return formatterDocument.format();
} catch (BadLocationException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -41,8 +41,8 @@ public DOMElementFormatter(XMLFormatterDocumentNew formatterDocument, DOMAttribu

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 @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import org.w3c.dom.Text;

/**
* Experimental XML formatter which generates several text edit to remove, add,
* New XML formatter which generates several text edit to remove, add,
* update spaces / indent.
*
* @author Angelo ZERR
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class XMLFormattingOptions extends org.eclipse.lemminx.settings.LSPFormat
"pre", //
"xd:pre");

private boolean experimental;
private boolean legacy;
private int maxLineWidth;

private boolean splitAttributes;
Expand Down Expand Up @@ -156,8 +156,8 @@ private void initializeDefaultSettings() {
this.setJoinCommentLines(false);
this.setJoinContentLines(false);
this.setEnabled(true);
this.setExperimental(false);
this.setMaxLineWidth(0);
this.setLegacy(false);
this.setMaxLineWidth(100);
this.setSpaceBeforeEmptyCloseTag(true);
this.setPreserveEmptyContent(false);
this.setPreservedNewlines(DEFAULT_PRESERVER_NEW_LINES);
Expand Down Expand Up @@ -234,22 +234,22 @@ public void setJoinContentLines(final boolean joinContentLines) {
}

/**
* Returns true if the experimental formatter must be used and false otherwise.
* Returns true if the legacy formatter must be used and false otherwise.
*
* @return true if the experimental formatter must be used and false otherwise.
* @return true if the legacy formatter must be used and false otherwise.
*/
public boolean isExperimental() {
return experimental;
public boolean isLegacy() {
return legacy;
}

/**
* Set true if the experimental formatter must be used and false otherwise.
* Set true if the legacy formatter must be used and false otherwise.
*
* @param experimental true if the experimental formatter must be used and false
* @param legacy true if the legacy formatter must be used and false
* otherwise.
*/
public void setExperimental(final boolean experimental) {
this.experimental = experimental;
public void setLegacy(final boolean legacy) {
this.legacy = legacy;
}

/**
Expand Down Expand Up @@ -436,7 +436,7 @@ public XMLFormattingOptions merge(XMLFormattingOptions formattingOptions) {
setInsertSpaces(formattingOptions.isInsertSpaces());
setTrimFinalNewlines(formattingOptions.isTrimFinalNewlines());
setTrimTrailingWhitespace(formattingOptions.isTrimTrailingWhitespace());
setExperimental(formattingOptions.isExperimental());
setLegacy(formattingOptions.isLegacy());
setMaxLineWidth(formattingOptions.getMaxLineWidth());
setSplitAttributes(formattingOptions.isSplitAttributes());
setJoinCDATALines(formattingOptions.isJoinCDATALines());
Expand All @@ -453,8 +453,7 @@ public XMLFormattingOptions merge(XMLFormattingOptions formattingOptions) {
setClosingBracketNewLine(formattingOptions.getClosingBracketNewLine());
setEmptyElement(formattingOptions.getEmptyElements());
setXsiSchemaLocationSplit(formattingOptions.getXsiSchemaLocationSplit());
// Experimental settings
setExperimental(formattingOptions.isExperimental());
// New formatter settings
setPreserveSpace(formattingOptions.getPreserveSpace());
setGrammarAwareFormatting(formattingOptions.isGrammarAwareFormatting());
setMaxLineWidth(formattingOptions.getMaxLineWidth());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1726,6 +1726,7 @@ public static void assertGrammarGenerator(String xml, FileContentGeneratorSettin

public static void assertGrammarGenerator(String xml, FileContentGeneratorSettings grammarSettings,
SharedSettings sharedSettings, String expected) {
sharedSettings.getFormattingSettings().setLegacy(true);
DOMDocument document = DOMParser.getInstance().parse(xml, "test.xml", null);
XMLLanguageService languageService = new XMLLanguageService();
FileContentGeneratorManager manager = new FileContentGeneratorManager(languageService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void schema() throws IOException {
}

@Test
public void schemaWithExperimentalFormatter() throws IOException {
public void schemaWithNewFormatter() throws IOException {
String xml = "<note>\r\n" + //
" <to>Tove</to>\r\n" + //
" <from>Jani</from>\r\n" + //
Expand All @@ -72,7 +72,6 @@ public void schemaWithExperimentalFormatter() throws IOException {
" </xs:element>" + lineSeparator() + //
"</xs:schema>";
SharedSettings settings = new SharedSettings();
settings.getFormattingSettings().setExperimental(true);
settings.getFormattingSettings().setGrammarAwareFormatting(false);
assertGrammarGenerator(xml, new XMLSchemaGeneratorSettings(), settings, xsd);
}
Expand Down
Loading

0 comments on commit 2c7f337

Please sign in to comment.