Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Synxtax error ranges, resources updated

Signed-off-by: Nikolas Komonen <nikolaskomonen@gmail.com>
  • Loading branch information
NikolasKomonen committed Sep 10, 2018
1 parent efef96d commit 0087fbe
Show file tree
Hide file tree
Showing 26 changed files with 300 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.HashMap;
import java.util.Map;

import static org.eclipse.lsp4xml.utils.XMLPositionUtility.*;
import org.apache.xerces.xni.QName;
import org.apache.xerces.xni.XMLLocator;
import org.eclipse.lsp4j.Range;
Expand Down Expand Up @@ -41,10 +42,10 @@ public enum XMLSyntaxErrorCode implements IXMLErrorCode {
ETagRequired, // https://wiki.xmldation.com/Support/Validator/ETagRequired
ETagUnterminated, // https://wiki.xmldation.com/Support/Validator/ETagUnterminated
EqRequiredInAttribute, the_element_type_lmsg("the-element-type-lmsg"), EqRequiredInXMLDecl, IllegalQName,
InvalidCommentStart, LessthanInAttValue, MarkupEntityMismatch, MarkupNotRecognizedInContent,
NameRequiredInReference, OpenQuoteExpected, PITargetRequired, PseudoAttrNameExpected, QuoteRequiredInXMLDecl,
SDDeclInvalid, SpaceRequiredBeforeEncodingInXMLDecl, SpaceRequiredBeforeStandalone, SpaceRequiredInPI,
VersionInfoRequired, VersionNotSupported, XMLDeclUnterminated; // https://wiki.xmldation.com/Support/Validator/EqRequiredInAttribute
InvalidCommentStart, LessthanInAttValue, MarkupEntityMismatch, MarkupNotRecognizedInContent, NameRequiredInReference,
OpenQuoteExpected, PITargetRequired, PseudoAttrNameExpected, QuoteRequiredInXMLDecl, SDDeclInvalid,
SpaceRequiredBeforeEncodingInXMLDecl, SpaceRequiredBeforeStandalone, SpaceRequiredInPI, VersionInfoRequired,
VersionNotSupported, XMLDeclUnterminated, CustomETag; // https://wiki.xmldation.com/Support/Validator/EqRequiredInAttribute

private final String code;

Expand Down Expand Up @@ -89,6 +90,8 @@ public static XMLSyntaxErrorCode get(String name) {
public static Range toLSPRange(XMLLocator location, XMLSyntaxErrorCode code, Object[] arguments,
XMLDocument document) {
int offset = location.getCharacterOffset() - 1;
Range r;
String tag;
// adjust positions
switch (code) {
case AttributeNotUnique:
Expand All @@ -110,17 +113,78 @@ public static Range toLSPRange(XMLLocator location, XMLSyntaxErrorCode code, Obj
String attrName = ((QName) arguments[0]).rawname;
return XMLPositionUtility.selectAttributeValue(attrName, offset, document);
}
case ElementPrefixUnbound:
case ElementUnterminated: {
return XMLPositionUtility.selectStartTag(offset, document);
}
case ETagRequired:
tag = (String) arguments[0];
return XMLPositionUtility.selectChildEndTag(tag, offset, document);
case ETagUnterminated:
String tag = (String) arguments[0];
tag = (String) arguments[0];
return XMLPositionUtility.selectEndTag(offset - 1, document);
case EncodingDeclRequired:
break;
case EqRequiredInAttribute:
tag = getNameFromArguents(arguments, 1);
return XMLPositionUtility.selectAttributeName(tag, offset, document);

case EqRequiredInXMLDecl:
tag = getNameFromArguents(arguments, 1);
return XMLPositionUtility.selectAttributeName(tag, offset, document);
case IllegalQName:
return XMLPositionUtility.createRange(offset, offset + 1, document);
case InvalidCommentStart:
return XMLPositionUtility.createRange(offset, offset + 1, document);
case LessthanInAttValue:
tag = getNameFromArguents(arguments, 1);
return XMLPositionUtility.selectAttributeValue(tag, offset, document);
case MarkupEntityMismatch:
// return XMLPositionUtility.selectStartTag(offset, document);
case MarkupNotRecognizedInContent:
return XMLPositionUtility.createRange(offset, offset + 1, document);

case NameRequiredInReference:
// Good as is
case OpenQuoteExpected:
// Working
break;
case PITargetRequired:
// Working
break;
case PseudoAttrNameExpected:
// Working
// Add better message
break;
case QuoteRequiredInXMLDecl:

case SDDeclInvalid:
return XMLPositionUtility.selectAttributeValue("standalone", offset, document);

case SpaceRequiredInPI:
int start = selectCurrentTagOffset(offset, document) + 1;
int end = offset + 1;
return XMLPositionUtility.createRange(start, end, document);

case SpaceRequiredBeforeStandalone:
case SpaceRequiredBeforeEncodingInXMLDecl:
case VersionInfoRequired:
tag = getNameFromArguents(arguments, 0);
r = selectStartTag(offset, document);
r.getEnd().setCharacter(r.getEnd().getCharacter() + 1);
return r;
case VersionNotSupported:
return XMLPositionUtility.selectAttributeValue("version", offset, document);
case XMLDeclUnterminated:
break;
case CustomETag:
tag = (String) arguments[0];
return XMLPositionUtility.selectEndTag(offset, document);

}


return null;

}

public static void registerCodeActionParticipants(Map<String, ICodeActionParticipant> codeActions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public LSPErrorReporter(XMLDocument document, List<Diagnostic> diagnostics) {
public String reportError(XMLLocator location, String domain, String key, Object[] arguments, short severity,
Exception exception) throws XNIException {
// format message

MessageFormatter messageFormatter = getMessageFormatter(domain);
String message;
if (messageFormatter != null) {
Expand All @@ -77,8 +78,9 @@ public String reportError(XMLLocator location, String domain, String key, Object
}
}
message = str.toString();

}

// Fill diagnostic
diagnostics.add(new Diagnostic(toLSPRange(location, key, arguments, document), message, toLSPSeverity(severity),
XML_DIAGNOSTIC_SOURCE, key));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* Copyright (c) 2018 Angelo ZERR
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation
*/
package org.eclipse.lsp4xml.contentmodel.participants.diagnostics;

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

import org.apache.xerces.xni.XMLLocator;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4xml.dom.XMLDocument;
import org.eclipse.lsp4xml.utils.XMLPositionUtility;

/**
* XML Schema error code.
*
* @see https://wiki.xmldation.com/Support/Validator
*
*/
public enum XMLSchemaErrorCode implements IXMLErrorCode{

cvc_complex_type_2_4_a("cvc-complex-type.2.4.a"), // https://wiki.xmldation.com/Support/Validator/cvc-complex-type-2-4-a
cvc_complex_type_2_4_d("cvc-complex-type.2.4.d"), // https://wiki.xmldation.com/Support/Validator/cvc-complex-type-2-4-d
cvc_complex_type_3_2_2("cvc-complex-type.3.2.2"), // https://wiki.xmldation.com/Support/Validator/cvc-complex-type-3-2-2
cvc_complex_type_4("cvc-complex-type.4"), // https://wiki.xmldation.com/Support/Validator/cvc-complex-type-4
cvc_type_3_1_1("cvc-type.3.1.1"); // https://wiki.xmldation.com/Support/Validator/cvc-type-3-1-1

private final String code;

private XMLSchemaErrorCode() {
this(null);
}

private XMLSchemaErrorCode(String code) {
this.code = code;
}

@Override
public String getCode() {
if (code == null) {
return name();
}
return code;
}

@Override
public String toString() {
return getCode();
}

private final static Map<String, XMLSchemaErrorCode> codes;

static {
codes = new HashMap<>();
for (XMLSchemaErrorCode errorCode : values()) {
codes.put(errorCode.getCode(), errorCode);
}
}

public static XMLSchemaErrorCode get(String name) {
return codes.get(name);
}

/**
* Create the LSP range from the SAX error.
*
* @param location
* @param key
* @param arguments
* @param document.ge
* @return the LSP range from the SAX error.
*/
public static Range toLSPRange(XMLLocator location, XMLSchemaErrorCode code, Object[] arguments,
XMLDocument document) {
int offset = location.getCharacterOffset() - 1;

// adjust positions
switch (code) {
case cvc_complex_type_2_4_a:
case cvc_complex_type_2_4_d:
return XMLPositionUtility.selectStartTag(offset, document);
case cvc_complex_type_3_2_2: {
String attrName = (String) arguments[0];
return XMLPositionUtility.selectAttributeName(attrName, offset, document);
}
case cvc_complex_type_4: {
// String tag = (String) arguments[0];
// String attrName = (String) arguments[1];
// startOffset = findOffsetOfStartTag(document.ge.getText(), offset, tag);
// endOffset = startOffset + tag.length();
break;
}
case cvc_type_3_1_1:
return XMLPositionUtility.selectAllAttributes(offset, document);
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/
package org.eclipse.lsp4xml.utils;

import java.util.List;

import org.apache.xerces.xni.XMLLocator;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;
Expand Down Expand Up @@ -44,6 +46,16 @@ public static Position toLSPPosition(int offset, XMLLocator location, TextDocume
}
}

public static String getNameFromArguents(Object[] arguments, int index) {
if(arguments == null) {
return "ARGUMENTS_ARE_NULL";
}
if(index < arguments.length) {
return (String) arguments[index];
}
return "INDEX_OUT_OF_RANGE";
}

public static Range selectAttributeName(String attrName, int offset, XMLDocument document) {
return selectAttributeName(attrName, offset, false, document);
}
Expand Down Expand Up @@ -78,6 +90,31 @@ public static Range selectAttributeValue(String attrName, int offset, XMLDocumen
return null;
}


public static Range selectChildEndTag(String childTag, int offset, XMLDocument document) {
Node parent = document.findNodeAt(offset);
if(parent.tag == null) {
return null;
}
if(parent != null) {
Node child = findChildNode(childTag,parent.getChildren());
if(child != null) {
return createRange(child.start + 1, child.start + 1 + childTag.length(), document);
}
}
return null;
}


static Node findChildNode(String childTag, List<Node> children) {
for (Node child : children) {
if(child.tag.equals(childTag) && !child.isClosed()) {
return child;
}
}
return null;
}

public static Range selectStartTag(int offset, XMLDocument document) {
Node element = document.findNodeAt(offset);
if (element != null) {
Expand All @@ -88,6 +125,15 @@ public static Range selectStartTag(int offset, XMLDocument document) {
return null;
}

public static int selectCurrentTagOffset(int offset, XMLDocument document) {
Node element = document.findNodeAt(offset);
if (element != null) {
return element.start; // <

}
return -1;
}

public static Range selectEndTag(int offset, XMLDocument document) {
Node element = document.findNodeAt(offset);
if (element != null) {
Expand Down Expand Up @@ -137,6 +183,30 @@ public static Range selectFirstNonWhitespaceText(int offset, XMLDocument documen
}
return null;
}
/**
* Finds the offset of the first tag it comes across behind the
* given offset.
*
* This excludes the tag it starts in if offset is within a tag.
*/
public static Range selectPreviousEndTag( int offset, XMLDocument document) {
//boolean firstBracket = false;
int i = offset;
char c = document.getText().charAt(i);
while(i >= 0) {
if(c == '>') {
//if(firstBracket) {
return selectStartTag(i, document);
//}
//else {
// firstBracket = true;
//}
}
i--;
c = document.getText().charAt(i);
}
return null;
}

public static Range createRange(int startOffset, int endOffset, XMLDocument document) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,4 @@ public static TextEdit te(int startLine, int startCharacter, int endLine, int en
textEdit.setRange(r(startLine, startCharacter, endLine, endCharacter));
return textEdit;
}
}
}
Loading

0 comments on commit 0087fbe

Please sign in to comment.