Search in sources :

Example 1 with SmartPsiElementPointer

use of com.intellij.psi.SmartPsiElementPointer in project intellij-community by JetBrains.

the class DefaultAddAction method performElementAddition.

@Nullable
protected T performElementAddition() {
    final DomElement parent = getParentDomElement();
    final DomManager domManager = parent.getManager();
    final TypeChooser[] oldChoosers = new TypeChooser[] { null };
    final Type[] aClass = new Type[] { null };
    final StableElement<T> result = new WriteCommandAction<StableElement<T>>(domManager.getProject(), DomUtil.getFile(parent)) {

        @Override
        protected void run(@NotNull Result<StableElement<T>> result) throws Throwable {
            final DomElement parentDomElement = getParentDomElement();
            final T t = (T) getDomCollectionChildDescription().addValue(parentDomElement, getElementType());
            tuneNewValue(t);
            aClass[0] = parent.getGenericInfo().getCollectionChildDescription(t.getXmlElementName()).getType();
            oldChoosers[0] = domManager.getTypeChooserManager().getTypeChooser(aClass[0]);
            final SmartPsiElementPointer pointer = SmartPointerManager.getInstance(getProject()).createSmartPsiElementPointer(t.getXmlTag());
            domManager.getTypeChooserManager().registerTypeChooser(aClass[0], new TypeChooser() {

                @Override
                public Type chooseType(final XmlTag tag) {
                    if (tag == pointer.getElement()) {
                        return getElementType();
                    }
                    return oldChoosers[0].chooseType(tag);
                }

                @Override
                public void distinguishTag(final XmlTag tag, final Type aClass) throws IncorrectOperationException {
                    oldChoosers[0].distinguishTag(tag, aClass);
                }

                @Override
                public Type[] getChooserTypes() {
                    return oldChoosers[0].getChooserTypes();
                }
            });
            result.setResult((StableElement<T>) t.createStableCopy());
        }
    }.execute().getResultObject();
    if (result != null) {
        domManager.getTypeChooserManager().registerTypeChooser(aClass[0], oldChoosers[0]);
        return result.getWrappedElement();
    }
    return null;
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result) Type(java.lang.reflect.Type) SmartPsiElementPointer(com.intellij.psi.SmartPsiElementPointer) IncorrectOperationException(com.intellij.util.IncorrectOperationException) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with SmartPsiElementPointer

use of com.intellij.psi.SmartPsiElementPointer in project intellij-community by JetBrains.

the class InspectionResultsViewComparator method compareEntities.

private static int compareEntities(final RefEntity entity1, final RefEntity entity2) {
    if (entity1 instanceof RefElement && entity2 instanceof RefElement) {
        final SmartPsiElementPointer p1 = ((RefElement) entity1).getPointer();
        final SmartPsiElementPointer p2 = ((RefElement) entity2).getPointer();
        if (p1 != null && p2 != null) {
            final VirtualFile file1 = p1.getVirtualFile();
            final VirtualFile file2 = p2.getVirtualFile();
            if (file1 != null && Comparing.equal(file1, file2) && file1.isValid()) {
                final int positionComparing = PsiUtilCore.compareElementsByPosition(((RefElement) entity1).getElement(), ((RefElement) entity2).getElement());
                if (positionComparing != 0) {
                    return positionComparing;
                }
            }
        }
    }
    if (entity1 instanceof RefFile && entity2 instanceof RefFile) {
        final VirtualFile file1 = ((RefFile) entity1).getPointer().getVirtualFile();
        final VirtualFile file2 = ((RefFile) entity2).getPointer().getVirtualFile();
        if (file1 != null && file2 != null) {
            if (file1.equals(file2))
                return 0;
            final int cmp = compareEntitiesByName(entity1, entity2);
            if (cmp != 0)
                return cmp;
            return file1.hashCode() - file2.hashCode();
        }
    }
    if (entity1 != null && entity2 != null) {
        return compareEntitiesByName(entity1, entity2);
    }
    if (entity1 != null)
        return -1;
    return entity2 != null ? 1 : 0;
}
Also used : RefElement(com.intellij.codeInspection.reference.RefElement) VirtualFile(com.intellij.openapi.vfs.VirtualFile) SmartPsiElementPointer(com.intellij.psi.SmartPsiElementPointer) RefFile(com.intellij.codeInspection.reference.RefFile)

Example 3 with SmartPsiElementPointer

use of com.intellij.psi.SmartPsiElementPointer in project intellij-community by JetBrains.

the class InvertBooleanProcessor method performRefactoring.

