Skip to content

Commit

Permalink
Improve error range for ETagUnterminated (#877)
Browse files Browse the repository at this point in the history
Fixes #875

Signed-off-by: azerr <[email protected]>

Co-authored-by: azerr <[email protected]>
  • Loading branch information
angelozerr and angelozerr authored Sep 16, 2020
1 parent 044bef0 commit 3ea8abb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public static Range toLSPRange(XMLLocator location, XMLSyntaxErrorCode code, Obj
String attrValue = getString(arguments[0]);
return XMLPositionUtility.selectAttributeValueByGivenValueAt(attrValue, offset, document);
}
case ETagUnterminated:
case ETagUnterminated: {
/**
* Cases:
*
Expand All @@ -204,7 +204,14 @@ public static Range toLSPRange(XMLLocator location, XMLSyntaxErrorCode code, Obj
*
* <a> <a> </a> </b
*/
return XMLPositionUtility.selectPreviousNodesEndTag(offset, document);
String text = document.getText();
char ch = text.charAt(offset);
while(Character.isWhitespace(ch)) {
offset--;
ch = text.charAt(offset);
}
return XMLPositionUtility.selectEndTagName(offset, document);
}
case CustomETag:
return XMLPositionUtility.selectEndTagName(offset, document);
case ETagRequired: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,61 +497,6 @@ public static Range selectEndTagName(DOMElement element, boolean localNameOnly)
return null;
}

/**
* Finds the offset of the first tag it comes across behind the given offset.
*
* <p>
* This will include the tag it starts in if the offset is within a tag's
* content:
* </p>
* <p>
* {@code <a> <b> | </b> </a> } , will give {@code </b>}
* </p>
*
* or within an unclosed end tag:
*
* <p>
* {@code <a> <b> </b> </a| <c>} , will give {@code </a>}
* </p>
*
*
* <p>
* {@code <a> <b|> </b> </a>} , will give {@code </a>}
* </p>
*
*/
public static Range selectPreviousNodesEndTag(int offset, DOMDocument document) {

DOMNode node = null;
DOMNode nodeAt = document.findNodeAt(offset);
if (nodeAt != null && nodeAt.isElement()) {
node = nodeAt;
} else {
DOMNode nodeBefore = document.findNodeBefore(offset);
if (nodeBefore != null && nodeBefore.isElement()) {
node = nodeBefore;
}
}
if (node != null) {
DOMElement element = (DOMElement) node;
if (element.isClosed() && !element.isEndTagClosed()) {
return selectEndTagName(element.getEnd(), document);
}
}

// boolean firstBracket = false;
int i = offset;
char c = document.getText().charAt(i);
while (i >= 0) {
if (c == '>') {
return selectEndTagName(i, document);
}
i--;
c = document.getText().charAt(i);
}
return null;
}

// ------------ Entities selection

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,41 @@ public void testETagUnterminated2() throws Exception {
testDiagnosticsFor(xml, d(3, 4, 3, 5, XMLSyntaxErrorCode.ETagUnterminated));
}

/**
* Test ETagUnterminated
*
* @see https://wiki.xmldation.com/Support/Validator/ETagUnterminated
* @throws Exception
*/
@Test
public void testETagUnterminated3() throws Exception {
String xml = "<foo>\r\n" + //
" <ba><ABCD></ABCD></bar>\r\n" + // // <-- error
"</foo>";
testDiagnosticsFor(xml, d(1, 21, 1, 24, XMLSyntaxErrorCode.ETagUnterminated));
}

/**
* Test ETagUnterminated
*
* @see https://wiki.xmldation.com/Support/Validator/ETagUnterminated
* @throws Exception
*/
@Test
public void testETagUnterminated4() throws Exception {
String xml = "<project>\r\n" + //
" <dependencies>\r\n" + //
" <dependency>\r\n" + //
" <scope>test</scope>\r\n" + //
" </dependency\r\n" + // <-- error
" <dependency>\r\n" + //
" <scope>test</scope>\r\n" + //
" </dependency>\r\n" + //
" </dependencies>\r\n" + //
"</project>";
testDiagnosticsFor(xml, d(4, 6, 4, 16, XMLSyntaxErrorCode.ETagUnterminated));
}

@Test
public void testETagRequiredWithText() throws Exception {
String xml = "<root>\r\n" + //
Expand Down

0 comments on commit 3ea8abb

Please sign in to comment.