Search in sources :

Example 1 with FileModificationService

use of com.intellij.codeInsight.FileModificationService 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)

Aggregations

FileModificationService (com.intellij.codeInsight.FileModificationService)1 AntDomTarget (com.intellij.lang.ant.dom.AntDomTarget)1 IProperty (com.intellij.lang.properties.IProperty)1 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 Navigatable (com.intellij.pom.Navigatable)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 XmlAttribute (com.intellij.psi.xml.XmlAttribute)1 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)1 XmlFile (com.intellij.psi.xml.XmlFile)1 XmlTag (com.intellij.psi.xml.XmlTag)1 DomElement (com.intellij.util.xml.DomElement)1