Search in sources :

Example 1 with IQuteSnippetContext

use of com.redhat.qute.services.snippets.IQuteSnippetContext 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)

Aggregations

BadLocationException (com.redhat.qute.ls.commons.BadLocationException)1 Node (com.redhat.qute.parser.template.Node)1 Template (com.redhat.qute.parser.template.Template)1 IQuteSnippetContext (com.redhat.qute.services.snippets.IQuteSnippetContext)1 CompletionItem (org.eclipse.lsp4j.CompletionItem)1 Range (org.eclipse.lsp4j.Range)1