Search in sources :

Example 1 with AntDomTarget

use of com.intellij.lang.ant.dom.AntDomTarget in project intellij-community by JetBrains.

the class AntBuildTargetImpl method findTask.

@Nullable
public BuildTask findTask(final String taskName) {
    final PsiFile psiFile = PsiManager.getInstance(myProject).findFile(myFile);
    final AntDomProject domProject = AntSupport.getAntDomProject(psiFile);
    if (domProject != null) {
        final AntDomTarget antTarget = domProject.findDeclaredTarget(myName);
        if (antTarget != null) {
            final Ref<AntDomElement> result = new Ref<>(null);
            antTarget.accept(new AntDomRecursiveVisitor() {

                public void visitAntDomElement(AntDomElement element) {
                    if (result.get() != null) {
                        return;
                    }
                    if (element.isTask() && taskName.equals(element.getXmlElementName())) {
                        result.set(element);
                        return;
                    }
                    super.visitAntDomElement(element);
                }
            });
            final AntDomElement task = result.get();
            if (task != null) {
                return new BuildTask(this, task);
            }
        }
    }
    return null;
}
Also used : Ref(com.intellij.openapi.util.Ref) AntDomElement(com.intellij.lang.ant.dom.AntDomElement) AntDomRecursiveVisitor(com.intellij.lang.ant.dom.AntDomRecursiveVisitor) PsiFile(com.intellij.psi.PsiFile) AntDomTarget(com.intellij.lang.ant.dom.AntDomTarget) AntDomProject(com.intellij.lang.ant.dom.AntDomProject) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with AntDomTarget

use of com.intellij.lang.ant.dom.AntDomTarget in project intellij-community by JetBrains.

the class AntDomDocumentationProvider method getQuickNavigateInfo.

