diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/dtd/DTDPlugin.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/dtd/DTDPlugin.java index 5416e7d20..64c7caa29 100644 --- a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/dtd/DTDPlugin.java +++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/dtd/DTDPlugin.java @@ -16,9 +16,11 @@ import org.eclipse.lsp4xml.extensions.dtd.contentmodel.CMDTDContentModelProvider; import org.eclipse.lsp4xml.extensions.dtd.participants.DTDDefinitionParticipant; import org.eclipse.lsp4xml.extensions.dtd.participants.DTDHighlightingParticipant; +import org.eclipse.lsp4xml.extensions.dtd.participants.DTDReferenceParticipant; import org.eclipse.lsp4xml.extensions.dtd.participants.diagnostics.DTDDiagnosticsParticipant; import org.eclipse.lsp4xml.services.extensions.IDefinitionParticipant; import org.eclipse.lsp4xml.services.extensions.IHighlightingParticipant; +import org.eclipse.lsp4xml.services.extensions.IReferenceParticipant; import org.eclipse.lsp4xml.services.extensions.IXMLExtension; import org.eclipse.lsp4xml.services.extensions.XMLExtensionsRegistry; import org.eclipse.lsp4xml.services.extensions.diagnostics.IDiagnosticsParticipant; @@ -32,11 +34,13 @@ public class DTDPlugin implements IXMLExtension { private final IDiagnosticsParticipant diagnosticsParticipant; private final IDefinitionParticipant definitionParticipant; private IHighlightingParticipant highlightingParticipant; + private IReferenceParticipant referenceParticipant; public DTDPlugin() { diagnosticsParticipant = new DTDDiagnosticsParticipant(); definitionParticipant = new DTDDefinitionParticipant(); highlightingParticipant = new DTDHighlightingParticipant(); + referenceParticipant = new DTDReferenceParticipant(); } @Override @@ -56,6 +60,8 @@ public void start(InitializeParams params, XMLExtensionsRegistry registry) { registry.registerDefinitionParticipant(definitionParticipant); // register highlighting participant registry.registerHighlightingParticipant(highlightingParticipant); + // register reference participant + registry.registerReferenceParticipant(referenceParticipant); } @Override @@ -66,5 +72,7 @@ public void stop(XMLExtensionsRegistry registry) { registry.unregisterDefinitionParticipant(definitionParticipant); // unregister highlighting participant registry.unregisterHighlightingParticipant(highlightingParticipant); + // register reference participant + registry.unregisterReferenceParticipant(referenceParticipant); } } diff --git a/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/dtd/participants/DTDReferenceParticipant.java b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/dtd/participants/DTDReferenceParticipant.java new file mode 100644 index 000000000..422c24167 --- /dev/null +++ b/org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/extensions/dtd/participants/DTDReferenceParticipant.java @@ -0,0 +1,53 @@ +/******************************************************************************* +* Copyright (c) 2019 Red Hat Inc. 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 +* +* Contributors: +* Red Hat Inc. - initial API and implementation +*******************************************************************************/ +package org.eclipse.lsp4xml.extensions.dtd.participants; + +import java.util.List; + +import org.eclipse.lsp4j.Location; +import org.eclipse.lsp4j.Position; +import org.eclipse.lsp4j.ReferenceContext; +import org.eclipse.lsp4j.jsonrpc.CancelChecker; +import org.eclipse.lsp4xml.dom.DOMDocument; +import org.eclipse.lsp4xml.dom.DOMNode; +import org.eclipse.lsp4xml.dom.DTDElementDecl; +import org.eclipse.lsp4xml.extensions.dtd.utils.DTDUtils; +import org.eclipse.lsp4xml.services.extensions.AbstractReferenceParticipant; +import org.eclipse.lsp4xml.utils.XMLPositionUtility; + +/** + * DTD reference + * + * @author Angelo ZERR + * + */ +public class DTDReferenceParticipant extends AbstractReferenceParticipant { + + @Override + protected boolean match(DOMDocument document) { + return true; + } + + @Override + protected void findReferences(DOMNode node, Position position, int offset, ReferenceContext context, + List locations, CancelChecker cancelChecker) { + // DTD reference works only when references is done on an locations.add(XMLPositionUtility.createLocation(origin)), cancelChecker); + } + +} diff --git a/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/dtd/DTDReferenceExtensionsTest.java b/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/dtd/DTDReferenceExtensionsTest.java new file mode 100644 index 000000000..f4ce03716 --- /dev/null +++ b/org.eclipse.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/dtd/DTDReferenceExtensionsTest.java @@ -0,0 +1,79 @@ +/******************************************************************************* +* Copyright (c) 2019 Red Hat Inc. 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 +* +* Contributors: +* Red Hat Inc. - initial API and implementation +*******************************************************************************/ +package org.eclipse.lsp4xml.extensions.dtd; + +import static org.eclipse.lsp4xml.XMLAssert.l; +import static org.eclipse.lsp4xml.XMLAssert.r; + +import org.eclipse.lsp4j.Location; +import org.eclipse.lsp4xml.XMLAssert; +import org.eclipse.lsp4xml.commons.BadLocationException; +import org.junit.Test; + +/** + * DTD references tests + * + */ +public class DTDReferenceExtensionsTest { + + @Test + public void referencesOnDTDElementName() throws BadLocationException { + String xml = "\r\n" + // + "\r\n" + // <-- references on \r\n" + // + "]>"; + testReferencesFor(xml, l("test.xml", r(2, 39, 2, 43)), l("test.xml", r(3, 11, 3, 15))); + } + + @Test + public void noReferencesAfterElementName() throws BadLocationException { + String xml = "\r\n" + // + "\r\n" + // <--after ?, no references + " \r\n" + // + "]>"; + testReferencesFor(xml); + } + + @Test + public void noReferencesAfterDTDAttrListName() throws BadLocationException { + String xml = "\r\n" + // + "\r\n" + + " \r\n" + // // <-- in CDATA, no references + "]>"; + testReferencesFor(xml); + } + + @Test + public void noReferencesOnDTDAttListName() throws BadLocationException { + String xml = "\r\n" + // + "\r\n" + // + " \r\n" + // <-- no references on "; + testReferencesFor(xml); + } + + @Test + public void noReferencesOnDTDElementChildName() throws BadLocationException { + String xml = "\r\n" + // + "\r\n" + // <-- no references on child (... no|te? + " \r\n" + // + "]>"; + testReferencesFor(xml); + } + + private void testReferencesFor(String xml, Location... expectedItems) throws BadLocationException { + XMLAssert.testReferencesFor(xml, "test.xml", expectedItems); + } +}