Пример #1
0
 public static <T extends DomElement> T addElementAfter(@NotNull final T anchor) {
   final DomElement parent = anchor.getParent();
   final DomCollectionChildDescription childDescription =
       (DomCollectionChildDescription) anchor.getChildDescription();
   assert parent != null;
   final List<? extends DomElement> list = childDescription.getValues(parent);
   final int i = list.indexOf(anchor);
   assert i >= 0;
   return (T) childDescription.addValue(parent, i + 1);
 }
Пример #2
0
 @Nullable
 public static <T extends DomElement> T findByName(
     @NotNull Collection<T> list, @NonNls @NotNull String name) {
   for (T element : list) {
     String elementName = element.getGenericInfo().getElementName(element);
     if (elementName != null && elementName.equals(name)) {
       return element;
     }
   }
   return null;
 }
Пример #3
0
 @SuppressWarnings("ForLoopReplaceableByForEach")
 public static <T extends DomElement> List<T> getChildrenOf(
     DomElement parent, final Class<T> type) {
   final List<T> list = new SmartList<>();
   List<? extends AbstractDomChildrenDescription> descriptions =
       parent.getGenericInfo().getChildrenDescriptions();
   for (int i = 0, descriptionsSize = descriptions.size(); i < descriptionsSize; i++) {
     AbstractDomChildrenDescription description = descriptions.get(i);
     if (description.getType() instanceof Class
         && type.isAssignableFrom((Class<?>) description.getType())) {
       List<T> values = (List<T>) description.getValues(parent);
       for (int j = 0, valuesSize = values.size(); j < valuesSize; j++) {
         T value = values.get(j);
         if (value.exists()) {
           list.add(value);
         }
       }
     }
   }
   return list;
 }
Пример #4
0
  @NotNull
  public static <T extends DomElement> T getOriginalElement(@NotNull final T domElement) {
    final XmlElement psiElement = domElement.getXmlElement();
    if (psiElement == null) return domElement;

    final PsiFile psiFile = psiElement.getContainingFile().getOriginalFile();
    final TextRange range = psiElement.getTextRange();
    final PsiElement element = psiFile.findElementAt(range.getStartOffset());
    final int maxLength = range.getLength();
    final boolean isAttribute = psiElement instanceof XmlAttribute;
    final Class<? extends XmlElement> clazz = isAttribute ? XmlAttribute.class : XmlTag.class;
    final DomManager domManager = domElement.getManager();
    DomElement current = null;
    for (XmlElement next = PsiTreeUtil.getParentOfType(element, clazz, false);
        next != null && next.getTextLength() <= maxLength;
        next = PsiTreeUtil.getParentOfType(next, clazz, true)) {
      current =
          isAttribute
              ? domManager.getDomElement((XmlAttribute) next)
              : domManager.getDomElement((XmlTag) next);
      if (current != null && domElement.getClass() != current.getClass()) current = null;
    }
    return (T) current;
  }