Search in sources :

Example 96 with ProgressIndicator

use of com.intellij.openapi.progress.ProgressIndicator in project intellij-community by JetBrains.

the class MethodDuplicatesHandler method invoke.

@Override
public void invoke(@NotNull final Project project, final Editor editor, PsiFile file, DataContext dataContext) {
    final int offset = editor.getCaretModel().getOffset();
    final PsiElement element = file.findElementAt(offset);
    final PsiMember member = PsiTreeUtil.getParentOfType(element, PsiMember.class);
    final String cannotRefactorMessage = getCannotRefactorMessage(member);
    if (cannotRefactorMessage != null) {
        String message = RefactoringBundle.getCannotRefactorMessage(cannotRefactorMessage);
        showErrorMessage(message, project, editor);
        return;
    }
    final AnalysisScope scope = new AnalysisScope(file);
    final Module module = ModuleUtilCore.findModuleForPsiElement(file);
    final BaseAnalysisActionDialog dlg = new BaseAnalysisActionDialog(RefactoringBundle.message("replace.method.duplicates.scope.chooser.title", REFACTORING_NAME), RefactoringBundle.message("replace.method.duplicates.scope.chooser.message"), project, scope, module != null ? module.getName() : null, false, AnalysisUIOptions.getInstance(project), element);
    if (dlg.showAndGet()) {
        AnalysisScope selectedScope = dlg.getScope(AnalysisUIOptions.getInstance(project), scope, project, module);
        ProgressManager.getInstance().run(new Task.Backgroundable(project, "Locate duplicates", true) {

            @Override
            public void run(@NotNull ProgressIndicator indicator) {
                indicator.setIndeterminate(true);
                invokeOnScope(project, member, selectedScope);
            }
        });
    }
}
Also used : AnalysisScope(com.intellij.analysis.AnalysisScope) Task(com.intellij.openapi.progress.Task) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) Module(com.intellij.openapi.module.Module) BaseAnalysisActionDialog(com.intellij.analysis.BaseAnalysisActionDialog)

Example 97 with ProgressIndicator

use of com.intellij.openapi.progress.ProgressIndicator in project intellij-community by JetBrains.

the class ForwardDependenciesBuilder method visit.

private void visit(final PsiFile file, final ProjectFileIndex fileIndex, final PsiManager psiManager, int depth) {
    final FileViewProvider viewProvider = file.getViewProvider();
    if (viewProvider.getBaseLanguage() != file.getLanguage())
        return;
    if (getScopeOfInterest() != null && !getScopeOfInterest().contains(file))
        return;
    ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
    final VirtualFile virtualFile = file.getVirtualFile();
    if (indicator != null) {
        if (indicator.isCanceled()) {
            throw new ProcessCanceledException();
        }
        indicator.setText(AnalysisScopeBundle.message("package.dependencies.progress.text"));
        if (virtualFile != null) {
            indicator.setText2(getRelativeToProjectPath(virtualFile));
        }
        if (myTotalFileCount > 0) {
            indicator.setFraction(((double) ++myFileCount) / myTotalFileCount);
        }
    }
    final boolean isInLibrary = virtualFile == null || fileIndex.isInLibrarySource(virtualFile) || fileIndex.isInLibraryClasses(virtualFile);
    final Set<PsiFile> collectedDeps = new HashSet<>();
    final HashSet<PsiFile> processed = new HashSet<>();
    collectedDeps.add(file);
    do {
        if (depth++ > getTransitiveBorder())
            return;
        for (PsiFile psiFile : new HashSet<>(collectedDeps)) {
            final VirtualFile vFile = psiFile.getVirtualFile();
            if (vFile != null) {
                if (indicator != null) {
                    indicator.setText2(getRelativeToProjectPath(vFile));
                }
                if (!isInLibrary && (fileIndex.isInLibraryClasses(vFile) || fileIndex.isInLibrarySource(vFile))) {
                    processed.add(psiFile);
                }
            }
            final Set<PsiFile> found = new HashSet<>();
            if (!processed.contains(psiFile)) {
                processed.add(psiFile);
                analyzeFileDependencies(psiFile, new DependencyProcessor() {

                    @Override
                    public void process(PsiElement place, PsiElement dependency) {
                        PsiFile dependencyFile = dependency.getContainingFile();
                        if (dependencyFile != null) {
                            if (viewProvider == dependencyFile.getViewProvider())
                                return;
                            if (dependencyFile.isPhysical()) {
                                final VirtualFile virtualFile = dependencyFile.getVirtualFile();
                                if (virtualFile != null && (fileIndex.isInContent(virtualFile) || fileIndex.isInLibraryClasses(virtualFile) || fileIndex.isInLibrarySource(virtualFile))) {
                                    final PsiElement navigationElement = dependencyFile.getNavigationElement();
                                    found.add(navigationElement instanceof PsiFile ? (PsiFile) navigationElement : dependencyFile);
                                }
                            }
                        }
                    }
                });
                Set<PsiFile> deps = getDependencies().get(file);
                if (deps == null) {
                    deps = new HashSet<>();
                    getDependencies().put(file, deps);
                }
                deps.addAll(found);
                getDirectDependencies().put(psiFile, new HashSet<>(found));
                collectedDeps.addAll(found);
                psiManager.dropResolveCaches();
                InjectedLanguageManager.getInstance(file.getProject()).dropFileCaches(file);
            }
        }
        collectedDeps.removeAll(processed);
    } while (isTransitive() && !collectedDeps.isEmpty());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) HashSet(java.util.HashSet)

