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