Skip to content

Commit

Permalink
this parameter support
Browse files Browse the repository at this point in the history
Fixes #483

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Dec 13, 2021
1 parent 278e2ed commit ef16649
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@
*/
public class TemplateDataCollector extends TemplateDataVisitor {

private static final String DATA_METHOD = "data";

private final DataModelTemplate<DataModelParameter> template;

public TemplateDataCollector(DataModelTemplate<DataModelParameter> template, IProgressMonitor monitor) {
Expand All @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,32 @@ 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
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) {
Expand All @@ -35,7 +52,7 @@ public boolean visit(MethodInvocation node) {
}
}
}
return super.visit(node);
return true;
}

public void setMethod(IMethod method) {
Expand All @@ -45,7 +62,7 @@ public void setMethod(IMethod method) {
public IMethod getMethod() {
return method;
}

protected abstract boolean visitParameter(Object paramName, Object paramType);

}

0 comments on commit ef16649

Please sign in to comment.