Search in sources :

Example 96 with Annotation

use of com.intellij.lang.annotation.Annotation 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)

Example 97 with Annotation

use of com.intellij.lang.annotation.Annotation in project intellij-plugins by JetBrains.

the class StrutsFileSetCheckingAnnotator method annotate.

public void annotate(@NotNull final PsiElement psiElement, @NotNull final AnnotationHolder holder) {
    if (!(psiElement instanceof XmlFile)) {
        return;
    }
    if (psiElement instanceof JspFile) {
        return;
    }
    final Module module = ModuleUtilCore.findModuleForPsiElement(psiElement);
    if (module == null) {
        return;
    }
    // do not run when facet not enabled
    if (StrutsFacet.getInstance(module) == null) {
        return;
    }
    final XmlFile xmlFile = (XmlFile) psiElement;
    final Project project = psiElement.getProject();
    final StrutsManager strutsManager = StrutsManager.getInstance(project);
    if (!strutsManager.isStruts2ConfigFile(xmlFile)) {
        return;
    }
    final VirtualFile currentVirtualFile = xmlFile.getVirtualFile();
    assert currentVirtualFile != null;
    final Set<StrutsFileSet> allConfigFileSets = strutsManager.getAllConfigFileSets(module);
    for (final StrutsFileSet configFileSet : allConfigFileSets) {
        if (configFileSet.hasFile(currentVirtualFile)) {
            return;
        }
    }
    final boolean fileSetAvailable = allConfigFileSets.size() != 0;
    final Annotation annotation = holder.createWarningAnnotation(xmlFile, fileSetAvailable ? StrutsBundle.message("annotators.fileset.file.not.registered") : StrutsBundle.message("annotators.fileset.no.file.sets"));
    annotation.setFileLevelAnnotation(true);
    if (fileSetAvailable) {
        final AddToFileSetFix addToFileSetFix = new AddToFileSetFix(xmlFile.getName());
        annotation.registerFix(addToFileSetFix);
    } else {
        annotation.registerFix(new IntentionAction() {

            @SuppressWarnings("DialogTitleCapitalization")
            @NotNull
            public String getText() {
                return StrutsBundle.message("annotators.fileset.edit.facet.settings");
            }

            @NotNull
            public String getFamilyName() {
                return StrutsBundle.message("intentions.family.name");
            }

            public boolean isAvailable(@NotNull final Project project, final Editor editor, final PsiFile psiFile) {
                return true;
            }

            public void invoke(@NotNull final Project project, final Editor editor, final PsiFile psiFile) throws IncorrectOperationException {
                final StrutsFacet strutsFacet = StrutsFacet.getInstance(module);
                assert strutsFacet != null;
                ModulesConfigurator.showFacetSettingsDialog(strutsFacet, null);
            }

            public boolean startInWriteAction() {
                return false;
            }
        });
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlFile(com.intellij.psi.xml.XmlFile) StrutsManager(com.intellij.struts2.dom.struts.model.StrutsManager) NotNull(org.jetbrains.annotations.NotNull) StrutsFileSet(com.intellij.struts2.facet.ui.StrutsFileSet) Annotation(com.intellij.lang.annotation.Annotation) Project(com.intellij.openapi.project.Project) JspFile(com.intellij.psi.jsp.JspFile) BaseIntentionAction(com.intellij.codeInsight.intention.impl.BaseIntentionAction) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) PsiFile(com.intellij.psi.PsiFile) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Module(com.intellij.openapi.module.Module) Editor(com.intellij.openapi.editor.Editor) StrutsFacet(com.intellij.struts2.facet.StrutsFacet)

Aggregations

Annotation (com.intellij.lang.annotation.Annotation)97 PsiElement (com.intellij.psi.PsiElement)24 ASTNode (com.intellij.lang.ASTNode)22 TextRange (com.intellij.openapi.util.TextRange)19 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)15 Project (com.intellij.openapi.project.Project)11 NotNull (org.jetbrains.annotations.NotNull)10 PsiFile (com.intellij.psi.PsiFile)8 GrModifierFix (org.jetbrains.plugins.groovy.codeInspection.bugs.GrModifierFix)6 HighlightDisplayLevel (com.intellij.codeHighlighting.HighlightDisplayLevel)4 Editor (com.intellij.openapi.editor.Editor)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 GrModifierList (org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.modifiers.GrModifierList)4 HighlightDisplayKey (com.intellij.codeInsight.daemon.HighlightDisplayKey)3 HighlightSeverity (com.intellij.lang.annotation.HighlightSeverity)3 PsiReference (com.intellij.psi.PsiReference)3 IElementType (com.intellij.psi.tree.IElementType)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 Nullable (org.jetbrains.annotations.Nullable)3 HbCloseBlockMustache (com.dmarcotte.handlebars.psi.HbCloseBlockMustache)2