From ef16649cfc53d83411fe499886ae99f7b0693c8d Mon Sep 17 00:00:00 2001 From: azerr Date: Mon, 13 Dec 2021 15:24:16 +0100 Subject: [PATCH] `this` parameter support Fixes #483 Signed-off-by: azerr --- .../template/TemplateDataCollector.java | 4 +-- .../template/TemplateDataLocation.java | 32 +++++++++++++------ .../template/TemplateDataVisitor.java | 25 ++++++++++++--- 3 files changed, 46 insertions(+), 15 deletions(-) diff --git a/qute.jdt/com.redhat.qute.jdt/src/main/java/com/redhat/qute/jdt/internal/template/TemplateDataCollector.java b/qute.jdt/com.redhat.qute.jdt/src/main/java/com/redhat/qute/jdt/internal/template/TemplateDataCollector.java index 39da80bfe..1398d9cda 100644 --- a/qute.jdt/com.redhat.qute.jdt/src/main/java/com/redhat/qute/jdt/internal/template/TemplateDataCollector.java +++ b/qute.jdt/com.redhat.qute.jdt/src/main/java/com/redhat/qute/jdt/internal/template/TemplateDataCollector.java @@ -47,8 +47,6 @@ */ public class TemplateDataCollector extends TemplateDataVisitor { - private static final String DATA_METHOD = "data"; - private final DataModelTemplate template; public TemplateDataCollector(DataModelTemplate template, IProgressMonitor monitor) { @@ -60,6 +58,8 @@ protected boolean visitParameter(Object name, Object type) { String paramName = null; if (name instanceof StringLiteral) { paramName = ((StringLiteral) name).getLiteralValue(); + } else if (name instanceof String) { + paramName = ((String) name); } if (paramName != null) { String paramType = "java.lang.Object"; diff --git a/qute.jdt/com.redhat.qute.jdt/src/main/java/com/redhat/qute/jdt/internal/template/TemplateDataLocation.java b/qute.jdt/com.redhat.qute.jdt/src/main/java/com/redhat/qute/jdt/internal/template/TemplateDataLocation.java index 8c2ea2144..a5bca4250 100644 --- a/qute.jdt/com.redhat.qute.jdt/src/main/java/com/redhat/qute/jdt/internal/template/TemplateDataLocation.java +++ b/qute.jdt/com.redhat.qute.jdt/src/main/java/com/redhat/qute/jdt/internal/template/TemplateDataLocation.java @@ -11,7 +11,12 @@ *******************************************************************************/ package com.redhat.qute.jdt.internal.template; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.eclipse.jdt.core.IMethod; import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.jdt.core.dom.ASTNode; import org.eclipse.jdt.core.dom.StringLiteral; import org.eclipse.lsp4j.Location; import org.eclipse.lsp4j.Range; @@ -48,6 +53,8 @@ */ public class TemplateDataLocation extends TemplateDataVisitor { + private static final Logger LOGGER = Logger.getLogger(TemplateDataLocation.class.getName()); + private final String parameterName; private final IJDTUtils utils; @@ -65,21 +72,28 @@ protected boolean visitParameter(Object paramName, Object paramType) { StringLiteral literal = ((StringLiteral) paramName); String paramNameString = literal.getLiteralValue(); if (parameterName.equals(paramNameString)) { - try { - Range range = utils.toRange(getMethod().getOpenable(), literal.getStartPosition(), - literal.getLength()); - String uri = utils.toUri(getMethod().getTypeRoot()); - this.location = new Location(uri, range); - } catch (JavaModelException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + this.location = createParameterLocation(literal); return false; } + } else if (THIS_PARAMETER_NAME.equals(paramName)) { + this.location = createParameterLocation((ASTNode) paramType); } return true; } + public Location createParameterLocation(ASTNode arg0) { + try { + IMethod method = getMethod(); + Range range = utils.toRange(method.getOpenable(), arg0.getStartPosition(), arg0.getLength()); + String uri = utils.toUri(method.getTypeRoot()); + return new Location(uri, range); + } catch (JavaModelException e) { + LOGGER.log(Level.SEVERE, + "Error while getting location of method template parameter of '" + parameterName + "'.", e); + return null; + } + } + public Location getLocation() { return location; } diff --git a/qute.jdt/com.redhat.qute.jdt/src/main/java/com/redhat/qute/jdt/internal/template/TemplateDataVisitor.java b/qute.jdt/com.redhat.qute.jdt/src/main/java/com/redhat/qute/jdt/internal/template/TemplateDataVisitor.java index 8d77addf7..ffb697039 100644 --- a/qute.jdt/com.redhat.qute.jdt/src/main/java/com/redhat/qute/jdt/internal/template/TemplateDataVisitor.java +++ b/qute.jdt/com.redhat.qute.jdt/src/main/java/com/redhat/qute/jdt/internal/template/TemplateDataVisitor.java @@ -10,6 +10,8 @@ public abstract class TemplateDataVisitor extends ASTVisitor { private static final String DATA_METHOD = "data"; + protected static final String THIS_PARAMETER_NAME = "this"; + private IMethod method; @Override @@ -17,8 +19,23 @@ public boolean visit(MethodInvocation node) { String methodName = node.getName().getIdentifier(); if (DATA_METHOD.equals(methodName)) { // .data("book", book) - @SuppressWarnings("rawtypes") - List arguments = node.arguments(); + return visitDataMethodInvocation(node); + } + return super.visit(node); + } + + private boolean visitDataMethodInvocation(MethodInvocation node) { + @SuppressWarnings("rawtypes") + List arguments = node.arguments(); + if (arguments.size() == 1) { + // One parameter + Object paramType = arguments.get(0); + boolean result = visitParameter(THIS_PARAMETER_NAME, paramType); + if (!result) { + return false; + } + } else { + // Several parameters Object paramName = null; for (int i = 0; i < arguments.size(); i++) { if (i % 2 == 0) { @@ -35,7 +52,7 @@ public boolean visit(MethodInvocation node) { } } } - return super.visit(node); + return true; } public void setMethod(IMethod method) { @@ -45,7 +62,7 @@ public void setMethod(IMethod method) { public IMethod getMethod() { return method; } - + protected abstract boolean visitParameter(Object paramName, Object paramType); }