Search in sources :

Example 86 with IncorrectOperationException

use of com.intellij.util.IncorrectOperationException in project intellij-community by JetBrains.

the class ExtractIncludeFileBase method doExtract.

@NotNull
protected String doExtract(final PsiDirectory targetDirectory, final String targetfileName, final T first, final T last, final Language includingLanguage) throws IncorrectOperationException {
    final PsiFile file = targetDirectory.createFile(targetfileName);
    Project project = targetDirectory.getProject();
    final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
    final Document document = documentManager.getDocument(file);
    document.replaceString(0, document.getTextLength(), first.getText().trim());
    documentManager.commitDocument(document);
    //TODO: adjustLineIndent
    CodeStyleManager.getInstance(PsiManager.getInstance(project).getProject()).reformat(file);
    final String relativePath = PsiFileSystemItemUtil.getRelativePath(first.getContainingFile(), file);
    if (relativePath == null)
        throw new IncorrectOperationException("Cannot extract!");
    return relativePath;
}
Also used : Project(com.intellij.openapi.project.Project) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Document(com.intellij.openapi.editor.Document) NotNull(org.jetbrains.annotations.NotNull)

Example 87 with IncorrectOperationException

use of com.intellij.util.IncorrectOperationException in project intellij-community by JetBrains.

the class MoveFilesOrDirectoriesProcessor method performRefactoring.

@Override
protected void performRefactoring(@NotNull UsageInfo[] usages) {
    try {
        final List<PsiFile> movedFiles = new ArrayList<>();
        final Map<PsiElement, PsiElement> oldToNewMap = new HashMap<>();
        for (final PsiElement element : myElementsToMove) {
            final RefactoringElementListener elementListener = getTransaction().getElementListener(element);
            if (element instanceof PsiDirectory) {
                MoveFilesOrDirectoriesUtil.doMoveDirectory((PsiDirectory) element, myNewParent);
                for (PsiElement psiElement : element.getChildren()) {
                    processDirectoryFiles(movedFiles, oldToNewMap, psiElement);
                }
            } else if (element instanceof PsiFile) {
                final PsiFile movedFile = (PsiFile) element;
                if (mySearchForReferences)
                    FileReferenceContextUtil.encodeFileReferences(element);
                MoveFileHandler.forElement(movedFile).prepareMovedFile(movedFile, myNewParent, oldToNewMap);
                PsiFile moving = myNewParent.findFile(movedFile.getName());
                if (moving == null) {
                    MoveFilesOrDirectoriesUtil.doMoveFile(movedFile, myNewParent);
                }
                moving = myNewParent.findFile(movedFile.getName());
                movedFiles.add(moving);
            }
            elementListener.elementMoved(element);
        }
        // sort by offset descending to process correctly several usages in one PsiElement [IDEADEV-33013]
        CommonRefactoringUtil.sortDepthFirstRightLeftOrder(usages);
        DumbService.getInstance(myProject).completeJustSubmittedTasks();
        // fix references in moved files to outer files
        for (PsiFile movedFile : movedFiles) {
            MoveFileHandler.forElement(movedFile).updateMovedFile(movedFile);
            if (mySearchForReferences)
                FileReferenceContextUtil.decodeFileReferences(movedFile);
        }
        retargetUsages(usages, oldToNewMap);
        if (MoveFilesOrDirectoriesDialog.isOpenInEditor()) {
            EditorHelper.openFilesInEditor(movedFiles.toArray(new PsiFile[movedFiles.size()]));
        }
        if (myMoveCallback != null) {
            myMoveCallback.refactoringCompleted();
        }
    } catch (IncorrectOperationException e) {
        final String message = e.getMessage();
        final int index = message != null ? message.indexOf("java.io.IOException") : -1;
        if (index >= 0 && message != null) {
            ApplicationManager.getApplication().invokeLater(() -> {
                String cause = message.substring(index + "java.io.IOException".length());
                Messages.showMessageDialog(myProject, cause, RefactoringBundle.message("error.title"), Messages.getErrorIcon());
            });
        } else {
            LOG.error(e);
        }
    }
}
Also used : HashMap(java.util.HashMap) RefactoringElementListener(com.intellij.refactoring.listeners.RefactoringElementListener) PsiDirectory(com.intellij.psi.PsiDirectory) ArrayList(java.util.ArrayList) PsiFile(com.intellij.psi.PsiFile) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PsiElement(com.intellij.psi.PsiElement)

