Search in sources :

Example 6 with Node

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

the class QuteCodeActions method doCodeActionsForUndefinedObject.

private static void doCodeActionsForUndefinedObject(Template template, Diagnostic diagnostic, QuteErrorCode errorCode, List<CodeAction> codeActions) {
    try {
        String varName = null;
        boolean isIterable = false;
        JsonObject data = (JsonObject) diagnostic.getData();
        if (data != null) {
            varName = data.get(DIAGNOSTIC_DATA_NAME).getAsString();
            isIterable = data.get(DIAGNOSTIC_DATA_ITERABLE).getAsBoolean();
        } else {
            int offset = template.offsetAt(diagnostic.getRange().getStart());
            Node node = template.findNodeAt(offset);
            node = QutePositionUtility.findBestNode(offset, node);
            if (node.getKind() == NodeKind.Expression) {
                Expression expression = (Expression) node;
                ObjectPart part = expression.getObjectPart();
                if (part != null) {
                    varName = part.getPartName();
                }
            }
        }
        if (varName != null) {
            TextDocument document = template.getTextDocument();
            String lineDelimiter = document.lineDelimiter(0);
            String title = MessageFormat.format(UNDEFINED_OBJECT_CODEACTION_TITLE, varName);
            Position position = new Position(0, 0);
            StringBuilder insertText = new StringBuilder("{@");
            if (isIterable) {
                insertText.append("java.util.List");
            } else {
                insertText.append("java.lang.String");
            }
            insertText.append(" ");
            insertText.append(varName);
            insertText.append("}");
            insertText.append(lineDelimiter);
            CodeAction insertParameterDeclarationQuickFix = CodeActionFactory.insert(title, position, insertText.toString(), document, diagnostic);
            codeActions.add(insertParameterDeclarationQuickFix);
            // CodeAction to set validation severity to ignore
            doCodeActionToSetIgnoreSeverity(template, Collections.singletonList(diagnostic), errorCode, codeActions, UNDEFINED_OBJECT_SEVERITY_SETTING);
        }
    } catch (BadLocationException e) {
    }
}
Also used : TextDocument(com.redhat.qute.ls.commons.TextDocument) ObjectPart(com.redhat.qute.parser.expression.ObjectPart) Expression(com.redhat.qute.parser.template.Expression) Position(org.eclipse.lsp4j.Position) Node(com.redhat.qute.parser.template.Node) CodeAction(org.eclipse.lsp4j.CodeAction) JsonObject(com.google.gson.JsonObject) BadLocationException(com.redhat.qute.ls.commons.BadLocationException)

Example 7 with Node

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

the class QutePositionUtility method findBestNode.

/**
 * Find the best node from the given expression at the given offset.
 *
 * @param expression the expression node.
 * @param offset     the offset.
 *
 * @return the best node from the given expression at the given offset.
 */
private static Node findBestNode(Expression expression, int offset) {
    Node expressionNode = expression.findNodeExpressionAt(offset);
    if (expressionNode != null) {
        if (expressionNode instanceof MethodPart) {
            MethodPart method = (MethodPart) expressionNode;
            Parameter parameter = method.getParameterAtOffset(offset);
            if (parameter != null) {
                Expression parameterExpression = parameter.getJavaTypeExpression();
                if (parameterExpression != null) {
                    return findBestNode(parameterExpression, offset);
                }
                return parameter;
            }
        }
        return expressionNode;
    }
    return expression;
}
Also used : Expression(com.redhat.qute.parser.template.Expression) Node(com.redhat.qute.parser.template.Node) Parameter(com.redhat.qute.parser.template.Parameter) MethodPart(com.redhat.qute.parser.expression.MethodPart)

Example 8 with Node

use of com.redhat.qute.parser.template.Node 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;
}
Also used : ObjectPart(com.redhat.qute.parser.expression.ObjectPart) Expression(com.redhat.qute.parser.template.Expression) Parts(com.redhat.qute.parser.expression.Parts) ObjectPart(com.redhat.qute.parser.expression.ObjectPart) NamespacePart(com.redhat.qute.parser.expression.NamespacePart) MethodPart(com.redhat.qute.parser.expression.MethodPart) Part(com.redhat.qute.parser.expression.Part) Node(com.redhat.qute.parser.template.Node) Parameter(com.redhat.qute.parser.template.Parameter) List(java.util.List) Range(org.eclipse.lsp4j.Range) MethodPart(com.redhat.qute.parser.expression.MethodPart)

