use of com.intellij.psi.impl.source.xml.SchemaPrefixReference in project intellij-community by JetBrains.
the class XmlUnboundNsPrefixInspection method buildVisitor.
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
return new XmlElementVisitor() {
private Boolean isXml;
private boolean isXmlFile(XmlElement element) {
if (isXml == null) {
final PsiFile file = element.getContainingFile();
isXml = file instanceof XmlFile && !InjectedLanguageManager.getInstance(element.getProject()).isInjectedFragment(file);
}
return isXml.booleanValue();
}
@Override
public void visitXmlToken(final XmlToken token) {
if (isXmlFile(token) && token.getTokenType() == XmlTokenType.XML_NAME) {
PsiElement element = token.getPrevSibling();
while (element instanceof PsiWhiteSpace) element = element.getPrevSibling();
if (element instanceof XmlToken && ((XmlToken) element).getTokenType() == XmlTokenType.XML_START_TAG_START) {
PsiElement parent = element.getParent();
if (parent instanceof XmlTag && !(token.getNextSibling() instanceof OuterLanguageElement)) {
XmlTag tag = (XmlTag) parent;
checkUnboundNamespacePrefix(tag, tag, tag.getNamespacePrefix(), token, holder, isOnTheFly);
}
}
}
}
@Override
public void visitXmlAttribute(final XmlAttribute attribute) {
if (!isXmlFile(attribute)) {
return;
}
final String namespace = attribute.getNamespace();
if (attribute.isNamespaceDeclaration() || XmlUtil.XML_SCHEMA_INSTANCE_URI.equals(namespace)) {
return;
}
XmlTag tag = attribute.getParent();
if (tag == null)
return;
XmlElementDescriptor elementDescriptor = tag.getDescriptor();
if (elementDescriptor == null || elementDescriptor instanceof AnyXmlElementDescriptor) {
return;
}
final String name = attribute.getName();
checkUnboundNamespacePrefix(attribute, tag, XmlUtil.findPrefixByQualifiedName(name), null, holder, isOnTheFly);
}
@Override
public void visitXmlAttributeValue(XmlAttributeValue value) {
PsiReference[] references = value.getReferences();
for (PsiReference reference : references) {
if (reference instanceof SchemaPrefixReference) {
if (!XML.equals(((SchemaPrefixReference) reference).getNamespacePrefix()) && reference.resolve() == null) {
holder.registerProblem(reference, XmlErrorMessages.message("unbound.namespace", ((SchemaPrefixReference) reference).getNamespacePrefix()), ProblemHighlightType.LIKE_UNKNOWN_SYMBOL);
}
}
}
}
};
}
use of com.intellij.psi.impl.source.xml.SchemaPrefixReference 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;
}
}
Aggregations