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;
}
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;
}
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);
}
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();
}
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();
}
Aggregations