From 68d929216d7d459cca1309f0a23eaad09ca825ca Mon Sep 17 00:00:00 2001 From: Nikolas Komonen Date: Fri, 18 Oct 2019 14:32:31 -0400 Subject: [PATCH] cvc pattern error range fix Fixes #554 This PR falls back to the range of the text content if it matches Signed-off-by: Nikolas Komonen --- .../contentmodel/participants/XMLSchemaErrorCode.java | 10 +++++++--- .../org/eclipse/lsp4xml/utils/XMLPositionUtility.java | 2 +- .../contentmodel/XMLSchemaDiagnosticsTest.java | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/contentmodel/participants/XMLSchemaErrorCode.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/contentmodel/participants/XMLSchemaErrorCode.java index d789fb733..f230497f4 100644 --- a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/contentmodel/participants/XMLSchemaErrorCode.java +++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/contentmodel/participants/XMLSchemaErrorCode.java @@ -154,8 +154,12 @@ public static Range toLSPRange(XMLLocator location, XMLSchemaErrorCode code, Obj return XMLPositionUtility.selectAttributeFromGivenNameAt(attrName, offset, document); } case cvc_pattern_valid: { - String attrValue = getString(arguments[0]); - return XMLPositionUtility.selectAttributeValueByGivenValueAt(attrValue, offset, document); + String value = getString(arguments[0]); + Range result = XMLPositionUtility.selectAttributeValueByGivenValueAt(value, offset, document); + if(result != null) { + return result; + } + return XMLPositionUtility.selectTrimmedText(offset, document); } case SchemaLocation: case schema_reference_4: { @@ -209,7 +213,7 @@ public static Range toLSPRange(XMLLocator location, XMLSchemaErrorCode code, Obj DOMElement element = (DOMElement) document.findNodeAt(offset); if (DOMUtils.containsTextOnly(element)) { - return XMLPositionUtility.selectTextTrimmed(offset, document); + return XMLPositionUtility.selectTrimmedText(offset, document); } else { return XMLPositionUtility.selectFirstChild(offset, document); } diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/utils/XMLPositionUtility.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/utils/XMLPositionUtility.java index 7bf8f5e9b..9bf409ab9 100644 --- a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/utils/XMLPositionUtility.java +++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/utils/XMLPositionUtility.java @@ -565,7 +565,7 @@ public static Range selectContent(int offset, DOMDocument document) { * @return range covering the trimmed text belonging to the node * located at offset */ - public static Range selectTextTrimmed(int offset, DOMDocument document) { + public static Range selectTrimmedText(int offset, DOMDocument document) { DOMNode node = document.findNodeAt(offset); if (node == null || !node.isElement()) { return null; diff --git a/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/contentmodel/XMLSchemaDiagnosticsTest.java b/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/contentmodel/XMLSchemaDiagnosticsTest.java index 8e30099ee..b07088d96 100644 --- a/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/contentmodel/XMLSchemaDiagnosticsTest.java +++ b/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/contentmodel/XMLSchemaDiagnosticsTest.java @@ -406,7 +406,7 @@ public void cvc_pattern_valid_With_Buffer() throws Exception { " FIX_ERROR_RANGE_HERE\r\n" + // <-- Error should follow pattern [0-9]{8} " \r\n" + ""; - Diagnostic patternValid = d(6, 47, 6, 47, XMLSchemaErrorCode.cvc_pattern_valid); + Diagnostic patternValid = d(6, 17, 6, 37, XMLSchemaErrorCode.cvc_pattern_valid); Diagnostic cvcType313 = d(6, 17, 6, 37, XMLSchemaErrorCode.cvc_type_3_1_3); Diagnostic cvcType24b = d(3, 5, 3, 10, XMLSchemaErrorCode.cvc_complex_type_2_4_b); testDiagnosticsFor(xml, patternValid, cvcType313, cvcType24b);