From 3ea8abb35e0cf396f0886c54c04eda2325db98a1 Mon Sep 17 00:00:00 2001
From: Angelo
Date: Wed, 16 Sep 2020 18:01:20 +0200
Subject: [PATCH] Improve error range for ETagUnterminated (#877)
Fixes #875
Signed-off-by: azerr
Co-authored-by: azerr
---
.../participants/XMLSyntaxErrorCode.java | 11 +++-
.../lemminx/utils/XMLPositionUtility.java | 55 -------------------
.../XMLSyntaxDiagnosticsTest.java | 35 ++++++++++++
3 files changed, 44 insertions(+), 57 deletions(-)
diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/contentmodel/participants/XMLSyntaxErrorCode.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/contentmodel/participants/XMLSyntaxErrorCode.java
index 6da8797f4..49b7d8293 100644
--- a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/contentmodel/participants/XMLSyntaxErrorCode.java
+++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/contentmodel/participants/XMLSyntaxErrorCode.java
@@ -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:
*
@@ -204,7 +204,14 @@ public static Range toLSPRange(XMLLocator location, XMLSyntaxErrorCode code, Obj
*
*
- * This will include the tag it starts in if the offset is within a tag's
- * content:
- *
- *
- * {@code | } , will give {@code }
- *
- *
- * or within an unclosed end tag:
- *
- *
- * {@code } , will give {@code }
- *
- *
- *
- *
- * {@code } , will give {@code }
- *
- *
- */
- 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
/**
diff --git a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/contentmodel/XMLSyntaxDiagnosticsTest.java b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/contentmodel/XMLSyntaxDiagnosticsTest.java
index 0c31b1e3e..272a57b8b 100644
--- a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/contentmodel/XMLSyntaxDiagnosticsTest.java
+++ b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/contentmodel/XMLSyntaxDiagnosticsTest.java
@@ -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 = "\r\n" + //
+ " \r\n" + // // <-- error
+ "";
+ 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 = "\r\n" + //
+ " \r\n" + //
+ " \r\n" + //
+ " test\r\n" + //
+ " \r\n" + //
+ " test\r\n" + //
+ " \r\n" + //
+ " \r\n" + //
+ "";
+ testDiagnosticsFor(xml, d(4, 6, 4, 16, XMLSyntaxErrorCode.ETagUnterminated));
+ }
+
@Test
public void testETagRequiredWithText() throws Exception {
String xml = "\r\n" + //