Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve getCurrentAttribute method for AbstractPositionReqest #584

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Contributor

@angelozerr angelozerr Oct 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace with

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

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

return TEST_FOR_ATTRIBUTENAME_HOVER;
if ("class".equals(request.getCurrentAttributeName())) {
return TEST_FOR_ATTRIBUTENAME_HOVER;
}
return null;
}
}
}
Expand Down