Skip to content

Commit

Permalink
improve getCurrentAttribute method for AbstractPositionReqest
Browse files Browse the repository at this point in the history
  • Loading branch information
Seiphon authored and angelozerr committed Oct 29, 2019
1 parent 68d9292 commit 6e8d4f1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@

import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4xml.commons.BadLocationException;
import org.eclipse.lsp4xml.dom.DOMAttr;
import org.eclipse.lsp4xml.dom.DOMDocument;
import org.eclipse.lsp4xml.dom.DOMElement;
import org.eclipse.lsp4xml.dom.DOMNode;
import org.eclipse.lsp4xml.dom.LineIndentInfo;
import org.eclipse.lsp4xml.services.extensions.IPositionRequest;
import org.eclipse.lsp4xml.services.extensions.XMLExtensionsRegistry;
import org.w3c.dom.Node;

/**
* Abstract class for position request.
Expand All @@ -30,7 +32,6 @@ abstract class AbstractPositionRequest implements IPositionRequest {
private final XMLExtensionsRegistry extensionsRegistry;
private final int offset;

private String currentAttributeName;
private final DOMNode node;
private LineIndentInfo indentInfo;

Expand Down Expand Up @@ -101,11 +102,27 @@ public String getCurrentTag() {

@Override
public String getCurrentAttributeName() {
return currentAttributeName;
DOMAttr attr = getCurrentAttribute();
return attr != null ? attr.getName() : null;
}

void setCurrentAttributeName(String currentAttributeName) {
this.currentAttributeName = currentAttributeName;
/**
* Returns the current attribute at the given offset and null otherwise.
*
* @return the current attribute at the given offset and null otherwise.
*/
private DOMAttr getCurrentAttribute() {
if (node == null) {
return null;
}
switch (node.getNodeType()) {
case Node.ELEMENT_NODE:
return node.findAttrAt(offset);
case Node.ATTRIBUTE_NODE:
return ((DOMAttr) node);
default:
return null;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ public CompletionList doComplete(DOMDocument xmlDocument, Position position, Sha

Scanner scanner = XMLScanner.createScanner(text, node.getStart(), isInsideDTDContent(node, xmlDocument));
String currentTag = "";
completionRequest.setCurrentAttributeName(null);
TokenType token = scanner.scan();
while (token != TokenType.EOS && scanner.getTokenOffset() <= offset) {
cancelChecker.checkCanceled();
Expand Down Expand Up @@ -120,7 +119,6 @@ public CompletionList doComplete(DOMDocument xmlDocument, Position position, Sha
completionResponse);
return completionResponse;
}
completionRequest.setCurrentAttributeName(scanner.getTokenText());
break;
case DelimiterAssign:
if (scanner.getTokenEnd() == offset) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,18 @@ class AggregatedHoverParticipant extends HoverParticipantAdapter {

@Override
public String onTag(IHoverRequest request) throws Exception {
return TEST_FOR_TAG_HOVER;
if ("bean".equals(request.getCurrentTag())) {
return TEST_FOR_TAG_HOVER;
}
return null;
}

@Override
public String onAttributeName(IHoverRequest request) throws Exception {
return TEST_FOR_ATTRIBUTENAME_HOVER;
if ("class".equals(request.getCurrentAttributeName())) {
return TEST_FOR_ATTRIBUTENAME_HOVER;
}
return null;
}
}
}
Expand Down

0 comments on commit 6e8d4f1

Please sign in to comment.