Search in sources :

Example 1 with IModuleRequestState

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

the class AbstractAdditionalDependencyInfo method updateKeysIfNeededAndSave.

/**
 * If info == null we're dealing with project info (otherwise we're dealing with interpreter info).
 *
 * The main difference is that we don't index builtin modules for projects (but maybe we should?). Still,
 * to index builtin modules we have to be a bit more careful, especially on changes (i.e.: when a builtin
 * becomes a source module and vice-versa).
 */
public void updateKeysIfNeededAndSave(PyPublicTreeMap<ModulesKey, ModulesKey> keysFound, InterpreterInfo info, IProgressMonitor monitor) {
    Map<CompleteIndexKey, CompleteIndexKey> keys = this.completeIndex.keys();
    ArrayList<ModulesKey> newKeys = new ArrayList<ModulesKey>();
    ArrayList<ModulesKey> removedKeys = new ArrayList<ModulesKey>();
    // temporary
    CompleteIndexKey tempKey = new CompleteIndexKey((ModulesKey) null);
    boolean isJython = info != null ? info.getInterpreterType() == IInterpreterManager.INTERPRETER_TYPE_JYTHON : true;
    Iterator<ModulesKey> it = keysFound.values().iterator();
    while (it.hasNext()) {
        ModulesKey next = it.next();
        if (next.file != null) {
            // Can be a .pyd or a .py
            long lastModified = FileUtils.lastModified(next.file);
            if (lastModified != 0) {
                tempKey.key = next;
                CompleteIndexKey completeIndexKey = keys.get(tempKey);
                if (completeIndexKey == null) {
                    newKeys.add(next);
                } else {
                    if (completeIndexKey.lastModified != lastModified) {
                        // Just re-add it if the time changed!
                        newKeys.add(next);
                    }
                }
            }
        } else {
            // at this point, it's always a compiled module (forced builtin), so, we can't check if it was modified (just re-add it).
            tempKey.key = next;
            CompleteIndexKey completeIndexKey = keys.get(tempKey);
            if (completeIndexKey == null) {
                // Only add if it's not there already.
                newKeys.add(next);
            }
        }
    }
    Iterator<CompleteIndexKey> it2 = keys.values().iterator();
    while (it2.hasNext()) {
        CompleteIndexKey next = it2.next();
        if (!keysFound.containsKey(next.key)) {
            removedKeys.add(next.key);
        }
    }
    boolean hasNew = newKeys.size() != 0;
    boolean hasRemoved = removedKeys.size() != 0;
    modulesAddedAndRemoved.call(new Tuple(newKeys, removedKeys));
    Set<File> ignoreFiles = new HashSet<File>();
    // Remove first!
    if (hasRemoved) {
        for (ModulesKey removedKey : removedKeys) {
            // Don't generate deltas (we'll save it in the end).
            this.removeInfoFromModule(removedKey.name, false);
        }
    }
    // Add last (a module could be removed/added).
    if (hasNew) {
        FastStringBuffer buffer = new FastStringBuffer();
        int currI = 0;
        int total = newKeys.size();
        IModuleRequestState moduleRequest = new BaseModuleRequest(true);
        for (ModulesKey newKey : newKeys) {
            currI += 1;
            if (monitor.isCanceled()) {
                return;
            }
            if (PythonPathHelper.canAddAstInfoForSourceModule(newKey)) {
                buffer.clear().append("Indexing ").append(currI).append(" of ").append(total).append(" (source module): ").append(newKey.name).append("  (").append(currI).append(" of ").append(total).append(")");
                try {
                    // Don't generate deltas (we'll save it in the end).
                    this.addAstInfo(newKey, false);
                } catch (Exception e) {
                    Log.log(e);
                }
            } else {
                if (info != null) {
                    if (isJython && ignoreFiles.contains(newKey.file)) {
                        continue;
                    }
                    buffer.clear().append("Indexing ").append(currI).append(" of ").append(total).append(" (builtin module): ").append(newKey.name);
                    monitor.setTaskName(buffer.toString());
                    IModule builtinModule = info.getModulesManager().getModule(newKey.name, info.getModulesManager().getNature(), true, moduleRequest);
                    if (builtinModule != null) {
                        if (builtinModule instanceof IAbstractJavaClassModule) {
                            if (newKey.file != null) {
                                ignoreFiles.add(newKey.file);
                            } else {
                                Log.log("Not expecting null file for java class module: " + newKey);
                            }
                            continue;
                        }
                        boolean removeFirst = keys.containsKey(new CompleteIndexKey(newKey));
                        addAstForCompiledModule(builtinModule, info, newKey, removeFirst);
                    }
                }
            }
        }
    }
    if (hasNew || hasRemoved) {
        if (DebugSettings.DEBUG_INTERPRETER_AUTO_UPDATE) {
            org.python.pydev.shared_core.log.ToLogFile.toLogFile(this, StringUtils.format("Additional info modules. Added: %s Removed: %s", newKeys, removedKeys));
        }
        save();
    }
}
Also used : IModule(org.python.pydev.core.IModule) FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer) IModuleRequestState(org.python.pydev.core.IModuleRequestState) BaseModuleRequest(org.python.pydev.core.BaseModuleRequest) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) MisconfigurationException(org.python.pydev.core.MisconfigurationException) CompleteIndexKey(org.python.pydev.core.cache.CompleteIndexKey) ModulesKey(org.python.pydev.core.ModulesKey) File(java.io.File) Tuple(org.python.pydev.shared_core.structure.Tuple) IAbstractJavaClassModule(org.python.pydev.ast.codecompletion.revisited.modules.IAbstractJavaClassModule) HashSet(java.util.HashSet)

