@Override public SchemaPrefix getPrefixDeclaration(final XmlTag context, String namespacePrefix) { @NonNls String nsDeclarationAttrName = null; for(XmlTag t = context; t != null; t = t.getParentTag()) { if (t.hasNamespaceDeclarations()) { if (nsDeclarationAttrName == null) nsDeclarationAttrName = namespacePrefix.length() > 0 ? "xmlns:"+namespacePrefix:"xmlns"; XmlAttribute attribute = t.getAttribute(nsDeclarationAttrName); if (attribute != null) { final String attrPrefix = attribute.getNamespacePrefix(); final TextRange textRange = TextRange.from(attrPrefix.length() + 1, namespacePrefix.length()); return new SchemaPrefix(attribute, textRange, namespacePrefix); } } } return null; }
@Override protected VariableInplaceRenamer createRenamer(@NotNull PsiElement elementToRename, Editor editor) { PossiblePrefixReference reference = getReference(elementToRename.getContainingFile(), editor); if (reference != null) { PsiElement prefix = reference.resolve(); if (prefix instanceof SchemaPrefix) { return new VariableInplaceRenamer((PsiNamedElement)prefix, editor) { @Override protected void addReferenceAtCaret(Collection<PsiReference> refs) {} @Override protected boolean isReferenceAtCaret(PsiElement selectedElement, PsiReference ref) { return false; } }; } } return null; }
@Override public SchemaPrefix getPrefixDeclaration(final XmlTag context, String namespacePrefix) { SchemaPrefix prefix = super.getPrefixDeclaration(context, namespacePrefix); if (prefix != null) { return prefix; } if (namespacePrefix.isEmpty()) { // In for example XHTML documents, the root element looks like this: // <html xmlns="http://www.w3.org/1999/xhtml"> // This means that the IDE can find the namespace for "". // // However, in Android XML files it's implicit, so just return a dummy SchemaPrefix so // // that we don't end up with a // Namespace ''{0}'' is not bound // error from {@link XmlUnboundNsPrefixInspection#checkUnboundNamespacePrefix} return new SchemaPrefix(null, new TextRange(0, 0), SdkConstants.ANDROID_NS_NAME); } return null; }
@Nullable public static PsiElement resolvePrefix(final String prefix, XmlElement context) { final String name = "xmlns:" + prefix; XmlTag parent = PsiTreeUtil.getParentOfType(context, XmlTag.class); while (parent != null) { final XmlAttribute attribute = parent.getAttribute(name, null); if (attribute != null) { final TextRange textRange = TextRange.from("xmlns:".length(), prefix.length()); return new SchemaPrefix(attribute, textRange, prefix) { @Override public boolean equals(Object obj) { if (obj instanceof SchemaPrefix) { final SchemaPrefix p = (SchemaPrefix)obj; return prefix.equals(p.getName()) && p.getParent() == attribute; } return super.equals(obj); } }; } parent = PsiTreeUtil.getParentOfType(parent, XmlTag.class); } return null; }
@Override protected VariableInplaceRenamer createRenamer(@NotNull PsiElement elementToRename, Editor editor) { PossiblePrefixReference reference = getReference(elementToRename.getContainingFile(), editor); if (reference != null) { PsiElement prefix = reference.resolve(); if (prefix instanceof SchemaPrefix) { return new VariableInplaceRenamer((PsiNamedElement)prefix, editor) { @Override protected void addReferenceAtCaret(Collection<PsiReference> refs) {} @Override protected boolean isReferenceAtCaret(PsiElement selectedElement, PsiReference ref) { return false; } }; } } if (ApplicationManager.getApplication().isUnitTestMode()) { System.out.println("Reference: " + reference); if (reference != null) { System.out.println("Resolved: " + reference.resolve()); } } return null; }
@Override public SchemaPrefix getPrefixDeclaration(final XmlTag context, String namespacePrefix) { @NonNls String nsDeclarationAttrName = null; for(XmlTag t = context; t != null; t = t.getParentTag()) { if(t.hasNamespaceDeclarations()) { if(nsDeclarationAttrName == null) { nsDeclarationAttrName = namespacePrefix.length() > 0 ? "xmlns:" + namespacePrefix : "xmlns"; } XmlAttribute attribute = t.getAttribute(nsDeclarationAttrName); if(attribute != null) { final String attrPrefix = attribute.getNamespacePrefix(); final TextRange textRange = TextRange.from(attrPrefix.length() + 1, namespacePrefix.length()); return new SchemaPrefix(attribute, textRange, namespacePrefix); } } } return null; }
private void visitOuterLanguageElement(@NotNull final PsiElement element) { myHolder.registerOuterLanguageElement(element); PsiReference[] references = element.getReferences(); for (PsiReference reference : references) { if (reference instanceof PossiblePrefixReference && ((PossiblePrefixReference)reference).isPrefixReference()) { PsiElement resolve = reference.resolve(); if (resolve instanceof SchemaPrefix) { myHolder.addUsedPrefix(((SchemaPrefix)resolve).getName()); } } } }
@Override @Nullable public String getQuickNavigateInfo(PsiElement element, PsiElement originalElement) { if (element instanceof SchemaPrefix) { return ((SchemaPrefix)element).getQuickNavigateInfo(); } return null; }
@Override @Nullable public PsiElement resolve() { final String prefix = getCanonicalText(); XmlTag tag = PsiTreeUtil.getParentOfType(getElement(), XmlTag.class); while (tag != null) { if (tag.getLocalNamespaceDeclarations().containsKey(prefix)) { final XmlAttribute attribute = tag.getAttribute("xmlns:" + prefix, ""); final TextRange textRange = TextRange.from("xmlns:".length(), prefix.length()); return new SchemaPrefix(attribute, textRange, prefix); } tag = tag.getParentTag(); } return null; }
@Override public boolean isReferenceTo(PsiElement element) { if (element instanceof SchemaPrefix && element.getContainingFile() == myElement.getContainingFile()) { final PsiElement e = resolve(); if (e instanceof SchemaPrefix) { final String s = ((SchemaPrefix)e).getName(); return s != null && s.equals(((SchemaPrefix)element).getName()); } } return super.isReferenceTo(element); }
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; } // also catch namespace declarations in "normal" XML files that have XPath injected into some attributes // 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(new Processor<PsiReference>() { public boolean process(PsiReference psiReference) { if (psiReference.getElement() == attr) { return true; } return false; } }); }
@Override public SchemaPrefix getPrefixDeclaration(XmlTag context, String namespacePrefix) { // if ("rt".equals(namespacePrefix)) { // return new SchemaPrefix(null, null, namespacePrefix); // } return super.getPrefixDeclaration(context, namespacePrefix); }
public SchemaPrefix getPrefixDeclaration(final XmlTag context, String namespacePrefix) { @NonNls String nsDeclarationAttrName = null; for(XmlTag t = context; t != null; t = t.getParentTag()) { if (t.hasNamespaceDeclarations()) { if (nsDeclarationAttrName == null) nsDeclarationAttrName = namespacePrefix.length() > 0 ? "xmlns:"+namespacePrefix:"xmlns"; XmlAttribute attribute = t.getAttribute(nsDeclarationAttrName); if (attribute != null) { final String attrPrefix = attribute.getNamespacePrefix(); final TextRange textRange = TextRange.from(attrPrefix.length() + 1, namespacePrefix.length()); return new SchemaPrefix(attribute, textRange, namespacePrefix); } } } return null; }
@Nullable public String getQuickNavigateInfo(PsiElement element, PsiElement originalElement) { if (element instanceof SchemaPrefix) { return ((SchemaPrefix)element).getQuickNavigateInfo(); } return null; }
@Nullable public PsiElement resolve() { final String prefix = getCanonicalText(); XmlTag tag = PsiTreeUtil.getParentOfType(getElement(), XmlTag.class); while (tag != null) { if (tag.getLocalNamespaceDeclarations().containsKey(prefix)) { final XmlAttribute attribute = tag.getAttribute("xmlns:" + prefix, ""); final TextRange textRange = TextRange.from("xmlns:".length(), prefix.length()); return new SchemaPrefix(attribute, textRange, prefix); } tag = tag.getParentTag(); } return null; }
@Override public void visitXmlAttributeValue(final XmlAttributeValue value) { final PsiElement element = value.getParent(); if (!(element instanceof XmlAttribute)) return; final XmlAttribute attribute = (XmlAttribute)element; final XmlTag tag = attribute.getParent(); if (tag == null) return; final XmlElementDescriptor descriptor = tag.getDescriptor(); if (descriptor == null) return; final XmlAttributeDescriptor attributeDescriptor = descriptor.getAttributeDescriptor(attribute); if (attributeDescriptor != null) { if (attributeDescriptor.hasIdType()) { updateMap(attribute, value, false); } else { final PsiReference[] references = value.getReferences(); for (PsiReference r : references) { if (r instanceof IdReferenceProvider.GlobalAttributeValueSelfReference /*&& !r.isSoft()*/) { updateMap(attribute, value, r.isSoft()); } else if (r instanceof SchemaPrefixReference) { SchemaPrefix prefix = ((SchemaPrefixReference)r).resolve(); if (prefix != null) { myHolder.addUsedPrefix(prefix.getName()); } } } } if (attributeDescriptor.hasIdRefType() && PsiTreeUtil.getChildOfType(value, OuterLanguageElement.class) == null) { myHolder.registerIdReference(value); } } String s = value.getValue(); detectPrefix(s); super.visitXmlAttributeValue(value); }
@Nullable public abstract SchemaPrefix getPrefixDeclaration(final XmlTag context, String namespacePrefix);
@Override protected boolean isAvailable(PsiElement element, Editor editor, PsiFile file) { PossiblePrefixReference ref = getReference(file, editor); return ref != null && ref.resolve() instanceof SchemaPrefix; }
@Override public void visitXmlAttributeValue(final XmlAttributeValue value) { final PsiElement element = value.getParent(); if (!(element instanceof XmlAttribute)) return; final XmlAttribute attribute = (XmlAttribute)element; final XmlTag tag = attribute.getParent(); if (tag == null) return; final XmlElementDescriptor descriptor = tag.getDescriptor(); if (descriptor == null) return; final XmlAttributeDescriptor attributeDescriptor = descriptor.getAttributeDescriptor(attribute); if (attributeDescriptor == null) return; if (attributeDescriptor.hasIdType()) { updateMap(attribute, value, false); } else { final PsiReference[] references = value.getReferences(); for (PsiReference r : references) { if (r instanceof IdReferenceProvider.GlobalAttributeValueSelfReference /*&& !r.isSoft()*/) { updateMap(attribute, value, r.isSoft()); } else if (r instanceof SchemaPrefixReference) { SchemaPrefix prefix = ((SchemaPrefixReference)r).resolve(); if (prefix != null) { myHolder.addUsedPrefix(prefix.getName()); } } } } if (attributeDescriptor.hasIdRefType() && PsiTreeUtil.getChildOfType(value, OuterLanguageElement.class) == null) { myHolder.registerIdReference(value); } String s = value.getValue(); detectPrefix(s); super.visitXmlAttributeValue(value); }
@Nullable @Override public SchemaPrefix getPrefixDeclaration(XmlTag xmlTag, String s) { return null; }