public static void doActualPsiChange(@NotNull final PsiFile file, final DiffLog diffLog) {
    file.getViewProvider().beforeContentsSynchronized();

    try {
      final Document document = file.getViewProvider().getDocument();
      PsiDocumentManagerImpl documentManager =
          (PsiDocumentManagerImpl) PsiDocumentManager.getInstance(file.getProject());
      PsiToDocumentSynchronizer.DocumentChangeTransaction transaction =
          documentManager.getSynchronizer().getTransaction(document);

      final PsiFileImpl fileImpl = (PsiFileImpl) file;

      if (transaction == null) {
        final PomModel model = PomManager.getModel(fileImpl.getProject());

        model.runTransaction(
            new PomTransactionBase(fileImpl, model.getModelAspect(TreeAspect.class)) {
              @Override
              public PomModelEvent runInner() {
                return new TreeAspectEvent(model, diffLog.performActualPsiChange(file));
              }
            });
      } else {
        diffLog.performActualPsiChange(file);
      }
    } catch (IncorrectOperationException e) {
      LOG.error(e);
    }
  }
Esempio n. 2
0
  public PsiElement setName(@NotNull final String name) throws IncorrectOperationException {
    final PomModel model = PomManager.getModel(getProject());
    final XmlAspect aspect = model.getModelAspect(XmlAspect.class);
    model.runTransaction(
        new PomTransactionBase(this, aspect) {
          public PomModelEvent runInner() throws IncorrectOperationException {
            final String oldName = getName();
            final XmlTagImpl dummyTag =
                (XmlTagImpl)
                    XmlElementFactory.getInstance(getProject())
                        .createTagFromText(XmlTagUtil.composeTagText(name, "aa"));
            final XmlTagImpl tag = XmlTagImpl.this;
            final CharTable charTableByTree = SharedImplUtil.findCharTableByTree(tag);
            ASTNode child = XmlChildRole.START_TAG_NAME_FINDER.findChild(tag);
            LOG.assertTrue(child != null, "It seems '" + name + "' is not a valid tag name");
            TreeElement tagElement =
                (TreeElement) XmlChildRole.START_TAG_NAME_FINDER.findChild(dummyTag);
            LOG.assertTrue(tagElement != null, "What's wrong with it? '" + name + "'");
            tag.replaceChild(child, ChangeUtil.copyElement(tagElement, charTableByTree));
            final ASTNode childByRole = XmlChildRole.CLOSING_TAG_NAME_FINDER.findChild(tag);
            if (childByRole != null) {
              final TreeElement treeElement =
                  (TreeElement) XmlChildRole.CLOSING_TAG_NAME_FINDER.findChild(dummyTag);
              if (treeElement != null) {
                tag.replaceChild(childByRole, ChangeUtil.copyElement(treeElement, charTableByTree));
              }
            }

            return XmlTagNameChangedImpl.createXmlTagNameChanged(model, tag, oldName);
          }
        });
    return this;
  }
Esempio n. 3
0
  public void collapseIfEmpty() {
    final XmlTag[] tags = getSubTags();
    if (tags.length > 0) {
      return;
    }
    final ASTNode closingName = XmlChildRole.CLOSING_TAG_NAME_FINDER.findChild(this);
    final ASTNode startTagEnd = XmlChildRole.START_TAG_END_FINDER.findChild(this);
    if (closingName == null || startTagEnd == null) {
      return;
    }

    final PomModel pomModel = PomManager.getModel(getProject());
    final PomTransactionBase transaction =
        new PomTransactionBase(this, pomModel.getModelAspect(XmlAspect.class)) {

          @Nullable
          public PomModelEvent runInner() {
            final ASTNode closingBracket = closingName.getTreeNext();
            removeRange(startTagEnd, closingBracket);
            final LeafElement emptyTagEnd =
                Factory.createSingleLeafElement(
                    XmlTokenType.XML_EMPTY_ELEMENT_END, "/>", 0, 2, null, getManager());
            replaceChild(closingBracket, emptyTagEnd);
            return null;
          }
        };
    try {
      pomModel.runTransaction(transaction);
    } catch (IncorrectOperationException e) {
      LOG.error(e);
    }
  }
 public void deleteChildInternal(@NotNull final ASTNode child) {
   final PomModel model = PomManager.getModel(getProject());
   final XmlAspect aspect = model.getModelAspect(XmlAspect.class);
   try {
     model.runTransaction(
         new PomTransactionBase(this, aspect) {
           public PomModelEvent runInner() {
             XmlDocumentImpl.super.deleteChildInternal(child);
             return XmlDocumentChangedImpl.createXmlDocumentChanged(model, XmlDocumentImpl.this);
           }
         });
   } catch (IncorrectOperationException ignored) {
   }
 }
 public TreeElement addInternal(
     final TreeElement first, final ASTNode last, final ASTNode anchor, final Boolean before) {
   final PomModel model = PomManager.getModel(getProject());
   final XmlAspect aspect = model.getModelAspect(XmlAspect.class);
   final TreeElement[] holder = new TreeElement[1];
   try {
     model.runTransaction(
         new PomTransactionBase(this, aspect) {
           public PomModelEvent runInner() {
             holder[0] = XmlDocumentImpl.super.addInternal(first, last, anchor, before);
             return XmlDocumentChangedImpl.createXmlDocumentChanged(model, XmlDocumentImpl.this);
           }
         });
   } catch (IncorrectOperationException ignored) {
   }
   return holder[0];
 }
Esempio n. 6
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);
        }
      }
    }
  }