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

Fixes issue with error messages not showing #557

Merged
merged 1 commit into from
Aug 19, 2019
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 @@ -10,6 +10,8 @@
*/
package org.eclipse.lsp4xml.extensions.contentmodel.participants;

import static org.eclipse.lsp4xml.utils.StringUtils.getString;

import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -119,14 +121,14 @@ public static Range toLSPRange(XMLLocator location, DTDErrorCode code, Object[]
return XMLPositionUtility.selectStartTag(offset, document);
}
case MSG_ATTRIBUTE_NOT_DECLARED: {
return XMLPositionUtility.selectAttributeValueAt((String)arguments[1], offset, document);
return XMLPositionUtility.selectAttributeValueAt(getString(arguments[1]), offset, document);
}
case MSG_FIXED_ATTVALUE_INVALID: {
String attrName = (String) arguments[1];
String attrName = getString(arguments[1]);
return XMLPositionUtility.selectAttributeValueAt(attrName, offset, document);
}
case MSG_ATTRIBUTE_VALUE_NOT_IN_LIST: {
String attrName = (String) arguments[0];
String attrName = getString(arguments[0]);
return XMLPositionUtility.selectAttributeValueAt(attrName, offset, document);
}

Expand All @@ -139,7 +141,7 @@ public static Range toLSPRange(XMLLocator location, DTDErrorCode code, Object[]
case IDREFSInvalid:
case IDREFInvalidWithNamespaces:
case IDInvalidWithNamespaces: {
String attrValue = (String) arguments[0];
String attrValue = getString(arguments[0]);
return XMLPositionUtility.selectAttributeValueByGivenValueAt(attrValue, offset, document);
}

Expand All @@ -161,7 +163,7 @@ public static Range toLSPRange(XMLLocator location, DTDErrorCode code, Object[]
Position position = document.positionAt(offset);
int line = position.getLine();
String text = document.lineText(line);
String name = (String) arguments[0];
String name = getString(arguments[0]);
String subString = "&" + name + ";";
int start = text.indexOf(subString);
int end = start + subString.length();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/
package org.eclipse.lsp4xml.extensions.contentmodel.participants;

import static org.eclipse.lsp4xml.utils.StringUtils.getString;

import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -134,11 +136,11 @@ public static Range toLSPRange(XMLLocator location, XMLSchemaErrorCode code, Obj
case TargetNamespace_2:
return XMLPositionUtility.selectStartTag(offset, document);
case cvc_complex_type_3_2_2: {
String attrName = (String) arguments[1];
String attrName = getString(arguments[1]);
return XMLPositionUtility.selectAttributeNameFromGivenNameAt(attrName, offset, document);
}
case cvc_elt_3_1: {
String namespaceAntAttrName = (String) arguments[1]; // http://www.w3.org/2001/XMLSchema-instance,nil
String namespaceAntAttrName = getString(arguments[1]); // http://www.w3.org/2001/XMLSchema-instance,nil
String attrName = namespaceAntAttrName;
int index = namespaceAntAttrName.indexOf(",");
if (index != -1) {
Expand All @@ -152,7 +154,7 @@ public static Range toLSPRange(XMLLocator location, XMLSchemaErrorCode code, Obj
return XMLPositionUtility.selectAttributeFromGivenNameAt(attrName, offset, document);
}
case cvc_pattern_valid: {
String attrValue = (String) arguments[0];
String attrValue = getString(arguments[0]);
return XMLPositionUtility.selectAttributeValueByGivenValueAt(attrValue, offset, document);
}
case SchemaLocation:
Expand Down Expand Up @@ -184,7 +186,7 @@ public static Range toLSPRange(XMLLocator location, XMLSchemaErrorCode code, Obj
case cvc_attribute_3:
case cvc_complex_type_3_1:
case cvc_elt_4_2: {
String attrName = (String) arguments[1];
String attrName = getString(arguments[1]);
return XMLPositionUtility.selectAttributeValueAt(attrName, offset, document);
}
case cvc_type_3_1_1:
Expand All @@ -203,7 +205,7 @@ public static Range toLSPRange(XMLLocator location, XMLSchemaErrorCode code, Obj
case cvc_minInclusive_valid: {
// this error can occur for attribute value or text
// Try for attribute value
String attrValue = (String) arguments[0];
String attrValue = getString(arguments[0]);
Range range = XMLPositionUtility.selectAttributeValueFromGivenValue(attrValue, offset, document);
if (range != null) {
return range;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
package org.eclipse.lsp4xml.extensions.contentmodel.participants;

import static org.eclipse.lsp4xml.utils.StringUtils.getString;
import static org.eclipse.lsp4xml.utils.XMLPositionUtility.selectCurrentTagOffset;

import java.util.HashMap;
Expand Down Expand Up @@ -103,33 +104,33 @@ public static Range toLSPRange(XMLLocator location, XMLSyntaxErrorCode code, Obj
case ElementUnterminated:
return XMLPositionUtility.selectStartTag(offset, document);
case EqRequiredInAttribute: {
String attrName = (String) arguments[1];
String attrName = getString(arguments[1]);
return XMLPositionUtility.selectAttributeNameFromGivenNameAt(attrName, offset, document);
}
case EncodingDeclRequired:
case EqRequiredInXMLDecl:
return XMLPositionUtility.selectAttributeNameAt(offset, document);
case AttributeNSNotUnique: {
String attrName = (String) arguments[1];
String attrName = getString(arguments[1]);
Range xmlns = XMLPositionUtility.selectAttributeNameFromGivenNameAt("xmlns:" + attrName, offset, document);
if (xmlns != null) {
return xmlns;
}
return XMLPositionUtility.selectAttributeNameFromGivenNameAt(attrName, offset, document);
}
case AttributeNotUnique: {
String attrName = (String) arguments[1];
String attrName = getString(arguments[1]);
return XMLPositionUtility.selectAttributeNameFromGivenNameAt(attrName, offset, document);
}
case AttributePrefixUnbound: {
return XMLPositionUtility.selectAttributePrefixFromGivenNameAt((String) arguments[1], offset, document);
return XMLPositionUtility.selectAttributePrefixFromGivenNameAt(getString(arguments[1]), offset, document);
}
case LessthanInAttValue: {
String attrName = (String) arguments[1];
String attrName = getString(arguments[1]);
return XMLPositionUtility.selectAttributeValueAt(attrName, offset, document);
}
case QuoteRequiredInXMLDecl: {
String attrName = (String) arguments[0];
String attrName = getString(arguments[0]);
return XMLPositionUtility.selectAttributeValueAt(attrName, offset, document);
}
case EmptyPrefixedAttName: {
Expand All @@ -138,7 +139,7 @@ public static Range toLSPRange(XMLLocator location, XMLSyntaxErrorCode code, Obj
}
case SDDeclInvalid:
case VersionNotSupported: {
String attrValue = (String) arguments[0];
String attrValue = getString(arguments[0]);
return XMLPositionUtility.selectAttributeValueByGivenValueAt(attrValue, offset, document);
}
case ETagUnterminated:
Expand All @@ -155,7 +156,7 @@ public static Range toLSPRange(XMLLocator location, XMLSyntaxErrorCode code, Obj
case CustomETag:
return XMLPositionUtility.selectEndTag(offset, document);
case ETagRequired: {
String tag = (String) arguments[0];
String tag = getString(arguments[0]);
return XMLPositionUtility.selectChildEndTag(tag, offset, document);
}
case ContentIllegalInProlog: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/
package org.eclipse.lsp4xml.extensions.xsd.participants;

import static org.eclipse.lsp4xml.utils.StringUtils.getString;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -97,7 +99,7 @@ public static Range toLSPRange(XMLLocator location, XSDErrorCode code, Object[]
// adjust positions
switch (code) {
case cos_all_limited_2: {
String nameValue = (String) arguments[1];
String nameValue = getString(arguments[1]);
DOMNode parent = document.findNodeAt(offset);
List<DOMNode> children = parent.getChildrenWithAttributeValue("name", nameValue);

Expand All @@ -109,7 +111,7 @@ public static Range toLSPRange(XMLLocator location, XSDErrorCode code, Object[]
return XMLPositionUtility.selectAttributeValueAt("maxOccurs", offset, document);
}
case ct_props_correct_3: {
String attrValue = (String) arguments[0];
String attrValue = getString(arguments[0]);
if (attrValue.charAt(0) == ':') {
attrValue = attrValue.substring(1);
}
Expand All @@ -128,28 +130,28 @@ public static Range toLSPRange(XMLLocator location, XSDErrorCode code, Object[]
case src_import_1_2:
return XMLPositionUtility.selectStartTag(offset, document);
case s4s_att_not_allowed: {
String attrName = (String) arguments[1];
String attrName = getString(arguments[1]);
return XMLPositionUtility.selectAttributeNameFromGivenNameAt(attrName, offset, document);
}
case s4s_att_invalid_value: {
String attrName = (String) arguments[1];
String attrName = getString(arguments[1]);
return XMLPositionUtility.selectAttributeValueAt(attrName, offset, document);
}
case s4s_elt_character:
return XMLPositionUtility.selectContent(offset, document);
case sch_props_correct_2: {
String argument = (String) arguments[0];
String argument = getString(arguments[0]);
String attrName = argument.substring(argument.indexOf(",") + 1);
return XMLPositionUtility.selectAttributeValueFromGivenValue(attrName, offset, document);
}
case src_ct_1:
return XMLPositionUtility.selectAttributeValueAt("base", offset, document);
case src_resolve_4_2: {
String attrValue = (String) arguments[2];
String attrValue = getString(arguments[2]);
return XMLPositionUtility.selectAttributeValueByGivenValueAt(attrValue, offset, document);
}
case src_resolve: {
String attrValue = (String) arguments[0];
String attrValue = getString(arguments[0]);
return XMLPositionUtility.selectAttributeValueByGivenValueAt(attrValue, offset, document);
}
case EmptyTargetNamespace:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.eclipse.lsp4xml.extensions.contentmodel.participants.XMLSchemaErrorCode;

import static org.eclipse.lsp4xml.dom.parser.Constants.*;
import static org.eclipse.lsp4xml.utils.StringUtils.getString;

/**
* SchemaMessageProvider implements an XMLMessageProvider that provides
Expand Down Expand Up @@ -220,19 +221,19 @@ private static Matcher getNamespaceMatcher(String name) {
* @return
*/
private static Object[] cvc_2_4_a_solution(Object[] arguments) {
Matcher m = getNamespaceMatcher((String) arguments[0]);
Matcher m = getNamespaceMatcher(getString(arguments[0]));
String schema = null;
String name = null;
String validNames = null;

if (m.matches()) {
name = m.group(2);
schema = "{" + m.group(1) + "}";
validNames = reformatElementNames(true, (String)arguments[1]);
validNames = reformatElementNames(true, getString(arguments[1]));
} else { // No namespace, so just element name
name = (String) arguments[0];
name = getString(arguments[0]);
schema = "{the schema}";
validNames = reformatElementNames(false, (String)arguments[1]);
validNames = reformatElementNames(false, getString(arguments[1]));
}
name = "- " + name;
return new Object[] { name, validNames, schema };
Expand All @@ -255,25 +256,25 @@ private static Object[] cvc_2_4_a_solution(Object[] arguments) {
* @return
*/
private static Object[] cvc_2_4_b_solution(Object[] arguments) {
Matcher m = getNamespaceMatcher((String) arguments[1]);
Matcher m = getNamespaceMatcher(getString(arguments[1]));

String element = null;
String missingChildElements = null;
String schema = null;

if (m.matches()) {
missingChildElements = reformatElementNames(true, (String)arguments[1]);
missingChildElements = reformatElementNames(true, getString(arguments[1]));
schema = "{" + m.group(1) + "}";
} else {
// No namespace, so just element name
missingChildElements = reformatElementNames(false, (String)arguments[1]);
missingChildElements = reformatElementNames(false, getString(arguments[1]));
schema = "{the schema}";
}
element = "- " + (String) arguments[0];
element = "- " + getString(arguments[0]);
return new Object[] { element, missingChildElements , schema };
}

public static Object[] enumeration_valid_solution(Object[] arguments) {
return new Object[] { (String) arguments[0], reformatArrayElementNames((String)arguments[1])};
return new Object[] { getString(arguments[0]), reformatArrayElementNames(getString(arguments[1]))};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -352,4 +352,11 @@ public static int findExprBeforeAt(String text, String expr, int offset) {
return startOffset - 1;
}

public static String getString(Object obj) {
if(obj != null) {
return obj.toString();
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ public static Range selectAttributeNameFromGivenNameAt(String attrName, int offs
* returned
*/
public static Range selectAttributePrefixFromGivenNameAt(String attrName, int offset, DOMDocument document) {
if(attrName == null) {
return null;
}
DOMNode element = document.findNodeAt(offset);
int prefixLength = attrName.indexOf(':');
if (element != null && element.hasAttributes()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,24 @@ public void cvc_pattern_valid() throws Exception {
testDiagnosticsFor(xml, patternValid, cvcAttribute3);
}

@Test
public void cvc_pattern_valid_With_Buffer() throws Exception {
String xml =
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\r\n" +
"<cpr xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \r\n" +
" xsi:noNamespaceSchemaLocation=\"https://www.dgai.de/cpr/schema/ev/cpr-ev-2.0.xsd\">\r\n" +
" <cprev>\r\n" +
" <VERSION>2.0</VERSION>\r\n" +
" <DATUM>2019-08-09</DATUM>\r\n" +
" <STOKENN>FIX_ERROR_RANGE_HERE</STOKENN>\r\n" + // <-- Error should follow pattern [0-9]{8}
" </cprev>\r\n" +
"</cpr>";
Diagnostic patternValid = d(6, 47, 6, 47, 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);
}

/**
* @see https://github.com/angelozerr/lsp4xml/issues/217
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ public void testIsTagOutsideOfBackTicks() {
assertTrue(isTagOutsideOfBackticks("test `<a></a>` <A></A>"));
assertTrue(isTagOutsideOfBackticks("<A></A> `<a></a>`"));
assertTrue(isTagOutsideOfBackticks("<A> `<a></a>`"));

}

@Test
Expand All @@ -96,6 +95,17 @@ public void testMatch() {
Assert.assertEquals(3, startOffset);
}

@Test
public void testGetString() {
StringBuffer buffer = new StringBuffer();
String bufferText = "This is buffer text";
buffer.append(bufferText);
assertEquals(bufferText, StringUtils.getString(buffer));

String regularText = "This is regular text";
assertEquals(regularText, StringUtils.getString(regularText));
}

private static void assertTrimNewLines(String valueToTrim, String expected) {
String actual = trimNewLines(valueToTrim);
Assert.assertEquals(expected, actual);
Expand Down