Search in sources :

Example 1 with HbOpenBlockMustache

use of com.dmarcotte.handlebars.psi.HbOpenBlockMustache in project idea-handlebars by dmarcotte.

the class HbBlockMismatchAnnotator method annotate.

@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
    if (element instanceof HbOpenBlockMustache) {
        HbOpenBlockMustache openBlockMustache = (HbOpenBlockMustache) element;
        HbMustacheName openBlockMustacheName = openBlockMustache.getBlockMustacheName();
        HbCloseBlockMustache closeBlockMustache = openBlockMustache.getPairedElement();
        if (closeBlockMustache != null) {
            HbMustacheName closeBlockMustacheName = closeBlockMustache.getBlockMustacheName();
            if (openBlockMustacheName == null || closeBlockMustacheName == null) {
                return;
            }
            String openBlockName = openBlockMustacheName.getName();
            String closeBlockName = closeBlockMustacheName.getName();
            if (!openBlockName.equals(closeBlockName)) {
                Annotation openBlockAnnotation = holder.createErrorAnnotation(openBlockMustacheName, HbBundle.message("hb.block.mismatch.inspection.open.block", openBlockName, closeBlockName));
                openBlockAnnotation.registerFix(new HbBlockMismatchFix(closeBlockName, openBlockName, true));
                openBlockAnnotation.registerFix(new HbBlockMismatchFix(openBlockName, closeBlockName, false));
                Annotation closeBlockAnnotation = holder.createErrorAnnotation(closeBlockMustacheName, HbBundle.message("hb.block.mismatch.inspection.close.block", openBlockName, closeBlockName));
                closeBlockAnnotation.registerFix(new HbBlockMismatchFix(openBlockName, closeBlockName, false));
                closeBlockAnnotation.registerFix(new HbBlockMismatchFix(closeBlockName, openBlockName, true));
            }
        } else {
            holder.createErrorAnnotation(openBlockMustacheName, HbBundle.message("hb.block.mismatch.inspection.missing.end.block", openBlockMustache.getName()));
        }
    }
    if (element instanceof HbCloseBlockMustache) {
        HbCloseBlockMustache closeBlockMustache = (HbCloseBlockMustache) element;
        PsiElement openBlockElement = closeBlockMustache.getPairedElement();
        if (openBlockElement == null) {
            HbMustacheName closeBlockMustacheName = closeBlockMustache.getBlockMustacheName();
            if (closeBlockMustacheName == null) {
                return;
            }
            holder.createErrorAnnotation(closeBlockMustacheName, HbBundle.message("hb.block.mismatch.inspection.missing.start.block", closeBlockMustache.getName()));
        }
    }
}
Also used : HbOpenBlockMustache(com.dmarcotte.handlebars.psi.HbOpenBlockMustache) HbMustacheName(com.dmarcotte.handlebars.psi.HbMustacheName) HbCloseBlockMustache(com.dmarcotte.handlebars.psi.HbCloseBlockMustache) Annotation(com.intellij.lang.annotation.Annotation) PsiElement(com.intellij.psi.PsiElement)

Example 2 with HbOpenBlockMustache

use of com.dmarcotte.handlebars.psi.HbOpenBlockMustache in project intellij-plugins by JetBrains.

the class HbBlockMismatchFix method invoke.