Example 9 with Node

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

the class QuteCompletionsForSnippets method collectSnippetSuggestions.

/**
 * Collect snippets suggestions.
 *
 * @param completionRequest completion request.
 * @param prefixFilter      prefix filter.
 * @param suffixToFind      suffix to found to eat it when completion snippet is
 *                          applied.
 * @param list              completion list to update.
 */
public void collectSnippetSuggestions(CompletionRequest completionRequest, String prefixFilter, String suffixToFind, CompletionList list) {
    Node node = completionRequest.getNode();
    int offset = completionRequest.getOffset();
    Template template = node.getOwnerTemplate();
    String text = template.getText();
    int endExpr = offset;
    // compute the from for search expression according to the node
    int fromSearchExpr = getExprLimitStart(node, endExpr);
    // compute the start expression
    int startExpr = getExprStart(text, fromSearchExpr, endExpr);
    try {
        Range replaceRange = getReplaceRange(startExpr, endExpr, offset, template);
        String lineDelimiter = template.lineDelimiter(replaceRange.getStart().getLine());
        List<CompletionItem> snippets = getSnippetRegistry().getCompletionItems(replaceRange, lineDelimiter, completionRequest.canSupportMarkupKind(MarkupKind.MARKDOWN), completionRequest.isCompletionSnippetsSupported(), (context, model) -> {
            if (context instanceof IQuteSnippetContext) {
                return (((IQuteSnippetContext) context).isMatch(completionRequest, model));
            }
            return false;
        }, (suffix) -> {
            // Search the suffix from the right of completion offset.
            for (int i = endExpr; i < text.length(); i++) {
                char ch = text.charAt(i);
                if (Character.isWhitespace(ch)) {
                    // whitespace, continue to eat character
                    continue;
                } else {
                    // the current character is not a whitespace, search the suffix index
                    Integer eatIndex = getSuffixIndex(text, suffix, i);
                    if (eatIndex != null) {
                        try {
                            return template.positionAt(eatIndex);
                        } catch (BadLocationException e) {
                            return null;
                        }
                    }
                    return null;
                }
            }
            return null;
        }, suffixToFind, prefixFilter);
        for (CompletionItem completionItem : snippets) {
            list.getItems().add(completionItem);
        }
    } catch (BadLocationException e) {
        LOGGER.log(Level.SEVERE, "In QuteCompletions, collectSnippetSuggestions position error", e);
    }
}
Also used : IQuteSnippetContext(com.redhat.qute.services.snippets.IQuteSnippetContext) CompletionItem(org.eclipse.lsp4j.CompletionItem) Node(com.redhat.qute.parser.template.Node) Range(org.eclipse.lsp4j.Range) BadLocationException(com.redhat.qute.ls.commons.BadLocationException) Template(com.redhat.qute.parser.template.Template)

Example 10 with Node

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

the class QuteReference method findReferences.

public List<? extends Location> findReferences(Template template, Position position, ReferenceContext context, CancelChecker cancelChecker) {
    try {
        int offset = template.offsetAt(position);
        Node node = template.findNodeAt(offset);
        if (node == null) {
            return Collections.emptyList();
        }
        node = QutePositionUtility.findBestNode(offset, node);
        List<Location> locations = new ArrayList<>();
        // 
        QuteSearchUtils.searchReferencedObjects(// 
        node, // 
        offset, (n, range) -> {
            Template targetDocument = n.getOwnerTemplate();
            Range targetRange = QutePositionUtility.createRange(n.getStart(), n.getEnd(), targetDocument);
            Location location = new Location(targetDocument.getUri(), targetRange);
            locations.add(location);
        }, false, cancelChecker);
        return locations;
    } catch (BadLocationException e) {
        LOGGER.log(Level.SEVERE, "In QuteReference the client provided Position is at a BadLocation", e);
        return Collections.emptyList();
    }
}
Also used : Node(com.redhat.qute.parser.template.Node) ArrayList(java.util.ArrayList) Range(org.eclipse.lsp4j.Range) BadLocationException(com.redhat.qute.ls.commons.BadLocationException) Location(org.eclipse.lsp4j.Location) Template(com.redhat.qute.parser.template.Template)

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