<commit_before>package com.redhat.ceylon.eclipse.code.modulesearch;

import static com.redhat.ceylon.cmr.ceylon.CeylonUtils.makeRepositoryManager;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.NavigableSet;

import com.redhat.ceylon.cmr.api.ModuleQuery;
import com.redhat.ceylon.cmr.api.ModuleQuery.Type;
import com.redhat.ceylon.cmr.api.ModuleSearchResult;
import com.redhat.ceylon.cmr.api.ModuleSearchResult.ModuleDetails;
import com.redhat.ceylon.cmr.api.ModuleVersionDetails;
import com.redhat.ceylon.cmr.api.ModuleVersionQuery;
import com.redhat.ceylon.cmr.api.ModuleVersionResult;
import com.redhat.ceylon.cmr.api.RepositoryManager;
import com.redhat.ceylon.eclipse.util.EclipseLogger;

public class ModuleSearchManager {
    
    private List<ModuleNode> modules;
    private String lastQuery;
    private ModuleSearchResult lastResult;
    private ModuleSearchViewPart moduleSearchViewPart;
    private RepositoryManager repositoryManager;
    
    public ModuleSearchManager(ModuleSearchViewPart moduleSearchViewPart) {
        this.moduleSearchViewPart = moduleSearchViewPart;
        this.repositoryManager = makeRepositoryManager(null, null, null, new EclipseLogger()); // TODO which repo manager ???
    }
    
    public void searchModules(final String query) {
        new ModuleSearchJobTemplate("Searching modules in repositories") {
            @Override
            protected void onRun() {
                lastQuery = query.trim();
                lastResult = repositoryManager.searchModules(new ModuleQuery(lastQuery, Type.JVM));
                modules = convertResult(lastResult.getResults());
            }
            @Override
            protected void onFinish() {
                moduleSearchViewPart.update(true);
            }
        }.schedule();
    }

    public void fetchNextModules() {
        new ModuleSearchJobTemplate("Searching modules in repositories") {
            @Override
            protected void onRun() {
                ModuleQuery moduleQuery = new ModuleQuery(lastQuery, Type.JVM);
                moduleQuery.setPagingInfo(lastResult.getNextPagingInfo());
                lastResult = repositoryManager.searchModules(moduleQuery);
                modules.addAll(convertResult(lastResult.getResults()));
            }
            @Override
            protected void onFinish() {
                moduleSearchViewPart.update(false);
            }
        }.schedule();
    }
    
    public void fetchDocumentation(final String moduleName, final String moduleVersion) {
        final ModuleVersionNode versionNode = getVersionNode(moduleName, moduleVersion);
        if (versionNode == null) {
            return;
        }
        
        new ModuleSearchJobTemplate("Loading module documentation") {
            @Override
            protected void onRun() {
                ModuleVersionQuery query = new ModuleVersionQuery(moduleName, moduleVersion, Type.JVM);
                ModuleVersionResult result = repositoryManager.completeVersions(query);
                if (result != null) {
                    ModuleVersionDetails detail = result.getVersions().get(moduleVersion);
                    if (detail != null) {
                        versionNode.setFilled(true);
                        versionNode.setDoc(detail.getDoc());
                        versionNode.setLicense(detail.getLicense());
                        versionNode.setAuthors(detail.getAuthors());
                    }
                }
            }
            @Override
            protected void onFinish() {
                moduleSearchViewPart.updateDoc();
            }
        }.schedule();        
    }    
    <commit_msg>ModuleSearchManager: added newModuleQuery() method that sets a page size #357<commit_after>package com.redhat.ceylon.eclipse.code.modulesearch;

import static com.redhat.ceylon.cmr.ceylon.CeylonUtils.makeRepositoryManager;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.NavigableSet;

import com.redhat.ceylon.cmr.api.ModuleQuery;
import com.redhat.ceylon.cmr.api.ModuleQuery.Type;
import com.redhat.ceylon.cmr.api.ModuleSearchResult;
import com.redhat.ceylon.cmr.api.ModuleSearchResult.ModuleDetails;
import com.redhat.ceylon.cmr.api.ModuleVersionDetails;
import com.redhat.ceylon.cmr.api.ModuleVersionQuery;
import com.redhat.ceylon.cmr.api.ModuleVersionResult;
import com.redhat.ceylon.cmr.api.RepositoryManager;
import com.redhat.ceylon.eclipse.util.EclipseLogger;

public class ModuleSearchManager {
    
    private List<ModuleNode> modules;
    private String lastQuery;
    private ModuleSearchResult lastResult;
    private ModuleSearchViewPart moduleSearchViewPart;
    private RepositoryManager repositoryManager;
    
    public ModuleSearchManager(ModuleSearchViewPart moduleSearchViewPart) {
        this.moduleSearchViewPart = moduleSearchViewPart;
        this.repositoryManager = makeRepositoryManager(null, null, null, new EclipseLogger()); // TODO which repo manager ???
    }
    
    public void searchModules(final String query) {
        new ModuleSearchJobTemplate("Searching modules in repositories") {
            @Override
            protected void onRun() {
                lastQuery = query.trim();
                lastResult = repositoryManager.searchModules(newModuleQuery(lastQuery));
                modules = convertResult(lastResult.getResults());
            }
            @Override
            protected void onFinish() {
                moduleSearchViewPart.update(true);
            }
        }.schedule();
    }

    public void fetchNextModules() {
        new ModuleSearchJobTemplate("Searching modules in repositories") {
            @Override
            protected void onRun() {
                ModuleQuery moduleQuery = newModuleQuery(lastQuery);
                moduleQuery.setPagingInfo(lastResult.getNextPagingInfo());
                lastResult = repositoryManager.searchModules(moduleQuery);
                modules.addAll(convertResult(lastResult.getResults()));
            }
            @Override
            protected void onFinish() {
                moduleSearchViewPart.update(false);
            }
        }.schedule();
    }
    
    protected ModuleQuery newModuleQuery(String search) {
        ModuleQuery query = new ModuleQuery(search, Type.JVM);
        query.setCount(20l);
        return query;
    }

    public void fetchDocumentation(final String moduleName, final String moduleVersion) {
        final ModuleVersionNode versionNode = getVersionNode(moduleName, moduleVersion);
        if (versionNode == null) {
            return;
        }
        
        new ModuleSearchJobTemplate("Loading module documentation") {
            @Override
            protected void onRun() {
                ModuleVersionQuery query = new ModuleVersionQuery(moduleName, moduleVersion, Type.JVM);
                ModuleVersionResult result = repositoryManager.completeVersions(query);
                if (result != null) {
                    ModuleVersionDetails detail = result.getVersions().get(moduleVersion);
                    if (detail != null) {
                        versionNode.setFilled(true);
                        versionNode.setDoc(detail.getDoc());
                        versionNode.setLicense(detail.getLicense());
                        versionNode.setAuthors(detail.getAuthors());
                    }
                }
            }
            @Override
            protected void onFinish() {
                moduleSearchViewPart.updateDoc();
            }
        }.schedule();        
    }    
    
{'fraction_non_alphanumeric': 0.08461837501619801, 'fraction_numerical': 0.000647920176234288, 'mean_word_length': 2.3732517482517483, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3529389', 'n_tokens_mistral': 1945, 'n_tokens_neox': 1876, 'n_words': 365}