Example 88 with IncorrectOperationException

use of com.intellij.util.IncorrectOperationException in project intellij-community by JetBrains.

the class MoveFilesOrDirectoriesUtil method checkMove.

/**
   * Checks if it is possible to move the specified PSI element under the specified container,
   * and throws an exception if the move is not possible. Does not actually modify anything.
   *
   * @param element      the element to check the move possibility.
   * @param newContainer the target container element to move into.
   * @throws IncorrectOperationException if the modification is not supported or not possible for some reason.
   */
public static void checkMove(@NotNull PsiElement element, @NotNull PsiElement newContainer) throws IncorrectOperationException {
    if (element instanceof PsiDirectoryContainer) {
        PsiDirectory[] dirs = ((PsiDirectoryContainer) element).getDirectories();
        if (dirs.length == 0) {
            throw new IncorrectOperationException();
        } else if (dirs.length > 1) {
            throw new IncorrectOperationException("Moving of packages represented by more than one physical directory is not supported.");
        }
        checkMove(dirs[0], newContainer);
        return;
    }
    //element.checkDelete(); //move != delete + add
    newContainer.checkAdd(element);
    checkIfMoveIntoSelf(element, newContainer);
}
Also used : IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 89 with IncorrectOperationException

use of com.intellij.util.IncorrectOperationException in project intellij-community by JetBrains.

the class CommonMoveUtil method retargetUsages.

public static NonCodeUsageInfo[] retargetUsages(final UsageInfo[] usages, final Map<PsiElement, PsiElement> oldToNewElementsMapping) throws IncorrectOperationException {
    Arrays.sort(usages, (o1, o2) -> {
        final VirtualFile file1 = o1.getVirtualFile();
        final VirtualFile file2 = o2.getVirtualFile();
        if (Comparing.equal(file1, file2)) {
            final ProperTextRange rangeInElement1 = o1.getRangeInElement();
            final ProperTextRange rangeInElement2 = o2.getRangeInElement();
            if (rangeInElement1 != null && rangeInElement2 != null) {
                return rangeInElement2.getStartOffset() - rangeInElement1.getStartOffset();
            }
            return 0;
        }
        if (file1 == null)
            return -1;
        if (file2 == null)
            return 1;
        return Comparing.compare(file1.getPath(), file2.getPath());
    });
    List<NonCodeUsageInfo> nonCodeUsages = new ArrayList<>();
    for (UsageInfo usage : usages) {
        if (usage instanceof NonCodeUsageInfo) {
            nonCodeUsages.add((NonCodeUsageInfo) usage);
        } else if (usage instanceof MoveRenameUsageInfo) {
            final MoveRenameUsageInfo moveRenameUsage = (MoveRenameUsageInfo) usage;
            final PsiElement oldElement = moveRenameUsage.getReferencedElement();
            final PsiElement newElement = oldToNewElementsMapping.get(oldElement);
            LOG.assertTrue(newElement != null, oldElement);
            final PsiReference reference = moveRenameUsage.getReference();
            if (reference != null) {
                try {
                    reference.bindToElement(newElement);
                } catch (IncorrectOperationException e) {
                //
                }
            }
        }
    }
    return nonCodeUsages.toArray(new NonCodeUsageInfo[nonCodeUsages.size()]);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProperTextRange(com.intellij.openapi.util.ProperTextRange) NonCodeUsageInfo(com.intellij.refactoring.util.NonCodeUsageInfo) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) PsiReference(com.intellij.psi.PsiReference) IncorrectOperationException(com.intellij.util.IncorrectOperationException) NonCodeUsageInfo(com.intellij.refactoring.util.NonCodeUsageInfo) MoveRenameUsageInfo(com.intellij.refactoring.util.MoveRenameUsageInfo) UsageInfo(com.intellij.usageView.UsageInfo) PsiElement(com.intellij.psi.PsiElement)

Example 90 with IncorrectOperationException

use of com.intellij.util.IncorrectOperationException in project intellij-community by JetBrains.

the class MoveDirectoryWithClassesProcessor method performRefactoring.