@Override
protected void performRefactoring(@NotNull UsageInfo[] usages) {
    if (myRenameProcessor != null) {
        for (final PsiElement element : myRenameProcessor.getElements()) {
            try {
                RenameUtil.doRename(element, myRenameProcessor.getNewName(element), extractUsagesForElement(element, usages), myProject, null);
            } catch (final IncorrectOperationException e) {
                RenameUtil.showErrorMessage(e, element, myProject);
                return;
            }
        }
    }
    for (UsageInfo usage : usages) {
        final SmartPsiElementPointer pointerToInvert = myToInvert.get(usage);
        if (pointerToInvert != null) {
            PsiElement element = pointerToInvert.getElement();
            LOG.assertTrue(element != null);
            InvertBooleanDelegate delegate = InvertBooleanDelegate.findInvertBooleanDelegate(element);
            try {
                (delegate != null ? delegate : myDelegate).replaceWithNegatedExpression(element);
            } catch (IncorrectOperationException e) {
                LOG.error(e);
            }
        }
    }
    myDelegate.invertElementInitializer(myElement);
}
Also used : SmartPsiElementPointer(com.intellij.psi.SmartPsiElementPointer) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PsiElement(com.intellij.psi.PsiElement) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo)

Example 4 with SmartPsiElementPointer

use of com.intellij.psi.SmartPsiElementPointer in project intellij-community by JetBrains.

the class DocumentationComponent method setData.

public void setData(PsiElement _element, String text, final boolean clearHistory, String effectiveExternalUrl, String ref) {
    if (myElement != null) {
        myBackStack.push(saveContext());
        myForwardStack.clear();
    }
    myEffectiveExternalUrl = effectiveExternalUrl;
    final SmartPsiElementPointer element = _element != null && _element.isValid() ? SmartPointerManager.getInstance(_element.getProject()).createSmartPsiElementPointer(_element) : null;
    if (element != null) {
        setElement(element);
    }
    myIsEmpty = false;
    updateControlState();
    setDataInternal(element, text, new Rectangle(0, 0), ref);
    if (clearHistory)
        clearHistory();
}
Also used : SmartPsiElementPointer(com.intellij.psi.SmartPsiElementPointer)

Example 5 with SmartPsiElementPointer

use of com.intellij.psi.SmartPsiElementPointer in project intellij-community by JetBrains.

the class FavoritesPanel method setupDnD.

