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