-
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.
CodeLens, References, Rename support for XML references
Signed-off-by: azerr <azerr@redhat.com>
- Loading branch information
1 parent
1663001
commit fa8f51b
Showing
47 changed files
with
3,048 additions
and
848 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
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
128 changes: 128 additions & 0 deletions
128
.../eclipse/lemminx/extensions/references/participants/XMLReferencesCodeLensParticipant.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,128 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 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 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.lemminx.extensions.references.participants; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.eclipse.lemminx.client.CodeLensKind; | ||
import org.eclipse.lemminx.dom.DOMAttr; | ||
import org.eclipse.lemminx.dom.DOMDocument; | ||
import org.eclipse.lemminx.dom.DOMElement; | ||
import org.eclipse.lemminx.dom.DOMNode; | ||
import org.eclipse.lemminx.extensions.references.XMLReferencesPlugin; | ||
import org.eclipse.lemminx.extensions.references.search.SearchEngine; | ||
import org.eclipse.lemminx.extensions.references.search.SearchNode; | ||
import org.eclipse.lemminx.extensions.references.search.SearchQuery; | ||
import org.eclipse.lemminx.extensions.references.search.SearchQuery.Direction; | ||
import org.eclipse.lemminx.extensions.references.search.SearchQueryFactory; | ||
import org.eclipse.lemminx.extensions.references.settings.XMLReferenceExpression; | ||
import org.eclipse.lemminx.services.extensions.codelens.ICodeLensParticipant; | ||
import org.eclipse.lemminx.services.extensions.codelens.ICodeLensRequest; | ||
import org.eclipse.lemminx.services.extensions.codelens.ReferenceCommand; | ||
import org.eclipse.lemminx.utils.XMLPositionUtility; | ||
import org.eclipse.lsp4j.CodeLens; | ||
import org.eclipse.lsp4j.Range; | ||
import org.eclipse.lsp4j.jsonrpc.CancelChecker; | ||
|
||
/** | ||
* XML references codelens support. | ||
* | ||
*/ | ||
public class XMLReferencesCodeLensParticipant implements ICodeLensParticipant { | ||
|
||
private static class Link { | ||
|
||
public final List<SearchNode> froms; | ||
public final List<SearchNode> tos; | ||
|
||
public Link() { | ||
froms = new ArrayList<>(); | ||
tos = new ArrayList<>(); | ||
} | ||
|
||
public void addTo(SearchNode to) { | ||
tos.add(to); | ||
} | ||
|
||
public void addFrom(SearchNode from) { | ||
froms.add(from); | ||
} | ||
} | ||
|
||
private final XMLReferencesPlugin plugin; | ||
|
||
public XMLReferencesCodeLensParticipant(XMLReferencesPlugin plugin) { | ||
this.plugin = plugin; | ||
} | ||
|
||
@Override | ||
public void doCodeLens(ICodeLensRequest request, List<CodeLens> lenses, CancelChecker cancelChecker) { | ||
DOMDocument document = request.getDocument(); | ||
SearchQuery query = SearchQueryFactory.createQuery(document, | ||
plugin.getReferencesSettings(), Direction.BOTH); | ||
if (query != null) { | ||
final Map<XMLReferenceExpression, Link> linksMap = new HashMap<>(); | ||
SearchEngine.getInstance().search(query, | ||
(fromSearchNode, toSearchNode, expression) -> { | ||
Link link = linksMap.get(expression); | ||
if (link == null) { | ||
link = new Link(); | ||
linksMap.put(expression, link); | ||
} | ||
if (fromSearchNode != null) { | ||
link.addFrom(fromSearchNode); | ||
} | ||
if (toSearchNode != null) { | ||
link.addTo(toSearchNode); | ||
} | ||
}, cancelChecker); | ||
if (!linksMap.isEmpty()) { | ||
boolean supportedByClient = request.isSupportedByClient(CodeLensKind.References); | ||
Map<DOMElement, CodeLens> cache = new HashMap<>(); | ||
Collection<Link> links = linksMap.values(); | ||
for (Link link : links) { | ||
for (SearchNode to : link.tos) { | ||
// Increment references count Codelens for the given target element | ||
DOMNode toNode = to.getNode(); | ||
DOMElement toElement = toNode.isAttribute() ? ((DOMAttr) toNode).getOwnerElement() | ||
: toNode.getParentElement(); | ||
if (toElement != null) { | ||
for (SearchNode from : link.froms) { | ||
if (from.matchesValue(to)) { | ||
|
||
CodeLens codeLens = cache.get(toElement); | ||
if (codeLens == null) { | ||
Range range = XMLPositionUtility.createRange(toNode); | ||
codeLens = new CodeLens(range); | ||
codeLens.setCommand( | ||
new ReferenceCommand(document.getDocumentURI(), range.getStart(), | ||
supportedByClient)); | ||
cache.put(toElement, codeLens); | ||
lenses.add(codeLens); | ||
} else { | ||
((ReferenceCommand) codeLens.getCommand()).increment(); | ||
} | ||
|
||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.