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

Fix tests on Windows OS #1306

Merged
merged 1 commit into from
Oct 4, 2022
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 @@ -388,6 +388,39 @@ public boolean isEndTagClosed() {
return getEndTagCloseOffset() != NULL_VALUE;
}

/**
* Returns the offset after the start tag name.
*
* <code>
* <foo| ></foo>
* </code>
*
* @return the offset after the start tag name.
*/
public int getOffsetAfterStartTag() {
if (hasTagName()) {
return getStartTagOpenOffset() + getTagName().length() + 1;
}
return getStartTagOpenOffset() + 1;
}

/**
* Returns the offset before the close of start tag name.
*
* <code>
* <foo |></foo>
* <foo |/>
* </code>
*
* @return the offset before the close of start tag name.
*/
public int getOffsetBeforeCloseOfStartTag() {
if (isSelfClosed()) {
return getEnd() - 2;
}
return getStartTagCloseOffset();
}

/**
* Returns true if the given element is an orphan end tag (which has no start
* tag, eg: </a>) and false otherwise.
Expand Down Expand Up @@ -566,11 +599,4 @@ public boolean isEmpty() {
return true;
}

public int getOffsetAfterStartTag() {
if (hasTagName()) {
return getStartTagOpenOffset() + 1;
}
return getStartTagOpenOffset() + getTagName().length() + 1;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.eclipse.lemminx.extensions.contentmodel.utils.XMLGenerator;
import org.eclipse.lemminx.services.extensions.codeaction.ICodeActionParticipant;
import org.eclipse.lemminx.services.extensions.codeaction.ICodeActionRequest;
import org.eclipse.lemminx.utils.StringUtils;
import org.eclipse.lsp4j.CodeAction;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.Position;
Expand All @@ -40,16 +39,10 @@
* Given this XML where the expected child elements are not present as defined
* in the XSD:
*
* <servlet></servlet>
* Error:
* Child elements are missing from element:
* - servlet
* <servlet></servlet> Error: Child elements are missing from element: - servlet
*
* The following elements are expected:
* - description
* - display-name
* - icon
* - servlet-name
* The following elements are expected: - description - display-name - icon -
* servlet-name
*
* To fix the error, the code action will suggest inserting the expected
* elements inside the parent tag
Expand Down Expand Up @@ -104,25 +97,22 @@ public void doCodeAction(ICodeActionRequest request, List<CodeAction> codeAction
// indent if needed
if (document.positionAt(element.getStartTagCloseOffset()).getLine() == document
.positionAt(element.getEndTagOpenOffset()).getLine()) {
int lineNum = document.positionAt(element.getStartTagCloseOffset()).getLine();
insertTextAll.append(document.getLineIndentInfo(lineNum).getLineDelimiter());
insertTextAll.append(StringUtils.getStartWhitespaces(document.lineText(lineNum)));
insertTextRequired.append(document.getLineIndentInfo(lineNum).getLineDelimiter());
insertTextRequired.append(StringUtils.getStartWhitespaces(document.lineText(lineNum)));
insertTextAll.append(generator.getLineDelimiter());
insertTextAll.append(generator.getWhitespacesIndent());
insertTextRequired.append(generator.getLineDelimiter());
insertTextRequired.append(generator.getWhitespacesIndent());
}

String insertStrAll = insertTextAll.toString();
String insertStrRequired = insertTextRequired.toString();

CodeAction insertAllExpectedElement = CodeActionFactory.insert(
"Insert all expected elements", childElementPosition, insertStrAll,
document.getTextDocument(), diagnostic);
CodeAction insertAllExpectedElement = CodeActionFactory.insert("Insert all expected elements",
childElementPosition, insertStrAll, document.getTextDocument(), diagnostic);

codeActions.add(insertAllExpectedElement);

CodeAction insertRequriedExpectedElement = CodeActionFactory.insert(
"Insert only required elements", childElementPosition, insertStrRequired,
document.getTextDocument(), diagnostic);
CodeAction insertRequriedExpectedElement = CodeActionFactory.insert("Insert only required elements",
childElementPosition, insertStrRequired, document.getTextDocument(), diagnostic);

codeActions.add(insertRequriedExpectedElement);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,24 @@ public XMLGenerator(SharedSettings sharedSettings, boolean autoCloseTags, String
this.maxLevel = maxLevel;
}

/**
* Returns the line delimiter.
*
* @return the line delimiter.
*/
public String getLineDelimiter() {
return lineDelimiter;
}

