use of com.redhat.qute.parser.template.Parameter in project quarkus-ls by redhat-developer.
the class QuteSearchUtils method tryToCollectObjectParts.
/**
* Try to collect all object parts from the given expression
* <code>nodeExpr</code> which matches the given part name
* <code>partName</code>.
*
* @param partName the part name.
* @param matcher the matcher strategy.
* @param nodeExpr the Qute node expression
* @param collector the node collector.
*
* @return true if the given nodeExpr reference the part name as object part and
* false otherwise.
*/
private static boolean tryToCollectObjectParts(String partName, PartNameMatcher matcher, Node nodeExpr, BiConsumer<Node, Range> collector) {
switch(nodeExpr.getKind()) {
case Expression:
{
Expression expression = (Expression) nodeExpr;
boolean result = false;
List<Node> nodes = expression.getExpressionContent();
for (Node node : nodes) {
// ex : partName=item
// node expression can be
// 1) a simple expression:
// --> {item_count} --> [ObjectPart=item_count]
// --> {item.name} --> [ObjectPart=item, PropertyPart=name)
// --> {item.getName()} --> [ObjectPart=item, MethodPart=getName())
// In those cases, one object part will be collected =>{|item|_count},
// {|item|.name}, {|item|.getName()}
// 2) an expression with method part which can host another expressions when
// method have parameters
// --> {item.getName(item.name)} --> [ObjectPart=item,
// MethodPart=getName(item.name))
// In this case, two object parts will be collected =>
// {|item|.getName(|item|.name)}
result |= tryToCollectObjectParts(partName, matcher, node, collector);
}
return result;
}
case ExpressionParts:
{
Parts parts = (Parts) nodeExpr;
boolean result = false;
for (Node partNode : parts.getChildren()) {
result |= tryToCollectObjectParts(partName, matcher, partNode, collector);
}
return result;
}
case ExpressionPart:
{
Part part = (Part) nodeExpr;
if (part.getPartKind() == PartKind.Object) {
ObjectPart objectPart = (ObjectPart) part;
if (isMatch(objectPart, partName, matcher)) {
Range range = QutePositionUtility.createRange(objectPart);
collector.accept(objectPart, range);
return true;
}
} else if (part.getPartKind() == PartKind.Method) {
MethodPart methodPart = (MethodPart) part;
List<Parameter> parameters = methodPart.getParameters();
boolean result = false;
for (Parameter parameter : parameters) {
Expression paramExpr = parameter.getJavaTypeExpression();
if (paramExpr != null) {
result |= tryToCollectObjectParts(partName, matcher, paramExpr, collector);
}
}
return result;
}
}
break;
default:
break;
}
return false;
}
use of com.redhat.qute.parser.template.Parameter in project quarkus-ls by redhat-developer.
the class ObjectPart method resolveJavaType.
public JavaTypeInfoProvider resolveJavaType() {
Template template = super.getOwnerTemplate();
String partName = getPartName();
boolean hasNamespace = getNamespace() != null;
if (hasNamespace) {
// ex : {data:item}
return template.findWithNamespace(this);
}
// ex : {item}
// Loop for parent section to discover the class name
Section section = super.getParentSection();
while (section != null) {
switch(section.getSectionKind()) {
case EACH:
case FOR:
LoopSection iterableSection = (LoopSection) section;
if (!iterableSection.isInElseBlock(getStart())) {
String alias = iterableSection.getAlias();
if (partName.equals(alias)) {
return iterableSection.getIterableParameter();
}
}
break;
case LET:
case SET:
List<Parameter> parameters = section.getParameters();
for (Parameter parameter : parameters) {
if (partName.equals(parameter.getName())) {
return parameter;
}
}
break;
default:
}
// ex : count for #each
JavaTypeInfoProvider metadata = section.getMetadata(partName);
if (metadata != null) {
return metadata;
}
section = section.getParentSection();
}
// - from @CheckedTemplate
return template.findInInitialDataModel(this);
}
use of com.redhat.qute.parser.template.Parameter in project quarkus-ls by redhat-developer.
the class QuteCodeLens method collectInsertCodeLenses.
private static void collectInsertCodeLenses(Node parent, Template template, QuteProject project, List<CodeLens> lenses, CancelChecker cancelChecker) {
cancelChecker.checkCanceled();
if (parent.getKind() == NodeKind.Section) {
Section section = (Section) parent;
if (section.getSectionKind() == SectionKind.INSERT) {
if (project != null) {
Parameter parameter = section.getParameterAtIndex(0);
if (parameter != null) {
String tag = parameter.getValue();
// TODO : implement findNbreferencesOfInsertTag correctly
// project.findNbreferencesOfInsertTag(template.getTemplateId(), tag);
int nbReferences = 0;
if (nbReferences > 0) {
String title = nbReferences == 1 ? "1 reference" : nbReferences + " references";
Range range = QutePositionUtility.createRange(parameter);
Command command = new Command(title, "");
CodeLens codeLens = new CodeLens(range, command, null);
lenses.add(codeLens);
}
}
}
}
}
List<Node> children = parent.getChildren();
for (Node node : children) {
collectInsertCodeLenses(node, template, project, lenses, cancelChecker);
}
}
use of com.redhat.qute.parser.template.Parameter in project quarkus-ls by redhat-developer.
the class QuteHover method doHoverForParameter.
private CompletableFuture<Hover> doHoverForParameter(Parameter parameter, Template template, HoverRequest hoverRequest) {
int offset = hoverRequest.getOffset();
if (parameter.isInName(offset)) {
// A parameter name is hovered
if (parameter.getOwnerSection() != null && (parameter.getOwnerSection().getSectionKind() == SectionKind.FOR || parameter.getOwnerSection().getSectionKind() == SectionKind.EACH)) {
// a parameter from #for section is hovered
LoopSection iterableSection = (LoopSection) parameter.getOwnerSection();
if (iterableSection.isInAlias(offset)) {
Parameter iterableParameter = iterableSection.getIterableParameter();
if (iterableParameter != null) {
boolean hasMarkdown = hoverRequest.canSupportMarkupKind(MarkupKind.MARKDOWN);
String projectUri = template.getProjectUri();
Part iterablePart = iterableParameter.getJavaTypeExpression().getLastPart();
return //
javaCache.resolveJavaType(iterablePart, projectUri).thenCompose(resolvedJavaType -> {
if (resolvedJavaType != null && resolvedJavaType.isIterable()) {
return //
javaCache.resolveJavaType(resolvedJavaType.getIterableOf(), projectUri).thenApply(resolvedIterableOf -> {
MarkupContent content = DocumentationUtils.getDocumentation(resolvedIterableOf, hasMarkdown);
Range range = QutePositionUtility.createRange(parameter);
return new Hover(content, range);
});
}
return NO_HOVER;
});
}
}
} else {
// Other parameter
// Check if part is a literal (ex: true, null, 123, 'abc', etc)
Expression expression = parameter.getJavaTypeExpression();
if (expression != null) {
String projectUri = template.getProjectUri();
String literalJavaType = expression.getLiteralJavaType();
if (literalJavaType != null) {
return //
javaCache.resolveJavaType(literalJavaType, projectUri).thenApply(resolvedJavaType -> {
if (resolvedJavaType != null) {
boolean hasMarkdown = hoverRequest.canSupportMarkupKind(MarkupKind.MARKDOWN);
MarkupContent content = DocumentationUtils.getDocumentation(resolvedJavaType, hasMarkdown);
Range range = QutePositionUtility.createRange(parameter);
return new Hover(content, range);
}
return null;
});
}
return //
javaCache.resolveJavaType(parameter, projectUri).thenApply(resolvedJavaType -> {
if (resolvedJavaType != null) {
boolean hasMarkdown = hoverRequest.canSupportMarkupKind(MarkupKind.MARKDOWN);
MarkupContent content = DocumentationUtils.getDocumentation(resolvedJavaType, hasMarkdown);
Range range = QutePositionUtility.createRange(parameter);
return new Hover(content, range);
}
return null;
});
}
}
}
return NO_HOVER;
}
use of com.redhat.qute.parser.template.Parameter in project quarkus-ls by redhat-developer.
the class QuteHover method doHover.
public CompletableFuture<Hover> doHover(Template template, Position position, SharedSettings settings, CancelChecker cancelChecker) {
cancelChecker.checkCanceled();
HoverRequest hoverRequest = null;
try {
hoverRequest = new HoverRequest(template, position, settings);
} catch (BadLocationException e) {
LOGGER.log(Level.SEVERE, "Failed creating HoverRequest", e);
return NO_HOVER;
}
Node node = hoverRequest.getNode();
if (node == null) {
return NO_HOVER;
}
switch(node.getKind()) {
case Section:
// - Start end tag definition
Section section = (Section) node;
return doHoverForSection(section, template, hoverRequest, cancelChecker);
case ParameterDeclaration:
ParameterDeclaration parameterDeclaration = (ParameterDeclaration) node;
return doHoverForParameterDeclaration(parameterDeclaration, template, hoverRequest, cancelChecker);
case ExpressionPart:
Part part = (Part) node;
return doHoverForExpressionPart(part, template, hoverRequest, cancelChecker);
case Parameter:
Parameter parameter = (Parameter) node;
return doHoverForParameter(parameter, template, hoverRequest);
default:
return NO_HOVER;
}
}
Aggregations