use of com.intellij.psi.impl.source.xml.SchemaPrefix in project intellij-community by JetBrains.
the class DefaultXmlExtension method getPrefixDeclaration.
@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;
}
use of com.intellij.psi.impl.source.xml.SchemaPrefix 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;
});
}
use of com.intellij.psi.impl.source.xml.SchemaPrefix in project intellij-plugins by JetBrains.
the class MxmlTagNameReference method bindToElement.
public PsiElement bindToElement(@NotNull final PsiElement element) throws IncorrectOperationException {
final String newPackage = getNewPackage(element);
if (newPackage == null) {
return super.bindToElement(element);
} else {
final XmlTag tag = getTagElement();
if (tag == null || !myStartTagFlag)
return tag;
final String newNamespace = newPackage.isEmpty() ? "*" : newPackage + ".*";
String newPrefix = tag.getPrefixByNamespace(newNamespace);
if (newPrefix == null) {
final XmlFile xmlFile = (XmlFile) tag.getContainingFile();
newPrefix = FlexSchemaHandler.getUniquePrefix(newNamespace, xmlFile);
final XmlTag rootTag = xmlFile.getRootTag();
assert rootTag != null;
insertNamespaceDeclaration(rootTag, newNamespace, newPrefix);
}
final SchemaPrefixReference schemaPrefixReference = getSchemaPrefixReference(tag);
final SchemaPrefix schemaPrefix = schemaPrefixReference == null ? null : schemaPrefixReference.resolve();
final String oldPrefix = tag.getNamespacePrefix();
final String newLocalName = FileUtil.getNameWithoutExtension(((PsiFile) element).getName());
tag.setName(StringUtil.isEmpty(newPrefix) ? newLocalName : (newPrefix + ":" + newLocalName));
fixSubTagsPrefixes(tag, oldPrefix, newPrefix);
removeNamespaceDeclarationIfNotUsed(schemaPrefix);
return tag;
}
}
use of com.intellij.psi.impl.source.xml.SchemaPrefix in project intellij-community by JetBrains.
the class XsltNamespaceContext method resolvePrefix.
@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;
}
use of com.intellij.psi.impl.source.xml.SchemaPrefix in project intellij-community by JetBrains.
the class ConvertSchemaPrefixToDefaultIntention method invoke.
@Override
public void invoke(@NotNull final Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
final XmlAttribute xmlns = getXmlnsDeclaration(element);
if (xmlns == null)
return;
SchemaPrefixReference prefixRef = null;
for (PsiReference ref : xmlns.getReferences()) {
if (ref instanceof SchemaPrefixReference) {
prefixRef = (SchemaPrefixReference) ref;
break;
}
}
if (prefixRef == null)
return;
final SchemaPrefix prefix = prefixRef.resolve();
final String ns = prefixRef.getNamespacePrefix();
final ArrayList<XmlTag> tags = new ArrayList<>();
final ArrayList<XmlAttribute> attrs = new ArrayList<>();
xmlns.getParent().accept(new XmlRecursiveElementVisitor() {
@Override
public void visitXmlTag(XmlTag tag) {
if (ns.equals(tag.getNamespacePrefix())) {
tags.add(tag);
}
super.visitXmlTag(tag);
}
@Override
public void visitXmlAttributeValue(XmlAttributeValue value) {
if (value.getValue().startsWith(ns + ":")) {
for (PsiReference ref : value.getReferences()) {
if (ref instanceof SchemaPrefixReference && ref.isReferenceTo(prefix)) {
attrs.add((XmlAttribute) value.getParent());
}
}
}
}
});
if (!FileModificationService.getInstance().preparePsiElementsForWrite(xmlns.getContainingFile()))
return;
CommandProcessor.getInstance().executeCommand(project, () -> {
convertTagsAndAttributes(ns, tags, attrs, project);
ApplicationManager.getApplication().runWriteAction(() -> {
xmlns.setName("xmlns");
});
}, NAME, null);
new WriteCommandAction(project, NAME, xmlns.getContainingFile()) {
@Override
protected void run(@NotNull Result result) throws Throwable {
xmlns.setName("xmlns");
}
}.execute();
}
Aggregations