Search in sources :

Example 21 with Template

use of com.redhat.qute.parser.template.Template in project quarkus-ls by redhat-developer.

the class QuteAssert method createTemplate.

private static Template createTemplate(String value, String fileUri, String projectUri, String templateBaseDir, QuteProjectRegistry projectRegistry) {
    Template template = TemplateParser.parse(value, fileUri != null ? fileUri : FILE_URI);
    template.setProjectUri(projectUri);
    projectRegistry.getProject(new ProjectInfo(projectUri, templateBaseDir));
    template.setProjectRegistry(projectRegistry);
    return template;
}
Also used : ProjectInfo(com.redhat.qute.commons.ProjectInfo) Template(com.redhat.qute.parser.template.Template)

Example 22 with Template

use of com.redhat.qute.parser.template.Template in project quarkus-ls by redhat-developer.

the class QutePositionUtility method createLocation.

/**
 * Returns the location for the given <code>target</code> node.
 *
 * @param target the target node.
 * @return the location for the given <code>target</code> node.
 */
public static Location createLocation(Node target) {
    Template targetDocument = target.getOwnerTemplate();
    Range targetRange = createRange(target.getStart(), target.getEnd(), targetDocument);
    return new Location(targetDocument.getUri(), targetRange);
}
Also used : Range(org.eclipse.lsp4j.Range) Template(com.redhat.qute.parser.template.Template) Location(org.eclipse.lsp4j.Location)

Example 23 with Template

use of com.redhat.qute.parser.template.Template in project quarkus-ls by redhat-developer.

the class QutePositionUtility method selectClassName.

public static Range selectClassName(ParameterDeclaration parameter) {
    Template template = parameter.getOwnerTemplate();
    int startOffset = parameter.getClassNameStart();
    int endOffset = parameter.getClassNameEnd();
    return createRange(startOffset, endOffset, template);
}
Also used : Template(com.redhat.qute.parser.template.Template)

Example 24 with Template

use of com.redhat.qute.parser.template.Template in project quarkus-ls by redhat-developer.

the class ExpressionParser method parse.

/**
 * Returns the parsing result of the given expression content.
 *
 * @param expression    the content to parse.
 * @param cancelChecker the cancel checker.
 *
 * @return the parsing result of the given expression content.
 */
public static List<Node> parse(Expression expression, CancelChecker cancelChecker) {
    if (cancelChecker == null) {
        cancelChecker = DEFAULT_CANCEL_CHECKER;
    }
    Template template = expression.getOwnerTemplate();
    String text = template.getText();
    int start = expression.getStartContentOffset();
    int end = expression.getEndContentOffset();
    ExpressionScanner scanner = ExpressionScanner.createScanner(text, start, end);
    TokenType token = scanner.scan();
    List<Node> expressionContent = new ArrayList<>();
    Parts currentParts = null;
    while (token != TokenType.EOS) {
        cancelChecker.checkCanceled();
        int tokenOffset = scanner.getTokenOffset();
        int tokenEnd = scanner.getTokenEnd();
        switch(token) {
            case Whitespace:
                currentParts = null;
                break;
            case NamespacePart:
                currentParts = new Parts(tokenOffset, tokenEnd);
                currentParts.setExpressionParent(expression);
                expressionContent.add(currentParts);
                NamespacePart namespacePart = new NamespacePart(tokenOffset, tokenEnd);
                currentParts.addPart(namespacePart);
                break;
            case ObjectPart:
                if (!(currentParts != null && currentParts.getChildCount() == 1 && currentParts.getChild(0).getPartKind() == PartKind.Namespace)) {
                    currentParts = new Parts(tokenOffset, tokenEnd);
                    currentParts.setExpressionParent(expression);
                    expressionContent.add(currentParts);
                }
                if (currentParts != null) {
                    ObjectPart objectPart = new ObjectPart(tokenOffset, tokenEnd);
                    currentParts.addPart(objectPart);
                }
                break;
            case PropertyPart:
                if (currentParts != null) {
                    PropertyPart propertyPart = new PropertyPart(tokenOffset, tokenEnd);
                    currentParts.addPart(propertyPart);
                }
                break;
            case MethodPart:
                if (currentParts != null) {
                    MethodPart methodPart = new MethodPart(tokenOffset, tokenEnd);
                    currentParts.addPart(methodPart);
                }
                break;
            case Dot:
                if (currentParts != null) {
                    currentParts.addDot(tokenOffset);
                }
                break;
            case ColonSpace:
                if (currentParts != null) {
                    currentParts.addColonSpace(tokenOffset);
                }
                break;
            case OpenBracket:
                if (currentParts != null) {
                    Node last = currentParts.getLastChild();
                    if (last instanceof MethodPart) {
                        ((MethodPart) last).setOpenBracket(tokenOffset);
                    }
                }
                break;
            case CloseBracket:
                if (currentParts != null) {
                    Node last = currentParts.getLastChild();
                    if (last instanceof MethodPart) {
                        ((MethodPart) last).setCloseBracket(tokenOffset);
                    }
                }
                break;
            case StartString:
            case String:
            case EndString:
                // ignore string tokens
                break;
            default:
                currentParts = null;
                break;
        }
        token = scanner.scan();
    }
    // adjust end offset for the current parts
    if (currentParts != null) {
        currentParts.setEnd(end);
        Node last = currentParts.getLastChild();
        if (last instanceof MethodPart) {
            if (!last.isClosed()) {
                // the current method is not closed with ')', adjust end method with the end
                // offset.
                ((MethodPart) last).setEnd(end);
            }
        }
    }
    return expressionContent;
}
Also used : Node(com.redhat.qute.parser.template.Node) ArrayList(java.util.ArrayList) Template(com.redhat.qute.parser.template.Template) TokenType(com.redhat.qute.parser.expression.scanner.TokenType) ExpressionScanner(com.redhat.qute.parser.expression.scanner.ExpressionScanner)

