Skip to content

Commit

Permalink
References support for text node
Browse files Browse the repository at this point in the history
Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr committed Jan 3, 2023
1 parent f4b33cf commit 3932b31
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,15 @@

import java.util.List;

import org.eclipse.lemminx.dom.DOMAttr;
import org.eclipse.lemminx.dom.DOMNode;
import org.eclipse.lemminx.extensions.references.XMLReferencesPlugin;
import org.eclipse.lemminx.extensions.references.settings.XMLReferenceExpression;
import org.eclipse.lemminx.extensions.references.utils.XMLReferencesUtils;
import org.eclipse.lemminx.extensions.xsd.DataType;
import org.eclipse.lemminx.services.extensions.completion.CompletionParticipantAdapter;
import org.eclipse.lemminx.services.extensions.completion.ICompletionRequest;
import org.eclipse.lemminx.services.extensions.completion.ICompletionResponse;
import org.eclipse.lsp4j.CompletionItem;
import org.eclipse.lsp4j.CompletionItemKind;
import org.eclipse.lsp4j.MarkupContent;
import org.eclipse.lsp4j.MarkupKind;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.TextEdit;
import org.eclipse.lsp4j.jsonrpc.CancelChecker;
Expand All @@ -45,33 +41,41 @@ public XMLReferencesCompletionParticipant(XMLReferencesPlugin plugin) {
this.plugin = plugin;
}

@Override
public void onXMLContent(ICompletionRequest request, ICompletionResponse response, CancelChecker cancelChecker)
throws Exception {
DOMNode fromNode = request.getNode();
searchToNodes(fromNode, request, response);
}

@Override
public void onAttributeValue(String valuePrefix, ICompletionRequest request, ICompletionResponse response,
CancelChecker cancelChecker) throws Exception {
DOMNode node = request.getNode();
Range fullRange = request.getReplaceRange();
DOMAttr originAttr = node.findAttrAt(request.getOffset());
DOMNode node = request.getNode();
DOMNode fromNode = node.findAttrAt(request.getOffset());
searchToNodes(fromNode, request, response);
}

