Search in sources :

Example 1 with OptimizeImportsProcessor

use of com.intellij.codeInsight.actions.OptimizeImportsProcessor in project intellij-community by JetBrains.

the class XmlNamespacesTest method doOptimizeImportsTest.

private void doOptimizeImportsTest(String after) {
    myFixture.testHighlighting();
    new WriteCommandAction(getProject(), getFile()) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            new OptimizeImportsProcessor(getProject(), getFile()).runWithoutProgress();
        }
    }.execute();
    myFixture.checkResult(after);
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) OptimizeImportsProcessor(com.intellij.codeInsight.actions.OptimizeImportsProcessor) Result(com.intellij.openapi.application.Result)

Example 2 with OptimizeImportsProcessor

use of com.intellij.codeInsight.actions.OptimizeImportsProcessor in project intellij-community by JetBrains.

the class AddImportAction method _addImport.

private void _addImport(PsiReference ref, PsiClass targetClass) {
    int caretOffset = myEditor.getCaretModel().getOffset();
    RangeMarker caretMarker = myEditor.getDocument().createRangeMarker(caretOffset, caretOffset);
    int colByOffset = myEditor.offsetToLogicalPosition(caretOffset).column;
    int col = myEditor.getCaretModel().getLogicalPosition().column;
    int virtualSpace = col == colByOffset ? 0 : col - colByOffset;
    int line = myEditor.getCaretModel().getLogicalPosition().line;
    LogicalPosition pos = new LogicalPosition(line, 0);
    myEditor.getCaretModel().moveToLogicalPosition(pos);
    try {
        bindReference(ref, targetClass);
        if (CodeInsightWorkspaceSettings.getInstance(myProject).optimizeImportsOnTheFly) {
            Document document = myEditor.getDocument();
            PsiFile psiFile = PsiDocumentManager.getInstance(myProject).getPsiFile(document);
            new OptimizeImportsProcessor(myProject, psiFile).runWithoutProgress();
        }
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
    line = myEditor.getCaretModel().getLogicalPosition().line;
    LogicalPosition pos1 = new LogicalPosition(line, col);
    myEditor.getCaretModel().moveToLogicalPosition(pos1);
    if (caretMarker.isValid()) {
        LogicalPosition pos2 = myEditor.offsetToLogicalPosition(caretMarker.getStartOffset());
        int newCol = pos2.column + virtualSpace;
        myEditor.getCaretModel().moveToLogicalPosition(new LogicalPosition(pos2.line, newCol));
        myEditor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
    }
}
Also used : OptimizeImportsProcessor(com.intellij.codeInsight.actions.OptimizeImportsProcessor) PsiFile(com.intellij.psi.PsiFile) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 3 with OptimizeImportsProcessor

use of com.intellij.codeInsight.actions.OptimizeImportsProcessor in project intellij-community by JetBrains.

the class CopyClassesHandler method doCopyClasses.

@Nullable
public static Collection<PsiFile> doCopyClasses(final Map<PsiFile, PsiClass[]> fileToClasses, @Nullable HashMap<PsiFile, String> map, final String copyClassName, final PsiDirectory targetDirectory, final Project project) throws IncorrectOperationException {
    PsiElement newElement = null;
    final Map<PsiClass, PsiElement> oldToNewMap = new HashMap<>();
    for (final PsiClass[] psiClasses : fileToClasses.values()) {
        if (psiClasses != null) {
            for (PsiClass aClass : psiClasses) {
                if (isSynthetic(aClass)) {
                    continue;
                }
                oldToNewMap.put(aClass, null);
            }
        }
    }
    final List<PsiFile> createdFiles = new ArrayList<>(fileToClasses.size());
    int[] choice = fileToClasses.size() > 1 ? new int[] { -1 } : null;
    List<PsiFile> files = new ArrayList<>();
    for (final Map.Entry<PsiFile, PsiClass[]> entry : fileToClasses.entrySet()) {
        final PsiFile psiFile = entry.getKey();
        final PsiClass[] sources = entry.getValue();
        if (psiFile instanceof PsiClassOwner && sources != null) {
            final PsiFile createdFile = copy(psiFile, targetDirectory, copyClassName, map == null ? null : map.get(psiFile), choice);
            if (createdFile == null) {
                //do not touch unmodified classes
                for (PsiClass aClass : ((PsiClassOwner) psiFile).getClasses()) {
                    oldToNewMap.remove(aClass);
                }
                continue;
            }
            PsiClass[] classes = ((PsiClassOwner) createdFile).getClasses();
            for (final PsiClass destination : classes) {
                if (isSynthetic(destination)) {
                    continue;
                }
                PsiClass source = findByName(sources, destination.getName());
                if (source != null) {
                    final PsiClass copy = copy(source, classes.length > 1 ? null : copyClassName);
                    newElement = WriteAction.compute(() -> destination.replace(copy));
                    oldToNewMap.put(source, newElement);
                } else {
                    WriteAction.run(() -> destination.delete());
                }
            }
            createdFiles.add(createdFile);
        } else {
            files.add(psiFile);
        }
    }
    for (PsiFile file : files) {
        try {
            PsiDirectory finalTarget = targetDirectory;
            final String relativePath = map != null ? map.get(file) : null;
            if (relativePath != null && !relativePath.isEmpty()) {
                finalTarget = WriteAction.compute(() -> buildRelativeDir(targetDirectory, relativePath).findOrCreateTargetDirectory());
            }
            final PsiFile fileCopy = CopyFilesOrDirectoriesHandler.copyToDirectory(file, getNewFileName(file, copyClassName), finalTarget, choice, null);
            if (fileCopy != null) {
                createdFiles.add(fileCopy);
            }
        } catch (IOException e) {
            throw new IncorrectOperationException(e.getMessage());
        }
    }
    WriteAction.run(() -> {
        final Set<PsiElement> rebindExpressions = new HashSet<>();
        for (PsiElement element : oldToNewMap.values()) {
            if (element == null) {
                LOG.error(oldToNewMap.keySet());
                continue;
            }
            decodeRefs(element, oldToNewMap, rebindExpressions);
        }
        final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project);
        for (PsiFile psiFile : createdFiles) {
            if (psiFile instanceof PsiJavaFile) {
                codeStyleManager.removeRedundantImports((PsiJavaFile) psiFile);
            }
        }
        for (PsiElement expression : rebindExpressions) {
            //new expressions/type elements, like: List<List<String>>; new Foo(new Foo()), etc
            if (expression.isValid()) {
                codeStyleManager.shortenClassReferences(expression);
            }
        }
    });
    new OptimizeImportsProcessor(project, createdFiles.toArray(new PsiFile[createdFiles.size()]), null).run();
    return createdFiles;
}
Also used : OptimizeImportsProcessor(com.intellij.codeInsight.actions.OptimizeImportsProcessor) IOException(java.io.IOException) JavaCodeStyleManager(com.intellij.psi.codeStyle.JavaCodeStyleManager) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with OptimizeImportsProcessor