@Override
public void performRefactoring(@NotNull UsageInfo[] usages) {
    //try to create all directories beforehand
    try {
        //top level directories should be created even if they are empty
        for (PsiDirectory directory : myDirectories) {
            getResultDirectory(directory).findOrCreateTargetDirectory();
        }
        for (PsiDirectory directory : myNestedDirsToMove.keySet()) {
            myNestedDirsToMove.get(directory).findOrCreateTargetDirectory();
        }
        for (PsiFile psiFile : myFilesToMove.keySet()) {
            myFilesToMove.get(psiFile).findOrCreateTargetDirectory();
        }
        DumbService.getInstance(myProject).completeJustSubmittedTasks();
        final List<PsiFile> movedFiles = new ArrayList<>();
        final Map<PsiElement, PsiElement> oldToNewElementsMapping = new HashMap<>();
        for (PsiFile psiFile : myFilesToMove.keySet()) {
            for (MoveDirectoryWithClassesHelper helper : MoveDirectoryWithClassesHelper.findAll()) {
                helper.beforeMove(psiFile);
            }
            final RefactoringElementListener listener = getTransaction().getElementListener(psiFile);
            final PsiDirectory moveDestination = myFilesToMove.get(psiFile).getTargetDirectory();
            for (MoveDirectoryWithClassesHelper helper : MoveDirectoryWithClassesHelper.findAll()) {
                boolean processed = helper.move(psiFile, moveDestination, oldToNewElementsMapping, movedFiles, listener);
                if (processed) {
                    break;
                }
            }
        }
        for (PsiElement newElement : oldToNewElementsMapping.values()) {
            for (MoveDirectoryWithClassesHelper helper : MoveDirectoryWithClassesHelper.findAll()) {
                helper.afterMove(newElement);
            }
        }
        // fix references in moved files to outer files
        for (PsiFile movedFile : movedFiles) {
            MoveFileHandler.forElement(movedFile).updateMovedFile(movedFile);
            FileReferenceContextUtil.decodeFileReferences(movedFile);
        }
        myNonCodeUsages = CommonMoveUtil.retargetUsages(usages, oldToNewElementsMapping);
        for (MoveDirectoryWithClassesHelper helper : MoveDirectoryWithClassesHelper.findAll()) {
            helper.postProcessUsages(usages, dir -> getResultDirectory(dir).findOrCreateTargetDirectory());
        }
        for (PsiDirectory directory : myDirectories) {
            final TargetDirectoryWrapper wrapper = myNestedDirsToMove.get(directory);
            final PsiDirectory targetDirectory = wrapper.getTargetDirectory();
            if (targetDirectory == null || !PsiTreeUtil.isAncestor(directory, targetDirectory, false)) {
                directory.delete();
            }
        }
    } catch (IncorrectOperationException e) {
        myNonCodeUsages = new NonCodeUsageInfo[0];
        RefactoringUIUtil.processIncorrectOperation(myProject, e);
    }
}
Also used : RefactoringElementListener(com.intellij.refactoring.listeners.RefactoringElementListener) NonCodeUsageInfo(com.intellij.refactoring.util.NonCodeUsageInfo) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PsiElement(com.intellij.psi.PsiElement)

Aggregations

IncorrectOperationException (com.intellij.util.IncorrectOperationException)485 Project (com.intellij.openapi.project.Project)91 NotNull (org.jetbrains.annotations.NotNull)91 Nullable (org.jetbrains.annotations.Nullable)54 PsiElement (com.intellij.psi.PsiElement)50 TextRange (com.intellij.openapi.util.TextRange)43 VirtualFile (com.intellij.openapi.vfs.VirtualFile)38 Document (com.intellij.openapi.editor.Document)37 ASTNode (com.intellij.lang.ASTNode)35 PsiFile (com.intellij.psi.PsiFile)35 Editor (com.intellij.openapi.editor.Editor)33 NonNls (org.jetbrains.annotations.NonNls)32 UsageInfo (com.intellij.usageView.UsageInfo)31 ArrayList (java.util.ArrayList)31 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)24 IOException (java.io.IOException)24 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)21 XmlTag (com.intellij.psi.xml.XmlTag)20 CodeStyleManager (com.intellij.psi.codeStyle.CodeStyleManager)19 Module (com.intellij.openapi.module.Module)18