Search in sources :

Example 1 with IProjectModulesManager

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

the class ProjectModulesManager method resolveModule.

/**
 * @see org.python.pydev.core.IProjectModulesManager#resolveModule(java.lang.String, boolean)
 */
@Override
public String resolveModule(String full, boolean checkSystemManager) {
    IModulesManager[] managersInvolved = this.getManagersInvolved(checkSystemManager);
    for (IModulesManager m : managersInvolved) {
        String mod;
        if (m instanceof IProjectModulesManager) {
            IProjectModulesManager pM = (IProjectModulesManager) m;
            mod = pM.resolveModuleInDirectManager(full);
        } else {
            mod = m.resolveModule(full);
        }
        if (mod != null) {
            return mod;
        }
    }
    return null;
}
Also used : IProjectModulesManager(org.python.pydev.core.IProjectModulesManager) IModulesManager(org.python.pydev.core.IModulesManager)

Example 2 with IProjectModulesManager

use of org.python.pydev.core.IProjectModulesManager 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)

Example 3 with IProjectModulesManager

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

the class ProjectModulesManager method getModuleAndRelatedModulesManager.

/**
 * @return a tuple with the IModule requested and the IModulesManager that contained that module.
 */
@Override
public Tuple<IModule, IModulesManager> getModuleAndRelatedModulesManager(String name, IPythonNature nature, boolean checkSystemManager, boolean dontSearchInit, IModuleRequestState moduleRequest) {
    IModule module = null;
    // only get the system manager here (to avoid recursion)
    IModulesManager[] managersInvolved = this.getManagersInvolved(true);
    for (IModulesManager m : managersInvolved) {
        if (m instanceof ISystemModulesManager) {
            module = ((ISystemModulesManager) m).getBuiltinModule(name, dontSearchInit, moduleRequest);
            if (module != null) {
                if (DEBUG_MODULES) {
                    System.out.println("Trying to get:" + name + " - " + " returned builtin:" + module + " - " + m.getClass());
                }
                return new Tuple<IModule, IModulesManager>(module, m);
            }
        }
    }
    for (IModulesManager m : managersInvolved) {
        if (m instanceof IProjectModulesManager) {
            IProjectModulesManager pM = (IProjectModulesManager) m;
            module = pM.getModuleInDirectManager(name, nature, dontSearchInit, moduleRequest);
        } else if (m instanceof ISystemModulesManager) {
            ISystemModulesManager systemModulesManager = (ISystemModulesManager) m;
            module = systemModulesManager.getModuleWithoutBuiltins(name, nature, dontSearchInit, moduleRequest);
        } else {
            throw new RuntimeException("Unexpected: " + m);
        }
        if (module != null) {
            if (DEBUG_MODULES) {
                System.out.println("Trying to get:" + name + " - " + " returned:" + module + " - " + m.getClass());
            }
            return new Tuple<IModule, IModulesManager>(module, m);
        }
    }
    if (DEBUG_MODULES) {
        System.out.println("Trying to get:" + name + " - " + " returned:null - " + this.getClass());
    }
    return null;
}
Also used : IProjectModulesManager(org.python.pydev.core.IProjectModulesManager) IModule(org.python.pydev.core.IModule) ISystemModulesManager(org.python.pydev.core.ISystemModulesManager) IModulesManager(org.python.pydev.core.IModulesManager) Tuple(org.python.pydev.shared_core.structure.Tuple)

Example 4 with IProjectModulesManager

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

the class RefactoringRenameTestBase method getReferencesForRename.

/**
 * Goes through all the workspace (in this case the refactoring project) and gathers the references
 * for the current selection.
 *
 * @param moduleName the name of the module we're currently in
 * @param line the line we're in
 * @param col the col we're in
 * @param expectError whether we are expecting some error or not
 * @return a map with the name of the module and the file representing it pointing to the
 * references found in that module.
 */
