Search in sources :

Example 1 with ImportOptimizer

use of com.intellij.lang.ImportOptimizer in project intellij-community by JetBrains.

the class OptimizeImportsQuickFix method optimizeImports.

private void optimizeImports(final Project project, final PsiFile file) {
    ImportOptimizer optimizer = new PyImportOptimizer();
    final Runnable runnable = optimizer.processFile(file);
    new WriteCommandAction.Simple(project, getFamilyName(), file) {

        @Override
        protected void run() throws Throwable {
            runnable.run();
        }
    }.execute();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) ImportOptimizer(com.intellij.lang.ImportOptimizer)

Example 2 with ImportOptimizer

use of com.intellij.lang.ImportOptimizer in project intellij-community by JetBrains.

the class OptimizeImportsProcessor method prepareTask.

@Override
@NotNull
protected FutureTask<Boolean> prepareTask(@NotNull PsiFile file, boolean processChangedTextOnly) {
    if (DumbService.isDumb(file.getProject())) {
        return new FutureTask<>(EmptyRunnable.INSTANCE, true);
    }
    final Set<ImportOptimizer> optimizers = LanguageImportStatements.INSTANCE.forFile(file);
    final List<Runnable> runnables = new ArrayList<>();
    List<PsiFile> files = file.getViewProvider().getAllFiles();
    for (ImportOptimizer optimizer : optimizers) {
        for (PsiFile psiFile : files) {
            if (optimizer.supports(psiFile)) {
                runnables.add(optimizer.processFile(psiFile));
            }
        }
    }
    Runnable runnable = !runnables.isEmpty() ? () -> {
        CodeStyleManagerImpl.setSequentialProcessingAllowed(false);
        try {
            for (Runnable runnable1 : runnables) {
                runnable1.run();
                retrieveAndStoreNotificationInfo(runnable1);
            }
            putNotificationInfoIntoCollector();
        } finally {
            CodeStyleManagerImpl.setSequentialProcessingAllowed(true);
        }
    } : EmptyRunnable.getInstance();
    return new FutureTask<>(runnable, true);
}
Also used : FutureTask(java.util.concurrent.FutureTask) EmptyRunnable(com.intellij.openapi.util.EmptyRunnable) ArrayList(java.util.ArrayList) PsiFile(com.intellij.psi.PsiFile) ImportOptimizer(com.intellij.lang.ImportOptimizer) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with ImportOptimizer

use of com.intellij.lang.ImportOptimizer in project intellij-plugins by JetBrains.

the class ActionScriptUnusedImportsPassFactory method createOptimizeImportsIntention.

private static IntentionAction createOptimizeImportsIntention() {
    return new IntentionAction() {

        @NotNull
        public String getText() {
            return JSBundle.message("javascript.fix.optimize.imports");
        }

        @NotNull
        public String getFamilyName() {
            return getText();
        }

        public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
            return true;
        }

        public void invoke(@NotNull final Project project, Editor editor, PsiFile file) {
            ImportOptimizer optimizer = new ECMAScriptImportOptimizer();
            final Runnable runnable = optimizer.processFile(file);
            ApplicationManager.getApplication().runWriteAction(new Runnable() {

                public void run() {
                    CommandProcessor.getInstance().executeCommand(project, runnable, getFamilyName(), this);
                }
            });
        }

        public boolean startInWriteAction() {
            return true;
        }
    };
}
Also used : Project(com.intellij.openapi.project.Project) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) PsiFile(com.intellij.psi.PsiFile) ImportOptimizer(com.intellij.lang.ImportOptimizer) Editor(com.intellij.openapi.editor.Editor) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ImportOptimizer (com.intellij.lang.ImportOptimizer)3 PsiFile (com.intellij.psi.PsiFile)2 NotNull (org.jetbrains.annotations.NotNull)2 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 Editor (com.intellij.openapi.editor.Editor)1 Project (com.intellij.openapi.project.Project)1 EmptyRunnable (com.intellij.openapi.util.EmptyRunnable)1 ArrayList (java.util.ArrayList)1 FutureTask (java.util.concurrent.FutureTask)1