Skip to content

Commit

Permalink
NPE on hover of malformed document
Browse files Browse the repository at this point in the history
Fixes #984

Signed-off-by: azerr <[email protected]>
Signed-off-by: David Thompson <[email protected]>
  • Loading branch information
angelozerr authored and datho7561 committed Mar 18, 2021
1 parent a62c858 commit 81298da
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ public Hover onAttributeValue(IHoverRequest hoverRequest) throws Exception {
public Hover onText(IHoverRequest hoverRequest) throws Exception {
DOMText text = (DOMText) hoverRequest.getNode();
DOMElement element = text.getParentElement();
if (element == null) {
return null;
}
try {
ContentModelManager contentModelManager = hoverRequest.getComponent(ContentModelManager.class);
Collection<CMDocument> cmDocuments = contentModelManager.findCMDocument(element);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,23 @@
package org.eclipse.lemminx.extensions.contentmodel;

import static org.eclipse.lemminx.XMLAssert.r;
import static org.junit.jupiter.api.Assertions.assertNull;

import org.apache.xerces.impl.XMLEntityManager;
import org.apache.xerces.util.URI.MalformedURIException;
import org.eclipse.lemminx.XMLAssert;
import org.eclipse.lemminx.commons.BadLocationException;
import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.dom.DOMElement;
import org.eclipse.lemminx.dom.DOMNode;
import org.eclipse.lemminx.dom.DOMText;
import org.eclipse.lemminx.dom.LineIndentInfo;
import org.eclipse.lemminx.extensions.contentmodel.participants.ContentModelHoverParticipant;
import org.eclipse.lemminx.extensions.xsi.XSISchemaModel;
import org.eclipse.lemminx.services.XMLLanguageService;
import org.eclipse.lemminx.services.extensions.IHoverRequest;
import org.eclipse.lemminx.settings.SharedSettings;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -199,7 +209,7 @@ public void testHoverAttributeValueNonExistent() throws BadLocationException, Ma

/**
* See https://github.com/redhat-developer/vscode-xml/issues/233
*
*
* @throws BadLocationException
* @throws MalformedURIException
*/
Expand Down Expand Up @@ -305,6 +315,80 @@ public void hoverTextWithUnion3() throws BadLocationException, MalformedURIExcep
r(2, 52, 3, 9));
}

@Test
public void hoverWithNullParentNode() throws Exception {
ContentModelHoverParticipant hoverParticipant = new ContentModelHoverParticipant();
IHoverRequest hoverRequest = new IHoverRequest() {

@Override
public DOMNode getNode() {
return new DOMText(0, 0);
}

@Override
public int getOffset() {
return 0;
}

@Override
public Position getPosition() {
return null;
}

@Override
public DOMElement getParentElement() {
return null;
}

@Override
public DOMDocument getXMLDocument() {
return null;
}

@Override
public String getCurrentTag() {
return null;
}

@Override
public String getCurrentAttributeName() {
return null;
}

@Override
public LineIndentInfo getLineIndentInfo() throws BadLocationException {
return null;
}

@Override
public <T> T getComponent(Class clazz) {
return null;
}

@Override
public boolean canSupportMarkupKind(String kind) {
return false;
}

@Override
public SharedSettings getSharedSettings() {
return null;
}

@Override
public Range getHoverRange() {
return null;
}

@Override
public boolean isOpen() {
return false;
}

};
assertNull(hoverParticipant.onText(hoverRequest));
}

private static void assertHover(String value, String expectedHoverLabel, Range expectedHoverRange)
throws BadLocationException {
XMLAssert.assertHover(new XMLLanguageService(), value, "src/test/resources/catalogs/catalog.xml", null,
Expand Down

0 comments on commit 81298da

Please sign in to comment.