Example 98 with ProgressIndicator

use of com.intellij.openapi.progress.ProgressIndicator in project intellij-community by JetBrains.

the class InspectionEngine method inspectElements.

// returns map tool.shortName -> list of descriptors found
@NotNull
static Map<String, List<ProblemDescriptor>> inspectElements(@NotNull List<LocalInspectionToolWrapper> toolWrappers, @NotNull final PsiFile file, @NotNull final InspectionManager iManager, final boolean isOnTheFly, boolean failFastOnAcquireReadAction, @NotNull ProgressIndicator indicator, @NotNull final List<PsiElement> elements, @NotNull final Set<String> elementDialectIds) {
    TextRange range = file.getTextRange();
    final LocalInspectionToolSession session = new LocalInspectionToolSession(file, range.getStartOffset(), range.getEndOffset());
    Map<LocalInspectionToolWrapper, Set<String>> toolToSpecifiedDialectIds = getToolsToSpecifiedLanguages(toolWrappers);
    List<Entry<LocalInspectionToolWrapper, Set<String>>> entries = new ArrayList<>(toolToSpecifiedDialectIds.entrySet());
    final Map<String, List<ProblemDescriptor>> resultDescriptors = new ConcurrentHashMap<>();
    Processor<Entry<LocalInspectionToolWrapper, Set<String>>> processor = entry -> {
        ProblemsHolder holder = new ProblemsHolder(iManager, file, isOnTheFly);
        final LocalInspectionTool tool = entry.getKey().getTool();
        Set<String> dialectIdsSpecifiedForTool = entry.getValue();
        createVisitorAndAcceptElements(tool, holder, isOnTheFly, session, elements, elementDialectIds, dialectIdsSpecifiedForTool);
        tool.inspectionFinished(session, holder);
        if (holder.hasResults()) {
            resultDescriptors.put(tool.getShortName(), ContainerUtil.filter(holder.getResults(), descriptor -> {
                PsiElement element = descriptor.getPsiElement();
                return element == null || !SuppressionUtil.inspectionResultSuppressed(element, tool);
            }));
        }
        return true;
    };
    JobLauncher.getInstance().invokeConcurrentlyUnderProgress(entries, indicator, failFastOnAcquireReadAction, processor);
    return resultDescriptors;
}
Also used : Language(com.intellij.lang.Language) java.util(java.util) JobLauncher(com.intellij.concurrency.JobLauncher) GlobalInspectionToolWrapper(com.intellij.codeInspection.ex.GlobalInspectionToolWrapper) Divider(com.intellij.codeInsight.daemon.impl.Divider) LocalInspectionToolWrapper(com.intellij.codeInspection.ex.LocalInspectionToolWrapper) THashSet(gnu.trove.THashSet) ContainerUtil(com.intellij.util.containers.ContainerUtil) THashMap(gnu.trove.THashMap) Conditions(com.intellij.openapi.util.Conditions) RefVisitor(com.intellij.codeInspection.reference.RefVisitor) Logger(com.intellij.openapi.diagnostic.Logger) CommonProcessors(com.intellij.util.CommonProcessors) RefManagerImpl(com.intellij.codeInspection.reference.RefManagerImpl) ProgressManager(com.intellij.openapi.progress.ProgressManager) RefElement(com.intellij.codeInspection.reference.RefElement) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) AnalysisScope(com.intellij.analysis.AnalysisScope) TextRange(com.intellij.openapi.util.TextRange) EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) Nullable(org.jetbrains.annotations.Nullable) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) Processor(com.intellij.util.Processor) SmartHashSet(com.intellij.util.containers.SmartHashSet) InspectionToolWrapper(com.intellij.codeInspection.ex.InspectionToolWrapper) Entry(java.util.Map.Entry) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) RefEntity(com.intellij.codeInspection.reference.RefEntity) THashSet(gnu.trove.THashSet) SmartHashSet(com.intellij.util.containers.SmartHashSet) TextRange(com.intellij.openapi.util.TextRange) Entry(java.util.Map.Entry) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LocalInspectionToolWrapper(com.intellij.codeInspection.ex.LocalInspectionToolWrapper) NotNull(org.jetbrains.annotations.NotNull)

Example 99 with ProgressIndicator

use of com.intellij.openapi.progress.ProgressIndicator in project intellij-community by JetBrains.

the class TreeModelBuilder method buildFileNode.

