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