@Nullable
public String getQuickNavigateInfo(PsiElement element, PsiElement originalElement) {
    // todo!
    if (element instanceof PomTargetPsiElement) {
        final PomTarget pomTarget = ((PomTargetPsiElement) element).getTarget();
        if (pomTarget instanceof DomTarget) {
            final DomElement domElement = ((DomTarget) pomTarget).getDomElement();
            if (domElement instanceof AntDomTarget) {
                final AntDomTarget antTarget = (AntDomTarget) domElement;
                final String description = antTarget.getDescription().getRawText();
                if (description != null && description.length() > 0) {
                    final String targetName = antTarget.getName().getRawText();
                    final StringBuilder builder = StringBuilderSpinAllocator.alloc();
                    try {
                        builder.append("Target");
                        if (targetName != null) {
                            builder.append(" \"").append(targetName).append("\"");
                        }
                        final XmlElement xmlElement = antTarget.getXmlElement();
                        if (xmlElement != null) {
                            final PsiFile containingFile = xmlElement.getContainingFile();
                            if (containingFile != null) {
                                final String fileName = containingFile.getName();
                                builder.append(" [").append(fileName).append("]");
                            }
                        }
                        return builder.append(" ").append(description).toString();
                    } finally {
                        StringBuilderSpinAllocator.dispose(builder);
                    }
                }
            }
        } else if (pomTarget instanceof DomChildrenDescription) {
            final DomChildrenDescription description = (DomChildrenDescription) pomTarget;
            Type type = null;
            try {
                type = description.getType();
            } catch (UnsupportedOperationException e) {
                LOG.info(e);
            }
            if (type instanceof Class && AntDomElement.class.isAssignableFrom(((Class) type))) {
                final String elemName = description.getName();
                if (elemName != null) {
                    final AntDomElement.Role role = description.getUserData(AntDomElement.ROLE);
                    final StringBuilder builder = StringBuilderSpinAllocator.alloc();
                    try {
                        if (role == AntDomElement.Role.TASK) {
                            builder.append("Task ");
                        } else if (role == AntDomElement.Role.DATA_TYPE) {
                            builder.append("Data structure ");
                        }
                        builder.append(elemName);
                        return builder.toString();
                    } finally {
                        StringBuilderSpinAllocator.dispose(builder);
                    }
                }
            }
        }
    }
    return null;
}
Also used : AntDomTarget(com.intellij.lang.ant.dom.AntDomTarget) DomTarget(com.intellij.util.xml.DomTarget) DomChildrenDescription(com.intellij.util.xml.reflect.DomChildrenDescription) AntDomTarget(com.intellij.lang.ant.dom.AntDomTarget) PomTarget(com.intellij.pom.PomTarget) Type(java.lang.reflect.Type) DomElement(com.intellij.util.xml.DomElement) AntDomElement(com.intellij.lang.ant.dom.AntDomElement) XmlElement(com.intellij.psi.xml.XmlElement) PsiFile(com.intellij.psi.PsiFile) PomTargetPsiElement(com.intellij.pom.PomTargetPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with AntDomTarget

use of com.intellij.lang.ant.dom.AntDomTarget in project intellij-community by JetBrains.

the class RunTargetAction method update.

@Override
public void update(AnActionEvent e) {
    super.update(e);
    final Presentation presentation = e.getPresentation();
    Pair<AntBuildFileBase, AntDomTarget> antTarget = findAntTarget(e);
    if (antTarget == null) {
        presentation.setEnabled(false);
        presentation.setText(AntActionsBundle.message("action.RunTargetAction.text", ""));
    } else {
        presentation.setEnabled(true);
        presentation.setText(AntActionsBundle.message("action.RunTargetAction.text", "'" + antTarget.second.getName().getValue() + "'"));
    }
}
Also used : AntBuildFileBase(com.intellij.lang.ant.config.AntBuildFileBase) Presentation(com.intellij.openapi.actionSystem.Presentation) AntDomTarget(com.intellij.lang.ant.dom.AntDomTarget)

Example 4 with AntDomTarget

use of com.intellij.lang.ant.dom.AntDomTarget in project intellij-community by JetBrains.

the class AntCreatePropertyFix method applyFix.

public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    final PsiElement psiElement = descriptor.getPsiElement();
    final PsiFile containingFile = psiElement.getContainingFile();
    final FileModificationService modificationService = FileModificationService.getInstance();
    Navigatable result = null;
    if (myPropFile != null) {
        final VirtualFile vFile = myPropFile.getVirtualFile();
        boolean canModify = true;
        if (myPropFile instanceof PsiFile) {
            canModify = modificationService.prepareFileForWrite((PsiFile) myPropFile);
        } else if (vFile != null) {
            canModify = modificationService.prepareVirtualFilesForWrite(project, Collections.singleton(vFile));
        }
        if (canModify) {
            final IProperty generatedProperty = myPropFile.addProperty(myCanonicalText, "");
            result = vFile != null ? new OpenFileDescriptor(project, vFile, generatedProperty.getPsiElement().getTextRange().getEndOffset()) : generatedProperty;
        }
    } else {
        if (containingFile instanceof XmlFile) {
            final XmlFile xmlFile = (XmlFile) containingFile;
            final XmlTag rootTag = xmlFile.getRootTag();
            if (rootTag != null && modificationService.prepareFileForWrite(xmlFile)) {
                final XmlTag propTag = rootTag.createChildTag(PROPERTY, rootTag.getNamespace(), null, false);
                propTag.setAttribute(NAME_ATTR, myCanonicalText);
                propTag.setAttribute(VALUE_ATTR, "");
                final DomElement contextElement = DomUtil.getDomElement(descriptor.getPsiElement());
                PsiElement generated;
                if (contextElement == null) {
                    generated = rootTag.addSubTag(propTag, true);
                } else {
                    final AntDomTarget containingTarget = contextElement.getParentOfType(AntDomTarget.class, false);
                    final DomElement anchor = containingTarget != null ? containingTarget : contextElement;
                    final XmlTag tag = anchor.getXmlTag();
                    if (!rootTag.equals(tag)) {
                        generated = tag.getParent().addBefore(propTag, tag);
                    } else {
                        generated = rootTag.addSubTag(propTag, true);
                    }
                }
                if (generated instanceof XmlTag) {
                    final XmlAttribute valueAttrib = ((XmlTag) generated).getAttribute(VALUE_ATTR);
                    if (valueAttrib != null) {
                        final XmlAttributeValue valueElement = valueAttrib.getValueElement();
                        if (valueElement instanceof Navigatable) {
                            result = (Navigatable) valueElement;
                        }
                    }
                }
                if (result == null && generated instanceof Navigatable) {
                    result = (Navigatable) generated;
                }
            }
        }
    }
    if (result != null) {
        result.navigate(true);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) AntDomTarget(com.intellij.lang.ant.dom.AntDomTarget) Navigatable(com.intellij.pom.Navigatable) DomElement(com.intellij.util.xml.DomElement) IProperty(com.intellij.lang.properties.IProperty) PsiFile(com.intellij.psi.PsiFile) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) PsiElement(com.intellij.psi.PsiElement) FileModificationService(com.intellij.codeInsight.FileModificationService) XmlTag(com.intellij.psi.xml.XmlTag)

