Refactor getting exact node to reuse for Value hover highlights

This commit is contained in:
nsingh@pivotal.io
2019-02-12 12:39:41 -08:00
parent 5afa356c2b
commit 9ac173feb6

View File

@@ -44,33 +44,43 @@ public class ValueHoverProvider implements HoverProvider {
protected static Logger logger = LoggerFactory.getLogger(ValueHoverProvider.class);
@Override
public Hover provideHover(ASTNode node, Annotation annotation, ITypeBinding type, int offset,
TextDocument doc, IJavaProject project, SpringBootApp[] runningApps) {
public Hover provideHover(ASTNode node, Annotation annotation, ITypeBinding type, int offset, TextDocument doc,
IJavaProject project, SpringBootApp[] runningApps) {
try {
ASTNode exactNode = NodeFinder.perform(node, offset, 0);
ASTNode exactNode = getExactNode(node, offset);
// case: @Value("prefix<*>")
if (exactNode != null && exactNode instanceof StringLiteral && exactNode.getParent() instanceof Annotation) {
if (exactNode.toString().startsWith("\"") && exactNode.toString().endsWith("\"")) {
return provideHover(exactNode.toString(), offset - exactNode.getStartPosition(), exactNode.getStartPosition(), doc, runningApps);
}
if (exactNode != null) {
return provideHover(exactNode.toString(), offset - exactNode.getStartPosition(),
exactNode.getStartPosition(), doc, runningApps);
}
// case: @Value(value="prefix<*>")
else if (exactNode != null && exactNode instanceof StringLiteral && exactNode.getParent() instanceof MemberValuePair
&& "value".equals(((MemberValuePair)exactNode.getParent()).getName().toString())) {
if (exactNode.toString().startsWith("\"") && exactNode.toString().endsWith("\"")) {
return provideHover(exactNode.toString(), offset - exactNode.getStartPosition(), exactNode.getStartPosition(), doc, runningApps);
}
}
}
catch (Exception e) {
} catch (Exception e) {
logger.error("Error while generating live hovers for @Value", e);
}
return null;
}
private ASTNode getExactNode(ASTNode node, int offset) {
ASTNode exactNode = NodeFinder.perform(node, offset, 0);
if (exactNode != null) {
// case: @Value("prefix<*>")
if (exactNode instanceof StringLiteral && exactNode.getParent() instanceof Annotation) {
if (exactNode.toString().startsWith("\"") && exactNode.toString().endsWith("\"")) {
return exactNode;
}
}
// case: @Value(value="prefix<*>")
else if (exactNode instanceof StringLiteral && exactNode.getParent() instanceof MemberValuePair
&& "value".equals(((MemberValuePair) exactNode.getParent()).getName().toString())) {
if (exactNode.toString().startsWith("\"") && exactNode.toString().endsWith("\"")) {
return exactNode;
}
}
}
return null;
}
private Hover provideHover(String value, int offset, int nodeStartOffset, TextDocument doc, SpringBootApp[] runningApps) {
try {