use of com.intellij.util.xml.reflect.CustomDomChildrenDescription in project intellij-community by JetBrains.
the class DomStubBuilderVisitor method visitXmlElement.
void visitXmlElement(XmlElement element, ElementStub parent, int index) {
DomInvocationHandler handler = myManager.getDomHandler(element);
if (handler == null || handler.getAnnotation(Stubbed.class) == null && !handler.getChildDescription().isStubbed())
return;
AbstractDomChildrenDescription description = handler.getChildDescription();
String nsKey = description instanceof DomChildrenDescription ? ((DomChildrenDescription) description).getXmlName().getNamespaceKey() : "";
if (element instanceof XmlTag) {
XmlTag tag = (XmlTag) element;
String elementClass = null;
if (handler.getAnnotation(StubbedOccurrence.class) != null) {
final Type type = description.getType();
elementClass = ((Class) type).getName();
}
ElementStub stub = new ElementStub(parent, StringRef.fromString(tag.getName()), StringRef.fromNullableString(nsKey), index, description instanceof CustomDomChildrenDescription, elementClass == null ? null : StringRef.fromNullableString(elementClass), tag.getSubTags().length == 0 ? tag.getValue().getTrimmedText() : "");
for (XmlAttribute attribute : tag.getAttributes()) {
visitXmlElement(attribute, stub, 0);
}
Map<String, Integer> indices = new HashMap<>();
for (final XmlTag subTag : tag.getSubTags()) {
String name = subTag.getName();
Integer i = indices.get(name);
i = i == null ? 0 : i + 1;
visitXmlElement(subTag, stub, i);
indices.put(name, i);
}
} else if (element instanceof XmlAttribute) {
new AttributeStub(parent, StringRef.fromString(((XmlAttribute) element).getLocalName()), StringRef.fromNullableString(nsKey), ((XmlAttribute) element).getValue());
}
}
use of com.intellij.util.xml.reflect.CustomDomChildrenDescription in project intellij-community by JetBrains.
the class DomSemContributor method registerSemProviders.
@Override
public void registerSemProviders(SemRegistrar registrar) {
registrar.registerSemElementProvider(DomManagerImpl.FILE_DESCRIPTION_KEY, xmlFile(), xmlFile -> {
ApplicationManager.getApplication().assertReadAccessAllowed();
return new FileDescriptionCachedValueProvider(DomManagerImpl.getDomManager(xmlFile.getProject()), xmlFile);
});
registrar.registerSemElementProvider(DomManagerImpl.DOM_HANDLER_KEY, xmlTag().withParent(psiElement(XmlElementType.XML_DOCUMENT).withParent(xmlFile())), xmlTag -> {
final FileDescriptionCachedValueProvider provider = mySemService.getSemElement(DomManagerImpl.FILE_DESCRIPTION_KEY, xmlTag.getContainingFile());
assert provider != null;
final DomFileElementImpl element = provider.getFileElement();
if (element != null) {
final DomRootInvocationHandler handler = element.getRootHandler();
if (handler.getXmlTag() == xmlTag) {
return handler;
}
}
return null;
});
final ElementPattern<XmlTag> nonRootTag = xmlTag().withParent(or(xmlTag(), xmlEntityRef().withParent(xmlTag())));
registrar.registerSemElementProvider(DomManagerImpl.DOM_INDEXED_HANDLER_KEY, nonRootTag, tag -> {
final XmlTag parentTag = PhysicalDomParentStrategy.getParentTag(tag);
assert parentTag != null;
DomInvocationHandler parent = getParentDom(parentTag);
if (parent == null)
return null;
final String localName = tag.getLocalName();
final String namespace = tag.getNamespace();
final DomFixedChildDescription description = findChildrenDescription(parent.getGenericInfo().getFixedChildrenDescriptions(), tag, parent);
if (description != null) {
final int totalCount = description.getCount();
int index = 0;
PsiElement current = tag;
while (true) {
current = current.getPrevSibling();
if (current == null) {
break;
}
if (current instanceof XmlTag) {
final XmlTag xmlTag = (XmlTag) current;
if (localName.equals(xmlTag.getLocalName()) && namespace.equals(xmlTag.getNamespace())) {
index++;
if (index >= totalCount) {
return null;
}
}
}
}
final DomManagerImpl myDomManager = parent.getManager();
return new IndexedElementInvocationHandler(parent.createEvaluatedXmlName(description.getXmlName()), (FixedChildDescriptionImpl) description, index, new PhysicalDomParentStrategy(tag, myDomManager), myDomManager, null);
}
return null;
});
registrar.registerSemElementProvider(DomManagerImpl.DOM_COLLECTION_HANDLER_KEY, nonRootTag, tag -> {
final XmlTag parentTag = PhysicalDomParentStrategy.getParentTag(tag);
assert parentTag != null;
DomInvocationHandler parent = getParentDom(parentTag);
if (parent == null)
return null;
final DomCollectionChildDescription description = findChildrenDescription(parent.getGenericInfo().getCollectionChildrenDescriptions(), tag, parent);
if (description != null) {
DomStub parentStub = parent.getStub();
if (parentStub != null) {
int index = ArrayUtil.indexOf(parentTag.findSubTags(tag.getName(), tag.getNamespace()), tag);
ElementStub stub = parentStub.getElementStub(tag.getLocalName(), index);
if (stub != null) {
XmlName name = description.getXmlName();
EvaluatedXmlNameImpl evaluatedXmlName = EvaluatedXmlNameImpl.createEvaluatedXmlName(name, name.getNamespaceKey(), true);
return new CollectionElementInvocationHandler(evaluatedXmlName, (AbstractDomChildDescriptionImpl) description, parent.getManager(), stub);
}
}
return new CollectionElementInvocationHandler(description.getType(), tag, (AbstractCollectionChildDescription) description, parent, null);
}
return null;
});
registrar.registerSemElementProvider(DomManagerImpl.DOM_CUSTOM_HANDLER_KEY, nonRootTag, new NullableFunction<XmlTag, CollectionElementInvocationHandler>() {
private final RecursionGuard myGuard = RecursionManager.createGuard("customDomParent");
@Override
public CollectionElementInvocationHandler fun(XmlTag tag) {
if (StringUtil.isEmpty(tag.getName()))
return null;
final XmlTag parentTag = PhysicalDomParentStrategy.getParentTag(tag);
assert parentTag != null;
DomInvocationHandler parent = myGuard.doPreventingRecursion(tag, true, (NullableComputable<DomInvocationHandler>) () -> getParentDom(parentTag));
if (parent == null)
return null;
DomGenericInfoEx info = parent.getGenericInfo();
final List<? extends CustomDomChildrenDescription> customs = info.getCustomNameChildrenDescription();
if (customs.isEmpty())
return null;
if (mySemService.getSemElement(DomManagerImpl.DOM_INDEXED_HANDLER_KEY, tag) == null && mySemService.getSemElement(DomManagerImpl.DOM_COLLECTION_HANDLER_KEY, tag) == null) {
String localName = tag.getLocalName();
XmlFile file = parent.getFile();
for (final DomFixedChildDescription description : info.getFixedChildrenDescriptions()) {
XmlName xmlName = description.getXmlName();
if (localName.equals(xmlName.getLocalName()) && DomImplUtil.isNameSuitable(xmlName, tag, parent, file)) {
return null;
}
}
for (CustomDomChildrenDescription description : customs) {
if (description.getTagNameDescriptor() != null) {
AbstractCollectionChildDescription desc = (AbstractCollectionChildDescription) description;
Type type = description.getType();
return new CollectionElementInvocationHandler(type, tag, desc, parent, null);
}
}
}
return null;
}
});
registrar.registerSemElementProvider(DomManagerImpl.DOM_ATTRIBUTE_HANDLER_KEY, xmlAttribute(), attribute -> {
final XmlTag tag = PhysicalDomParentStrategy.getParentTag(attribute);
final DomInvocationHandler handler = tag == null ? null : getParentDom(tag);
if (handler == null)
return null;
final String localName = attribute.getLocalName();
final Ref<AttributeChildInvocationHandler> result = Ref.create(null);
handler.getGenericInfo().processAttributeChildrenDescriptions(description -> {
if (description.getXmlName().getLocalName().equals(localName)) {
final EvaluatedXmlName evaluatedXmlName = handler.createEvaluatedXmlName(description.getXmlName());
final String ns = evaluatedXmlName.getNamespace(tag, handler.getFile());
if (ns.equals(tag.getNamespace()) && localName.equals(attribute.getName()) || ns.equals(attribute.getNamespace())) {
final DomManagerImpl myDomManager = handler.getManager();
final AttributeChildInvocationHandler attributeHandler = new AttributeChildInvocationHandler(evaluatedXmlName, description, myDomManager, new PhysicalDomParentStrategy(attribute, myDomManager), null);
result.set(attributeHandler);
return false;
}
}
return true;
});
return result.get();
});
}
use of com.intellij.util.xml.reflect.CustomDomChildrenDescription in project intellij-community by JetBrains.
the class DomGenericInfoEx method findChildrenDescription.
@Nullable
public final AbstractDomChildrenDescription findChildrenDescription(DomInvocationHandler handler, final String localName, String namespace, boolean attribute, final String qName) {
for (final AbstractDomChildrenDescription description : getChildrenDescriptions()) {
if (description instanceof DomChildDescriptionImpl && description instanceof AttributeChildDescriptionImpl == attribute) {
final XmlName xmlName = ((DomChildDescriptionImpl) description).getXmlName();
if (attribute && StringUtil.isEmpty(namespace) && xmlName.getLocalName().equals(localName))
return description;
final EvaluatedXmlName evaluatedXmlName = handler.createEvaluatedXmlName(xmlName);
if (DomImplUtil.isNameSuitable(evaluatedXmlName, localName, qName, namespace, handler.getFile())) {
return description;
}
}
}
List<? extends CustomDomChildrenDescription> list = getCustomNameChildrenDescription();
for (CustomDomChildrenDescription description : list) {
if (attribute) {
// todo
} else if (description.getTagNameDescriptor() != null) {
return description;
}
}
return null;
}
Aggregations