Search in sources :

Example 1 with BadLocationException

use of com.redhat.qute.ls.commons.BadLocationException 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);
}
Also used : Expression(com.redhat.qute.parser.template.Expression) QuteCompletionsForExpression(com.redhat.qute.services.completions.QuteCompletionsForExpression) Parts(com.redhat.qute.parser.expression.Parts) Part(com.redhat.qute.parser.expression.Part) Node(com.redhat.qute.parser.template.Node) CompletionRequest(com.redhat.qute.services.completions.CompletionRequest) QuteCompletionForTagSection(com.redhat.qute.services.completions.QuteCompletionForTagSection) Section(com.redhat.qute.parser.template.Section) BadLocationException(com.redhat.qute.ls.commons.BadLocationException)

Example 2 with BadLocationException

use of com.redhat.qute.ls.commons.BadLocationException 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 3 with BadLocationException

use of com.redhat.qute.ls.commons.BadLocationException 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 4 with BadLocationException

use of com.redhat.qute.ls.commons.BadLocationException 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)

Example 5 with BadLocationException

use of com.redhat.qute.ls.commons.BadLocationException in project quarkus-ls by redhat-developer.

the class QuteDefinition method findDefinition.

public CompletableFuture<List<? extends LocationLink>> findDefinition(Template template, Position position, CancelChecker cancelChecker) {
    try {
        DefinitionRequest definitionRequest = new DefinitionRequest(template, position);
        Node node = definitionRequest.getNode();
        if (node == null) {
            return NO_DEFINITION;
        }
        int offset = definitionRequest.getOffset();
        switch(node.getKind()) {
            case Section:
                // - Java data model definition
                return findDefinitionFromSection(offset, (Section) node, template, cancelChecker);
            case ParameterDeclaration:
                // Return Java class definition
                return findDefinitionFromParameterDeclaration(offset, (ParameterDeclaration) node, template);
            case Expression:
                return findDefinitionFromExpression(offset, (Expression) node, template, cancelChecker);
            case ExpressionPart:
                Part part = (Part) node;
                return findDefinitionFromPart(part, template, cancelChecker);
            default:
                // no definitions
                return NO_DEFINITION;
        }
    } catch (BadLocationException e) {
        LOGGER.log(Level.SEVERE, "Failed creating DefinitionRequest", e);
        return NO_DEFINITION;
    }
}
Also used : DefinitionRequest(com.redhat.qute.services.definition.DefinitionRequest) ObjectPart(com.redhat.qute.parser.expression.ObjectPart) MethodPart(com.redhat.qute.parser.expression.MethodPart) NamespacePart(com.redhat.qute.parser.expression.NamespacePart) Part(com.redhat.qute.parser.expression.Part) PropertyPart(com.redhat.qute.parser.expression.PropertyPart) Node(com.redhat.qute.parser.template.Node) BadLocationException(com.redhat.qute.ls.commons.BadLocationException)

Aggregations

BadLocationException (com.redhat.qute.ls.commons.BadLocationException)10 Node (com.redhat.qute.parser.template.Node)10 Part (com.redhat.qute.parser.expression.Part)4 Section (com.redhat.qute.parser.template.Section)4 ObjectPart (com.redhat.qute.parser.expression.ObjectPart)3 ArrayList (java.util.ArrayList)3 Range (org.eclipse.lsp4j.Range)3 JsonObject (com.google.gson.JsonObject)2 NamespacePart (com.redhat.qute.parser.expression.NamespacePart)2 Expression (com.redhat.qute.parser.template.Expression)2 Parameter (com.redhat.qute.parser.template.Parameter)2 Template (com.redhat.qute.parser.template.Template)2 CodeAction (org.eclipse.lsp4j.CodeAction)2 Position (org.eclipse.lsp4j.Position)2 DataModelParameter (com.redhat.qute.commons.datamodel.DataModelParameter)1 TextDocument (com.redhat.qute.ls.commons.TextDocument)1 MethodPart (com.redhat.qute.parser.expression.MethodPart)1 Parts (com.redhat.qute.parser.expression.Parts)1 PropertyPart (com.redhat.qute.parser.expression.PropertyPart)1 ParameterDeclaration (com.redhat.qute.parser.template.ParameterDeclaration)1