Java 类com.intellij.psi.impl.source.tree.ChangeUtil 实例源码

项目:intellij-ce-playground    文件:PropertiesFileImpl.java   
@Override
@NotNull
public PsiElement addPropertyAfter(@NotNull final IProperty property, @Nullable final IProperty anchor) throws IncorrectOperationException {
  final TreeElement copy = ChangeUtil.copyToElement(property.getPsiElement());
  List<IProperty> properties = getProperties();
  ASTNode anchorBefore = anchor == null ? properties.isEmpty() ? null : properties.get(0).getPsiElement().getNode()
                         : anchor.getPsiElement().getNode().getTreeNext();
  if (anchorBefore != null) {
    if (anchorBefore.getElementType() == TokenType.WHITE_SPACE) {
      anchorBefore = anchorBefore.getTreeNext();
    }
  }
  if (anchorBefore == null && haveToAddNewLine()) {
    insertLineBreakBefore(null);
  }
  getPropertiesList().addChild(copy, anchorBefore);
  if (anchorBefore != null) {
    insertLineBreakBefore(anchorBefore);
  }
  return copy.getPsi();
}
项目:tools-idea    文件:PropertiesFileImpl.java   
@Override
@NotNull
public PsiElement addPropertyAfter(@NotNull final Property property, @Nullable final Property anchor) throws IncorrectOperationException {
  final TreeElement copy = ChangeUtil.copyToElement(property);
  List<IProperty> properties = getProperties();
  ASTNode anchorBefore = anchor == null ? properties.isEmpty() ? null : properties.get(0).getPsiElement().getNode()
                         : anchor.getNode().getTreeNext();
  if (anchorBefore != null) {
    if (anchorBefore.getElementType() == TokenType.WHITE_SPACE) {
      anchorBefore = anchorBefore.getTreeNext();
    }
  }
  if (anchorBefore == null && haveToAddNewLine()) {
    insertLineBreakBefore(null);
  }
  getPropertiesList().addChild(copy, anchorBefore);
  if (anchorBefore != null) {
    insertLineBreakBefore(anchorBefore);
  }
  return copy.getPsi();
}
项目:intellij-ce-playground    文件:JavaStubPsiElement.java   
@Override
public PsiElement add(@NotNull PsiElement element) throws IncorrectOperationException {
  CheckUtil.checkWritable(this);
  TreeElement elementCopy = ChangeUtil.copyToElement(element);
  calcTreeElement().addInternal(elementCopy, elementCopy, null, null);
  elementCopy = ChangeUtil.decodeInformation(elementCopy);
  return SourceTreeToPsiMap.treeElementToPsi(elementCopy);
}
项目:intellij-ce-playground    文件:JavaStubPsiElement.java   
@Override
public PsiElement addBefore(@NotNull PsiElement element, PsiElement anchor) throws IncorrectOperationException {
  CheckUtil.checkWritable(this);
  TreeElement elementCopy = ChangeUtil.copyToElement(element);
  calcTreeElement().addInternal(elementCopy, elementCopy, SourceTreeToPsiMap.psiElementToTree(anchor), Boolean.TRUE);
  elementCopy = ChangeUtil.decodeInformation(elementCopy);
  return SourceTreeToPsiMap.treeElementToPsi(elementCopy);
}
项目:intellij-ce-playground    文件:JavaStubPsiElement.java   
@Override
public PsiElement addAfter(@NotNull PsiElement element, @Nullable PsiElement anchor) throws IncorrectOperationException {
  CheckUtil.checkWritable(this);
  TreeElement elementCopy = ChangeUtil.copyToElement(element);
  calcTreeElement().addInternal(elementCopy, elementCopy, SourceTreeToPsiMap.psiElementToTree(anchor), Boolean.FALSE);
  elementCopy = ChangeUtil.decodeInformation(elementCopy);
  return SourceTreeToPsiMap.treeElementToPsi(elementCopy);
}
项目:intellij-ce-playground    文件:ASTDelegatePsiElement.java   
private PsiElement addInnerBefore(final PsiElement element, final PsiElement anchor) throws IncorrectOperationException {
  CheckUtil.checkWritable(this);
  TreeElement elementCopy = ChangeUtil.copyToElement(element);
  ASTNode treeElement = addInternal(elementCopy, elementCopy, SourceTreeToPsiMap.psiElementToTree(anchor), Boolean.TRUE);
  if (treeElement != null) {
    if (treeElement instanceof TreeElement) {
      return ChangeUtil.decodeInformation((TreeElement) treeElement).getPsi();
    }
    return treeElement.getPsi();
  }
  throw new IncorrectOperationException("Element cannot be added");
}
项目:intellij-ce-playground    文件:ASTDelegatePsiElement.java   
@Override
public PsiElement addAfter(@NotNull PsiElement element, PsiElement anchor) throws IncorrectOperationException {
  CheckUtil.checkWritable(this);
  TreeElement elementCopy = ChangeUtil.copyToElement(element);
  ASTNode treeElement = addInternal(elementCopy, elementCopy, SourceTreeToPsiMap.psiElementToTree(anchor), Boolean.FALSE);
  if (treeElement instanceof TreeElement) {
    return ChangeUtil.decodeInformation((TreeElement) treeElement).getPsi();
  }
  return treeElement.getPsi();
}
项目:intellij-ce-playground    文件:ASTDelegatePsiElement.java   
@Override
public PsiElement replace(@NotNull final PsiElement newElement) throws IncorrectOperationException {
  CheckUtil.checkWritable(this);
  TreeElement elementCopy = ChangeUtil.copyToElement(newElement);
  if (getParent() instanceof ASTDelegatePsiElement) {
    final ASTDelegatePsiElement parentElement = (ASTDelegatePsiElement)getParent();
    parentElement.replaceChildInternal(this, elementCopy);
  }
  else {
    CodeEditUtil.replaceChild(getParent().getNode(), getNode(), elementCopy);
  }
  elementCopy = ChangeUtil.decodeInformation(elementCopy);
  return SourceTreeToPsiMap.treeElementToPsi(elementCopy);
}
项目:intellij-ce-playground    文件:PsiDirectoryImpl.java   
private static void updateAddedFile(@NotNull PsiFile copyPsi) throws IncorrectOperationException {
  final UpdateAddedFileProcessor processor = UpdateAddedFileProcessor.forElement(copyPsi);
  if (processor != null) {
    final TreeElement tree = (TreeElement)SourceTreeToPsiMap.psiElementToTree(copyPsi);
    if (tree != null) {
      ChangeUtil.encodeInformation(tree);
    }
    processor.update(copyPsi, null);
    if (tree != null) {
      ChangeUtil.decodeInformation(tree);
    }
  }
}
项目:sqlitemagic    文件:SqliteMagicLightMethodGenerator.java   
@Nullable
@Override
public TreeElement generateTreeFor(PsiElement original, CharTable table, PsiManager manager) {
  TreeElement result = null;
  if (original instanceof SqliteMagicLightMethodBuilder) {
    result = ChangeUtil.copyElement((TreeElement) SourceTreeToPsiMap.psiElementToTree(original), table);
  }
  return result;
}
项目:tools-idea    文件:JavaStubPsiElement.java   
@Override
public PsiElement add(@NotNull PsiElement element) throws IncorrectOperationException {
  CheckUtil.checkWritable(this);
  TreeElement elementCopy = ChangeUtil.copyToElement(element);
  calcTreeElement().addInternal(elementCopy, elementCopy, null, null);
  elementCopy = ChangeUtil.decodeInformation(elementCopy);
  return SourceTreeToPsiMap.treeElementToPsi(elementCopy);
}
项目:tools-idea    文件:JavaStubPsiElement.java   
@Override
public PsiElement addBefore(@NotNull PsiElement element, PsiElement anchor) throws IncorrectOperationException {
  CheckUtil.checkWritable(this);
  TreeElement elementCopy = ChangeUtil.copyToElement(element);
  calcTreeElement().addInternal(elementCopy, elementCopy, SourceTreeToPsiMap.psiElementToTree(anchor), Boolean.TRUE);
  elementCopy = ChangeUtil.decodeInformation(elementCopy);
  return SourceTreeToPsiMap.treeElementToPsi(elementCopy);
}
项目:tools-idea    文件:JavaStubPsiElement.java   
@Override
public PsiElement addAfter(@NotNull PsiElement element, @Nullable PsiElement anchor) throws IncorrectOperationException {
  CheckUtil.checkWritable(this);
  TreeElement elementCopy = ChangeUtil.copyToElement(element);
  calcTreeElement().addInternal(elementCopy, elementCopy, SourceTreeToPsiMap.psiElementToTree(anchor), Boolean.FALSE);
  elementCopy = ChangeUtil.decodeInformation(elementCopy);
  return SourceTreeToPsiMap.treeElementToPsi(elementCopy);
}
项目:tools-idea    文件:PsiDirectoryImpl.java   
private static void updateAddedFile(PsiFile copyPsi) throws IncorrectOperationException {
  final UpdateAddedFileProcessor processor = UpdateAddedFileProcessor.forElement(copyPsi);
  if (processor != null) {
    final TreeElement tree = (TreeElement)SourceTreeToPsiMap.psiElementToTree(copyPsi);
    if (tree != null) {
      ChangeUtil.encodeInformation(tree);
    }
    processor.update(copyPsi, null);
    if (tree != null) {
      ChangeUtil.decodeInformation(tree);
    }
  }
}
项目:tools-idea    文件:PropertiesFileImpl.java   
@Override
@NotNull
public PsiElement addProperty(@NotNull IProperty property) throws IncorrectOperationException {
  if (haveToAddNewLine()) {
    insertLineBreakBefore(null);
  }
  final TreeElement copy = ChangeUtil.copyToElement(property.getPsiElement());
  getPropertiesList().addChild(copy);
  return copy.getPsi();
}
项目:lombok-intellij-plugin    文件:LombokLightMethodTreeGenerator.java   
@Nullable
public TreeElement generateTreeFor(PsiElement original, CharTable table, PsiManager manager) {
  TreeElement result = null;
  if (original instanceof LombokLightMethodBuilder) {
    result = ChangeUtil.copyElement((TreeElement) SourceTreeToPsiMap.psiElementToTree(original), table);
  }
  return result;
}
项目:consulo    文件:PsiDirectoryImpl.java   
private static void updateAddedFile(PsiFile copyPsi) throws IncorrectOperationException {
  final UpdateAddedFileProcessor processor = UpdateAddedFileProcessor.forElement(copyPsi);
  if (processor != null) {
    final TreeElement tree = (TreeElement)SourceTreeToPsiMap.psiElementToTree(copyPsi);
    if (tree != null) {
      ChangeUtil.encodeInformation(tree);
    }
    processor.update(copyPsi, null);
    if (tree != null) {
      ChangeUtil.decodeInformation(tree);
    }
  }
}
项目:consulo-xml    文件:XmlTagImpl.java   
@Override
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)
    {
        @Override
        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;
}
项目:consulo-java    文件:JavaStubPsiElement.java   
@Override
public PsiElement add(@NotNull PsiElement element) throws IncorrectOperationException
{
    CheckUtil.checkWritable(this);
    TreeElement elementCopy = ChangeUtil.copyToElement(element);
    calcTreeElement().addInternal(elementCopy, elementCopy, null, null);
    elementCopy = ChangeUtil.decodeInformation(elementCopy);
    return SourceTreeToPsiMap.treeElementToPsi(elementCopy);
}
项目:consulo-java    文件:JavaStubPsiElement.java   
@Override
public PsiElement addBefore(@NotNull PsiElement element, PsiElement anchor) throws IncorrectOperationException
{
    CheckUtil.checkWritable(this);
    TreeElement elementCopy = ChangeUtil.copyToElement(element);
    calcTreeElement().addInternal(elementCopy, elementCopy, SourceTreeToPsiMap.psiElementToTree(anchor), Boolean.TRUE);
    elementCopy = ChangeUtil.decodeInformation(elementCopy);
    return SourceTreeToPsiMap.treeElementToPsi(elementCopy);
}
项目:consulo-java    文件:JavaStubPsiElement.java   
@Override
public PsiElement addAfter(@NotNull PsiElement element, @Nullable PsiElement anchor) throws IncorrectOperationException
{
    CheckUtil.checkWritable(this);
    TreeElement elementCopy = ChangeUtil.copyToElement(element);
    calcTreeElement().addInternal(elementCopy, elementCopy, SourceTreeToPsiMap.psiElementToTree(anchor), Boolean.FALSE);
    elementCopy = ChangeUtil.decodeInformation(elementCopy);
    return SourceTreeToPsiMap.treeElementToPsi(elementCopy);
}
项目:consulo-xml    文件:XmlTagImpl.java   
@Override
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)
            {
                @Override
                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();
        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();

            final String newValue = prevText.getValue() + nextText.getValue();

            // merging two XmlText-s should be done in one transaction to preserve smart pointers
            ChangeUtil.prepareAndRunChangeAction(new ChangeUtil.ChangeAction()
            {
                @Override
                public void makeChange(TreeChangeEvent destinationTreeChange)
                {
                    PsiElement anchor = prevText.getPrevSibling();
                    prevText.delete();
                    nextText.delete();
                    XmlText text = (XmlText) addAfter(XmlElementFactory.getInstance(getProject()).createDisplayText("x"), anchor);
                    text.setValue(newValue);
                }
            }, this);

        }
    }
}