Search in sources :

Example 21 with XmlDocument

use of com.intellij.psi.xml.XmlDocument in project intellij-plugins by JetBrains.

the class FlexSchemaHandler method getUniquePrefix.

static String getUniquePrefix(final String namespace, final XmlFile xmlFile) {
    String prefix = getDefaultPrefix(namespace);
    final XmlDocument document = xmlFile.getDocument();
    final XmlTag tag = document == null ? null : document.getRootTag();
    final String[] knownPrefixes = getKnownPrefixes(tag);
    if (ArrayUtil.contains(prefix, knownPrefixes)) {
        for (int i = 2; ; i++) {
            final String newPrefix = prefix + i;
            if (!ArrayUtil.contains(newPrefix, knownPrefixes)) {
                prefix = newPrefix;
                break;
            }
        }
    }
    return prefix;
}
Also used : XmlDocument(com.intellij.psi.xml.XmlDocument) XmlTag(com.intellij.psi.xml.XmlTag)

Example 22 with XmlDocument

use of com.intellij.psi.xml.XmlDocument in project intellij-plugins by JetBrains.

the class ActionScriptSmartCompletionContributor method getEventsMap.

public static Map<String, String> getEventsMap(JSClass clazzToProcess) {
    if (clazzToProcess == null)
        return Collections.emptyMap();
    final Map<String, String> eventsMap = new THashMap<>();
    class EventsDataCollector extends ResolveProcessor implements ActionScriptResolveUtil.MetaDataProcessor {

        public EventsDataCollector() {
            super(null);
            setToProcessHierarchy(true);
            setToProcessMembers(false);
            setTypeContext(true);
            setLocalResolve(true);
        }

        @Override
        public boolean process(@NotNull final JSAttribute jsAttribute) {
            if ("Event".equals(jsAttribute.getName())) {
                final JSAttributeNameValuePair eventAttr = jsAttribute.getValueByName("name");
                JSAttributeNameValuePair typeAttr = jsAttribute.getValueByName("type");
                if (eventAttr != null && typeAttr != null) {
                    final String simpleValue = eventAttr.getSimpleValue();
                    if (simpleValue != null) {
                        eventsMap.put(simpleValue, typeAttr.getSimpleValue());
                    }
                }
            }
            return true;
        }

        @Override
        public boolean handleOtherElement(final PsiElement el, final PsiElement context, final Ref<PsiElement> continuePassElement) {
            return true;
        }

        @Override
        public boolean execute(@NotNull PsiElement element, @NotNull ResolveState state) {
            if (element instanceof JSClass) {
                ActionScriptResolveUtil.processMetaAttributesForClass(element, this, true);
            }
            return true;
        }
    }
    final EventsDataCollector eventsDataCollector = new EventsDataCollector();
    if (clazzToProcess instanceof XmlBackedJSClassImpl) {
        XmlFile file = (XmlFile) clazzToProcess.getParent().getContainingFile();
        if (file != null && JavaScriptSupportLoader.isFlexMxmFile(file)) {
            final XmlDocument xmlDocument = file.getDocument();
            final XmlTag rootTag = xmlDocument == null ? null : xmlDocument.getRootTag();
            final XmlTag[] tags = rootTag == null ? XmlTag.EMPTY : MxmlJSClass.findLanguageSubTags(rootTag, FlexPredefinedTagNames.METADATA);
            JSResolveUtil.JSInjectedFilesVisitor injectedFilesVisitor = new JSResolveUtil.JSInjectedFilesVisitor() {

                @Override
                protected void process(JSFile file) {
                    for (PsiElement element : file.getChildren()) {
                        if (element instanceof JSAttributeList) {
                            ActionScriptResolveUtil.processAttributeList(eventsDataCollector, null, (JSAttributeList) element, true, true);
                        }
                    }
                }
            };
            for (XmlTag tag : tags) {
                JSResolveUtil.processInjectedFileForTag(tag, injectedFilesVisitor);
            }
        }
    }
    clazzToProcess.processDeclarations(eventsDataCollector, ResolveState.initial(), clazzToProcess, clazzToProcess);
    return eventsMap;
}
Also used : JSAttributeList(com.intellij.lang.javascript.psi.ecmal4.JSAttributeList) XmlBackedJSClassImpl(com.intellij.lang.javascript.flex.XmlBackedJSClassImpl) XmlFile(com.intellij.psi.xml.XmlFile) XmlDocument(com.intellij.psi.xml.XmlDocument) NotNull(org.jetbrains.annotations.NotNull) ResolveState(com.intellij.psi.ResolveState) Ref(com.intellij.openapi.util.Ref) THashMap(gnu.trove.THashMap) JSAttribute(com.intellij.lang.javascript.psi.ecmal4.JSAttribute) JSAttributeNameValuePair(com.intellij.lang.javascript.psi.ecmal4.JSAttributeNameValuePair) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) MxmlJSClass(com.intellij.javascript.flex.mxml.MxmlJSClass) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 23 with XmlDocument

