Search in sources :

Example 1 with PairProcessor

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

the class RepositoryAttachHandler method searchArtifacts.

public static void searchArtifacts(final Project project, String coord, final PairProcessor<Collection<Pair<MavenArtifactInfo, MavenRepositoryInfo>>, Boolean> resultProcessor) {
    if (coord == null || coord.length() == 0)
        return;
    final MavenArtifactInfo template;
    if (coord.indexOf(':') == -1 && Character.isUpperCase(coord.charAt(0))) {
        template = new MavenArtifactInfo(null, null, null, "jar", null, coord, null);
    } else {
        template = new MavenArtifactInfo(getMavenId(coord), "jar", null);
    }
    ProgressManager.getInstance().run(new Task.Backgroundable(project, "Maven", false) {

        public void run(@NotNull ProgressIndicator indicator) {
            String[] urls = MavenRepositoryServicesManager.getServiceUrls();
            boolean tooManyResults = false;
            final AtomicBoolean proceedFlag = new AtomicBoolean(true);
            for (int i = 0, length = urls.length; i < length; i++) {
                if (!proceedFlag.get())
                    break;
                final List<Pair<MavenArtifactInfo, MavenRepositoryInfo>> resultList = new ArrayList<>();
                try {
                    String serviceUrl = urls[i];
                    final List<MavenArtifactInfo> artifacts;
                    artifacts = MavenRepositoryServicesManager.findArtifacts(template, serviceUrl);
                    if (!artifacts.isEmpty()) {
                        if (!proceedFlag.get()) {
                            break;
                        }
                        List<MavenRepositoryInfo> repositories = MavenRepositoryServicesManager.getRepositories(serviceUrl);
                        Map<String, MavenRepositoryInfo> map = new THashMap<>();
                        for (MavenRepositoryInfo repository : repositories) {
                            map.put(repository.getId(), repository);
                        }
                        for (MavenArtifactInfo artifact : artifacts) {
                            if (artifact == null) {
                                tooManyResults = true;
                            } else {
                                MavenRepositoryInfo repository = map.get(artifact.getRepositoryId());
                                // because it won't be resolved anyway
                                if (repository == null)
                                    continue;
                                resultList.add(Pair.create(artifact, repository));
                            }
                        }
                    }
                } catch (Exception e) {
                    MavenLog.LOG.error(e);
                } finally {
                    if (!proceedFlag.get())
                        break;
                    final Boolean aBoolean = i == length - 1 ? tooManyResults : null;
                    ApplicationManager.getApplication().invokeLater(() -> proceedFlag.set(resultProcessor.process(resultList, aBoolean)), o -> !proceedFlag.get());
                }
            }
        }
    });
}
Also used : MavenRepositoryServicesManager(org.jetbrains.idea.maven.services.MavenRepositoryServicesManager) MavenProgressIndicator(org.jetbrains.idea.maven.utils.MavenProgressIndicator) JBIterable(com.intellij.util.containers.JBIterable) VirtualFile(com.intellij.openapi.vfs.VirtualFile) THashMap(gnu.trove.THashMap) MavenGeneralSettings(org.jetbrains.idea.maven.project.MavenGeneralSettings) VirtualFileManager(com.intellij.openapi.vfs.VirtualFileManager) PairProcessor(com.intellij.util.PairProcessor) MavenEmbeddersManager(org.jetbrains.idea.maven.project.MavenEmbeddersManager) Task(com.intellij.openapi.progress.Task) SmartList(com.intellij.util.SmartList) SoutMavenConsole(org.jetbrains.idea.maven.execution.SoutMavenConsole) ProjectBundle(org.jetbrains.idea.maven.project.ProjectBundle) RepositoryAttachDialog(org.jetbrains.idea.maven.utils.RepositoryAttachDialog) Messages(com.intellij.openapi.ui.Messages) FileUtil(com.intellij.openapi.util.io.FileUtil) Notifications(com.intellij.notification.Notifications) ProgressManager(com.intellij.openapi.progress.ProgressManager) OrderRootType(com.intellij.openapi.roots.OrderRootType) LibraryEditor(com.intellij.openapi.roots.ui.configuration.libraryEditor.LibraryEditor) NotificationType(com.intellij.notification.NotificationType) MavenProjectsManager(org.jetbrains.idea.maven.project.MavenProjectsManager) Notification(com.intellij.notification.Notification) Nullable(org.jetbrains.annotations.Nullable) MavenEmbedderWrapper(org.jetbrains.idea.maven.server.MavenEmbedderWrapper) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) NewLibraryConfiguration(com.intellij.openapi.roots.libraries.NewLibraryConfiguration) Processor(com.intellij.util.Processor) ApplicationManager(com.intellij.openapi.application.ApplicationManager) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) java.util(java.util) WriteAction(com.intellij.openapi.application.WriteAction) MavenLog(org.jetbrains.idea.maven.utils.MavenLog) MavenExtraArtifactType(org.jetbrains.idea.maven.importing.MavenExtraArtifactType) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) JavadocOrderRootType(com.intellij.openapi.roots.JavadocOrderRootType) OrderRoot(com.intellij.openapi.roots.libraries.ui.OrderRoot) DialogWrapper(com.intellij.openapi.ui.DialogWrapper) CommonBundle(com.intellij.CommonBundle) Project(com.intellij.openapi.project.Project) org.jetbrains.idea.maven.model(org.jetbrains.idea.maven.model) VfsUtilCore(com.intellij.openapi.vfs.VfsUtilCore) MavenProcessCanceledException(org.jetbrains.idea.maven.utils.MavenProcessCanceledException) IOException(java.io.IOException) File(java.io.File) Pair(com.intellij.openapi.util.Pair) MavenDependenciesRemoteManager(org.jetbrains.idea.maven.utils.library.remote.MavenDependenciesRemoteManager) VfsUtil(com.intellij.openapi.vfs.VfsUtil) javax.swing(javax.swing) Task(com.intellij.openapi.progress.Task) MavenProcessCanceledException(org.jetbrains.idea.maven.utils.MavenProcessCanceledException) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MavenProgressIndicator(org.jetbrains.idea.maven.utils.MavenProgressIndicator) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) SmartList(com.intellij.util.SmartList) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) THashMap(gnu.trove.THashMap)

