Skip to content

Commit

Permalink
Definition for user tag parameter
Browse files Browse the repository at this point in the history
Fixes #789

Signed-off-by: azerr <azerr@redhat.com>
  • Loading branch information
angelozerr committed Feb 10, 2023
1 parent 65ec61d commit 5e784f6
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,24 @@ public Collection<UserTagParameter> getParameters() {
return parameters.values();
}

/**
* Returns the parameter which have the given name <code>parameterName</code>
* and null otherwise.
*
* @param parameterName the parameter name.
*
* @return the parameter which have the given name <code>parameterName</code>
* and null otherwise.
*/
public UserTagParameter findParameter(String parameterName) {
for (UserTagParameter parameter : getParameters()) {
if (parameterName.equals(parameter.getPartName())) {
return parameter;
}
}
return null;
}

/**
* Returns parameters of the user tag.
*
Expand All @@ -189,7 +207,7 @@ private Template getTemplate() {
if (content == null) {
return null;
}
return TemplateParser.parse(content, getFileName());
return TemplateParser.parse(content, getUri());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import com.redhat.qute.project.datamodel.JavaDataModelCache;
import com.redhat.qute.project.indexing.QuteIndex;
import com.redhat.qute.project.tags.UserTag;
import com.redhat.qute.project.tags.UserTagParameter;
import com.redhat.qute.services.definition.DefinitionRequest;
import com.redhat.qute.utils.QutePositionUtility;
import com.redhat.qute.utils.QuteSearchUtils;
Expand Down Expand Up @@ -86,22 +87,24 @@ public CompletableFuture<List<? extends LocationLink>> findDefinition(Template t
}
int offset = definitionRequest.getOffset();
switch (node.getKind()) {
case Section:
// - Start end tag definition
// - Java data model definition
return findDefinitionFromSection(offset, (Section) node, template, cancelChecker);
case ParameterDeclaration:
// Return Java class definition
return findDefinitionFromParameterDeclaration(offset, (ParameterDeclaration) node, template,
cancelChecker);
case Expression:
return findDefinitionFromExpression(offset, (Expression) node, template, cancelChecker);
case ExpressionPart:
Part part = (Part) node;
return findDefinitionFromPart(part, template, cancelChecker);
default:
// no definitions
return NO_DEFINITION;
case Section:
// - Start end tag definition
// - Java data model definition
return findDefinitionFromSection(offset, (Section) node, template, cancelChecker);
case ParameterDeclaration:
// Return Java class definition
return findDefinitionFromParameterDeclaration(offset, (ParameterDeclaration) node, template,
cancelChecker);
case Expression:
return findDefinitionFromExpression(offset, (Expression) node, template, cancelChecker);
case ExpressionPart:
Part part = (Part) node;
return findDefinitionFromPart(part, template, cancelChecker);
case Parameter:
return findDefinitionFromParameter(offset, (Parameter) node, template, cancelChecker);
default:
// no definitions
return NO_DEFINITION;
}

} catch (BadLocationException e) {
Expand Down Expand Up @@ -262,14 +265,14 @@ private CompletableFuture<List<? extends LocationLink>> findDefinitionFromPart(P
}

switch (part.getPartKind()) {
case Object:
return findDefinitionFromObjectPart((ObjectPart) part, template, cancelChecker);
case Property:
return findDefinitionFromPropertyPart((PropertyPart) part, template, cancelChecker);
case Method:
return findDefinitionFromPropertyPart((MethodPart) part, template, cancelChecker);
default:
return NO_DEFINITION;
case Object:
return findDefinitionFromObjectPart((ObjectPart) part, template, cancelChecker);
case Property:
return findDefinitionFromPropertyPart((PropertyPart) part, template, cancelChecker);
case Method:
return findDefinitionFromPropertyPart((MethodPart) part, template, cancelChecker);
default:
return NO_DEFINITION;
}
}

Expand Down Expand Up @@ -435,16 +438,46 @@ private CompletableFuture<List<? extends LocationLink>> findDefinitionFromJavaMe
QuteJavaDefinitionParams params = new QuteJavaDefinitionParams(sourceType, projectUri);
if (member != null) {
switch (member.getJavaElementKind()) {
case FIELD:
params.setSourceField(member.getName());
break;
case METHOD:
params.setSourceMethod(member.getName());
break;
default:
case FIELD:
params.setSourceField(member.getName());
break;
case METHOD:
params.setSourceMethod(member.getName());
break;
default:
}
}
return findJavaDefinition(params, cancelChecker, () -> QutePositionUtility.createRange(part));
}

private CompletableFuture<List<? extends LocationLink>> findDefinitionFromParameter(int offset, Parameter parameter,
Template template, CancelChecker cancelChecker) {
if (!parameter.isInName(offset)) {
return NO_DEFINITION;
}
Section section = parameter.getOwnerSection();
if (section == null || section.getSectionKind() != SectionKind.CUSTOM) {
return NO_DEFINITION;
}
QuteProject project = template.getProject();
if (project == null) {
return NO_DEFINITION;
}
UserTag userTag = project.findUserTag(section.getTag());
if (userTag == null) {
return NO_DEFINITION;
}
UserTagParameter userTagParameter = userTag.findParameter(parameter.getName());
if (userTagParameter == null) {
return NO_DEFINITION;
}
ObjectPart part = userTagParameter.getPart();
List<LocationLink> locations = new ArrayList<>();
String targetUri = part.getOwnerTemplate().getUri();
Range targetRange = QutePositionUtility.createRange(part);
Range originSelectionRange = QutePositionUtility.selectParameterName(parameter);
LocationLink locationLink = new LocationLink(targetUri, targetRange, targetRange, originSelectionRange);
locations.add(locationLink);
return CompletableFuture.completedFuture(locations);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import static com.redhat.qute.QuteAssert.r;
import static com.redhat.qute.QuteAssert.testDefinitionFor;

import java.nio.file.Paths;

import org.junit.jupiter.api.Test;

/**
Expand Down Expand Up @@ -51,4 +53,21 @@ public void noIt() throws Exception {
testDefinitionFor(template);
}

@Test
public void inputName() throws Exception {
String inputUri = getUserTagUri("input");
String template = "{#input na|me=\"name\"}";
testDefinitionFor(template, "test.html", //
ll(inputUri, r(0, 8, 0, 12), r(0, 14, 0, 18)));
}

@Test
public void inputNoDef() throws Exception {
String template = "{#input na|me2=\"name\"}";
testDefinitionFor(template);
}

private static String getUserTagUri(String name) {
return Paths.get("src/test/resources/templates/tags/" + name + ".html").toUri().toString();
}
}

0 comments on commit 5e784f6

Please sign in to comment.