protected Map<Tuple<String, File>, HashSet<ASTEntry>> getReferencesForRename(String moduleName, int line, int col, boolean expectError) {
    Map<Tuple<String, File>, HashSet<ASTEntry>> occurrencesToReturn = null;
    try {
        IProjectModulesManager modulesManager = (IProjectModulesManager) natureRefactoring.getAstManager().getModulesManager();
        IModule module = modulesManager.getModuleInDirectManager(moduleName, natureRefactoring, true, new BaseModuleRequest(true));
        if (module == null) {
            throw new RuntimeException("Unable to get source module for module:" + moduleName);
        }
        String strDoc = FileUtils.getFileContents(module.getFile());
        Document doc = new Document(strDoc);
        PySelection ps = new PySelection(doc, line, col);
        RefactoringRequest request = new RefactoringRequest(null, ps, natureRefactoring);
        request.setAdditionalInfo(RefactoringRequest.FIND_REFERENCES_ONLY_IN_LOCAL_SCOPE, false);
        request.moduleName = moduleName;
        request.inputName = "new_name";
        request.fillActivationTokenAndQualifier();
        PyRenameEntryPoint processor = new PyRenameEntryPoint(request);
        NullProgressMonitor nullProgressMonitor = new NullProgressMonitor();
        checkStatus(processor.checkInitialConditions(nullProgressMonitor), expectError);
        lastProcessorUsed = processor;
        checkProcessors();
        checkStatus(processor.checkFinalConditions(nullProgressMonitor, null), expectError);
        occurrencesToReturn = processor.getOccurrencesInOtherFiles();
        occurrencesToReturn.put(new Tuple<String, File>(moduleName, module.getFile()), processor.getOccurrences());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return occurrencesToReturn;
}
Also used : IProjectModulesManager(org.python.pydev.core.IProjectModulesManager) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IModule(org.python.pydev.core.IModule) RefactoringRequest(org.python.pydev.ast.refactoring.RefactoringRequest) BaseModuleRequest(org.python.pydev.core.BaseModuleRequest) Document(org.eclipse.jface.text.Document) MisconfigurationException(org.python.pydev.core.MisconfigurationException) PyRenameEntryPoint(com.python.pydev.analysis.refactoring.wizards.rename.PyRenameEntryPoint) PySelection(org.python.pydev.core.docutils.PySelection) IFile(org.eclipse.core.resources.IFile) File(java.io.File) Tuple(org.python.pydev.shared_core.structure.Tuple) HashSet(java.util.HashSet)

Example 5 with IProjectModulesManager

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

the class PythonBaseModelProvider method getChildrenForIWrappedResource.

/**
 * @param wrappedResourceParent: this is the parent that is an IWrappedResource (which means
 * that children will also be IWrappedResources)
 *
 * @return the children (an array of IWrappedResources)
 */
private Object[] getChildrenForIWrappedResource(IWrappedResource wrappedResourceParent) {
    // ------------------------------------------------------------------- get python nature
    PythonNature nature = null;
    Object[] childrenToReturn = null;
    Object obj = wrappedResourceParent.getActualObject();
    IProject project = null;
    if (obj instanceof IResource) {
        IResource resource = (IResource) obj;
        project = resource.getProject();
        if (project != null && project.isOpen()) {
            nature = PythonNature.getPythonNature(project);
        }
    }
    // ------------------------------------------------------------------- treat python nodes
    if (wrappedResourceParent instanceof PythonNode) {
        PythonNode node = (PythonNode) wrappedResourceParent;
        childrenToReturn = getChildrenFromParsedItem(wrappedResourceParent, node.entry, node.pythonFile);
    // ------------------------------------- treat python files (add the classes/methods,etc)
    } else if (wrappedResourceParent instanceof PythonFile) {
        // if it's a file, we want to show the classes and methods
        PythonFile file = (PythonFile) wrappedResourceParent;
        if (PythonPathHelper.isValidSourceFile(file.getActualObject())) {
            if (nature != null) {
                ICodeCompletionASTManager astManager = nature.getAstManager();
                // the nature may still not be completely restored...
                if (astManager != null) {
                    IModulesManager modulesManager = astManager.getModulesManager();
                    if (modulesManager instanceof IProjectModulesManager) {
                        IProjectModulesManager projectModulesManager = (IProjectModulesManager) modulesManager;
                        String moduleName = projectModulesManager.resolveModuleInDirectManager(file.getActualObject());
                        if (moduleName != null) {
                            BaseModuleRequest moduleRequest = new BaseModuleRequest(true);
                            IModule module = projectModulesManager.getModuleInDirectManager(moduleName, nature, true, moduleRequest);
                            if (module == null) {
                                // ok, something strange happened... it shouldn't be null... maybe empty, but not null at this point
                                // so, if it exists, let's try to create it...
                                // TODO: This should be moved to somewhere else.
                                String resourceOSString = SharedCorePlugin.getIResourceOSString(file.getActualObject());
                                if (resourceOSString != null) {
                                    File f = new File(resourceOSString);
                                    if (f.exists()) {
                                        projectModulesManager.addModule(new ModulesKey(moduleName, f));
                                        module = projectModulesManager.getModuleInDirectManager(moduleName, nature, true, moduleRequest);
                                    }
                                }
                            }
                            if (module instanceof SourceModule) {
                                SourceModule sourceModule = (SourceModule) module;
                                OutlineCreatorVisitor visitor = OutlineCreatorVisitor.create(sourceModule.getAst());
                                ParsedItem root = new ParsedItem(visitor.getAll().toArray(new ASTEntryWithChildren[0]), null);
                                childrenToReturn = getChildrenFromParsedItem(wrappedResourceParent, root, file);
                            }
                        }
                    }
                }
            }
        }
    } else // ------------------------------------------------------------- treat folders and others
    {
        Object[] children = super.getChildren(wrappedResourceParent.getActualObject());
        childrenToReturn = wrapChildren(wrappedResourceParent, wrappedResourceParent.getSourceFolder(), children);
    }
    return childrenToReturn;
}
Also used : IProjectModulesManager(org.python.pydev.core.IProjectModulesManager) SourceModule(org.python.pydev.ast.codecompletion.revisited.modules.SourceModule) IModule(org.python.pydev.core.IModule) IPythonNature(org.python.pydev.core.IPythonNature) PythonNature(org.python.pydev.plugin.nature.PythonNature) BaseModuleRequest(org.python.pydev.core.BaseModuleRequest) PythonNode(org.python.pydev.navigator.elements.PythonNode) IModulesManager(org.python.pydev.core.IModulesManager) PythonFile(org.python.pydev.navigator.elements.PythonFile) IProject(org.eclipse.core.resources.IProject) ICodeCompletionASTManager(org.python.pydev.core.ICodeCompletionASTManager) IParsedItem(org.python.pydev.shared_ui.outline.IParsedItem) ParsedItem(org.python.pydev.outline.ParsedItem) ModulesKey(org.python.pydev.core.ModulesKey) PythonFile(org.python.pydev.navigator.elements.PythonFile) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IResource(org.eclipse.core.resources.IResource) OutlineCreatorVisitor(org.python.pydev.parser.visitors.scope.OutlineCreatorVisitor)

Aggregations

IProjectModulesManager (org.python.pydev.core.IProjectModulesManager)6 IModule (org.python.pydev.core.IModule)5 BaseModuleRequest (org.python.pydev.core.BaseModuleRequest)4 Tuple (org.python.pydev.shared_core.structure.Tuple)4 File (java.io.File)3 IModulesManager (org.python.pydev.core.IModulesManager)3 PyRenameEntryPoint (com.python.pydev.analysis.refactoring.wizards.rename.PyRenameEntryPoint)2 HashSet (java.util.HashSet)2 IFile (org.eclipse.core.resources.IFile)2 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 SourceModule (org.python.pydev.ast.codecompletion.revisited.modules.SourceModule)2 IPythonNature (org.python.pydev.core.IPythonNature)2 ModulesKey (org.python.pydev.core.ModulesKey)2 PythonNature (org.python.pydev.plugin.nature.PythonNature)2 List (java.util.List)1 IProject (org.eclipse.core.resources.IProject)1 IResource (org.eclipse.core.resources.IResource)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)1 Document (org.eclipse.jface.text.Document)1