Search in sources :

Example 56 with ModulesKey

use of org.python.pydev.core.ModulesKey in project Pydev by fabioz.

the class ReferenceSearches method search.

@Override
public List<ModulesKey> search(IProject project, OrderedMap<String, Set<String>> fieldNameToValues, IProgressMonitor monitor) {
    final List<ModulesKey> ret = new ArrayList<ModulesKey>();
    AbstractAdditionalDependencyInfo abstractAdditionalDependencyInfo = this.abstractAdditionalDependencyInfo.get();
    if (abstractAdditionalDependencyInfo == null) {
        Log.log("AbstractAdditionalDependencyInfo already collected!");
        return ret;
    }
    final NullProgressMonitor nullMonitor = new NullProgressMonitor();
    Set<String> pythonPathFolders = abstractAdditionalDependencyInfo.getPythonPathFolders();
    LinkedBlockingQueue<Command> queue = new LinkedBlockingQueue<>();
    int searchers = Runtime.getRuntime().availableProcessors();
    // The 'ret' should be filled with the module keys where the tokens are found.
    final Object retLock = new Object();
    // Create 2 consumers
    Thread[] threads = new Thread[searchers];
    for (int i = 0; i < searchers; i++) {
        Searcher searcher = new Searcher(queue, fieldNameToValues.get(IReferenceSearches.FIELD_CONTENTS), ret, retLock);
        // Spawn a thread to do the search while we load the contents.
        Thread t = new Thread(searcher);
        threads[i] = t;
        t.start();
    }
    try {
        PythonPathHelper pythonPathHelper = new PythonPathHelper();
        pythonPathHelper.setPythonPath(new ArrayList<String>(pythonPathFolders));
        ModulesFoundStructure modulesFound = pythonPathHelper.getModulesFoundStructure(project, nullMonitor);
        int totalSteps = modulesFound.regularModules.size() + modulesFound.zipContents.size();
        monitor.beginTask("Get modules with token in: " + abstractAdditionalDependencyInfo.getUIRepresentation(), totalSteps);
        PyPublicTreeMap<ModulesKey, ModulesKey> keys = new PyPublicTreeMap<>();
        // no point in searching dlls.
        boolean includeOnlySourceModules = true;
        ModulesManager.buildKeysForRegularEntries(nullMonitor, modulesFound, keys, includeOnlySourceModules);
        // Get from regular files found
        for (ModulesKey entry : keys.values()) {
            if (entry instanceof ModulesKeyForFolder) {
                continue;
            }
            if (monitor.isCanceled()) {
                break;
            }
            if (AbstractAdditionalDependencyInfo.DEBUG) {
                System.out.println("Loading: " + entry);
            }
            final File file = entry.file;
            try {
                queue.put(new Command(entry, new IBufferFiller() {

                    @Override
                    public void fillBuffer(FastStringBuffer bufFileContents) {
                        try (FileInputStream stream = new FileInputStream(file)) {
                            fill(bufFileContents, stream);
                        } catch (Exception e) {
                            Log.log(e);
                        }
                    }
                }));
            } catch (InterruptedException e) {
                Log.log(e);
            }
        }
        // Get from zip files found
        List<ZipContents> allZipsZipContents = modulesFound.zipContents;
        for (ZipContents zipContents : allZipsZipContents) {
            keys.clear();
            if (monitor.isCanceled()) {
                break;
            }
            ModulesManager.buildKeysForZipContents(keys, zipContents);
            try (ZipFile zipFile = new ZipFile(zipContents.zipFile)) {
                for (ModulesKey entry : keys.values()) {
                    if (AbstractAdditionalDependencyInfo.DEBUG) {
                        System.out.println("Loading: " + entry);
                    }
                    if (monitor.isCanceled()) {
                        break;
                    }
                    final ModulesKeyForZip z = (ModulesKeyForZip) entry;
                    if (!z.isFile) {
                        continue;
                    }
                    queue.put(new Command(entry, new IBufferFiller() {

                        @Override
                        public void fillBuffer(FastStringBuffer bufFileContents) {
                            try (InputStream stream = zipFile.getInputStream(zipFile.getEntry(z.zipModulePath))) {
                                fill(bufFileContents, stream);
                            } catch (Exception e) {
                                Log.log(e);
                            }
                        }
                    }));
                }
            } catch (Exception e) {
                Log.log(e);
            }
        }
    } finally {
        for (int i = 0; i < searchers; i++) {
            // add it to wait for the thread to finish.
            queue.add(new Command());
        }
    }
    int j = 0;
    while (true) {
        j++;
        boolean liveFound = false;
        for (Thread t : threads) {
            if (t.isAlive()) {
                liveFound = true;
                break;
            }
        }
        if (liveFound) {
            if (j % 50 == 0) {
                monitor.setTaskName("Searching references...");
                monitor.worked(1);
            }
            Thread.yield();
        } else {
            break;
        }
    }
    return ret;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) ModulesFoundStructure(org.python.pydev.ast.codecompletion.revisited.ModulesFoundStructure) PyPublicTreeMap(org.python.pydev.ast.codecompletion.revisited.PyPublicTreeMap) ArrayList(java.util.ArrayList) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ModulesKeyForZip(org.python.pydev.core.ModulesKeyForZip) FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) PythonPathHelper(org.python.pydev.ast.codecompletion.revisited.PythonPathHelper) ModulesKeyForFolder(org.python.pydev.core.ModulesKeyForFolder) ZipFile(java.util.zip.ZipFile) IBufferFiller(com.python.pydev.analysis.additionalinfo.AbstractAdditionalDependencyInfo.IBufferFiller) ModulesKey(org.python.pydev.core.ModulesKey) ZipContents(org.python.pydev.ast.codecompletion.revisited.ModulesFoundStructure.ZipContents) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 57 with ModulesKey