@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
    final int offset = editor.getCaretModel().getOffset();
    PsiElement psiElement = file.findElementAt(offset);
    if (psiElement == null)
        return;
    if (psiElement instanceof PsiWhiteSpace)
        psiElement = PsiTreeUtil.prevLeaf(psiElement);
    HbBlockMustache blockMustache = (HbBlockMustache) PsiTreeUtil.findFirstParent(psiElement, true, psiElement1 -> psiElement1 instanceof HbBlockMustache);
    if (blockMustache == null) {
        return;
    }
    HbBlockMustache targetBlockMustache = blockMustache;
    // ensure we update the open or close mustache for this block appropriately
    if (myUpdateOpenMustache != (targetBlockMustache instanceof HbOpenBlockMustache)) {
        targetBlockMustache = blockMustache.getPairedElement();
    }
    HbPath path = PsiTreeUtil.findChildOfType(targetBlockMustache, HbPath.class);
    final Document document = PsiDocumentManager.getInstance(project).getDocument(file);
    if (path != null && document != null) {
        final TextRange textRange = path.getTextRange();
        document.replaceString(textRange.getStartOffset(), textRange.getEndOffset(), myCorrectedName);
    }
}
Also used : IncorrectOperationException(com.intellij.util.IncorrectOperationException) Document(com.intellij.openapi.editor.Document) HbPath(com.dmarcotte.handlebars.psi.HbPath) TextRange(com.intellij.openapi.util.TextRange) Editor(com.intellij.openapi.editor.Editor) HbBundle(com.dmarcotte.handlebars.HbBundle) HbBlockMustache(com.dmarcotte.handlebars.psi.HbBlockMustache) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) NotNull(org.jetbrains.annotations.NotNull) HbOpenBlockMustache(com.dmarcotte.handlebars.psi.HbOpenBlockMustache) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) HbOpenBlockMustache(com.dmarcotte.handlebars.psi.HbOpenBlockMustache) HbPath(com.dmarcotte.handlebars.psi.HbPath) HbBlockMustache(com.dmarcotte.handlebars.psi.HbBlockMustache) TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 3 with HbOpenBlockMustache

use of com.dmarcotte.handlebars.psi.HbOpenBlockMustache in project idea-handlebars by dmarcotte.

the class HbBlockMismatchFix method invoke.

@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
    final int offset = editor.getCaretModel().getOffset();
    PsiElement psiElement = file.findElementAt(offset);
    if (psiElement == null || !psiElement.isValid())
        return;
    if (!FileModificationService.getInstance().prepareFileForWrite(psiElement.getContainingFile()))
        return;
    if (psiElement instanceof PsiWhiteSpace)
        psiElement = PsiTreeUtil.prevLeaf(psiElement);
    HbBlockMustache blockMustache = (HbBlockMustache) PsiTreeUtil.findFirstParent(psiElement, true, new Condition<PsiElement>() {

        @Override
        public boolean value(PsiElement psiElement) {
            return psiElement instanceof HbBlockMustache;
        }
    });
    if (blockMustache == null) {
        return;
    }
    HbBlockMustache targetBlockMustache = blockMustache;
    // ensure we update the open or close mustache for this block appropriately
    if (myUpdateOpenMustache != (targetBlockMustache instanceof HbOpenBlockMustache)) {
        targetBlockMustache = blockMustache.getPairedElement();
    }
    HbPath path = PsiTreeUtil.findChildOfType(targetBlockMustache, HbPath.class);
    final Document document = PsiDocumentManager.getInstance(project).getDocument(file);
    if (path != null && document != null) {
        final TextRange textRange = path.getTextRange();
        document.replaceString(textRange.getStartOffset(), textRange.getEndOffset(), myCorrectedName);
    }
}
Also used : Condition(com.intellij.openapi.util.Condition) HbOpenBlockMustache(com.dmarcotte.handlebars.psi.HbOpenBlockMustache) HbPath(com.dmarcotte.handlebars.psi.HbPath) HbBlockMustache(com.dmarcotte.handlebars.psi.HbBlockMustache) TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace)

Example 4 with HbOpenBlockMustache

use of com.dmarcotte.handlebars.psi.HbOpenBlockMustache in project intellij-plugins by JetBrains.

the class HbBlockMismatchAnnotator method annotate.

