Search in sources :

Example 1 with HbBlockMustache

use of com.dmarcotte.handlebars.psi.HbBlockMustache 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 2 with HbBlockMustache

use of com.dmarcotte.handlebars.psi.HbBlockMustache 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)

Aggregations

HbBlockMustache (com.dmarcotte.handlebars.psi.HbBlockMustache)2 HbOpenBlockMustache (com.dmarcotte.handlebars.psi.HbOpenBlockMustache)2 HbPath (com.dmarcotte.handlebars.psi.HbPath)2 Document (com.intellij.openapi.editor.Document)2 TextRange (com.intellij.openapi.util.TextRange)2 PsiElement (com.intellij.psi.PsiElement)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