List<XMLReferenceExpression> references = XMLReferencesUtils.findExpressionsWhichMatcheFrom(originAttr,
private void searchToNodes(DOMNode fromNode, ICompletionRequest request, ICompletionResponse response) {
List<XMLReferenceExpression> references = XMLReferencesUtils.findExpressionsWhichMatcheFrom(fromNode,
plugin.getReferencesSettings());
if (references != null && !references.isEmpty()) {
XMLReferencesUtils.searchToAttributes(originAttr, references, false, true,
(targetNamespacePrefix, targetAttr, expression) -> {
XMLReferencesUtils.searchToNodes(fromNode, references, false, true,
(targetNamespacePrefix, targetNode, expression) -> {
CompletionItem item = new CompletionItem();
item.setDocumentation(
new MarkupContent(MarkupKind.MARKDOWN, DataType.getDocumentation(targetAttr)));
String value = createReferenceValue(targetAttr, targetNamespacePrefix, expression);
String value = createReferenceValue(targetNode, targetNamespacePrefix, expression);
String insertText = request.getInsertAttrValue(value);
item.setLabel(value);
item.setKind(CompletionItemKind.Value);
item.setFilterText(insertText);
Range fullRange = request.getReplaceRange();
item.setTextEdit(Either.forLeft(new TextEdit(fullRange, insertText)));
response.addCompletionItem(item);
});
}
}

private static String createReferenceValue(DOMAttr targetAttr, String targetNamespacePrefix,
private static String createReferenceValue(DOMNode targetNode, String targetNamespacePrefix,
XMLReferenceExpression expression) {
StringBuilder value = new StringBuilder();
if (expression.getPrefix() != null) {
Expand All @@ -81,7 +85,8 @@ private static String createReferenceValue(DOMAttr targetAttr, String targetName
value.append(targetNamespacePrefix);
value.append(":");
}
value.append(targetAttr.getValue());
value.append(XMLReferencesUtils.getNodeValue(targetNode));
return value.toString();
}

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

import java.util.List;

import org.eclipse.lemminx.dom.DOMAttr;
import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.dom.DOMNode;
import org.eclipse.lemminx.extensions.references.XMLReferencesPlugin;
Expand Down Expand Up @@ -47,18 +46,15 @@ protected boolean match(DOMDocument document) {
@Override
protected void doFindDefinition(IDefinitionRequest request, List<LocationLink> locations,
CancelChecker cancelChecker) {
DOMNode node = request.getNode();
if (!node.isAttribute()) {
return;
}
DOMAttr attr = (DOMAttr) node;
List<XMLReferenceExpression> references = XMLReferencesUtils.findExpressionsWhichMatcheFrom(attr,
DOMNode fromNode = request.getNode();
List<XMLReferenceExpression> references = XMLReferencesUtils.findExpressionsWhichMatcheFrom(fromNode,
plugin.getReferencesSettings());
if (references != null && !references.isEmpty()) {
XMLReferencesUtils.searchToAttributes(attr, references, true, true,
(targetNamespacePrefix, targetAttr, expression) -> {
LocationLink location = XMLPositionUtility.createLocationLink(attr.getNodeAttrValue(),
targetAttr.getNodeAttrValue());
XMLReferencesUtils.searchToNodes(fromNode, references, true, true,
(targetNamespacePrefix, targetNode, expression) -> {
LocationLink location = XMLPositionUtility.createLocationLink(
XMLReferencesUtils.getNodeRange(fromNode),
XMLReferencesUtils.getNodeRange(targetNode));
locations.add(location);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

import java.util.List;

import org.eclipse.lemminx.dom.DOMAttr;
import org.eclipse.lemminx.dom.DOMNode;
import org.eclipse.lemminx.extensions.references.XMLReferencesPlugin;
import org.eclipse.lemminx.extensions.references.settings.XMLReferenceExpression;
Expand Down Expand Up @@ -42,24 +41,21 @@ public XMLReferencesHighlightingParticipant(XMLReferencesPlugin plugin) {
@Override
public void findDocumentHighlights(DOMNode node, Position position, int offset, List<DocumentHighlight> highlights,
CancelChecker cancelChecker) {
// Highlight works only when attribute is selected (origin or target attribute)
DOMAttr fromAttr = node.findAttrAt(offset);
if (fromAttr == null || fromAttr.getNodeAttrValue() == null) {
return;
DOMNode fromNode = node;
if (fromNode.isElement()) {
fromNode = fromNode.findAttrAt(offset);
}

List<XMLReferenceExpression> references = XMLReferencesUtils.findExpressionsWhichMatcheFrom(fromAttr,
List<XMLReferenceExpression> references = XMLReferencesUtils.findExpressionsWhichMatcheFrom(fromNode,
plugin.getReferencesSettings());
if (references != null && !references.isEmpty()) {
highlights
.add(new DocumentHighlight(XMLPositionUtility.createRange(fromAttr.getNodeAttrValue().getStart(),
fromAttr.getNodeAttrValue().getEnd(), fromAttr.getOwnerDocument()),
.add(new DocumentHighlight(
XMLPositionUtility.createRange(XMLReferencesUtils.getNodeRange(fromNode)),
DocumentHighlightKind.Read));
XMLReferencesUtils.searchToAttributes(fromAttr, references, true, false,
(targetNamespacePrefix, toAttr, expression) -> {
XMLReferencesUtils.searchToNodes(fromNode, references, true, false,
(targetNamespacePrefix, toNode, expression) -> {
highlights.add(new DocumentHighlight(
XMLPositionUtility.createRange(toAttr.getNodeAttrValue().getStart(),
toAttr.getNodeAttrValue().getEnd(), toAttr.getOwnerDocument()),
XMLPositionUtility.createRange(XMLReferencesUtils.getNodeRange(toNode)),
DocumentHighlightKind.Write));
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*******************************************************************************/
package org.eclipse.lemminx.extensions.references.utils;

import org.eclipse.lemminx.dom.DOMAttr;
import org.eclipse.lemminx.dom.DOMNode;
import org.eclipse.lemminx.extensions.references.settings.XMLReferenceExpression;

/**
Expand All @@ -28,9 +28,9 @@ public interface IXMLReferenceTosCollector {
* Collect the given to attribute which matches the given expression.
*
* @param namespacePrefix namespace prefix.
* @param toAttr the to attribute to collect.
* @param toNode the to attribute, text node to collect.
* @param expression the reference expression which matches the to
* attribute.
* node.
*/
void collect(String namespacePrefix, DOMAttr toAttr, XMLReferenceExpression expression);
void collect(String namespacePrefix, DOMNode toNode, XMLReferenceExpression expression);
}
Loading

0 comments on commit 3932b31

Please sign in to comment.