/**
* Returns the whitespaces indentation.
*
* @return the whitespaces indentation.
*/
public String getWhitespacesIndent() {
return whitespacesIndent;
}

/**
* Returns the XML generated from the given element declaration.
*
Expand Down Expand Up @@ -113,16 +131,16 @@ public String generate(CMElementDeclaration elementDeclaration, String prefix, b
}

private int generate(CMElementDeclaration elementDeclaration, String prefix, boolean generateEndTag,
boolean generateOnlyChildren, int level,
int snippetIndex, XMLBuilder xml, List<CMElementDeclaration> generatedElements,
Collection<String> existingElementNames, boolean generateOnlyRequired) {
boolean generateOnlyChildren, int level, int snippetIndex, XMLBuilder xml,
List<CMElementDeclaration> generatedElements, Collection<String> existingElementNames,
boolean generateOnlyRequired) {

if (generateOnlyChildren) {
Collection<CMElementDeclaration> childElements = elementDeclaration.getElements();
for (CMElementDeclaration child : childElements) {
if (isGenerateChild(elementDeclaration, generateOnlyRequired, child.getName())) {
snippetIndex = generate(child, prefix, true, false, level + 1, snippetIndex, xml,
generatedElements, existingElementNames, generateOnlyRequired);
snippetIndex = generate(child, prefix, true, false, level + 1, snippetIndex, xml, generatedElements,
existingElementNames, generateOnlyRequired);
}
}
return snippetIndex;
Expand All @@ -149,8 +167,8 @@ private int generate(CMElementDeclaration elementDeclaration, String prefix, boo
for (CMElementDeclaration child : children) {
if (isGenerateChild(elementDeclaration, generateOnlyRequired, child.getName())) {
level++;
snippetIndex = generate(child, prefix, true, false, level, snippetIndex, xml,
generatedElements, existingElementNames, generateOnlyRequired);
snippetIndex = generate(child, prefix, true, false, level, snippetIndex, xml, generatedElements,
existingElementNames, generateOnlyRequired);
level--;
xml.linefeed();
xml.indent(level);
Expand Down Expand Up @@ -401,8 +419,7 @@ public static MarkupContent createMarkupContent(CMElementDeclaration cmElement,
*/
private boolean isGenerateChild(CMElementDeclaration elementDeclaration, boolean generateOnlyRequired,
String childName) {
if (!generateOnlyRequired
|| (!elementDeclaration.isOptional(childName) && generateOnlyRequired)) {
if (!generateOnlyRequired || (!elementDeclaration.isOptional(childName) && generateOnlyRequired)) {
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,22 +159,25 @@ public String getCurrentLineIndent(XMLBuilder xml, XMLFormattingOptions formatti
}

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

XSISchemaLocationSplit split = XSISchemaLocationSplit.getSplit(formattingOptions);

if (split == XSISchemaLocationSplit.none || !XSISchemaModel.isXSISchemaLocationAttr(attr.getName(), attr)
|| getFirstContentOffset(attr.getOriginalValue()) == -1) {
if (split == XSISchemaLocationSplit.none || !XSISchemaModel.isXSISchemaLocationAttr(attr.getName(), attr)) {
return false;
}

int firstContentOffset = getFirstContentOffset(attr.getOriginalValue());
if (firstContentOffset == -1) {
return false;
}
int attrValueStart = attr.getNodeAttrValue().getStart();
// Remove extra spaces between start of xsi:schemaLocation attribute value quote
// and actual value
formatterDocument.removeLeftSpaces(attrValueStart + 1, // <... xsi:schemaLocation="| value"
// <... xsi:schemaLocation=" |value"
attrValueStart + getFirstContentOffset(attr.getOriginalValue()), edits);
attrValueStart + firstContentOffset, edits);

int tabSize = formattingOptions.getTabSize();
int indentSpaceOffset;
Expand All @@ -184,8 +187,8 @@ public boolean formatAttributeValue(DOMAttr attr, XMLFormatterDocumentNew format
indentSpaceOffset = (attrValueStart + 1) - attr.getNodeAttrName().getStart()
+ formattingOptions.getSplitAttributesIndentSize() * tabSize;
} else if (formattingOptions.isPreserveAttributeLineBreaks()) {
indentSpaceOffset = attrValueStart
- formatterDocument.getOffsetWithPreserveLineBreaks(startOfLineOffset, attrValueStart, tabSize, formattingOptions.isInsertSpaces());
indentSpaceOffset = attrValueStart - formatterDocument.getOffsetWithPreserveLineBreaks(startOfLineOffset,
attrValueStart, tabSize, formattingOptions.isInsertSpaces());
} else {
indentSpaceOffset = formatterDocument.getNormalizedLength(startOfLineOffset, attrValueStart + 1)
- startOfLineOffset;
Expand All @@ -195,15 +198,14 @@ public boolean formatAttributeValue(DOMAttr attr, XMLFormatterDocumentNew format
int locationNum = 1;
String attrValue = attr.getOriginalValue();

for (int i = 0; i < attrValue.length(); i++) {
int from = formatterDocument.getLeftWhitespacesOffset(attrValueStart, attrValueStart + i + 1);
for (int i = firstContentOffset; i < attrValue.length(); i++) {
int from = formatterDocument.adjustOffsetWithLeftWhitespaces(attrValueStart, attrValueStart + i + 1);
if (Character.isWhitespace(attrValue.charAt(i)) && !Character.isWhitespace(attrValue.charAt(i + 1))
&& !StringUtils.isQuote(attrValue.charAt(from - attrValueStart))) {
// Insert newline and indent where required based on setting
if (locationNum % lineFeed == 0) {
formatterDocument.replaceLeftSpacesWithIndentationWithOffsetSpaces(indentSpaceOffset,
attrValueStart + i + 1,
true, edits);
attrValueStart, attrValueStart + i + 1, true, edits);
} else {
formatterDocument.replaceLeftSpacesWithOneSpace(indentSpaceOffset, attrValueStart + i + 1, edits);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
import java.util.List;

import org.eclipse.lemminx.dom.DOMAttr;
import org.eclipse.lsp4j.TextEdit;
import org.eclipse.lemminx.settings.EnforceQuoteStyle;
import org.eclipse.lemminx.utils.StringUtils;
import org.eclipse.lemminx.settings.XMLFormattingOptions;
import org.eclipse.lsp4j.TextEdit;

/**
* DOM attribute formatter.
Expand All @@ -42,12 +41,12 @@ public void formatAttribute(DOMAttr attr, int prevOffset, boolean singleAttribut
if (useSettings) {
int indentLevel = parentConstraints.getIndentLevel();
if (isPreserveAttributeLineBreaks() && hasLineBreak(prevOffset, attr.getStart())) {
replaceLeftSpacesWithIndentation(indentLevel + 1, attr.getStart(), true, edits);
replaceLeftSpacesWithIndentation(indentLevel + 1, prevOffset, attr.getStart(), true, edits);
alreadyIndented = true;
} else if (isSplitAttributes() && !singleAttribute) {
// move the attribute to a new line and indent it.
replaceLeftSpacesWithIndentation(indentLevel + getSplitAttributesIndentSize(), attr.getStart(), true,
edits);
replaceLeftSpacesWithIndentation(indentLevel + getSplitAttributesIndentSize(), prevOffset,
attr.getStart(), true, edits);
alreadyIndented = true;
}
}
Expand Down Expand Up @@ -79,7 +78,7 @@ public void formatAttribute(DOMAttr attr, int prevOffset, boolean singleAttribut
int attrValueStart = attr.getNodeAttrValue().getStart(); // <foo attr = |""
removeLeftSpaces(delimiterOffset, attrValueStart, edits);
}
formatterDocument.formatAttributeValue(attr, formatterDocument, parentConstraints.getIndentLevel(), getFormattingSettings(), edits);
formatAttributeValue(attr, parentConstraints.getIndentLevel(), edits);
}

// replace current quote with preferred quote in case of attribute value
Expand All @@ -88,26 +87,33 @@ public void formatAttribute(DOMAttr attr, int prevOffset, boolean singleAttribut
// --> <a name='value'> </a>
String originalValue = attr.getOriginalValue();
if (getEnforceQuoteStyle() == EnforceQuoteStyle.preferred && originalValue != null) {
if (originalValue.charAt(0) != getQuotationAsChar()
&& StringUtils.isQuote(originalValue.charAt(0))) {
formatterDocument.replaceQuoteWithPreferred(attr.getNodeAttrValue().getStart(),
attr.getNodeAttrValue().getStart() + 1, getQuotationAsString(), edits);
if (originalValue.charAt(0) != getQuotationAsChar() && StringUtils.isQuote(originalValue.charAt(0))) {
replaceQuoteWithPreferred(attr.getNodeAttrValue().getStart(), attr.getNodeAttrValue().getStart() + 1,
edits);
}
if (originalValue.charAt(originalValue.length() - 1) != getQuotationAsChar()
&& StringUtils.isQuote(originalValue.charAt(originalValue.length() - 1))) {
formatterDocument.replaceQuoteWithPreferred(attr.getNodeAttrValue().getEnd() - 1,
attr.getNodeAttrValue().getEnd(), getQuotationAsString(), edits);
replaceQuoteWithPreferred(attr.getNodeAttrValue().getEnd() - 1, attr.getNodeAttrValue().getEnd(),
edits);
}
}
}

private void formatAttributeValue(DOMAttr attr, int indentLevel, List<TextEdit> edits) {
formatterDocument.formatAttributeValue(attr, indentLevel, edits);
}

private void replaceQuoteWithPreferred(int from, int to, List<TextEdit> edits) {
formatterDocument.replaceQuoteWithPreferred(from, to, edits);
}

private void replaceLeftSpacesWithOneSpace(int from, int to, List<TextEdit> edits) {
formatterDocument.replaceLeftSpacesWithOneSpace(from, to, edits);
}

private void replaceLeftSpacesWithIndentation(int indentLevel, int offset, boolean addLineSeparator,
private void replaceLeftSpacesWithIndentation(int indentLevel, int leftLimit, int to, boolean addLineSeparator,
List<TextEdit> edits) {
formatterDocument.replaceLeftSpacesWithIndentation(indentLevel, offset, addLineSeparator, edits);
formatterDocument.replaceLeftSpacesWithIndentation(indentLevel, leftLimit, to, addLineSeparator, edits);
}

private void removeLeftSpaces(int from, int to, List<TextEdit> edits) {
Expand All @@ -122,10 +128,6 @@ private int getSplitAttributesIndentSize() {
return formatterDocument.getSharedSettings().getFormattingSettings().getSplitAttributesIndentSize();
}

private XMLFormattingOptions getFormattingSettings() {
return formatterDocument.getSharedSettings().getFormattingSettings();
}

boolean isPreserveAttributeLineBreaks() {
return formatterDocument.getSharedSettings().getFormattingSettings().isPreserveAttributeLineBreaks();
}
Expand All @@ -138,10 +140,6 @@ private char getQuotationAsChar() {
return formatterDocument.getSharedSettings().getPreferences().getQuotationAsChar();
}

private String getQuotationAsString() {
return formatterDocument.getSharedSettings().getPreferences().getQuotationAsString();
}

private EnforceQuoteStyle getEnforceQuoteStyle() {
return formatterDocument.getSharedSettings().getFormattingSettings().getEnforceQuoteStyle();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,19 @@ public void formatCDATASection(DOMCDATASection cDATANode, XMLFormattingConstrain
if (availableLineWidth <= 0) {
if (spaceStart != -1) {
// Add new line when the comment extends over the maximum line width
replaceLeftSpacesWithIndentation(parentConstraints.getIndentLevel(), contentStart,
true, edits);
replaceLeftSpacesWithIndentation(parentConstraints.getIndentLevel(), spaceStart,
contentStart, true, edits);
int indentSpaces = (getTabSize() * parentConstraints.getIndentLevel());
availableLineWidth = getMaxLineWidth() - indentSpaces - (contentEnd + 1 - contentStart);
}
} else if (spaceStart == cDATAStartContent || contentEnd == cDATAEndContent) {
// Remove spaces before and after the start and ending bracket of content
removeLeftSpaces(spaceStart + 1, contentStart, edits);
} else if (spaceStart == cDATAStartContent) {
// Remove spaces before the start bracket of content
removeLeftSpaces(spaceStart, contentStart, edits);
spaceStart = -1;
spaceEnd = -1;
} else if (contentEnd == cDATAEndContent) {
// Remove spaces after the ending bracket of content
removeLeftSpaces(spaceStart, contentEnd, edits);
spaceStart = -1;
spaceEnd = -1;
} else {
Expand Down Expand Up @@ -105,8 +110,8 @@ private void replaceSpacesWithOneSpace(int spaceStart, int spaceEnd, List<TextEd
formatterDocument.replaceSpacesWithOneSpace(spaceStart, spaceEnd, edits);
}

private int replaceLeftSpacesWithIndentation(int indentLevel, int offset, boolean addLineSeparator,
private int replaceLeftSpacesWithIndentation(int indentLevel, int from, int to, boolean addLineSeparator,
List<TextEdit> edits) {
return formatterDocument.replaceLeftSpacesWithIndentation(indentLevel, offset, addLineSeparator, edits);
return formatterDocument.replaceLeftSpacesWithIndentation(indentLevel, from, to, addLineSeparator, edits);
}
}
Loading