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();
    }
}

@@ -195,21 +195,20 @@
 uidField
-Chain
+Expr
 ;%0Aimport
@@ -1438,21 +1438,20 @@
 uidField
-Chain
+Expr
 .class)%0A

Fix f:debug postfix completion