Skip to content

Commit

Permalink
Add support for textDocument/codeLens for XML DTD
Browse files Browse the repository at this point in the history
Fixes #252

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Jul 25, 2019
1 parent f3f9afd commit 4f648a9
Show file tree
Hide file tree
Showing 14 changed files with 204 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public static boolean isIncluded(DOMRange node, int offset) {
}

public static boolean isIncluded(int start, int end, int offset) {
return offset > start && offset <= end;
return offset >= start && offset <= end;
}

public DOMAttr findAttrAt(int offset) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.eclipse.lsp4xml.extensions.contentmodel.model.ContentModelManager;
import org.eclipse.lsp4xml.extensions.contentmodel.model.ContentModelProvider;
import org.eclipse.lsp4xml.extensions.dtd.contentmodel.CMDTDContentModelProvider;
import org.eclipse.lsp4xml.extensions.dtd.participants.DTDCodeLensParticipant;
import org.eclipse.lsp4xml.extensions.dtd.participants.DTDDefinitionParticipant;
import org.eclipse.lsp4xml.extensions.dtd.participants.DTDHighlightingParticipant;
import org.eclipse.lsp4xml.extensions.dtd.participants.DTDReferenceParticipant;
Expand All @@ -23,6 +24,7 @@
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.codelens.ICodeLensParticipant;
import org.eclipse.lsp4xml.services.extensions.diagnostics.IDiagnosticsParticipant;
import org.eclipse.lsp4xml.services.extensions.save.ISaveContext;