Example 2 with IModuleRequestState

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

the class AbstractRenameWorkspaceRefactorProcess method doCheckInitialOnWorkspace.

/**
 * This method is made to be used in the checkInitialOnWorkspace implementation.
 *
 * It will find files with possible references in the workspace (from the token
 * name we're searching) and for each file that maps to a module it will
 * call getOccurrencesInOtherModule, and will add those occurrences to
 * the map with the file pointing to the entries.
 *
 * @param status used to add some error status to the refactoring
 * @param request the request used for the refactoring
 */
protected void doCheckInitialOnWorkspace(RefactoringStatus status, RefactoringRequest request) {
    try {
        request.getMonitor().beginTask("Check references on workspace", 100);
        List<Tuple<List<ModulesKey>, IPythonNature>> references;
        try {
            request.pushMonitor(new SubProgressMonitor(request.getMonitor(), 90));
            references = findFilesWithPossibleReferences(request);
            if (request.getMonitor().isCanceled()) {
                return;
            }
        } finally {
            request.popMonitor().done();
        }
        int total = references.size();
        try {
            request.pushMonitor(new SubProgressMonitor(request.getMonitor(), 10));
            request.getMonitor().beginTask("Analyzing references found", total);
            int i = 0;
            IModuleRequestState moduleRequest = new BaseModuleRequest(request.acceptTypeshed);
            for (Tuple<List<ModulesKey>, IPythonNature> file : references) {
                i++;
                request.communicateWork(StringUtils.format("Analyzing %s (%s of %s)", file.o2.getProject(), i, total));
                PythonNature nature = (PythonNature) file.o2;
                if (nature != null) {
                    if (!nature.startRequests()) {
                        continue;
                    }
                    try {
                        for (ModulesKey key : file.o1) {
                            IProjectModulesManager modulesManager = (IProjectModulesManager) nature.getAstManager().getModulesManager();
                            request.checkCancelled();
                            String modName = key.name;
                            if (modName != null) {
                                if (!request.moduleName.equals(modName)) {
                                    // we've already checked the module from the request...
                                    request.checkCancelled();
                                    IModule module = modulesManager.getModuleInDirectManager(modName, nature, false, moduleRequest);
                                    if (module instanceof SourceModule) {
                                        SourceModule sourceModule = (SourceModule) module;
                                        if (sourceModule.getAst() == null) {
                                            status.addWarning("Unable to get AST for: " + modName);
                                            continue;
                                        }
                                        request.checkCancelled();
                                        List<ASTEntry> entryOccurrences = getOccurrencesInOtherModule(status, request, request.qualifier, (SourceModule) module, nature);
                                        if (entryOccurrences.size() > 0) {
                                            addOccurrences(entryOccurrences, key.file, modName);
                                        }
                                    }
                                }
                            }
                        }
                    } finally {
                        nature.endRequests();
                    }
                }
            }
        } finally {
            request.popMonitor().done();
        }
    } catch (OperationCanceledException e) {
    // that's ok
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        request.getMonitor().done();
    }
}
Also used : IProjectModulesManager(org.python.pydev.core.IProjectModulesManager) SourceModule(org.python.pydev.ast.codecompletion.revisited.modules.SourceModule) IModule(org.python.pydev.core.IModule) IModuleRequestState(org.python.pydev.core.IModuleRequestState) IPythonNature(org.python.pydev.core.IPythonNature) PythonNature(org.python.pydev.plugin.nature.PythonNature) BaseModuleRequest(org.python.pydev.core.BaseModuleRequest) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IPythonNature(org.python.pydev.core.IPythonNature) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) ModulesKey(org.python.pydev.core.ModulesKey) List(java.util.List) Tuple(org.python.pydev.shared_core.structure.Tuple)

Aggregations

BaseModuleRequest (org.python.pydev.core.BaseModuleRequest)2 IModule (org.python.pydev.core.IModule)2 IModuleRequestState (org.python.pydev.core.IModuleRequestState)2 ModulesKey (org.python.pydev.core.ModulesKey)2 Tuple (org.python.pydev.shared_core.structure.Tuple)2 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)1 IAbstractJavaClassModule (org.python.pydev.ast.codecompletion.revisited.modules.IAbstractJavaClassModule)1 SourceModule (org.python.pydev.ast.codecompletion.revisited.modules.SourceModule)1 IProjectModulesManager (org.python.pydev.core.IProjectModulesManager)1 IPythonNature (org.python.pydev.core.IPythonNature)1 MisconfigurationException (org.python.pydev.core.MisconfigurationException)1 CompleteIndexKey (org.python.pydev.core.cache.CompleteIndexKey)1 ASTEntry (org.python.pydev.parser.visitors.scope.ASTEntry)1