示例#1
0
  private TreeElement addInternal(TreeElement child, ASTNode anchor, boolean before)
      throws IncorrectOperationException {
    final PomModel model = PomManager.getModel(getProject());
    if (anchor != null && child.getElementType() == XmlElementType.XML_TEXT) {
      XmlText psi = null;
      if (anchor.getPsi() instanceof XmlText) {
        psi = (XmlText) anchor.getPsi();
      } else {
        final ASTNode other = before ? anchor.getTreePrev() : anchor.getTreeNext();
        if (other != null && other.getPsi() instanceof XmlText) {
          before = !before;
          psi = (XmlText) other.getPsi();
        }
      }

      if (psi != null) {
        if (before) {
          psi.insertText(((XmlText) child.getPsi()).getValue(), 0);
        } else {
          psi.insertText(((XmlText) child.getPsi()).getValue(), psi.getValue().length());
        }
        return (TreeElement) psi.getNode();
      }
    }
    LOG.assertTrue(child.getPsi() instanceof XmlAttribute || child.getPsi() instanceof XmlTagChild);
    final InsertTransaction transaction;
    if (child.getElementType() == XmlElementType.XML_ATTRIBUTE) {
      transaction = new InsertAttributeTransaction(child, anchor, before, model);
    } else if (anchor == null) {
      transaction = getBodyInsertTransaction(child);
    } else {
      transaction = new GenericInsertTransaction(child, anchor, before);
    }
    model.runTransaction(transaction);
    return transaction.getFirstInserted();
  }
示例#2
0
  public void deleteChildInternal(@NotNull final ASTNode child) {
    final PomModel model = PomManager.getModel(getProject());
    final XmlAspect aspect = model.getModelAspect(XmlAspect.class);

    if (child.getElementType() == XmlElementType.XML_ATTRIBUTE) {
      try {
        model.runTransaction(
            new PomTransactionBase(this, aspect) {
              public PomModelEvent runInner() {
                final String name = ((XmlAttribute) child).getName();
                XmlTagImpl.super.deleteChildInternal(child);
                return XmlAttributeSetImpl.createXmlAttributeSet(
                    model, XmlTagImpl.this, name, null);
              }
            });
      } catch (IncorrectOperationException e) {
        LOG.error(e);
      }
    } else {
      final ASTNode treePrev = child.getTreePrev();
      final ASTNode treeNext = child.getTreeNext();
      XmlTagImpl.super.deleteChildInternal(child);
      if (treePrev != null
          && treeNext != null
          && treePrev.getElementType() == XmlElementType.XML_TEXT
          && treeNext.getElementType() == XmlElementType.XML_TEXT) {
        final XmlText prevText = (XmlText) treePrev.getPsi();
        final XmlText nextText = (XmlText) treeNext.getPsi();
        try {
          prevText.setValue(prevText.getValue() + nextText.getValue());
          nextText.delete();
        } catch (IncorrectOperationException e) {
          LOG.error(e);
        }
      }
    }
  }