Example 5 with AntDomTarget

use of com.intellij.lang.ant.dom.AntDomTarget in project intellij-community by JetBrains.

the class AntDuplicateTargetsInspection method checkDomElement.

protected void checkDomElement(DomElement element, final DomElementAnnotationHolder holder, DomHighlightingHelper helper) {
    if (element instanceof AntDomProject) {
        final AntDomProject project = (AntDomProject) element;
        TargetResolver.validateDuplicateTargets(project.getContextAntProject(), new TargetResolver.TargetSink() {

            public void duplicateTargetDetected(AntDomTarget existingTarget, AntDomTarget duplicatingTarget, String targetEffectiveName) {
                final AntDomProject existingTargetProj = existingTarget.getAntProject();
                final AntDomProject duplucatingTargetProj = duplicatingTarget.getAntProject();
                final boolean isFromDifferentFiles = !Comparing.equal(existingTargetProj, duplucatingTargetProj);
                if (project.equals(existingTargetProj)) {
                    final String duplicatedMessage = isFromDifferentFiles ? AntBundle.message("target.is.duplicated.in.imported.file", targetEffectiveName, duplucatingTargetProj != null ? duplucatingTargetProj.getName() : "") : AntBundle.message("target.is.duplicated", targetEffectiveName);
                    holder.createProblem(existingTarget.getName(), duplicatedMessage);
                }
                if (project.equals(duplucatingTargetProj)) {
                    final String duplicatedMessage = isFromDifferentFiles ? AntBundle.message("target.is.duplicated.in.imported.file", targetEffectiveName, existingTargetProj != null ? existingTargetProj.getName() : "") : AntBundle.message("target.is.duplicated", targetEffectiveName);
                    holder.createProblem(duplicatingTarget.getName(), duplicatedMessage);
                }
            }
        });
    }
}
Also used : AntDomTarget(com.intellij.lang.ant.dom.AntDomTarget) TargetResolver(com.intellij.lang.ant.dom.TargetResolver) AntDomProject(com.intellij.lang.ant.dom.AntDomProject)

Aggregations

AntDomTarget (com.intellij.lang.ant.dom.AntDomTarget)8 PsiFile (com.intellij.psi.PsiFile)5 XmlTag (com.intellij.psi.xml.XmlTag)4 DomElement (com.intellij.util.xml.DomElement)4 Nullable (org.jetbrains.annotations.Nullable)4 AntDomProject (com.intellij.lang.ant.dom.AntDomProject)3 PsiElement (com.intellij.psi.PsiElement)3 XmlFile (com.intellij.psi.xml.XmlFile)3 AntBuildFileBase (com.intellij.lang.ant.config.AntBuildFileBase)2 AntDomElement (com.intellij.lang.ant.dom.AntDomElement)2 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 Navigatable (com.intellij.pom.Navigatable)2 FileModificationService (com.intellij.codeInsight.FileModificationService)1 AntDomRecursiveVisitor (com.intellij.lang.ant.dom.AntDomRecursiveVisitor)1 TargetResolver (com.intellij.lang.ant.dom.TargetResolver)1 IProperty (com.intellij.lang.properties.IProperty)1 Presentation (com.intellij.openapi.actionSystem.Presentation)1 Editor (com.intellij.openapi.editor.Editor)1 Project (com.intellij.openapi.project.Project)1