Skip to content

Commit

Permalink
Fixed EOF Error
Browse files Browse the repository at this point in the history
Fixes #157

The most common ocurrences of this error are
when the document is empty or there is just the
xml prolog (<?xml ... ?>). Errors that give negative values
will be ignored for now.

Signed-off-by: Nikolas <[email protected]>
  • Loading branch information
NikolasKomonen committed Mar 18, 2019
1 parent 3988871 commit 8e2d63d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ EqRequiredInAttribute, the_element_type_lmsg("the-element-type-lmsg"), EqRequire
InvalidCommentStart, LessthanInAttValue, MarkupEntityMismatch, MarkupNotRecognizedInContent,
NameRequiredInReference, OpenQuoteExpected, PITargetRequired, PseudoAttrNameExpected, QuoteRequiredInXMLDecl,
SDDeclInvalid, SpaceRequiredBeforeEncodingInXMLDecl, SpaceRequiredBeforeStandalone, SpaceRequiredInPI,
VersionInfoRequired, VersionNotSupported, XMLDeclUnterminated, CustomETag; // https://wiki.xmldation.com/Support/Validator/EqRequiredInAttribute
VersionInfoRequired, VersionNotSupported, XMLDeclUnterminated, CustomETag, PrematureEOF; // https://wiki.xmldation.com/Support/Validator/EqRequiredInAttribute

private final String code;

Expand Down Expand Up @@ -173,6 +173,7 @@ public static Range toLSPRange(XMLLocator location, XMLSyntaxErrorCode code, Obj
int start = selectCurrentTagOffset(offset, document) + 1;
int end = offset + 1;
return XMLPositionUtility.createRange(start, end, document);
case PrematureEOF:
case XMLDeclUnterminated:
break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,13 @@ public String reportError(XMLLocator location, String domain, String key, Object
message = str.toString();
}

Range adjustedRange = internalToLSPRange(location, key, arguments, xmlDocument);

if(adjustedRange == null) {
return null;
}
// Fill diagnostic
diagnostics.add(new Diagnostic(internalToLSPRange(location, key, arguments, xmlDocument), message,
diagnostics.add(new Diagnostic(adjustedRange, message,
toLSPSeverity(severity), source, key));

if (severity == SEVERITY_FATAL_ERROR && !fContinueAfterFatalError) {
Expand Down Expand Up @@ -127,6 +132,10 @@ private Range internalToLSPRange(XMLLocator location, String key, Object[] argum
int startOffset = location.getCharacterOffset() - 1;
int endOffset = location.getCharacterOffset() - 1;

if(startOffset < 0 || endOffset < 0) {
return null;
}

// Create LSP range
Position start = toLSPPosition(startOffset, location, document.getTextDocument());
Position end = toLSPPosition(endOffset, location, document.getTextDocument());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/
package org.eclipse.lsp4xml;

import static org.junit.Assert.assertTrue;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
Expand Down Expand Up @@ -280,7 +282,10 @@ public static void testDiagnosticsFor(String xml, String catalogPath, Consumer<X

List<Diagnostic> actual = xmlLanguageService.doDiagnostics(xmlDocument, () -> {
}, settings.getValidation());

if(expected == null) {
assertTrue(actual.isEmpty());
return;
}
assertDiagnostics(actual, Arrays.asList(expected), filter);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@
*/
public class XMLSchemaDiagnosticsTest {

@Test
public void prematureEOFError() throws Exception {
String xml = " ";
testDiagnosticsFor(xml);
}

@Test
public void prematureEOFErrorWithProlog() throws Exception {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?> ";
testDiagnosticsFor(xml);
}

@Test
public void cvc_complex_type_2_3() throws Exception {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + //
Expand Down

0 comments on commit 8e2d63d

Please sign in to comment.