use of org.python.pydev.core.ModulesKey in project Pydev by fabioz.

the class AdditionalProjectInterpreterInfo method getModulesWithToken.

/**
 * @param token the token we want to search for (must be an exact match). Only tokens which are valid identifiers
 * may be searched (i.e.: no dots in it or anything alike).
 *
 * @return List<ModulesKey> a list with all the modules that contains the passed token.
 *
 * Note: if it's a name with dots, we'll split it and search for each one.
 */
public List<ModulesKey> getModulesWithToken(String token, IProgressMonitor monitor) throws OperationCanceledException {
    NullProgressMonitor nullMonitor = new NullProgressMonitor();
    if (monitor == null) {
        monitor = nullMonitor;
    }
    int length = token.length();
    if (token == null || length == 0) {
        return new ArrayList<>();
    }
    for (int i = 0; i < length; i++) {
        char c = token.charAt(i);
        if (!Character.isJavaIdentifierPart(c) && c != '.') {
            throw new RuntimeException(StringUtils.format("Token: %s is not a valid token to search for.", token));
        }
    }
    StringUtils.checkTokensValidForWildcardQuery(token);
    OrderedMap<String, Set<String>> fieldNameToValues = new OrderedMap<>();
    Set<String> split = new HashSet<>();
    for (String s : StringUtils.splitForIndexMatching(token)) {
        // We need to search in lowercase (we only index case-insensitive).
        split.add(s.toLowerCase());
    }
    fieldNameToValues.put(IReferenceSearches.FIELD_CONTENTS, split);
    List<ModulesKey> search = getReferenceSearches().search(project, fieldNameToValues, monitor);
    // }
    return search;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) HashSet(java.util.HashSet) Set(java.util.Set) ArrayList(java.util.ArrayList) ModulesKey(org.python.pydev.core.ModulesKey) OrderedMap(org.python.pydev.shared_core.structure.OrderedMap) HashSet(java.util.HashSet)

Example 58 with ModulesKey

use of org.python.pydev.core.ModulesKey in project Pydev by fabioz.

the class ClassHierarchySearchTest method assertIsIn.

private HierarchyNodeModel assertIsIn(String name, String modName, List<HierarchyNodeModel> parents) {
    FastStringBuffer available = new FastStringBuffer();
    for (HierarchyNodeModel model : parents) {
        available.append(model.name).append(" - ").append(model.moduleName);
        if (model.name.equals(name)) {
            if (modName == null) {
                return model;
            } else if (model.moduleName.equals(modName) || model.moduleName.startsWith(modName)) {
                return model;
            }
        }
    }
    try {
        RefactorerFindReferences references = new RefactorerFindReferences();
        RefactoringRequest request = new RefactoringRequest(null, null, nature);
        request.qualifier = name;
        List<Tuple<List<ModulesKey>, IPythonNature>> findPossibleReferences = references.findPossibleReferences(request);
        String errorMsg = "Unable to find node with name:" + name + " mod:" + modName + "\nAvailable:" + available + "\n\nPythonpath: " + nature.getPythonPathNature().getOnlyProjectPythonPathStr(true) + "\n" + "Found possible references: " + StringUtils.join("\n", findPossibleReferences);
        fail(errorMsg);
    } catch (CoreException e) {
        throw new RuntimeException(e);
    }
    return null;
}
Also used : FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer) CoreException(org.eclipse.core.runtime.CoreException) RefactoringRequest(org.python.pydev.ast.refactoring.RefactoringRequest) ModulesKey(org.python.pydev.core.ModulesKey) RefactorerFindReferences(com.python.pydev.analysis.refactoring.refactorer.RefactorerFindReferences) HierarchyNodeModel(org.python.pydev.ast.refactoring.HierarchyNodeModel) Tuple(org.python.pydev.shared_core.structure.Tuple)

Aggregations

ModulesKey (org.python.pydev.core.ModulesKey)58 File (java.io.File)23 ArrayList (java.util.ArrayList)13 HashSet (java.util.HashSet)11 MisconfigurationException (org.python.pydev.core.MisconfigurationException)11 ModulesKeyForZip (org.python.pydev.core.ModulesKeyForZip)10 FastStringBuffer (org.python.pydev.shared_core.string.FastStringBuffer)10 IOException (java.io.IOException)9 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)9 SourceModule (org.python.pydev.ast.codecompletion.revisited.modules.SourceModule)9 Tuple (org.python.pydev.shared_core.structure.Tuple)9 IFile (org.eclipse.core.resources.IFile)8 IPythonNature (org.python.pydev.core.IPythonNature)8 HashMap (java.util.HashMap)7 ModulesKeyForFolder (org.python.pydev.core.ModulesKeyForFolder)7 IModule (org.python.pydev.core.IModule)6 List (java.util.List)5 IDocument (org.eclipse.jface.text.IDocument)5 AbstractModule (org.python.pydev.ast.codecompletion.revisited.modules.AbstractModule)5 AbstractAdditionalTokensInfo (com.python.pydev.analysis.additionalinfo.AbstractAdditionalTokensInfo)4