Search in sources :

Example 31 with XmlFile

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

the class XmlNSDescriptorImpl method doFindIn.

private TypeDescriptor doFindIn(final XmlTag[] tags, final String name, final String namespace, final Pair<QNameKey, XmlTag> pair, final XmlTag rootTag) {
    for (final XmlTag tag : tags) {
        if (equalsToSchemaName(tag, "complexType")) {
            if (name == null) {
                CachedValue<TypeDescriptor> value = createAndPutTypesCachedValue(tag, pair);
                return value.getValue();
            }
            String nameAttribute = tag.getAttributeValue("name");
            if (isSameName(name, namespace, nameAttribute)) {
                CachedValue<TypeDescriptor> cachedValue = createAndPutTypesCachedValue(tag, pair);
                return cachedValue.getValue();
            }
        } else if (equalsToSchemaName(tag, "simpleType")) {
            if (name == null) {
                CachedValue<TypeDescriptor> value = createAndPutTypesCachedValueSimpleType(tag, pair);
                return value.getValue();
            }
            String nameAttribute = tag.getAttributeValue("name");
            if (isSameName(name, namespace, nameAttribute)) {
                CachedValue<TypeDescriptor> cachedValue = createAndPutTypesCachedValue(tag, pair);
                return cachedValue.getValue();
            }
        } else if (equalsToSchemaName(tag, INCLUDE_TAG_NAME) || (equalsToSchemaName(tag, IMPORT_TAG_NAME) && (namespace == null || !namespace.equals(getDefaultNamespace())))) {
            final String schemaLocation = tag.getAttributeValue("schemaLocation");
            if (schemaLocation != null) {
                final XmlFile xmlFile = XmlUtil.findNamespace(rootTag.getContainingFile(), schemaLocation);
                if (xmlFile != null) {
                    final XmlDocument document = xmlFile.getDocument();
                    if (document != null) {
                        final CachedValue<TypeDescriptor> value = CachedValuesManager.getManager(tag.getProject()).createCachedValue(() -> {
                            final String currentName = tag.getAttributeValue("name");
                            if ((currentName != null && !currentName.equals(XmlUtil.findLocalNameByQualifiedName(name))) || !xmlFile.isValid() || xmlFile.getDocument() == null) {
                                myTypesMap.remove(pair);
                                return new CachedValueProvider.Result<>(null, PsiModificationTracker.MODIFICATION_COUNT);
                            }
                            final XmlDocument document1 = xmlFile.getDocument();
                            final XmlNSDescriptorImpl nsDescriptor = findNSDescriptor(tag, document1);
                            if (nsDescriptor == null) {
                                myTypesMap.remove(pair);
                                return new CachedValueProvider.Result<>(null, PsiModificationTracker.MODIFICATION_COUNT);
                            }
                            final XmlTag rTag = document1.getRootTag();
                            final TypeDescriptor complexTypeDescriptor = nsDescriptor.findTypeDescriptorImpl(rTag, name, namespace);
                            return new CachedValueProvider.Result<>(complexTypeDescriptor, rTag);
                        }, false);
                        if (value.getValue() != null) {
                            myTypesMap.put(pair, value);
                            return value.getValue();
                        }
                    }
                }
            }
        } else if (equalsToSchemaName(tag, REDEFINE_TAG_NAME)) {
            final XmlTag[] subTags = tag.getSubTags();
            TypeDescriptor descriptor = doFindIn(subTags, name, namespace, pair, rootTag);
            if (descriptor != null)
                return descriptor;
            final XmlNSDescriptorImpl nsDescriptor = getRedefinedElementDescriptor(tag);
            if (nsDescriptor != null) {
                final XmlTag redefinedRootTag = ((XmlDocument) nsDescriptor.getDeclaration()).getRootTag();
                descriptor = doFindIn(redefinedRootTag.getSubTags(), name, namespace, pair, redefinedRootTag);
                if (descriptor != null)
                    return descriptor;
            }
        }
    }
    return null;
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) XmlDocument(com.intellij.psi.xml.XmlDocument) CachedValue(com.intellij.psi.util.CachedValue) XmlTag(com.intellij.psi.xml.XmlTag)

Example 32 with XmlFile

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

the class XmlNSDescriptorImpl method getAttributeImpl.