@Nullable
private PackageDependenciesNode buildFileNode(final VirtualFile file, @Nullable PackageDependenciesNode parent) {
    final ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
    if (indicator != null) {
        ((PanelProgressIndicator) indicator).update(SCANNING_PACKAGES_MESSAGE, false, ((double) myScannedFileCount++) / myTotalFileCount);
    }
    boolean isMarked = myMarker != null && myMarker.isMarked(file);
    if (isMarked)
        myMarkedFileCount++;
    if (isMarked || myAddUnmarkedFiles) {
        PackageDependenciesNode dirNode = parent != null ? parent : getFileParentNode(file);
        if (dirNode == null)
            return null;
        if (myShowFiles) {
            FileNode fileNode = new FileNode(file, myProject, isMarked);
            dirNode.add(fileNode);
        } else {
            dirNode.addFile(file, isMarked);
        }
        return dirNode;
    }
    return null;
}
Also used : ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) Nullable(org.jetbrains.annotations.Nullable)

Example 100 with ProgressIndicator

use of com.intellij.openapi.progress.ProgressIndicator in project intellij-community by JetBrains.

the class FindJarFix method findJarsForFqn.

private void findJarsForFqn(final String fqn, final Editor editor) {
    final Map<String, String> libs = new HashMap<>();
    final Runnable runnable = () -> {
        try {
            final DOMParser parser = new DOMParser();
            parser.parse(CLASS_ROOT_URL + fqn.replace('.', '/') + CLASS_PAGE_EXT);
            final Document doc = parser.getDocument();
            if (doc != null) {
                final NodeList links = doc.getElementsByTagName(LINK_TAG_NAME);
                for (int i = 0; i < links.getLength(); i++) {
                    final Node link = links.item(i);
                    final String libName = link.getTextContent();
                    final NamedNodeMap attributes = link.getAttributes();
                    if (attributes != null) {
                        final Node href = attributes.getNamedItem(LINK_ATTR_NAME);
                        if (href != null) {
                            final String pathToJar = href.getTextContent();
                            if (pathToJar != null && (pathToJar.startsWith("/jar/") || pathToJar.startsWith("/class/../"))) {
                                libs.put(libName, SERVICE_URL + pathToJar);
                            }
                        }
                    }
                }
            }
        } catch (IOException ignore) {
        //
        } catch (Exception e) {
            //
            LOG.warn(e);
        }
    };
    final Task.Modal task = new Task.Modal(editor.getProject(), "Looking for libraries", true) {

        @Override
        public void run(@NotNull ProgressIndicator indicator) {
            indicator.setIndeterminate(true);
            runUncanceledRunnableWithProgress(runnable, indicator);
        }

        @Override
        public void onSuccess() {
            super.onSuccess();
            if (libs.isEmpty()) {
                HintManager.getInstance().showInformationHint(editor, "No libraries found for '" + fqn + "'");
            } else {
                final ArrayList<String> variants = new ArrayList<>(libs.keySet());
                Collections.sort(variants, (o1, o2) -> o1.compareTo(o2));
                final JBList libNames = new JBList(variants);
                libNames.installCellRenderer(o -> new JLabel(o.toString(), PlatformIcons.JAR_ICON, SwingConstants.LEFT));
                if (libs.size() == 1) {
                    final String jarName = libs.keySet().iterator().next();
                    final String url = libs.get(jarName);
                    initiateDownload(url, jarName);
                } else {
                    JBPopupFactory.getInstance().createListPopupBuilder(libNames).setTitle("Select a JAR file").setItemChoosenCallback(() -> {
                        final Object value = libNames.getSelectedValue();
                        if (value instanceof String) {
                            final String jarName = (String) value;
                            final String url = libs.get(jarName);
                            if (url != null) {
                                initiateDownload(url, jarName);
                            }
                        }
                    }).createPopup().showInBestPositionFor(editor);
                }
            }
        }
    };
    ProgressManager.getInstance().run(task);
}
Also used : Task(com.intellij.openapi.progress.Task) NamedNodeMap(org.w3c.dom.NamedNodeMap) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) IOException(java.io.IOException) Document(org.w3c.dom.Document) NotNull(org.jetbrains.annotations.NotNull) IncorrectOperationException(com.intellij.util.IncorrectOperationException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) JBList(com.intellij.ui.components.JBList) DOMParser(org.cyberneko.html.parsers.DOMParser)

Aggregations

ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)351 Task (com.intellij.openapi.progress.Task)138 NotNull (org.jetbrains.annotations.NotNull)98 VirtualFile (com.intellij.openapi.vfs.VirtualFile)96 Project (com.intellij.openapi.project.Project)78 File (java.io.File)52 IOException (java.io.IOException)47 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)44 Nullable (org.jetbrains.annotations.Nullable)43 ProgressManager (com.intellij.openapi.progress.ProgressManager)35 List (java.util.List)30 EmptyProgressIndicator (com.intellij.openapi.progress.EmptyProgressIndicator)28 Ref (com.intellij.openapi.util.Ref)27 VcsException (com.intellij.openapi.vcs.VcsException)25 ArrayList (java.util.ArrayList)23 ApplicationManager (com.intellij.openapi.application.ApplicationManager)22 Module (com.intellij.openapi.module.Module)21 Logger (com.intellij.openapi.diagnostic.Logger)20 java.util (java.util)20 Processor (com.intellij.util.Processor)18