use of com.intellij.psi.xml.XmlDocument in project intellij-community by JetBrains.

the class ExternalAnnotationsManagerImpl method annotateExternally.

private void annotateExternally(@NotNull final PsiModifierListOwner listOwner, @NotNull final String annotationFQName, @Nullable final XmlFile xmlFile, @NotNull final PsiFile codeUsageFile, @Nullable final PsiNameValuePair[] values) {
    if (xmlFile == null) {
        notifyAfterAnnotationChanging(listOwner, annotationFQName, false);
        return;
    }
    try {
        final XmlDocument document = xmlFile.getDocument();
        if (document != null) {
            final XmlTag rootTag = document.getRootTag();
            final String externalName = getExternalName(listOwner, false);
            if (externalName == null) {
                LOG.info("member without external name: " + listOwner);
            }
            if (rootTag != null && externalName != null) {
                XmlTag anchor = null;
                for (XmlTag item : rootTag.getSubTags()) {
                    int compare = Comparing.compare(externalName, StringUtil.unescapeXml(item.getAttributeValue("name")));
                    if (compare == 0) {
                        anchor = null;
                        for (XmlTag annotation : item.getSubTags()) {
                            compare = Comparing.compare(annotationFQName, annotation.getAttributeValue("name"));
                            if (compare == 0) {
                                annotation.delete();
                                break;
                            }
                            anchor = annotation;
                        }
                        XmlTag newTag = XmlElementFactory.getInstance(myPsiManager.getProject()).createTagFromText(createAnnotationTag(annotationFQName, values));
                        item.addAfter(newTag, anchor);
                        commitChanges(xmlFile);
                        notifyAfterAnnotationChanging(listOwner, annotationFQName, true);
                        return;
                    }
                    if (compare < 0)
                        break;
                    anchor = item;
                }
                @NonNls String text = "<item name=\'" + StringUtil.escapeXml(externalName) + "\'>\n";
                text += createAnnotationTag(annotationFQName, values);
                text += "</item>";
                rootTag.addAfter(XmlElementFactory.getInstance(myPsiManager.getProject()).createTagFromText(text), anchor);
                commitChanges(xmlFile);
                notifyAfterAnnotationChanging(listOwner, annotationFQName, true);
                return;
            }
        }
        notifyAfterAnnotationChanging(listOwner, annotationFQName, false);
    } catch (IncorrectOperationException e) {
        LOG.error(e);
        notifyAfterAnnotationChanging(listOwner, annotationFQName, false);
    } finally {
        dropCache();
        if (codeUsageFile.getVirtualFile().isInLocalFileSystem()) {
            UndoUtil.markPsiFileForUndo(codeUsageFile);
        }
    }
}
Also used : NonNls(org.jetbrains.annotations.NonNls) XmlDocument(com.intellij.psi.xml.XmlDocument) IncorrectOperationException(com.intellij.util.IncorrectOperationException) XmlTag(com.intellij.psi.xml.XmlTag)

Example 24 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 25 with XmlDocument

use of com.intellij.psi.xml.XmlDocument in project intellij-community by JetBrains.

the class AntDomFileDescription method isAntFile.

public static boolean isAntFile(final XmlFile xmlFile) {
    final XmlDocument document = xmlFile.getDocument();
    if (document != null) {
        final XmlTag tag = document.getRootTag();
        final VirtualFile vFile = xmlFile.getOriginalFile().getVirtualFile();
        if (tag != null && ROOT_TAG_NAME.equals(tag.getName()) && tag.getContext() instanceof XmlDocument) {
            if (tag.getAttributeValue("name") != null && tag.getAttributeValue("default") != null && vFile != null && ForcedAntFileAttribute.mayBeAntFile(vFile)) {
                return true;
            }
        }
        if (vFile != null && ForcedAntFileAttribute.isAntFile(vFile)) {
            return true;
        }
    }
    return false;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlDocument(com.intellij.psi.xml.XmlDocument) 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