@Override
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
    if (element instanceof HbOpenBlockMustache) {
        HbOpenBlockMustache openBlockMustache = (HbOpenBlockMustache) element;
        HbMustacheName openBlockMustacheName = openBlockMustache.getBlockMustacheName();
        HbCloseBlockMustache closeBlockMustache = openBlockMustache.getPairedElement();
        if (closeBlockMustache != null) {
            HbMustacheName closeBlockMustacheName = closeBlockMustache.getBlockMustacheName();
            if (openBlockMustacheName == null || closeBlockMustacheName == null) {
                return;
            }
            String openBlockName = openBlockMustacheName.getName();
            String closeBlockName = closeBlockMustacheName.getName();
            if (!openBlockName.equals(closeBlockName)) {
                Annotation openBlockAnnotation = holder.createErrorAnnotation(openBlockMustacheName, HbBundle.message("hb.block.mismatch.inspection.open.block", openBlockName, closeBlockName));
                openBlockAnnotation.registerFix(new HbBlockMismatchFix(closeBlockName, openBlockName, true));
                openBlockAnnotation.registerFix(new HbBlockMismatchFix(openBlockName, closeBlockName, false));
                Annotation closeBlockAnnotation = holder.createErrorAnnotation(closeBlockMustacheName, HbBundle.message("hb.block.mismatch.inspection.close.block", openBlockName, closeBlockName));
                closeBlockAnnotation.registerFix(new HbBlockMismatchFix(openBlockName, closeBlockName, false));
                closeBlockAnnotation.registerFix(new HbBlockMismatchFix(closeBlockName, openBlockName, true));
            }
        } else {
            if (openBlockMustacheName == null) {
                return;
            }
            holder.createErrorAnnotation(openBlockMustacheName, HbBundle.message("hb.block.mismatch.inspection.missing.end.block", openBlockMustache.getName()));
        }
    }
    if (element instanceof HbCloseBlockMustache) {
        HbCloseBlockMustache closeBlockMustache = (HbCloseBlockMustache) element;
        PsiElement openBlockElement = closeBlockMustache.getPairedElement();
        if (openBlockElement == null) {
            HbMustacheName closeBlockMustacheName = closeBlockMustache.getBlockMustacheName();
            if (closeBlockMustacheName == null) {
                return;
            }
            holder.createErrorAnnotation(closeBlockMustacheName, HbBundle.message("hb.block.mismatch.inspection.missing.start.block", closeBlockMustache.getName()));
        }
    }
}
Also used : HbOpenBlockMustache(com.dmarcotte.handlebars.psi.HbOpenBlockMustache) HbMustacheName(com.dmarcotte.handlebars.psi.HbMustacheName) HbCloseBlockMustache(com.dmarcotte.handlebars.psi.HbCloseBlockMustache) Annotation(com.intellij.lang.annotation.Annotation) PsiElement(com.intellij.psi.PsiElement)

Aggregations

HbOpenBlockMustache (com.dmarcotte.handlebars.psi.HbOpenBlockMustache)4 PsiElement (com.intellij.psi.PsiElement)4 HbBlockMustache (com.dmarcotte.handlebars.psi.HbBlockMustache)2 HbCloseBlockMustache (com.dmarcotte.handlebars.psi.HbCloseBlockMustache)2 HbMustacheName (com.dmarcotte.handlebars.psi.HbMustacheName)2 HbPath (com.dmarcotte.handlebars.psi.HbPath)2 Annotation (com.intellij.lang.annotation.Annotation)2 Document (com.intellij.openapi.editor.Document)2 TextRange (com.intellij.openapi.util.TextRange)2 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)2 HbBundle (com.dmarcotte.handlebars.HbBundle)1 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)1 Editor (com.intellij.openapi.editor.Editor)1 Project (com.intellij.openapi.project.Project)1 Condition (com.intellij.openapi.util.Condition)1 PsiDocumentManager (com.intellij.psi.PsiDocumentManager)1 PsiFile (com.intellij.psi.PsiFile)1 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 NotNull (org.jetbrains.annotations.NotNull)1