<commit_before>package com.cedricziel.idea.fluid.codeInsight.template.postfix.templates;

import com.cedricziel.idea.fluid.codeInsight.template.LiveTemplateFactory;
import com.cedricziel.idea.fluid.lang.psi.FluidFieldChain;
import com.cedricziel.idea.fluid.lang.psi.FluidInlineStatement;
import com.cedricziel.idea.fluid.lang.psi.FluidTypes;
import com.cedricziel.idea.fluid.lang.psi.FluidViewHelperExpr;
import com.intellij.codeInsight.template.Template;
import com.intellij.codeInsight.template.TemplateManager;
import com.intellij.codeInsight.template.impl.TextExpression;
import com.intellij.codeInsight.template.postfix.templates.PostfixTemplate;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.patterns.PlatformPatterns;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;

public class DebugInlinePostfixTemplate extends PostfixTemplate {

    protected DebugInlinePostfixTemplate() {
        super("f:debug", "Pipes the expression result into a debug statement");
    }

    @Override
    public boolean isApplicable(@NotNull PsiElement context, @NotNull Document copyDocument, int newOffset) {
        return PlatformPatterns
            .or(
                PlatformPatterns.psiElement(FluidTypes.IDENTIFIER).withParent(
                    PlatformPatterns.psiElement(FluidFieldChain.class)
                ),
                PlatformPatterns.and(
                    PlatformPatterns.psiElement().withParent(
                        FluidViewHelperExpr.class
                    ),
                    PlatformPatterns.psiElement(FluidTypes.RIGHT_PARENTH)
                )
            )
            .accepts(context);
    }

    @Override
    public void expand(@NotNull PsiElement context, @NotNull Editor editor) {
        FluidInlineStatement expression = (FluidInlineStatement) PsiTreeUtil.findFirstParent(context, psiElement -> psiElement instanceof FluidInlineStatement);
        if (expression == null) {
            return;
        }

        Template tagTemplate = LiveTemplateFactory.createInlinePipeToDebugTemplate(expression);
        tagTemplate.addVariable("EXPR", new TextExpression(expression.getInlineChain().getText()), true);

        int textOffset = expression.getTextOffset() + expression.getTextLength();
        editor.getCaretModel().moveToOffset(textOffset);

        TemplateManager.getInstance(context.getProject()).startTemplate(editor, tagTemplate);
        PsiDocumentManager.getInstance(context.getProject()).commitDocument(editor.getDocument());

        expression.delete();
    }
}<commit_msg>[Fluid] Fix f:debug postfix completion<commit_after>package com.cedricziel.idea.fluid.codeInsight.template.postfix.templates;

import com.cedricziel.idea.fluid.codeInsight.template.LiveTemplateFactory;
import com.cedricziel.idea.fluid.lang.psi.FluidFieldExpr;
import com.cedricziel.idea.fluid.lang.psi.FluidInlineStatement;
import com.cedricziel.idea.fluid.lang.psi.FluidTypes;
import com.cedricziel.idea.fluid.lang.psi.FluidViewHelperExpr;
import com.intellij.codeInsight.template.Template;
import com.intellij.codeInsight.template.TemplateManager;
import com.intellij.codeInsight.template.impl.TextExpression;
import com.intellij.codeInsight.template.postfix.templates.PostfixTemplate;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.patterns.PlatformPatterns;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;

public class DebugInlinePostfixTemplate extends PostfixTemplate {

    protected DebugInlinePostfixTemplate() {
        super("f:debug", "Pipes the expression result into a debug statement");
    }

    @Override
    public boolean isApplicable(@NotNull PsiElement context, @NotNull Document copyDocument, int newOffset) {
        return PlatformPatterns
            .or(
                PlatformPatterns.psiElement(FluidTypes.IDENTIFIER).withParent(
                    PlatformPatterns.psiElement(FluidFieldExpr.class)
                ),
                PlatformPatterns.and(
                    PlatformPatterns.psiElement().withParent(
                        FluidViewHelperExpr.class
                    ),
                    PlatformPatterns.psiElement(FluidTypes.RIGHT_PARENTH)
                )
            )
            .accepts(context);
    }

    @Override
    public void expand(@NotNull PsiElement context, @NotNull Editor editor) {
        FluidInlineStatement expression = (FluidInlineStatement) PsiTreeUtil.findFirstParent(context, psiElement -> psiElement instanceof FluidInlineStatement);
        if (expression == null) {
            return;
        }

        Template tagTemplate = LiveTemplateFactory.createInlinePipeToDebugTemplate(expression);
        tagTemplate.addVariable("EXPR", new TextExpression(expression.getInlineChain().getText()), true);

        int textOffset = expression.getTextOffset() + expression.getTextLength();
        editor.getCaretModel().moveToOffset(textOffset);

        TemplateManager.getInstance(context.getProject()).startTemplate(editor, tagTemplate);
        PsiDocumentManager.getInstance(context.getProject()).commitDocument(editor.getDocument());

        expression.delete();
    }
}
{'fraction_non_alphanumeric': 0.0915764360433107, 'fraction_numerical': 0.0, 'mean_word_length': 3.591406908171862, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4338669', 'n_tokens_mistral': 1493, 'n_tokens_neox': 1458, 'n_words': 220}