use of com.redhat.qute.parser.expression.Parts in project quarkus-ls by redhat-developer.
the class QuteCompletions method doComplete.
/**
* Returns completion list for the given position
*
* @param template the Qute template
* @param position the position where completion was triggered
* @param completionSettings the completion settings.
* @param formattingSettings the formatting settings.
* @param cancelChecker the cancel checker
* @return completion list for the given position
*/
public CompletableFuture<CompletionList> doComplete(Template template, Position position, QuteCompletionSettings completionSettings, QuteFormattingSettings formattingSettings, CancelChecker cancelChecker) {
CompletionRequest completionRequest = null;
try {
completionRequest = new CompletionRequest(template, position, completionSettings, formattingSettings);
} catch (BadLocationException e) {
LOGGER.log(Level.SEVERE, "Creation of CompletionRequest failed", e);
return EMPTY_FUTURE_COMPLETION;
}
Node node = completionRequest.getNode();
if (node == null) {
return EMPTY_FUTURE_COMPLETION;
}
String text = template.getText();
int offset = completionRequest.getOffset();
if (node.getKind() == NodeKind.Expression || node.getKind() == NodeKind.ExpressionParts || node.getKind() == NodeKind.ExpressionPart) {
Expression expression = null;
Node nodeExpression = null;
if (node.getKind() == NodeKind.Expression) {
expression = (Expression) node;
} else if (node.getKind() == NodeKind.ExpressionParts) {
nodeExpression = node;
expression = ((Parts) node).getParent();
} else if (node.getKind() == NodeKind.ExpressionPart) {
nodeExpression = node;
expression = ((Part) node).getParent().getParent();
}
return completionForExpression.doCompleteExpression(completionRequest, expression, nodeExpression, template, offset, completionSettings, formattingSettings, cancelChecker);
} else if (node.getKind() == NodeKind.Text) {
// The completion is triggered in text node (before node)
Section parent = node.getParentSection();
if (parent != null && (parent.isInEndTagName(offset))) {
// The completion is triggered inside end tag
return EMPTY_FUTURE_COMPLETION;
}
// The completion is triggered in text node
// Check if completion is triggered after a start bracket character and if it's
// a valid expression
int nbBrackets = 0;
int bracketOffset = offset - 1;
char previousChar = text.charAt(bracketOffset);
if (previousChar == '#') {
// {#
bracketOffset--;
}
while (bracketOffset >= 0 && text.charAt(bracketOffset) == '{') {
bracketOffset--;
nbBrackets++;
}
if (nbBrackets > 0) {
if (nbBrackets % 2 != 0) {
// The completion is triggered in text node after bracket '{' character
return completionForExpression.doCompleteExpression(completionRequest, null, node, template, offset, completionSettings, formattingSettings, cancelChecker);
}
return EMPTY_FUTURE_COMPLETION;
}
} else if (node.getKind() == NodeKind.ParameterDeclaration) {
return completionsForParameterDeclaration.doCollectJavaClassesSuggestions((ParameterDeclaration) node, template, offset, completionSettings);
} else if (node.getKind() == NodeKind.Section) {
// {#|}
return completionForTagSection.doCompleteTagSection(completionRequest, completionSettings, formattingSettings, cancelChecker);
}
return collectSnippetSuggestions(completionRequest);
}
use of com.redhat.qute.parser.expression.Parts in project quarkus-ls by redhat-developer.
the class QuteDiagnostics method validateExpression.
private ResolvedJavaTypeInfo validateExpression(Expression expression, Section ownerSection, Template template, QuteValidationSettings validationSettings, ResolutionContext resolutionContext, ResolvingJavaTypeContext resolvingJavaTypeContext, List<Diagnostic> diagnostics) {
try {
String projectUri = template.getProjectUri();
String literalJavaType = expression.getLiteralJavaType();
if (literalJavaType != null) {
// The expression is a literal:
// - {'abcd'} : string literal
// - {true} : boolean literal
// - {null} : null literal
// - {123} : integer literal
ResolvedJavaTypeInfo resolvedLiteralType = javaCache.resolveJavaType(literalJavaType, projectUri).getNow(null);
if (resolvedLiteralType == null) {
return null;
}
return validateIterable(expression.getLastPart(), ownerSection, resolvedLiteralType, resolvedLiteralType.getName(), diagnostics);
}
// The expression reference Java data model (ex : {item})
ResolvedJavaTypeInfo resolvedJavaType = null;
List<Node> expressionChildren = expression.getExpressionContent();
for (Node expressionChild : expressionChildren) {
if (expressionChild.getKind() == NodeKind.ExpressionParts) {
Parts parts = (Parts) expressionChild;
resolvedJavaType = validateExpressionParts(parts, ownerSection, template, projectUri, validationSettings, resolutionContext, resolvingJavaTypeContext, diagnostics);
}
}
return resolvedJavaType;
} catch (CancellationException e) {
throw e;
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Error while validating expression '" + expression.getContent() + "' in '" + template.getUri() + "'.", e);
return null;
}
}
use of com.redhat.qute.parser.expression.Parts in project quarkus-ls by redhat-developer.
the class QuteHover method doHoverForPropertyPart.
private CompletableFuture<Hover> doHoverForPropertyPart(Part part, String projectUri, HoverRequest hoverRequest) {
Parts parts = part.getParent();
Part previousPart = parts.getPreviousPart(part);
return //
javaCache.resolveJavaType(previousPart, projectUri).thenCompose(resolvedJavaType -> {
if (resolvedJavaType != null) {
if (resolvedJavaType.isIterable() && !resolvedJavaType.isArray()) {
// Expression uses iterable type
// {@java.util.List<org.acme.Item items>
// {items.si|ze()}
// Property, method to find as hover must be done for iterable type (ex :
// java.util.List>
String iterableType = resolvedJavaType.getIterableType();
CompletableFuture<ResolvedJavaTypeInfo> iterableResolvedTypeFuture = javaCache.resolveJavaType(iterableType, projectUri);
return iterableResolvedTypeFuture.thenApply((iterableResolvedType) -> {
return doHoverForPropertyPart(part, projectUri, iterableResolvedType, resolvedJavaType, hoverRequest);
});
}
Hover hover = doHoverForPropertyPart(part, projectUri, resolvedJavaType, null, hoverRequest);
return CompletableFuture.completedFuture(hover);
}
return NO_HOVER;
});
}
use of com.redhat.qute.parser.expression.Parts in project quarkus-ls by redhat-developer.
the class JavaDataModelCache method resolveJavaType.
private CompletableFuture<ResolvedJavaTypeInfo> resolveJavaType(Parts parts, int partIndex, String projectUri, boolean nullIfDontMatchWithIterable) {
CompletableFuture<ResolvedJavaTypeInfo> future = null;
for (int i = 0; i < partIndex + 1; i++) {
Part current = ((Part) parts.getChild(i));
switch(current.getPartKind()) {
case Object:
ObjectPart objectPart = (ObjectPart) current;
future = resolveJavaType(objectPart, projectUri, nullIfDontMatchWithIterable);
break;
case Property:
case Method:
if (future != null) {
future = //
future.thenCompose(resolvedType -> {
if (resolvedType == null) {
return RESOLVED_JAVA_TYPE_INFO_NULL_FUTURE;
}
return resolveJavaType(current, projectUri, resolvedType);
});
}
break;
default:
}
}
return future != null ? future : RESOLVED_JAVA_TYPE_INFO_NULL_FUTURE;
}
use of com.redhat.qute.parser.expression.Parts in project quarkus-ls by redhat-developer.
the class Expression method getLastPart.
/**
* Returns the last part of the expression and null otherwise.
*
* @return the last part of the expression and null otherwise.
*/
public Part getLastPart() {
List<Node> nodes = getExpressionContent();
if (nodes.isEmpty()) {
return null;
}
Parts parts = (Parts) nodes.get(0);
return (Part) parts.getLastChild();
}
Aggregations