From 9827ac2533966276e18c8d89e4c4d9a389a47ff9 Mon Sep 17 00:00:00 2001 From: Matt Bowersox Date: Thu, 4 Aug 2022 15:26:47 -0500 Subject: [PATCH] Checking base complex type for annotations Signed-off-by: Matt Bowersox --- .../contentmodel/CMXSDElementDeclaration.java | 27 +++++- ...XMLSchemaHoverExtendedComplexTypeTest.java | 91 +++++++++++++++++++ .../resources/xsd/extendedComplexType.xsd | 21 +++++ 3 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/contentmodel/XMLSchemaHoverExtendedComplexTypeTest.java create mode 100644 org.eclipse.lemminx/src/test/resources/xsd/extendedComplexType.xsd diff --git a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/xsd/contentmodel/CMXSDElementDeclaration.java b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/xsd/contentmodel/CMXSDElementDeclaration.java index 7e6d76958..88ec7125f 100644 --- a/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/xsd/contentmodel/CMXSDElementDeclaration.java +++ b/org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/extensions/xsd/contentmodel/CMXSDElementDeclaration.java @@ -28,6 +28,7 @@ import org.apache.xerces.impl.xs.models.CMBuilder; import org.apache.xerces.impl.xs.models.CMNodeFactory; import org.apache.xerces.impl.xs.models.XSCMValidator; +import org.apache.xerces.impl.xs.util.XSObjectListImpl; import org.apache.xerces.xni.QName; import org.apache.xerces.xs.XSAttributeUse; import org.apache.xerces.xs.XSComplexTypeDefinition; @@ -398,8 +399,32 @@ private XSObjectList getElementAnnotations() { if (typeDefinition == null) { return null; } + return getElementAnnotations(typeDefinition); + } + + private XSObjectList getElementAnnotations(XSTypeDefinition typeDefinition) { if (typeDefinition.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE) { - return ((XSComplexTypeDecl) typeDefinition).getAnnotations(); + XSObjectList annotation = ((XSComplexTypeDecl) typeDefinition).getAnnotations(); + // Get annotations for types derived with extension:base + if (((XSComplexTypeDecl) typeDefinition).getDerivationMethod() == XSConstants.DERIVATION_EXTENSION) { + XSObjectListImpl allAnnotations = new XSObjectListImpl(); + XSTypeDefinition baseType = ((XSComplexTypeDecl) typeDefinition).getBaseType(); + //Get annotations for current type + for (Object xsObject : annotation.toArray()) { + if (((XSObject) xsObject) != null) { + allAnnotations.addXSObject((XSObject) xsObject); + } + } + //Get annotations for base type + for (Object xsObject : getElementAnnotations(baseType).toArray()) { + if (((XSObject) xsObject) != null) { + allAnnotations.addXSObject((XSObject) xsObject); + } + } + return allAnnotations; + } else { + return annotation; + } } else if (typeDefinition.getTypeCategory() == XSTypeDefinition.SIMPLE_TYPE) { return ((XSSimpleTypeDecl) typeDefinition).getAnnotations(); } diff --git a/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/contentmodel/XMLSchemaHoverExtendedComplexTypeTest.java b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/contentmodel/XMLSchemaHoverExtendedComplexTypeTest.java new file mode 100644 index 000000000..6d3159cb9 --- /dev/null +++ b/org.eclipse.lemminx/src/test/java/org/eclipse/lemminx/extensions/contentmodel/XMLSchemaHoverExtendedComplexTypeTest.java @@ -0,0 +1,91 @@ +/******************************************************************************* +* Copyright (c) 2022 IBM Corporation and others. +* All rights reserved. This program and the accompanying materials +* which accompanies this distribution, and is available at +* http://www.eclipse.org/legal/epl-v20.html +* +* SPDX-License-Identifier: EPL-2.0 +* +* Contributors: +* IBM Corporation - initial API and implementation +*******************************************************************************/ +package org.eclipse.lemminx.extensions.contentmodel; + +import static org.eclipse.lemminx.XMLAssert.r; + +import java.util.Arrays; + +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.services.XMLLanguageService; +import org.eclipse.lemminx.settings.SchemaDocumentationType; +import org.eclipse.lemminx.settings.SharedSettings; +import org.eclipse.lsp4j.HoverCapabilities; +import org.eclipse.lsp4j.MarkupKind; +import org.eclipse.lsp4j.Range; +import org.junit.jupiter.api.Test; + +public class XMLSchemaHoverExtendedComplexTypeTest { + + @Test + public void testHoverComplexTypeDocumentation() throws BadLocationException, MalformedURIException { + String schemaURI = getXMLSchemaFileURI("extendedComplexType.xsd"); + + String xml = "\n" + + "\n"; + + String expected = "base type documentation value" + // + System.lineSeparator() + // + System.lineSeparator() + // + "Source: [extendedComplexType.xsd](" + schemaURI + ")"; + + assertHover(xml, expected, r(0, 1, 0, 9)); + } + + @Test + public void testHoverExtendedComplexTypeDocumentation() throws BadLocationException, MalformedURIException { + String schemaURI = getXMLSchemaFileURI("extendedComplexType.xsd"); + + String xml = "\n" + + "\n"; + + String expected = "extending type documentation value" + // + System.lineSeparator() + // + System.lineSeparator() + // + "base type documentation value" + // + System.lineSeparator() + // + System.lineSeparator() + // + "Source: [extendedComplexType.xsd](" + schemaURI + ")"; + + assertHover(xml, expected, r(0, 1, 0, 17)); + } + + private SharedSettings createSharedSettings(SchemaDocumentationType docSource, boolean markdownSupported) { + SharedSettings settings = new SharedSettings(); + if (markdownSupported) { + HoverCapabilities capabilities = new HoverCapabilities(Arrays.asList(MarkupKind.MARKDOWN), false); + settings.getHoverSettings().setCapabilities(capabilities); + } + settings.getPreferences() + .setShowSchemaDocumentationType(docSource); + return settings; + } + + private static String getXMLSchemaFileURI(String schemaURI) throws MalformedURIException { + return XMLEntityManager.expandSystemId("xsd/" + schemaURI, "src/test/resources/test.xml", true).replace("///", + "/"); + } + + private void assertHover(String xml, String expected, Range range) throws BadLocationException, MalformedURIException { + XMLAssert.assertHover(new XMLLanguageService(), xml, null, "src/test/resources/extendedComplexType.xml", expected, range, // + createSharedSettings(SchemaDocumentationType.documentation, true)); + } +} diff --git a/org.eclipse.lemminx/src/test/resources/xsd/extendedComplexType.xsd b/org.eclipse.lemminx/src/test/resources/xsd/extendedComplexType.xsd new file mode 100644 index 000000000..8fa9974ba --- /dev/null +++ b/org.eclipse.lemminx/src/test/resources/xsd/extendedComplexType.xsd @@ -0,0 +1,21 @@ + + + + + base type documentation value + base type appinfo value + + + + + + + extending type documentation value + extending type appinfo value + + + + + + + \ No newline at end of file