use of com.intellij.codeInsight.actions.OptimizeImportsProcessor in project intellij-community by JetBrains.

the class OptimizeImportsBeforeCheckinHandler method runCheckinHandlers.

@Override
public void runCheckinHandlers(@NotNull final Runnable finishAction) {
    final VcsConfiguration configuration = VcsConfiguration.getInstance(myProject);
    final Collection<VirtualFile> files = myPanel.getVirtualFiles();
    final Runnable performCheckoutAction = new Runnable() {

        @Override
        public void run() {
            FileDocumentManager.getInstance().saveAllDocuments();
            finishAction.run();
        }
    };
    if (configuration.OPTIMIZE_IMPORTS_BEFORE_PROJECT_COMMIT && !DumbService.isDumb(myProject)) {
        new OptimizeImportsProcessor(myProject, CheckinHandlerUtil.getPsiFiles(myProject, files), COMMAND_NAME, performCheckoutAction).run();
    } else {
        finishAction.run();
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) OptimizeImportsProcessor(com.intellij.codeInsight.actions.OptimizeImportsProcessor) VcsConfiguration(com.intellij.openapi.vcs.VcsConfiguration)

Example 5 with OptimizeImportsProcessor

use of com.intellij.codeInsight.actions.OptimizeImportsProcessor in project intellij-community by JetBrains.

the class JavaFXOptimizeImportsTest method doTest.

private void doTest() throws Exception {
    myFixture.configureByFile(getTestName(true) + ".fxml");
    new OptimizeImportsProcessor(getProject(), myFixture.getFile()).run();
    myFixture.checkResultByFile(getTestName(true) + "_after.fxml");
}
Also used : OptimizeImportsProcessor(com.intellij.codeInsight.actions.OptimizeImportsProcessor)

Aggregations

OptimizeImportsProcessor (com.intellij.codeInsight.actions.OptimizeImportsProcessor)6 PsiFile (com.intellij.psi.PsiFile)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 ReformatCodeProcessor (com.intellij.codeInsight.actions.ReformatCodeProcessor)1 Result (com.intellij.openapi.application.Result)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 VcsConfiguration (com.intellij.openapi.vcs.VcsConfiguration)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)1 IOException (java.io.IOException)1 Nullable (org.jetbrains.annotations.Nullable)1