Example 25 with Template

use of com.redhat.qute.parser.template.Template in project quarkus-ls by redhat-developer.

the class ParameterParser method parse.

public static List<Parameter> parse(ParametersContainer expression, boolean methodParameters, CancelChecker cancelChecker) {
    if (cancelChecker == null) {
        cancelChecker = DEFAULT_CANCEL_CHECKER;
    }
    Template template = expression.getOwnerTemplate();
    String text = template.getText();
    int start = expression.getStartParametersOffset();
    int end = expression.getEndParametersOffset();
    ParameterScanner scanner = ParameterScanner.createScanner(text, start, end, methodParameters);
    TokenType token = scanner.scan();
    List<Parameter> parameters = new ArrayList<>();
    Parameter currentParameter = null;
    while (token != TokenType.EOS) {
        cancelChecker.checkCanceled();
        int tokenOffset = scanner.getTokenOffset();
        int tokenEnd = scanner.getTokenEnd();
        switch(token) {
            case Whitespace:
                currentParameter = null;
                break;
            case ParameterName:
                currentParameter = new Parameter(tokenOffset, tokenEnd);
                currentParameter.setParameterParent(expression);
                parameters.add(currentParameter);
                break;
            case ParameterValue:
                if (currentParameter != null) {
                    // current parameter can be null when parameter name is not defined
                    // ex : {#let =123} (instead of {#let name=123})
                    currentParameter.setStartValue(tokenOffset);
                    currentParameter.setEndValue(tokenEnd);
                }
                break;
            default:
        }
        token = scanner.scan();
    }
    return parameters;
}
Also used : TokenType(com.redhat.qute.parser.parameter.scanner.TokenType) ArrayList(java.util.ArrayList) ParameterScanner(com.redhat.qute.parser.parameter.scanner.ParameterScanner) Parameter(com.redhat.qute.parser.template.Parameter) Template(com.redhat.qute.parser.template.Template)

Aggregations

Template (com.redhat.qute.parser.template.Template)29 JavaDataModelCache (com.redhat.qute.project.datamodel.JavaDataModelCache)13 MockQuteProjectRegistry (com.redhat.qute.project.MockQuteProjectRegistry)11 QuteProjectRegistry (com.redhat.qute.project.QuteProjectRegistry)11 QuteLanguageService (com.redhat.qute.services.QuteLanguageService)11 Position (org.eclipse.lsp4j.Position)8 Node (com.redhat.qute.parser.template.Node)6 Range (org.eclipse.lsp4j.Range)6 ArrayList (java.util.ArrayList)5 Parameter (com.redhat.qute.parser.template.Parameter)4 BadLocationException (com.redhat.qute.ls.commons.BadLocationException)3 Section (com.redhat.qute.parser.template.Section)3 LoopSection (com.redhat.qute.parser.template.sections.LoopSection)3 QuteProject (com.redhat.qute.project.QuteProject)3 CompletionItem (org.eclipse.lsp4j.CompletionItem)3 ResolvedJavaTypeInfo (com.redhat.qute.commons.ResolvedJavaTypeInfo)2 NamespacePart (com.redhat.qute.parser.expression.NamespacePart)2 Part (com.redhat.qute.parser.expression.Part)2 Parts (com.redhat.qute.parser.expression.Parts)2 Expression (com.redhat.qute.parser.template.Expression)2