Expand All @@ -33,14 +35,16 @@ public class DTDPlugin implements IXMLExtension {

private final IDiagnosticsParticipant diagnosticsParticipant;
private final IDefinitionParticipant definitionParticipant;
private IHighlightingParticipant highlightingParticipant;
private IReferenceParticipant referenceParticipant;
private final IHighlightingParticipant highlightingParticipant;
private final IReferenceParticipant referenceParticipant;
private final ICodeLensParticipant codeLensParticipant;

public DTDPlugin() {
diagnosticsParticipant = new DTDDiagnosticsParticipant();
definitionParticipant = new DTDDefinitionParticipant();
highlightingParticipant = new DTDHighlightingParticipant();
referenceParticipant = new DTDReferenceParticipant();
codeLensParticipant = new DTDCodeLensParticipant();
}

@Override
Expand All @@ -62,6 +66,8 @@ public void start(InitializeParams params, XMLExtensionsRegistry registry) {
registry.registerHighlightingParticipant(highlightingParticipant);
// register reference participant
registry.registerReferenceParticipant(referenceParticipant);
// register codelens participant
registry.registerCodeLensParticipant(codeLensParticipant);
}

@Override
Expand All @@ -74,5 +80,7 @@ public void stop(XMLExtensionsRegistry registry) {
registry.unregisterHighlightingParticipant(highlightingParticipant);
// register reference participant
registry.unregisterReferenceParticipant(referenceParticipant);
// unregister codelens participant
registry.unregisterCodeLensParticipant(codeLensParticipant);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*******************************************************************************
* 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.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.lsp4j.CodeLens;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.jsonrpc.CancelChecker;
import org.eclipse.lsp4xml.client.CodeLensKind;
import org.eclipse.lsp4xml.dom.DOMDocument;
import org.eclipse.lsp4xml.dom.DTDDeclNode;
import org.eclipse.lsp4xml.extensions.dtd.utils.DTDUtils;
import org.eclipse.lsp4xml.services.extensions.codelens.ICodeLensParticipant;
import org.eclipse.lsp4xml.services.extensions.codelens.ICodeLensRequest;
import org.eclipse.lsp4xml.services.extensions.codelens.ReferenceCommand;
import org.eclipse.lsp4xml.utils.DOMUtils;
import org.eclipse.lsp4xml.utils.XMLPositionUtility;

/**
* DTD CodeLens to show references count for referenced <!ELEMENT
*
* @author Angelo ZERR
*
*/
public class DTDCodeLensParticipant implements ICodeLensParticipant {

@Override
public void doCodeLens(ICodeLensRequest request, List<CodeLens> lenses, CancelChecker cancelChecker) {
DOMDocument xmlDocument = request.getDocument();
// DTD CodeLens is applicable only for DTD or XML which defines a DOCTYPE
if (!(DOMUtils.isDTD(xmlDocument.getDocumentURI()) || xmlDocument.hasDTD())) {
return;
}
boolean supportedByClient = request.isSupportedByClient(CodeLensKind.References);
// Add references CodeLens for <!ELEMENT
Map<DTDDeclNode, CodeLens> cache = new HashMap<>();
DTDUtils.searchDTDOriginElementDecls(xmlDocument.getDoctype(), (origin, target) -> {
// Increment references count Codelens for the given target element <!ELEMENT
DTDDeclNode targetElement = target.getOwnerNode();
if (targetElement.isDTDElementDecl()) {
CodeLens codeLens = cache.get(targetElement);
if (codeLens == null) {
Range range = XMLPositionUtility.createRange(target);
codeLens = new CodeLens(range);
codeLens.setCommand(
new ReferenceCommand(xmlDocument.getDocumentURI(), range.getStart(), supportedByClient));
cache.put(targetElement, codeLens);
lenses.add(codeLens);
} else {
((ReferenceCommand) codeLens.getCommand()).increment();
}
}
}, cancelChecker);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
import org.eclipse.lsp4xml.extensions.xsd.participants.XSDHighlightingParticipant;
import org.eclipse.lsp4xml.extensions.xsd.participants.XSDReferenceParticipant;
import org.eclipse.lsp4xml.extensions.xsd.participants.diagnostics.XSDDiagnosticsParticipant;
import org.eclipse.lsp4xml.services.extensions.ICodeLensParticipant;
import org.eclipse.lsp4xml.services.extensions.ICompletionParticipant;
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.codelens.ICodeLensParticipant;
import org.eclipse.lsp4xml.services.extensions.diagnostics.IDiagnosticsParticipant;
import org.eclipse.lsp4xml.services.extensions.save.ISaveContext;
import org.eclipse.lsp4xml.utils.DOMUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,20 @@
*******************************************************************************/
package org.eclipse.lsp4xml.extensions.xsd.participants;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.lsp4j.CodeLens;
import org.eclipse.lsp4j.Command;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.jsonrpc.CancelChecker;
import org.eclipse.lsp4xml.client.ClientCommands;
import org.eclipse.lsp4xml.client.CodeLensKind;
import org.eclipse.lsp4xml.dom.DOMDocument;
import org.eclipse.lsp4xml.dom.DOMElement;
import org.eclipse.lsp4xml.extensions.xsd.utils.XSDUtils;
import org.eclipse.lsp4xml.services.extensions.ICodeLensParticipant;
import org.eclipse.lsp4xml.services.extensions.ICodeLensRequest;
import org.eclipse.lsp4xml.services.extensions.codelens.ICodeLensParticipant;
import org.eclipse.lsp4xml.services.extensions.codelens.ICodeLensRequest;
import org.eclipse.lsp4xml.services.extensions.codelens.ReferenceCommand;
import org.eclipse.lsp4xml.utils.DOMUtils;
import org.eclipse.lsp4xml.utils.XMLPositionUtility;

Expand All @@ -37,29 +34,6 @@
*/
public class XSDCodeLensParticipant implements ICodeLensParticipant {

static class ReferenceCommand extends Command {

private transient int nbReferences = 1;

public ReferenceCommand(String uri, Position position, boolean supportedByClient) {
super(getTitle(1), supportedByClient ? ClientCommands.SHOW_REFERENCES : "");
super.setArguments(Arrays.asList(uri, position));
}

public void increment() {
nbReferences++;
super.setTitle(getTitle(nbReferences));
}

private static String getTitle(int nbReferences) {
if (nbReferences == 1) {
return nbReferences + " reference";
}
return nbReferences + " references";
}

}

@Override
public void doCodeLens(ICodeLensRequest request, List<CodeLens> lenses, CancelChecker cancelChecker) {
DOMDocument xmlDocument = request.getDocument();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
package org.eclipse.lsp4xml.services;

import org.eclipse.lsp4xml.dom.DOMDocument;
import org.eclipse.lsp4xml.services.extensions.ICodeLensRequest;
import org.eclipse.lsp4xml.services.extensions.codelens.ICodeLensRequest;
import org.eclipse.lsp4xml.settings.XMLCodeLensSettings;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
import org.eclipse.lsp4j.CodeLens;
import org.eclipse.lsp4j.jsonrpc.CancelChecker;
import org.eclipse.lsp4xml.dom.DOMDocument;
import org.eclipse.lsp4xml.services.extensions.ICodeLensParticipant;
import org.eclipse.lsp4xml.services.extensions.ICodeLensRequest;
import org.eclipse.lsp4xml.services.extensions.XMLExtensionsRegistry;
import org.eclipse.lsp4xml.services.extensions.codelens.ICodeLensParticipant;
import org.eclipse.lsp4xml.services.extensions.codelens.ICodeLensRequest;
import org.eclipse.lsp4xml.settings.XMLCodeLensSettings;

/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.eclipse.lsp4j.InitializeParams;
import org.eclipse.lsp4xml.services.IXMLDocumentProvider;
import org.eclipse.lsp4xml.services.extensions.codelens.ICodeLensParticipant;
import org.eclipse.lsp4xml.services.extensions.diagnostics.IDiagnosticsParticipant;
import org.eclipse.lsp4xml.services.extensions.save.ISaveContext;
import org.eclipse.lsp4xml.uriresolver.URIResolverExtensionManager;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*******************************************************************************
* 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.services.extensions.codelens;

import java.util.List;

import org.eclipse.lsp4j.CodeLens;
import org.eclipse.lsp4j.jsonrpc.CancelChecker;

/**
* CodeLens participant API.
*
*/
public interface ICodeLensParticipant {

void doCodeLens(ICodeLensRequest request, List<CodeLens> lenses, CancelChecker cancelChecker);

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.lsp4xml.services.extensions;
package org.eclipse.lsp4xml.services.extensions.codelens;

import org.eclipse.lsp4xml.dom.DOMDocument;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*******************************************************************************
* 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.services.extensions.codelens;

import java.util.Arrays;

import org.eclipse.lsp4j.Command;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4xml.client.ClientCommands;

/**
* References command for CodeLens.
*
* @author Angelo ZERR
*
*/
public class ReferenceCommand extends Command {

private transient int nbReferences = 1;

public ReferenceCommand(String uri, Position position, boolean supportedByClient) {
super(getTitle(1), supportedByClient ? ClientCommands.SHOW_REFERENCES : "");
super.setArguments(Arrays.asList(uri, position));
}

public void increment() {
nbReferences++;
super.setTitle(getTitle(nbReferences));
}

private static String getTitle(int nbReferences) {
if (nbReferences == 1) {
return nbReferences + " reference";
}
return nbReferences + " references";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*******************************************************************************
* 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.cl;
import static org.eclipse.lsp4xml.XMLAssert.r;
import static org.eclipse.lsp4xml.client.ClientCommands.SHOW_REFERENCES;

import org.eclipse.lsp4xml.XMLAssert;
import org.eclipse.lsp4xml.commons.BadLocationException;
import org.junit.Test;

/**
* DTD codelens tests
*
*/
public class DTDCodeLensExtensionsTest {

@Test
public void codeLensOnDTDElementInDOCTYPE() throws BadLocationException {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n" + //
"<!DOCTYPE note [\r\n" + //
" <!ELEMENT note (to,from,heading,body, note?)>\r\n" + //
" <!ELEMENT from (#PCDATA)>\r\n" + //
" <!ATTLIST note version CDATA #REQUIRED>\r\n" + //
"]>";
XMLAssert.testCodeLensFor(xml, "test.xml", cl(r(2, 11, 2, 15), "2 references", SHOW_REFERENCES),
cl(r(3, 11, 3, 15), "1 reference", SHOW_REFERENCES));
}

@Test
public void codeLensOnDTDElementInDTD() throws BadLocationException {
String xml = "<!ELEMENT note (to,from,heading,body, note?)>\r\n" + //
" <!ELEMENT from (#PCDATA)>\r\n" + //
" <!ATTLIST note version CDATA #REQUIRED>";
XMLAssert.testCodeLensFor(xml, "test.dtd", cl(r(0, 10, 0, 14), "2 references", SHOW_REFERENCES),
cl(r(1, 11, 1, 15), "1 reference", SHOW_REFERENCES));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.junit.Test;

/**
* XSD references tests
* XSD codelens tests
*
*/
public class XSDCodeLensExtensionsTest {
Expand Down

0 comments on commit 4f648a9

Please sign in to comment.