Search in sources :

Example 21 with Node

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

the class QuteSymbolsProvider method findDocumentSymbols.

public List<DocumentSymbol> findDocumentSymbols(Template template, CancelChecker cancelChecker) {
    List<DocumentSymbol> symbols = new ArrayList<>();
    for (int i = 0; i < template.getChildCount(); i++) {
        Node child = template.getChild(i);
        findDocumentSymbols(child, symbols, template, cancelChecker);
    }
    return symbols;
}
Also used : Node(com.redhat.qute.parser.template.Node) ArrayList(java.util.ArrayList) DocumentSymbol(org.eclipse.lsp4j.DocumentSymbol)

Example 22 with Node

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

the class QuteSymbolsProvider method findDocumentSymbols.

private void findDocumentSymbols(Node node, List<DocumentSymbol> symbols, Template template, CancelChecker cancelChecker) {
    if (!isNodeSymbol(node)) {
        return;
    }
    cancelChecker.checkCanceled();
    String name = nodeToName(node);
    Range selectionRange = getSymbolRange(node);
    Range range = selectionRange;
    List<DocumentSymbol> children = new ArrayList<>();
    DocumentSymbol symbol = new DocumentSymbol(name, getSymbolKind(node), range, selectionRange, null, children);
    symbols.add(symbol);
    if (node.getChildCount() > 0) {
        for (int i = 0; i < node.getChildCount(); i++) {
            Node child = node.getChild(i);
            findDocumentSymbols(child, children, template, cancelChecker);
        }
    }
}
Also used : Node(com.redhat.qute.parser.template.Node) ArrayList(java.util.ArrayList) DocumentSymbol(org.eclipse.lsp4j.DocumentSymbol) Range(org.eclipse.lsp4j.Range)

Example 23 with Node

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

the class QuteSearchUtils method searchDeclaredObjectInParameter.

private static Parameter searchDeclaredObjectInParameter(ObjectPart part, CancelChecker cancelChecker) {
    String partName = part.getPartName();
    Node parent = part.getParentSection();
    while (parent != null) {
        if (parent.getKind() == NodeKind.Section) {
            Section section = (Section) parent;
            switch(section.getSectionKind()) {
                case EACH:
                case FOR:
                    LoopSection iterableSection = (LoopSection) section;
                    if (!iterableSection.isInElseBlock(part.getStart())) {
                        String alias = iterableSection.getAlias();
                        if (partName.equals(alias) || section.isMetadata(partName)) {
                            // - or the metadata of the section (ex : item_count)
                            return iterableSection.getAliasParameter();
                        }
                    }
                    break;
                case LET:
                case SET:
                    List<Parameter> parameters = section.getParameters();
                    for (Parameter parameter : parameters) {
                        if (partName.equals(parameter.getName())) {
                            return parameter;
                        }
                    }
                    break;
                default:
            }
        }
        parent = parent.getParent();
    }
    return null;
}
Also used : LoopSection(com.redhat.qute.parser.template.sections.LoopSection) Node(com.redhat.qute.parser.template.Node) Parameter(com.redhat.qute.parser.template.Parameter) LoopSection(com.redhat.qute.parser.template.sections.LoopSection) BaseWhenSection(com.redhat.qute.parser.template.sections.BaseWhenSection) WithSection(com.redhat.qute.parser.template.sections.WithSection) Section(com.redhat.qute.parser.template.Section)

Example 24 with Node

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

the class QuteProject method collectInsert.

private void collectInsert(String insertParamater, Node parent, Template template, List<QuteIndex> indexes) {
    if (parent.getKind() == NodeKind.Section) {
        Section section = (Section) parent;
        if (section.getSectionKind() == SectionKind.INSERT) {
            Parameter parameter = section.getParameterAtIndex(0);
            if (parameter != null) {
                try {
                    if (insertParamater == null || insertParamater.equals(parameter.getValue())) {
                        Position position = template.positionAt(parameter.getStart());
                        Path path = createPath(template.getUri());
                        QuteTemplateIndex templateIndex = new QuteTemplateIndex(path, template.getTemplateId());
                        QuteIndex index = new QuteIndex("insert", parameter.getValue(), position, SectionKind.INSERT, templateIndex);
                        indexes.add(index);
                    }
                } catch (BadLocationException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    List<Node> children = parent.getChildren();
    for (Node node : children) {
        collectInsert(insertParamater, node, template, indexes);
    }
}
Also used : Path(java.nio.file.Path) FileUtils.createPath(com.redhat.qute.utils.FileUtils.createPath) QuteTemplateIndex(com.redhat.qute.project.indexing.QuteTemplateIndex) Position(org.eclipse.lsp4j.Position) Node(com.redhat.qute.parser.template.Node) QuteIndex(com.redhat.qute.project.indexing.QuteIndex) Parameter(com.redhat.qute.parser.template.Parameter) DataModelParameter(com.redhat.qute.commons.datamodel.DataModelParameter) Section(com.redhat.qute.parser.template.Section) BadLocationException(com.redhat.qute.ls.commons.BadLocationException)

Example 25 with Node

use of com.redhat.qute.parser.template.Node 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)

Aggregations

Node (com.redhat.qute.parser.template.Node)27 Section (com.redhat.qute.parser.template.Section)14 BadLocationException (com.redhat.qute.ls.commons.BadLocationException)10 Parameter (com.redhat.qute.parser.template.Parameter)10 Range (org.eclipse.lsp4j.Range)10 Expression (com.redhat.qute.parser.template.Expression)8 ArrayList (java.util.ArrayList)7 Part (com.redhat.qute.parser.expression.Part)6 ObjectPart (com.redhat.qute.parser.expression.ObjectPart)5 Template (com.redhat.qute.parser.template.Template)5 Parts (com.redhat.qute.parser.expression.Parts)4 IncludeSection (com.redhat.qute.parser.template.sections.IncludeSection)4 LoopSection (com.redhat.qute.parser.template.sections.LoopSection)4 List (java.util.List)4 ResolvedJavaTypeInfo (com.redhat.qute.commons.ResolvedJavaTypeInfo)3 MethodPart (com.redhat.qute.parser.expression.MethodPart)3 NamespacePart (com.redhat.qute.parser.expression.NamespacePart)3 QuteProject (com.redhat.qute.project.QuteProject)3 QuteIndex (com.redhat.qute.project.indexing.QuteIndex)3 JsonObject (com.google.gson.JsonObject)2