Search in sources :

Example 36 with XmlDocument

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();
    }
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) ArrayList(java.util.ArrayList) XmlDocument(com.intellij.psi.xml.XmlDocument) IncorrectOperationException(com.intellij.util.IncorrectOperationException) XmlTag(com.intellij.psi.xml.XmlTag)

Example 37 with XmlDocument

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);
}
Also used : XmlDocument(com.intellij.psi.xml.XmlDocument)

Example 38 with XmlDocument

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());
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) XmlDocument(com.intellij.psi.xml.XmlDocument)

Example 39 with XmlDocument

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);
}
Also used : PsiComment(com.intellij.psi.PsiComment) XmlFile(com.intellij.psi.xml.XmlFile) XmlDoctype(com.intellij.psi.xml.XmlDoctype) ArrayList(java.util.ArrayList) XmlDocument(com.intellij.psi.xml.XmlDocument) XmlProlog(com.intellij.psi.xml.XmlProlog) PsiElement(com.intellij.psi.PsiElement)

Example 40 with XmlDocument

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);
    }
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XPathVariableReference(org.intellij.lang.xpath.psi.XPathVariableReference) XmlDocument(com.intellij.psi.xml.XmlDocument) IncorrectOperationException(com.intellij.util.IncorrectOperationException) UsageInfo(com.intellij.usageView.UsageInfo) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

XmlDocument (com.intellij.psi.xml.XmlDocument)57 XmlTag (com.intellij.psi.xml.XmlTag)39 XmlFile (com.intellij.psi.xml.XmlFile)32 PsiElement (com.intellij.psi.PsiElement)13 Nullable (org.jetbrains.annotations.Nullable)11 XmlNSDescriptor (com.intellij.xml.XmlNSDescriptor)9 PsiFile (com.intellij.psi.PsiFile)8 VirtualFile (com.intellij.openapi.vfs.VirtualFile)6 ArrayList (java.util.ArrayList)6 XmlAttribute (com.intellij.psi.xml.XmlAttribute)5 PsiMetaData (com.intellij.psi.meta.PsiMetaData)4 IncorrectOperationException (com.intellij.util.IncorrectOperationException)4 NotNull (org.jetbrains.annotations.NotNull)4 Project (com.intellij.openapi.project.Project)3 XmlText (com.intellij.psi.xml.XmlText)3 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)3 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)2 Editor (com.intellij.openapi.editor.Editor)2 PsiErrorElement (com.intellij.psi.PsiErrorElement)2 FilterElementProcessor (com.intellij.psi.scope.processor.FilterElementProcessor)2