Example 2 with PairProcessor

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

the class SSBasedInspection method buildVisitor.

@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
    final Map<Configuration, MatchContext> compiledOptions = SSBasedInspectionCompiledPatternsCache.getCompiledOptions(myConfigurations, holder.getProject());
    if (compiledOptions.isEmpty())
        return super.buildVisitor(holder, isOnTheFly);
    return new PsiElementVisitor() {

        final Matcher matcher = new Matcher(holder.getManager().getProject());

        final PairProcessor<MatchResult, Configuration> processor = (matchResult, configuration) -> {
            PsiElement element = matchResult.getMatch();
            String name = configuration.getName();
            LocalQuickFix fix = createQuickFix(holder.getManager().getProject(), matchResult, configuration);
            holder.registerProblem(holder.getManager().createProblemDescriptor(element, name, fix, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, isOnTheFly));
            return true;
        };

        @Override
        public void visitElement(PsiElement element) {
            synchronized (LOCK) {
                if (LexicalNodesFilter.getInstance().accepts(element))
                    return;
                final SsrFilteringNodeIterator matchedNodes = new SsrFilteringNodeIterator(element);
                for (Map.Entry<Configuration, MatchContext> entry : compiledOptions.entrySet()) {
                    Configuration configuration = entry.getKey();
                    MatchContext context = entry.getValue();
                    if (MatcherImpl.checkIfShouldAttemptToMatch(context, matchedNodes)) {
                        final int nodeCount = context.getPattern().getNodeCount();
                        try {
                            matcher.processMatchesInElement(context, configuration, new CountingNodeIterator(nodeCount, matchedNodes), processor);
                        } catch (StructuralSearchException e) {
                            if (myProblemsReported.add(configuration.getName())) {
                                // don't overwhelm the user with messages
                                Notifications.Bus.notify(new Notification(SSRBundle.message("structural.search.title"), SSRBundle.message("template.problem", configuration.getName()), e.getMessage(), NotificationType.ERROR), element.getProject());
                            }
                        }
                        matchedNodes.reset();
                    }
                }
            }
        }
    };
}
Also used : ReplaceConfiguration(com.intellij.structuralsearch.plugin.replace.ui.ReplaceConfiguration) Configuration(com.intellij.structuralsearch.plugin.ui.Configuration) Matcher(com.intellij.structuralsearch.Matcher) PsiElementVisitor(com.intellij.psi.PsiElementVisitor) PairProcessor(com.intellij.util.PairProcessor) Notification(com.intellij.notification.Notification) StructuralSearchException(com.intellij.structuralsearch.StructuralSearchException) CountingNodeIterator(com.intellij.dupLocator.iterators.CountingNodeIterator) MatchContext(com.intellij.structuralsearch.impl.matcher.MatchContext) SsrFilteringNodeIterator(com.intellij.structuralsearch.impl.matcher.iterators.SsrFilteringNodeIterator) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with PairProcessor

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

