-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for
textDocument/codeLens
for XML DTD
Fixes #252 Signed-off-by: azerr <azerr@redhat.com>
- Loading branch information
1 parent
f3f9afd
commit 4f648a9
Showing
14 changed files
with
204 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
...src/main/java/org/eclipse/lsp4xml/extensions/dtd/participants/DTDCodeLensParticipant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 0 additions & 12 deletions
12
...e.lsp4xml/src/main/java/org/eclipse/lsp4xml/services/extensions/ICodeLensParticipant.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
.../src/main/java/org/eclipse/lsp4xml/services/extensions/codelens/ICodeLensParticipant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...4xml/src/main/java/org/eclipse/lsp4xml/services/extensions/codelens/ReferenceCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
...e.lsp4xml/src/test/java/org/eclipse/lsp4xml/extensions/dtd/DTDCodeLensExtensionsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters