use of com.intellij.psi.xml.XmlDocument in project intellij-community by JetBrains.
the class ExternalAnnotationsManagerImpl method processExistingExternalAnnotations.
private boolean processExistingExternalAnnotations(@NotNull final PsiModifierListOwner listOwner, @NotNull final String annotationFQN, @NotNull final Processor<XmlTag> annotationTagProcessor) {
try {
final List<XmlFile> files = findExternalAnnotationsXmlFiles(listOwner);
if (files == null) {
notifyAfterAnnotationChanging(listOwner, annotationFQN, false);
return false;
}
boolean processedAnything = false;
for (final XmlFile file : files) {
if (!file.isValid()) {
continue;
}
if (ReadonlyStatusHandler.getInstance(myPsiManager.getProject()).ensureFilesWritable(file.getVirtualFile()).hasReadonlyFiles()) {
continue;
}
final XmlDocument document = file.getDocument();
if (document == null) {
continue;
}
final XmlTag rootTag = document.getRootTag();
if (rootTag == null) {
continue;
}
final String externalName = getExternalName(listOwner, false);
final List<XmlTag> tagsToProcess = new ArrayList<>();
for (XmlTag tag : rootTag.getSubTags()) {
String className = StringUtil.unescapeXml(tag.getAttributeValue("name"));
if (!Comparing.strEqual(className, externalName)) {
continue;
}
for (XmlTag annotationTag : tag.getSubTags()) {
if (!Comparing.strEqual(annotationTag.getAttributeValue("name"), annotationFQN)) {
continue;
}
tagsToProcess.add(annotationTag);
processedAnything = true;
}
}
if (tagsToProcess.isEmpty()) {
continue;
}
WriteCommandAction.runWriteCommandAction(myPsiManager.getProject(), ExternalAnnotationsManagerImpl.class.getName(), null, () -> {
PsiDocumentManager.getInstance(myPsiManager.getProject()).commitAllDocuments();
try {
for (XmlTag annotationTag : tagsToProcess) {
annotationTagProcessor.process(annotationTag);
}
commitChanges(file);
} catch (IncorrectOperationException e) {
LOG.error(e);
}
});
}
notifyAfterAnnotationChanging(listOwner, annotationFQN, processedAnything);
return processedAnything;
} finally {
dropCache();
}
}
use of com.intellij.psi.xml.XmlDocument in project intellij-community by JetBrains.
the class NamespaceCollector method collectInfo.
public static CollectedInfo collectInfo(final XmlFile psiFile) {
final NamespaceCollector namespaceCollector = new NamespaceCollector();
final XmlDocument document = psiFile.getDocument();
if (document != null) {
document.accept(namespaceCollector);
}
return new CollectedInfo(namespaceCollector.namespaces, namespaceCollector.elements, namespaceCollector.attributes);
}
use of com.intellij.psi.xml.XmlDocument in project intellij-community by JetBrains.
the class JavaFxNamespaceDescriptor method init.
@Override
public void init(PsiElement element) {
XmlDocument document = (XmlDocument) element;
myFile = ((XmlFile) document.getContainingFile());
}
use of com.intellij.psi.xml.XmlDocument in project intellij-community by JetBrains.
the class UpdateJspxFileCopyright method scanFile.
protected void scanFile() {
logger.debug("updating " + getFile().getVirtualFile());
XmlDocument doc = ((XmlFile) getFile()).getDocument();
XmlProlog xmlProlog = doc.getProlog();
if (xmlProlog == null) {
return;
}
PsiElement elem = xmlProlog.getFirstChild();
PsiElement docTypeStart = null;
while (elem != null) {
if (elem instanceof XmlDoctype) {
docTypeStart = elem;
break;
}
elem = getNextSibling(elem);
}
PsiElement first = xmlProlog.getFirstChild();
int location = getLanguageOptions().getFileLocation();
if (docTypeStart != null) {
final ArrayList<PsiComment> comments = new ArrayList<>();
collectComments(doc.getFirstChild(), xmlProlog, comments);
collectComments(first, docTypeStart, comments);
checkComments(first, location == XmlOptions.LOCATION_BEFORE_DOCTYPE, comments);
checkComments(docTypeStart, doc.getRootTag(), location == XmlOptions.LOCATION_BEFORE_ROOTTAG);
return;
} else if (location == XmlOptions.LOCATION_BEFORE_DOCTYPE) {
location = XmlOptions.LOCATION_BEFORE_ROOTTAG;
}
final ArrayList<PsiComment> comments = new ArrayList<>();
collectComments(doc.getFirstChild(), xmlProlog, comments);
collectComments(first, doc.getRootTag(), comments);
checkComments(doc.getRootTag(), location == XmlOptions.LOCATION_BEFORE_ROOTTAG, comments);
}
use of com.intellij.psi.xml.XmlDocument in project intellij-community by JetBrains.
the class IntroduceParameterProcessor method performRefactoring.
protected void performRefactoring(@NotNull UsageInfo[] usageInfos) {
XmlTag tag;
if (myTemplate != null) {
tag = myTemplate.getTag();
} else if ((tag = XsltCodeInsightUtil.getTemplateTag(myExpression, true, false)) == null) {
final XmlDocument document = PsiTreeUtil.getContextOfType(myExpression, XmlDocument.class, true);
assert document != null;
tag = document.getRootTag();
}
assert tag != null;
final XmlTag param = tag.createChildTag("param", XsltSupport.XSLT_NS, null, false);
try {
param.setAttribute("name", mySettings.getName());
if (mySettings.isCreateDefault()) {
param.setAttribute("select", myExpression.getText());
}
XmlTag anchorParam = null;
for (UsageInfo info : usageInfos) {
if (info instanceof XPathUsageInfo) {
final XPathUsageInfo x = (XPathUsageInfo) info;
final XPathVariableReference variableReference = XPathChangeUtil.createVariableReference(x.getExpression(), mySettings.getName());
final XmlAttribute attribute = x.getAttribute();
assert attribute != null;
x.getExpression().replace(variableReference);
if (XsltSupport.isParam(attribute.getParent())) {
if (anchorParam == null) {
anchorParam = attribute.getParent();
} else if (attribute.getParent().getTextOffset() < anchorParam.getTextOffset()) {
anchorParam = attribute.getParent();
}
}
} else {
final XmlTag t = (XmlTag) info.getElement();
if (t != null) {
final XmlTag p = t.createChildTag("with-param", t.getNamespace(), null, false);
p.setAttribute("name", mySettings.getName());
p.setAttribute("select", myExpression.getText());
t.add(p);
}
}
}
if (anchorParam != null) {
RefactoringUtil.addParameter(tag, param, PsiTreeUtil.getPrevSiblingOfType(anchorParam, XmlTag.class));
} else {
RefactoringUtil.addParameter(tag, param);
}
} catch (IncorrectOperationException e) {
Logger.getInstance(getClass().getName()).error(e);
}
}
Aggregations