From 81298daf1d6aab4a5fd06fab3675affbf8fbff5d Mon Sep 17 00:00:00 2001 From: azerr Date: Wed, 3 Feb 2021 19:20:09 +0100 Subject: [PATCH] NPE on hover of malformed document Fixes #984 Signed-off-by: azerr Signed-off-by: David Thompson --- .../ContentModelHoverParticipant.java | 3 + .../XMLSchemaHoverExtensionsTest.java | 86 ++++++++++++++++++- 2 files changed, 88 insertions(+), 1 deletion(-) diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/contentmodel/participants/ContentModelHoverParticipant.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/contentmodel/participants/ContentModelHoverParticipant.java index a017b56ed..6c9cf0c39 100644 --- a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/contentmodel/participants/ContentModelHoverParticipant.java +++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/contentmodel/participants/ContentModelHoverParticipant.java @@ -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 cmDocuments = contentModelManager.findCMDocument(element); diff --git a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/contentmodel/XMLSchemaHoverExtensionsTest.java b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/contentmodel/XMLSchemaHoverExtensionsTest.java index d7f43fa09..032d211ff 100644 --- a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/contentmodel/XMLSchemaHoverExtensionsTest.java +++ b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/contentmodel/XMLSchemaHoverExtensionsTest.java @@ -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; @@ -199,7 +209,7 @@ public void testHoverAttributeValueNonExistent() throws BadLocationException, Ma /** * See https://github.com/redhat-developer/vscode-xml/issues/233 - * + * * @throws BadLocationException * @throws MalformedURIException */ @@ -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 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,