private void setupDnD() {
    DnDSupport.createBuilder(myTree).setBeanProvider(info -> {
        final TreePath path = myTree.getPathForLocation(info.getPoint().x, info.getPoint().y);
        if (path != null && path.getPathCount() == 3) {
            Object o = path.getLastPathComponent();
            if (o instanceof DefaultMutableTreeNode) {
                o = ((DefaultMutableTreeNode) o).getUserObject();
                if (o instanceof FavoritesTreeNodeDescriptor) {
                    FavoritesTreeNodeDescriptor root = ((FavoritesTreeNodeDescriptor) o).getFavoritesRoot();
                    if (root != null && root != o) {
                        o = root.getElement();
                        if (o instanceof FavoritesListNode && ((FavoritesListNode) o).getProvider() == null) {
                            return new DnDDragStartBean(path);
                        }
                    }
                }
            }
        }
        if (path != null && path.getPathCount() == 2) {
            Object o = path.getLastPathComponent();
            if (o instanceof DefaultMutableTreeNode) {
                o = ((DefaultMutableTreeNode) o).getUserObject();
                if (o instanceof FavoritesTreeNodeDescriptor) {
                    FavoritesTreeNodeDescriptor root = ((FavoritesTreeNodeDescriptor) o).getFavoritesRoot();
                    if (root == o) {
                        return new DnDDragStartBean(path);
                    }
                }
            }
        }
        return new DnDDragStartBean("") {

            @Override
            public boolean isEmpty() {
                return true;
            }
        };
    }).setTargetChecker(new DnDTargetChecker() {

        @Override
        public boolean update(DnDEvent event) {
            final Object obj = event.getAttachedObject();
            if ("".equals(obj)) {
                event.setDropPossible(false);
                return false;
            }
            int pathCount = 0;
            if (obj instanceof TreePath) {
                pathCount = ((TreePath) obj).getPathCount();
                if (pathCount < 2) {
                    event.setDropPossible(false);
                    return true;
                }
            }
            FavoritesListNode node = myViewPanel.findFavoritesListNode(event.getPoint());
            if ((obj instanceof TreePath && pathCount == 3 && myViewPanel.myTree.getPath(node).isDescendant((TreePath) obj)) || (node != null && node.getProvider() != null)) {
                event.setDropPossible(false);
                return false;
            }
            highlight(node, event);
            if (obj instanceof TreePath && pathCount == 2 && node != null && node.getProvider() == null) {
                event.setDropPossible(true);
                return true;
            }
            if (node != null) {
                event.setDropPossible(true);
                return true;
            }
            event.setDropPossible(false);
            return false;
        }
    }).setDropHandler(new DnDDropHandler() {

        @Override
        public void drop(DnDEvent event) {
            final FavoritesListNode node = myViewPanel.findFavoritesListNode(event.getPoint());
            final FavoritesManager mgr = FavoritesManager.getInstance(myProject);
            if (node == null)
                return;
            final String listTo = node.getValue();
            final Object obj = event.getAttachedObject();
            if (obj instanceof TreePath) {
                final TreePath path = (TreePath) obj;
                final String listFrom = FavoritesTreeViewPanel.getListNodeFromPath(path).getValue();
                if (listTo.equals(listFrom))
                    return;
                if (path.getPathCount() == 3) {
                    final AbstractTreeNode abstractTreeNode = ((FavoritesTreeNodeDescriptor) ((DefaultMutableTreeNode) path.getLastPathComponent()).getUserObject()).getElement();
                    Object element = abstractTreeNode.getValue();
                    mgr.removeRoot(listFrom, Collections.singletonList(abstractTreeNode));
                    if (element instanceof SmartPsiElementPointer) {
                        element = ((SmartPsiElementPointer) element).getElement();
                    }
                    mgr.addRoots(listTo, null, element);
                }
                if (path.getPathCount() == 2) {
                    //favorites lists manual sorting
                    Rectangle bounds = myTree.getPathBounds(myTree.getPath(node));
                    if (bounds != null) {
                        mgr.setOrder(listFrom, listTo, event.getPoint().y < bounds.y + bounds.height / 2);
                    }
                }
            } else if (obj instanceof TransferableWrapper) {
                myViewPanel.dropPsiElements(mgr, node, ((TransferableWrapper) obj).getPsiElements());
            } else if (obj instanceof DnDNativeTarget.EventInfo) {
                myViewPanel.dropPsiElements(mgr, node, getPsiFiles(FileCopyPasteUtil.getFileList(((DnDNativeTarget.EventInfo) obj).getTransferable())));
            }
        }
    }).setImageProvider(info -> new DnDImage(myFavoritesImage, new Point(-myFavoritesImage.getWidth(null) / 2, -myFavoritesImage.getHeight(null) / 2))).enableAsNativeTarget().setDisposableParent(myProject).install();
}
Also used : AllIcons(com.intellij.icons.AllIcons) TreePath(javax.swing.tree.TreePath) VirtualFile(com.intellij.openapi.vfs.VirtualFile) AbstractTreeBuilder(com.intellij.ide.util.treeView.AbstractTreeBuilder) DnDAwareTree(com.intellij.ide.dnd.aware.DnDAwareTree) LocalFileSystem(com.intellij.openapi.vfs.LocalFileSystem) File(java.io.File) PsiManager(com.intellij.psi.PsiManager) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) ArrayList(java.util.ArrayList) java.awt(java.awt) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) RelativeRectangle(com.intellij.ui.awt.RelativeRectangle) SmartPsiElementPointer(com.intellij.psi.SmartPsiElementPointer) Disposer(com.intellij.openapi.util.Disposer) Project(com.intellij.openapi.project.Project) com.intellij.ide.dnd(com.intellij.ide.dnd) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) Collections(java.util.Collections) IconUtil(com.intellij.util.IconUtil) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) RelativeRectangle(com.intellij.ui.awt.RelativeRectangle) AbstractTreeNode(com.intellij.ide.util.treeView.AbstractTreeNode) TreePath(javax.swing.tree.TreePath) SmartPsiElementPointer(com.intellij.psi.SmartPsiElementPointer)

Aggregations

SmartPsiElementPointer (com.intellij.psi.SmartPsiElementPointer)14 PsiElement (com.intellij.psi.PsiElement)7 Project (com.intellij.openapi.project.Project)4 AbstractTreeNode (com.intellij.ide.util.treeView.AbstractTreeNode)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 ArrayList (java.util.ArrayList)3 NotNull (org.jetbrains.annotations.NotNull)3 Nullable (org.jetbrains.annotations.Nullable)3 PsiDirectory (com.intellij.psi.PsiDirectory)2 PsiFile (com.intellij.psi.PsiFile)2 MoveRenameUsageInfo (com.intellij.refactoring.util.MoveRenameUsageInfo)2 UsageInfo (com.intellij.usageView.UsageInfo)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 File (java.io.File)2 List (java.util.List)2 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)2 RefElement (com.intellij.codeInspection.reference.RefElement)1 RefFile (com.intellij.codeInspection.reference.RefFile)1 DiagramNodeContainer (com.intellij.diagram.components.DiagramNodeContainer)1 DiagramExtras (com.intellij.diagram.extras.DiagramExtras)1