@Nullable
private XmlAttributeDescriptor getAttributeImpl(String localName, String namespace, @Nullable Set<XmlTag> visited) {
    if (myTag == null)
        return null;
    XmlNSDescriptor nsDescriptor = myTag.getNSDescriptor(namespace, true);
    if (nsDescriptor != this && nsDescriptor instanceof XmlNSDescriptorImpl) {
        return ((XmlNSDescriptorImpl) nsDescriptor).getAttributeImpl(localName, namespace, visited);
    }
    if (visited == null)
        visited = new HashSet<>(1);
    else if (visited.contains(myTag))
        return null;
    visited.add(myTag);
    XmlTag[] tags = myTag.getSubTags();
    for (XmlTag tag : tags) {
        if (equalsToSchemaName(tag, ATTRIBUTE_TAG_NAME)) {
            String name = tag.getAttributeValue("name");
            if (name != null) {
                if (checkElementNameEquivalence(localName, namespace, name, tag)) {
                    return createAttributeDescriptor(tag);
                }
            }
        } else if (equalsToSchemaName(tag, INCLUDE_TAG_NAME) || (equalsToSchemaName(tag, IMPORT_TAG_NAME) && namespace.equals(tag.getAttributeValue("namespace")))) {
            final String schemaLocation = tag.getAttributeValue("schemaLocation");
            if (schemaLocation != null) {
                final XmlFile xmlFile = XmlUtil.findNamespace(myTag.getContainingFile(), schemaLocation);
                if (xmlFile != null) {
                    final XmlDocument includedDocument = xmlFile.getDocument();
                    if (includedDocument != null) {
                        final PsiMetaData data = includedDocument.getMetaData();
                        if (data instanceof XmlNSDescriptorImpl) {
                            final XmlAttributeDescriptor attributeDescriptor = ((XmlNSDescriptorImpl) data).getAttributeImpl(localName, namespace, visited);
                            if (attributeDescriptor != null) {
                                final CachedValue<XmlAttributeDescriptor> value = CachedValuesManager.getManager(includedDocument.getProject()).createCachedValue(() -> {
                                    Object[] deps = attributeDescriptor.getDependences();
                                    if (deps.length == 0) {
                                        LOG.error(attributeDescriptor + " (" + attributeDescriptor.getClass() + ") returned no dependencies");
                                    }
                                    return new CachedValueProvider.Result<>(attributeDescriptor, deps);
                                }, false);
                                return value.getValue();
                            }
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : CachedValueProvider(com.intellij.psi.util.CachedValueProvider) XmlFile(com.intellij.psi.xml.XmlFile) XmlDocument(com.intellij.psi.xml.XmlDocument) CachedValue(com.intellij.psi.util.CachedValue) XmlNSDescriptor(com.intellij.xml.XmlNSDescriptor) PsiMetaData(com.intellij.psi.meta.PsiMetaData) XmlAttributeDescriptor(com.intellij.xml.XmlAttributeDescriptor) THashSet(gnu.trove.THashSet) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Example 33 with XmlFile

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

the class JavaFxPropertyRenameHandler method getReferences.

@NotNull
private static PsiReference[] getReferences(DataContext dataContext) {
    final Editor editor = CommonDataKeys.EDITOR.getData(dataContext);
    PsiFile file = CommonDataKeys.PSI_FILE.getData(dataContext);
    if (file == null && editor != null && ApplicationManager.getApplication().isUnitTestMode()) {
        final Project project = editor.getProject();
        if (project != null) {
            file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument());
        }
    }
    if (editor != null && file instanceof XmlFile && JavaFxFileTypeFactory.isFxml(file)) {
        final int offset = editor.getCaretModel().getOffset();
        final PsiReference reference = file.findReferenceAt(offset);
        if (reference instanceof PsiMultiReference) {
            return ((PsiMultiReference) reference).getReferences();
        }
        if (isKnown(reference))
            return new PsiReference[] { reference };
    }
    return PsiReference.EMPTY_ARRAY;
}
Also used : Project(com.intellij.openapi.project.Project) XmlFile(com.intellij.psi.xml.XmlFile) PsiMultiReference(com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference) Editor(com.intellij.openapi.editor.Editor) NotNull(org.jetbrains.annotations.NotNull)

Example 34 with XmlFile

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

the class XsltExtractFunctionAction method extractImpl.

protected boolean extractImpl(XPathExpression expression, Set<XPathExpression> matchingExpressions, List<XmlTag> otherMatches, RefactoringOptions dlg) {
    final XmlAttribute attribute = PsiTreeUtil.getContextOfType(expression, XmlAttribute.class, true);
    assert attribute != null;
    try {
        final String name = dlg.getName();
        final XmlTag rootTag = ((XmlFile) attribute.getParent().getContainingFile()).getRootTag();
        final XmlTag[] templates = rootTag.findSubTags("template", XsltSupport.XSLT_NS);
        final XmlTag insertionPoint = templates.length > 0 ? templates[0] : rootTag.getSubTags()[0];
        final XmlTag parentTag = insertionPoint.getParentTag();
        assert parentTag != null : "Could not locate position to create function at";
        final XmlTag xmlTag = parentTag.createChildTag("function", XsltSupport.XSLT_NS, null, false);
        xmlTag.setAttribute("name", name);
        final XPathType type = ExpectedTypeUtil.mapType(expression, expression.getType());
        xmlTag.setAttribute("as", prefixedName(type, insertionPoint));
        final StringBuilder argList = new StringBuilder();
        final List<XPathVariableReference> references = RefactoringUtil.collectVariableReferences(expression);
        for (XPathVariableReference reference : references) {
            final XPathVariable variable = reference.resolve();
            if (variable instanceof XsltVariable) {
                // don't pass through global parameters and variables
                if (XsltCodeInsightUtil.getTemplateTag(variable, false) != null) {
                    final XmlTag param = parentTag.createChildTag("param", XsltSupport.XSLT_NS, null, false);
                    param.setAttribute("name", variable.getName());
                    if (!variable.getType().isAbstract()) {
                        param.setAttribute("as", prefixedName(ExpectedTypeUtil.mapType(expression, variable.getType()), parentTag));
                    }
                    RefactoringUtil.addParameter(xmlTag, param);
                    if (argList.length() > 0) {
                        argList.append(", ");
                    }
                    argList.append("$").append(variable.getName());
                }
            }
        }
        final XmlTag seqTag = parentTag.createChildTag("sequence", XsltSupport.XSLT_NS, null, false);
        seqTag.setAttribute("select", expression.getText());
        xmlTag.add(seqTag);
        // TODO: revisit the formatting
        final PsiElement element = parentTag.addBefore(xmlTag, insertionPoint);
        final ASTNode node1 = parentTag.getNode();
        assert node1 != null;
        final ASTNode node2 = element.getNode();
        assert node2 != null;
        CodeStyleManager.getInstance(xmlTag.getManager().getProject()).reformatNewlyAddedElement(node1, node2);
        final XPathExpression var = XPathChangeUtil.createExpression(expression, name + "(" + argList + ")");
        expression.replace(var);
        return true;
    } catch (IncorrectOperationException e) {
        Logger.getInstance(getClass().getName()).error(e);
        return false;
    }
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) XsltVariable(org.intellij.lang.xpath.xslt.psi.XsltVariable) ASTNode(com.intellij.lang.ASTNode) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 35 with XmlFile

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

the class XsltImplicitUsagesProvider method isImplicitUsage.

public boolean isImplicitUsage(PsiElement element) {
    if (!(element instanceof XmlAttribute)) {
        return false;
    }
    final XmlAttribute attr = (XmlAttribute) element;
    if (!attr.isNamespaceDeclaration()) {
        return false;
    }
    final PsiFile file = attr.getContainingFile();
    if (!(file instanceof XmlFile)) {
        return false;
    }
    // ContextProvider.hasXPathInjections() is an optimization that avoids to run the references search on totally XPath-free XML files
    if (!ContextProvider.hasXPathInjections((XmlFile) file) && !XsltSupport.isXsltFile(file)) {
        return false;
    }
    // This need to catch both prefix references from injected XPathFiles and prefixes from mode declarations/references:
    // <xsl:template match="*" mode="prefix:name" />
    // BTW: Almost the same logic applies to other XML dialects (RELAX-NG).
    // Pull this class into the platform?
    final String prefix = attr.getLocalName();
    final SchemaPrefix target = new SchemaPrefix(attr, TextRange.from("xmlns:".length(), prefix.length()), prefix);
    final Query<PsiReference> q = ReferencesSearch.search(target, new LocalSearchScope(attr.getParent()));
    return !q.forEach(psiReference -> {
        if (psiReference.getElement() == attr) {
            return true;
        }
        return false;
    });
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) ReferencesSearch(com.intellij.psi.search.searches.ReferencesSearch) SchemaPrefix(com.intellij.psi.impl.source.xml.SchemaPrefix) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) XsltSupport(org.intellij.lang.xpath.xslt.XsltSupport) PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) Query(com.intellij.util.Query) Processor(com.intellij.util.Processor) PsiElement(com.intellij.psi.PsiElement) PsiFile(com.intellij.psi.PsiFile) ImplicitUsageProvider(com.intellij.codeInsight.daemon.ImplicitUsageProvider) ContextProvider(org.intellij.lang.xpath.context.ContextProvider) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) SchemaPrefix(com.intellij.psi.impl.source.xml.SchemaPrefix) PsiReference(com.intellij.psi.PsiReference) PsiFile(com.intellij.psi.PsiFile)

Aggregations

XmlFile (com.intellij.psi.xml.XmlFile)409 XmlTag (com.intellij.psi.xml.XmlTag)155 PsiFile (com.intellij.psi.PsiFile)121 VirtualFile (com.intellij.openapi.vfs.VirtualFile)102 Nullable (org.jetbrains.annotations.Nullable)74 Project (com.intellij.openapi.project.Project)69 NotNull (org.jetbrains.annotations.NotNull)66 PsiElement (com.intellij.psi.PsiElement)64 XmlAttribute (com.intellij.psi.xml.XmlAttribute)39 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)34 Module (com.intellij.openapi.module.Module)34 XmlDocument (com.intellij.psi.xml.XmlDocument)32 Result (com.intellij.openapi.application.Result)28 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)23 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)22 XmlNSDescriptor (com.intellij.xml.XmlNSDescriptor)21 ArrayList (java.util.ArrayList)20 Document (com.intellij.openapi.editor.Document)19 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)18 Editor (com.intellij.openapi.editor.Editor)15