public static <T> void replaceInjectionsWithUndo( final Project project, final T add, final T remove, final List<? extends PsiElement> psiElementsToRemove, final PairProcessor<T, T> actualProcessor) { final UndoableAction action = new GlobalUndoableAction() { public void undo() { actualProcessor.process(remove, add); } public void redo() { actualProcessor.process(add, remove); } }; final List<PsiFile> psiFiles = ContainerUtil.mapNotNull( psiElementsToRemove, new NullableFunction<PsiElement, PsiFile>() { public PsiFile fun(final PsiElement psiAnnotation) { return psiAnnotation instanceof PsiCompiledElement ? null : psiAnnotation.getContainingFile(); } }); new WriteCommandAction.Simple( project, "Language Injection Configuration Update", PsiUtilCore.toPsiFileArray(psiFiles)) { public void run() { for (PsiElement annotation : psiElementsToRemove) { annotation.delete(); } actualProcessor.process(add, remove); UndoManager.getInstance(project).undoableActionPerformed(action); } @Override protected UndoConfirmationPolicy getUndoConfirmationPolicy() { return UndoConfirmationPolicy.REQUEST_CONFIRMATION; } }.execute(); }
public PsiFile[] getRelatedFiles(final XPathFile file) { final XmlAttribute attribute = PsiTreeUtil.getContextOfType(file, XmlAttribute.class, false); assert attribute != null; final PsiFile psiFile = attribute.getContainingFile(); assert psiFile != null; final List<PsiFile> files = new ArrayList<PsiFile>(); psiFile.accept( new XmlRecursiveElementVisitor() { @Override public void visitXmlAttribute(XmlAttribute attribute) { final PsiFile[] _files = XsltSupport.getFiles(attribute); for (PsiFile _file : _files) { if (_file != file) files.add(_file); } } }); return PsiUtilCore.toPsiFileArray(files); }
public void doCopy(PsiElement[] elements, PsiDirectory defaultTargetDirectory) { FeatureUsageTracker.getInstance().triggerFeatureUsed("refactoring.copyClass"); final HashMap<PsiFile, String> relativePathsMap = new HashMap<PsiFile, String>(); final Map<PsiFile, PsiClass[]> classes = convertToTopLevelClasses(elements, false, "", relativePathsMap); assert classes != null; if (defaultTargetDirectory == null) { final PsiFile psiFile = classes.keySet().iterator().next(); defaultTargetDirectory = psiFile.getContainingDirectory(); LOG.assertTrue(defaultTargetDirectory != null, psiFile); } else { Project project = defaultTargetDirectory.getProject(); VirtualFile sourceRootForFile = ProjectRootManager.getInstance(project) .getFileIndex() .getSourceRootForFile(defaultTargetDirectory.getVirtualFile()); if (sourceRootForFile == null) { final List<PsiElement> files = new ArrayList<PsiElement>(); for (int i = 0, elementsLength = elements.length; i < elementsLength; i++) { PsiFile containingFile = elements[i].getContainingFile(); if (containingFile != null) { files.add(containingFile); } else if (elements[i] instanceof PsiDirectory) { files.add(elements[i]); } } CopyFilesOrDirectoriesHandler.copyAsFiles( files.toArray(new PsiElement[files.size()]), defaultTargetDirectory, project); return; } } Project project = defaultTargetDirectory.getProject(); Object targetDirectory = null; String className = null; boolean openInEditor = true; if (copyOneClass(classes)) { final String commonPath = ArrayUtil.find(elements, classes.values().iterator().next()) == -1 ? normalizeRelativeMap(relativePathsMap) : null; CopyClassDialog dialog = new CopyClassDialog( classes.values().iterator().next()[0], defaultTargetDirectory, project, false) { @Override protected String getQualifiedName() { if (commonPath != null && !commonPath.isEmpty()) { return StringUtil.getQualifiedName( super.getQualifiedName(), commonPath.replaceAll("/", ".")); } return super.getQualifiedName(); } }; dialog.setTitle(RefactoringBundle.message("copy.handler.copy.class")); dialog.show(); if (dialog.isOK()) { openInEditor = dialog.openInEditor(); targetDirectory = dialog.getTargetDirectory(); className = dialog.getClassName(); if (className == null || className.length() == 0) return; } } else { if (ApplicationManager.getApplication().isUnitTestMode()) { targetDirectory = defaultTargetDirectory; } else { defaultTargetDirectory = CopyFilesOrDirectoriesHandler.resolveDirectory(defaultTargetDirectory); if (defaultTargetDirectory == null) return; PsiElement[] files = PsiUtilCore.toPsiFileArray(classes.keySet()); if (classes.keySet().size() == 1) { // do not choose a new name for a file when multiple classes exist in one file final PsiClass[] psiClasses = classes.values().iterator().next(); if (psiClasses != null) { files = psiClasses; } } final CopyFilesOrDirectoriesDialog dialog = new CopyFilesOrDirectoriesDialog(files, defaultTargetDirectory, project, false); dialog.show(); if (dialog.isOK()) { targetDirectory = dialog.getTargetDirectory(); className = dialog.getNewName(); openInEditor = dialog.openInEditor(); } } } if (targetDirectory != null) { copyClassesImpl( className, project, classes, relativePathsMap, targetDirectory, defaultTargetDirectory, RefactoringBundle.message("copy.handler.copy.class"), false, openInEditor); } }