the class XsdEnumerationDescriptor method processEnumerationImpl.

private boolean processEnumerationImpl(final XmlTag declaration, final PairProcessor<PsiElement, String> pairProcessor, boolean forCompletion) {
    XmlAttribute name = declaration.getAttribute("name");
    if (name != null && "boolean".equals(name.getValue())) {
        XmlAttributeValue valueElement = name.getValueElement();
        pairProcessor.process(valueElement, "true");
        pairProcessor.process(valueElement, "false");
        if (!forCompletion) {
            pairProcessor.process(valueElement, "1");
            pairProcessor.process(valueElement, "0");
        }
        myExhaustiveEnum = true;
        return true;
    } else {
        final Ref<Boolean> found = new Ref<>(Boolean.FALSE);
        myExhaustiveEnum = XmlUtil.processEnumerationValues(declaration, tag -> {
            found.set(Boolean.TRUE);
            XmlAttribute name1 = tag.getAttribute("value");
            return name1 == null || pairProcessor.process(tag, name1.getValue());
        });
        return found.get();
    }
}
Also used : ArrayUtil(com.intellij.util.ArrayUtil) XmlAttribute(com.intellij.psi.xml.XmlAttribute) PairProcessor(com.intellij.util.PairProcessor) Nullable(org.jetbrains.annotations.Nullable) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) List(java.util.List) SmartList(com.intellij.util.SmartList) XmlUtil(com.intellij.xml.util.XmlUtil) XmlEnumerationDescriptor(com.intellij.xml.impl.XmlEnumerationDescriptor) Processor(com.intellij.util.Processor) PsiElement(com.intellij.psi.PsiElement) XmlElement(com.intellij.psi.xml.XmlElement) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) Ref(com.intellij.openapi.util.Ref) XmlTag(com.intellij.psi.xml.XmlTag) Ref(com.intellij.openapi.util.Ref) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue)

Aggregations

PairProcessor (com.intellij.util.PairProcessor)3 Notification (com.intellij.notification.Notification)2 Ref (com.intellij.openapi.util.Ref)2 PsiElement (com.intellij.psi.PsiElement)2 Processor (com.intellij.util.Processor)2 SmartList (com.intellij.util.SmartList)2 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 CommonBundle (com.intellij.CommonBundle)1 CountingNodeIterator (com.intellij.dupLocator.iterators.CountingNodeIterator)1 NotificationType (com.intellij.notification.NotificationType)1 Notifications (com.intellij.notification.Notifications)1 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 WriteAction (com.intellij.openapi.application.WriteAction)1 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)1 ProgressManager (com.intellij.openapi.progress.ProgressManager)1 Task (com.intellij.openapi.progress.Task)1 Project (com.intellij.openapi.project.Project)1 JavadocOrderRootType (com.intellij.openapi.roots.JavadocOrderRootType)1 OrderRootType (com.intellij.openapi.roots.OrderRootType)1