Search in sources :

Example 1 with AdditionalProjectInterpreterInfo

use of com.python.pydev.analysis.additionalinfo.AdditionalProjectInterpreterInfo in project Pydev by fabioz.

the class GlobalsTwoPanelElementSelector2 method fillContentProvider.

/**
 * This is the place where we put all the info in the content provider. Note that here we must add
 * ALL the info -- later, we'll filter it based on the active working set.
 */
@Override
protected void fillContentProvider(AbstractContentProvider contentProvider, ItemsFilter itemsFilter, IProgressMonitor progressMonitor) throws CoreException {
    if (itemsFilter instanceof InfoFilter) {
        if (progressMonitor != null) {
            progressMonitor.beginTask("Searching...", this.additionalInfo.size());
        }
        for (AbstractAdditionalTokensInfo additionalInfo : this.additionalInfo) {
            if (progressMonitor != null) {
                if (progressMonitor.isCanceled()) {
                    return;
                } else {
                    progressMonitor.worked(1);
                }
            }
            // no duplicates
            Collection<IInfo> allTokens = new HashSet<IInfo>(additionalInfo.getAllTokens());
            for (IInfo iInfo : allTokens) {
                contentProvider.add(new AdditionalInfoAndIInfo(additionalInfo, iInfo), itemsFilter);
            }
            // Also show to the user the modules available as globals (2.2.3)
            IModulesManager modulesManager = null;
            try {
                if (additionalInfo instanceof AdditionalProjectInterpreterInfo) {
                    AdditionalProjectInterpreterInfo projectInterpreterInfo = (AdditionalProjectInterpreterInfo) additionalInfo;
                    IProject project = projectInterpreterInfo.getProject();
                    PythonNature nature = PythonNature.getPythonNature(project);
                    if (nature != null) {
                        ICodeCompletionASTManager astManager = nature.getAstManager();
                        if (astManager != null) {
                            modulesManager = astManager.getModulesManager();
                        }
                    }
                } else if (additionalInfo instanceof AdditionalSystemInterpreterInfo) {
                    AdditionalSystemInterpreterInfo systemInterpreterInfo = (AdditionalSystemInterpreterInfo) additionalInfo;
                    IInterpreterInfo defaultInterpreterInfo = systemInterpreterInfo.getManager().getDefaultInterpreterInfo(false);
                    modulesManager = defaultInterpreterInfo.getModulesManager();
                }
            } catch (Throwable e) {
                Log.log(e);
            }
            if (modulesManager != null) {
                SortedMap<ModulesKey, ModulesKey> allDirectModulesStartingWith = modulesManager.getAllDirectModulesStartingWith("");
                Collection<ModulesKey> values = allDirectModulesStartingWith.values();
                for (ModulesKey modulesKey : values) {
                    contentProvider.add(new AdditionalInfoAndIInfo(additionalInfo, new ModInfo(modulesKey.name, modulesManager.getNature(), modulesKey.file != null ? modulesKey.file.toString() : null, 1, 1)), itemsFilter);
                }
            }
        }
    }
    if (progressMonitor != null) {
        progressMonitor.done();
    }
}
Also used : IPythonNature(org.python.pydev.core.IPythonNature) PythonNature(org.python.pydev.plugin.nature.PythonNature) IModulesManager(org.python.pydev.core.IModulesManager) IProject(org.eclipse.core.resources.IProject) AdditionalSystemInterpreterInfo(com.python.pydev.analysis.additionalinfo.AdditionalSystemInterpreterInfo) AdditionalInfoAndIInfo(com.python.pydev.analysis.additionalinfo.AdditionalInfoAndIInfo) IInfo(org.python.pydev.core.IInfo) AdditionalProjectInterpreterInfo(com.python.pydev.analysis.additionalinfo.AdditionalProjectInterpreterInfo) ICodeCompletionASTManager(org.python.pydev.core.ICodeCompletionASTManager) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) AdditionalInfoAndIInfo(com.python.pydev.analysis.additionalinfo.AdditionalInfoAndIInfo) ModulesKey(org.python.pydev.core.ModulesKey) AbstractAdditionalTokensInfo(com.python.pydev.analysis.additionalinfo.AbstractAdditionalTokensInfo) ModInfo(com.python.pydev.analysis.additionalinfo.ModInfo) HashSet(java.util.HashSet)

Example 2 with AdditionalProjectInterpreterInfo

use of com.python.pydev.analysis.additionalinfo.AdditionalProjectInterpreterInfo in project Pydev by fabioz.

the class RefactorerFindReferences method findPossibleReferences.

/**
 * Find the references that may have the text we're looking for.
 *
 * @param request the request with the info for the find
 * @return an array of IFile with the files that may have the references we're
 * interested about (note that those may not actually contain the matches we're
 * interested in -- it is just a helper to refine our search).
 */
public List<Tuple<List<ModulesKey>, IPythonNature>> findPossibleReferences(RefactoringRequest request) throws OperationCanceledException {
    String initialName = request.qualifier;
    List<Tuple<List<ModulesKey>, IPythonNature>> ret = request.getPossibleReferences(initialName);
    if (ret != null) {
        return ret;
    }
    if (FORCED_RETURN != null) {
        ret = new ArrayList<Tuple<List<ModulesKey>, IPythonNature>>();
        for (Tuple<List<ModulesKey>, IPythonNature> f : FORCED_RETURN) {
            // only for testing purposes
            for (ModulesKey k : f.o1) {
                String object = FileUtils.getFileContents(k.file);
                if (object.indexOf(request.qualifier) != -1) {
                    ret.add(new Tuple<List<ModulesKey>, IPythonNature>(Arrays.asList(k), f.o2));
                }
            }
        }
        return ret;
    }
    ret = new ArrayList<Tuple<List<ModulesKey>, IPythonNature>>();
    try {
        try {
            IProject project = request.nature.getProject();
            List<Tuple<AbstractAdditionalTokensInfo, IPythonNature>> infoAndNature = null;
            if (project == null) {
                if (request.nature instanceof SystemPythonNature) {
                    SystemPythonNature systemPythonNature = (SystemPythonNature) request.nature;
                    int interpreterType = systemPythonNature.getInterpreterType();
                    List<IPythonNature> naturesRelatedTo = PythonNature.getPythonNaturesRelatedTo(interpreterType);
                    infoAndNature = new ArrayList<Tuple<AbstractAdditionalTokensInfo, IPythonNature>>();
                    for (IPythonNature iPythonNature : naturesRelatedTo) {
                        if (iPythonNature.getProject() != null && iPythonNature.getProject().isAccessible()) {
                            AbstractAdditionalTokensInfo o1 = AdditionalProjectInterpreterInfo.getAdditionalInfoForProject(iPythonNature);
                            if (o1 != null) {
                                infoAndNature.add(new Tuple<AbstractAdditionalTokensInfo, IPythonNature>(o1, iPythonNature));
                            }
                        }
                    }
                }
            } else {
                infoAndNature = AdditionalProjectInterpreterInfo.getAdditionalInfoAndNature(request.nature, false, true, true);
            }
            if (infoAndNature == null || infoAndNature.size() == 0) {
                return ret;
            }
            // long initial = System.currentTimeMillis();
            request.getMonitor().beginTask("Find possible references", infoAndNature.size());
            request.getMonitor().setTaskName("Find possible references");
            try {
                for (Tuple<AbstractAdditionalTokensInfo, IPythonNature> tuple : infoAndNature) {
                    try {
                        SubProgressMonitor sub = new SubProgressMonitor(request.getMonitor(), 1);
                        request.pushMonitor(sub);
                        if (tuple.o1 instanceof AdditionalProjectInterpreterInfo && tuple.o2 != null) {
                            AdditionalProjectInterpreterInfo info = (AdditionalProjectInterpreterInfo) tuple.o1;
                            List<ModulesKey> modulesWithToken = info.getModulesWithToken(initialName, sub);
                            if (sub.isCanceled()) {
                                break;
                            }
                            ret.add(new Tuple<List<ModulesKey>, IPythonNature>(modulesWithToken, tuple.o2));
                        }
                    } finally {
                        request.popMonitor().done();
                    }
                }
            } finally {
                request.getMonitor().done();
            }
        // System.out.println("Total: " + ((System.currentTimeMillis() - initial) / 1000.));
        } catch (MisconfigurationException e) {
            throw new RuntimeException(e);
        }
    } catch (OperationCanceledException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    request.setPossibleReferences(initialName, ret);
    return ret;
}
Also used : MisconfigurationException(org.python.pydev.core.MisconfigurationException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IPythonNature(org.python.pydev.core.IPythonNature) SystemPythonNature(org.python.pydev.plugin.nature.SystemPythonNature) IProject(org.eclipse.core.resources.IProject) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) MisconfigurationException(org.python.pydev.core.MisconfigurationException) AdditionalProjectInterpreterInfo(com.python.pydev.analysis.additionalinfo.AdditionalProjectInterpreterInfo) ModulesKey(org.python.pydev.core.ModulesKey) ArrayList(java.util.ArrayList) List(java.util.List) AbstractAdditionalTokensInfo(com.python.pydev.analysis.additionalinfo.AbstractAdditionalTokensInfo) Tuple(org.python.pydev.shared_core.structure.Tuple)

Example 3 with AdditionalProjectInterpreterInfo

use of com.python.pydev.analysis.additionalinfo.AdditionalProjectInterpreterInfo in project Pydev by fabioz.

the class RefactorerFinds method findLikelyModulesWithChildren.

private HashSet<SourceModule> findLikelyModulesWithChildren(RefactoringRequest request, HierarchyNodeModel model, List<AbstractAdditionalDependencyInfo> infoForProject) {
    // get the modules that are most likely to have that declaration.
    HashSet<SourceModule> modulesToAnalyze = new HashSet<SourceModule>();
    for (AbstractAdditionalDependencyInfo additionalInfo : infoForProject) {
        IProgressMonitor monitor = request.getMonitor();
        if (monitor == null) {
            monitor = new NullProgressMonitor();
        }
        monitor.beginTask("Find likely modules with children", 100);
        monitor.setTaskName("Searching: " + model.name);
        try {
            List<ModulesKey> modules;
            try {
                request.pushMonitor(new SubProgressMonitor(monitor, 90));
                if (additionalInfo instanceof AdditionalProjectInterpreterInfo) {
                    AdditionalProjectInterpreterInfo additionalProjectInterpreterInfo = (AdditionalProjectInterpreterInfo) additionalInfo;
                    modules = additionalProjectInterpreterInfo.getModulesWithToken(model.name, monitor);
                } else {
                    continue;
                }
                if (monitor.isCanceled()) {
                    throw new OperationCanceledException();
                }
            } finally {
                request.popMonitor().done();
            }
            try {
                request.pushMonitor(new SubProgressMonitor(monitor, 10));
                request.getMonitor().beginTask("Find likely modules with children", modules.size());
                for (ModulesKey declaringModuleName : modules) {
                    if (DEBUG) {
                        System.out.println("findLikelyModulesWithChildren: " + declaringModuleName);
                    }
                    IModule module = null;
                    IPythonNature pythonNature = null;
                    if (additionalInfo instanceof AdditionalProjectInterpreterInfo) {
                        AdditionalProjectInterpreterInfo projectInterpreterInfo = (AdditionalProjectInterpreterInfo) additionalInfo;
                        pythonNature = PythonNature.getPythonNature(projectInterpreterInfo.getProject());
                    }
                    if (pythonNature == null) {
                        pythonNature = request.nature;
                    }
                    BaseModuleRequest moduleRequest = new BaseModuleRequest(request.acceptTypeshed);
                    module = pythonNature.getAstManager().getModule(declaringModuleName.name, pythonNature, false, moduleRequest);
                    if (module == null && pythonNature != request.nature) {
                        module = request.nature.getAstManager().getModule(declaringModuleName.name, request.nature, false, moduleRequest);
                    }
                    if (module instanceof SourceModule) {
                        modulesToAnalyze.add((SourceModule) module);
                    }
                    request.getMonitor().worked(1);
                }
            } finally {
                request.popMonitor().done();
            }
        } finally {
            monitor.done();
        }
    }
    return modulesToAnalyze;
}
Also used : SourceModule(org.python.pydev.ast.codecompletion.revisited.modules.SourceModule) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IModule(org.python.pydev.core.IModule) BaseModuleRequest(org.python.pydev.core.BaseModuleRequest) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IPythonNature(org.python.pydev.core.IPythonNature) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) AdditionalProjectInterpreterInfo(com.python.pydev.analysis.additionalinfo.AdditionalProjectInterpreterInfo) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ModulesKey(org.python.pydev.core.ModulesKey) AbstractAdditionalDependencyInfo(com.python.pydev.analysis.additionalinfo.AbstractAdditionalDependencyInfo) HashSet(java.util.HashSet)

Example 4 with AdditionalProjectInterpreterInfo

use of com.python.pydev.analysis.additionalinfo.AdditionalProjectInterpreterInfo in project Pydev by fabioz.

the class ModuleIInfoLabelProvider method getText.

@Override
public String getText(Object element) {
    if (element instanceof AdditionalInfoAndIInfo) {
        AdditionalInfoAndIInfo additional = (AdditionalInfoAndIInfo) element;
        element = additional.info;
        String suffix = null;
        try {
            if (additional.additionalInfo instanceof AdditionalProjectInterpreterInfo) {
                AdditionalProjectInterpreterInfo projectInterpreterInfo = (AdditionalProjectInterpreterInfo) additional.additionalInfo;
                suffix = projectInterpreterInfo.getProject().getName();
            } else if (additional.additionalInfo instanceof AdditionalSystemInterpreterInfo) {
                AdditionalSystemInterpreterInfo systemInterpreterInfo = (AdditionalSystemInterpreterInfo) additional.additionalInfo;
                suffix = systemInterpreterInfo.getManager().getDefaultInterpreterInfo(false).getName();
            }
        } catch (Throwable e) {
            Log.log(e);
        }
        String iInfoText = getIInfoText((IInfo) element, suffix);
        return iInfoText;
    }
    if (element instanceof String) {
        return (String) element;
    }
    return getIInfoText((IInfo) element, null);
}
Also used : AdditionalProjectInterpreterInfo(com.python.pydev.analysis.additionalinfo.AdditionalProjectInterpreterInfo) AdditionalInfoAndIInfo(com.python.pydev.analysis.additionalinfo.AdditionalInfoAndIInfo) AdditionalSystemInterpreterInfo(com.python.pydev.analysis.additionalinfo.AdditionalSystemInterpreterInfo)

Example 5 with AdditionalProjectInterpreterInfo

use of com.python.pydev.analysis.additionalinfo.AdditionalProjectInterpreterInfo in project Pydev by fabioz.

the class PyGlobalsBrowser method doSelect.

/**
 * @param pythonNatures the natures from were we can get info
 * @param additionalInfo the additional informations
 * @param selectedText the text that should be initially set as a filter
 */
public static void doSelect(List<IPythonNature> pythonNatures, List<AbstractAdditionalTokensInfo> additionalInfo, String selectedText) {
    SelectionDialog dialog = GlobalsDialogFactory.create(EditorUtils.getShell(), additionalInfo, selectedText);
    dialog.open();
    Object[] result = dialog.getResult();
    if (result != null && result.length > 0) {
        for (Object obj : result) {
            IInfo entry;
            if (obj instanceof AdditionalInfoAndIInfo) {
                AdditionalInfoAndIInfo additional = (AdditionalInfoAndIInfo) obj;
                try {
                    // all of the input natures).
                    if (additional.additionalInfo instanceof AdditionalProjectInterpreterInfo) {
                        AdditionalProjectInterpreterInfo projectInterpreterInfo = (AdditionalProjectInterpreterInfo) additional.additionalInfo;
                        IProject project = projectInterpreterInfo.getProject();
                        PythonNature pythonNature = PythonNature.getPythonNature(project);
                        if (pythonNature != null) {
                            pythonNatures = new ArrayList<IPythonNature>();
                            pythonNatures.add(pythonNature);
                        }
                    } else if (additional.additionalInfo instanceof AdditionalSystemInterpreterInfo) {
                        AdditionalSystemInterpreterInfo systemInterpreterInfo = (AdditionalSystemInterpreterInfo) additional.additionalInfo;
                        SystemPythonNature pythonNature = new SystemPythonNature(systemInterpreterInfo.getManager());
                        pythonNatures = new ArrayList<IPythonNature>();
                        pythonNatures.add(pythonNature);
                    }
                } catch (Throwable e) {
                    Log.log(e);
                }
                entry = additional.info;
            } else {
                entry = (IInfo) obj;
            }
            List<ItemPointer> pointers = new ArrayList<ItemPointer>();
            ICompletionState completionCache = new CompletionState();
            for (IPythonNature pythonNature : pythonNatures) {
                // try to find in one of the natures...
                ICodeCompletionASTManager astManager = pythonNature.getAstManager();
                if (astManager == null) {
                    continue;
                }
                AnalysisPlugin.getDefinitionFromIInfo(pointers, astManager, pythonNature, entry, completionCache, false, true);
                if (pointers.size() > 0) {
                    new PyOpenAction().run(pointers.get(0));
                    // don't check the other natures
                    break;
                }
            }
        }
    }
}
Also used : IPythonNature(org.python.pydev.core.IPythonNature) PythonNature(org.python.pydev.plugin.nature.PythonNature) SystemPythonNature(org.python.pydev.plugin.nature.SystemPythonNature) PyOpenAction(org.python.pydev.editor.actions.PyOpenAction) IPythonNature(org.python.pydev.core.IPythonNature) ArrayList(java.util.ArrayList) SystemPythonNature(org.python.pydev.plugin.nature.SystemPythonNature) ICompletionState(org.python.pydev.core.ICompletionState) CompletionState(org.python.pydev.ast.codecompletion.revisited.CompletionState) SelectionDialog(org.eclipse.ui.dialogs.SelectionDialog) IProject(org.eclipse.core.resources.IProject) AdditionalSystemInterpreterInfo(com.python.pydev.analysis.additionalinfo.AdditionalSystemInterpreterInfo) IInfo(org.python.pydev.core.IInfo) AdditionalInfoAndIInfo(com.python.pydev.analysis.additionalinfo.AdditionalInfoAndIInfo) AdditionalProjectInterpreterInfo(com.python.pydev.analysis.additionalinfo.AdditionalProjectInterpreterInfo) ICodeCompletionASTManager(org.python.pydev.core.ICodeCompletionASTManager) AdditionalInfoAndIInfo(com.python.pydev.analysis.additionalinfo.AdditionalInfoAndIInfo) ICompletionState(org.python.pydev.core.ICompletionState) ItemPointer(org.python.pydev.ast.item_pointer.ItemPointer)

Aggregations

AdditionalProjectInterpreterInfo (com.python.pydev.analysis.additionalinfo.AdditionalProjectInterpreterInfo)5 IPythonNature (org.python.pydev.core.IPythonNature)4 AdditionalInfoAndIInfo (com.python.pydev.analysis.additionalinfo.AdditionalInfoAndIInfo)3 AdditionalSystemInterpreterInfo (com.python.pydev.analysis.additionalinfo.AdditionalSystemInterpreterInfo)3 IProject (org.eclipse.core.resources.IProject)3 ModulesKey (org.python.pydev.core.ModulesKey)3 AbstractAdditionalTokensInfo (com.python.pydev.analysis.additionalinfo.AbstractAdditionalTokensInfo)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)2 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)2 ICodeCompletionASTManager (org.python.pydev.core.ICodeCompletionASTManager)2 IInfo (org.python.pydev.core.IInfo)2 PythonNature (org.python.pydev.plugin.nature.PythonNature)2 SystemPythonNature (org.python.pydev.plugin.nature.SystemPythonNature)2 AbstractAdditionalDependencyInfo (com.python.pydev.analysis.additionalinfo.AbstractAdditionalDependencyInfo)1 ModInfo (com.python.pydev.analysis.additionalinfo.ModInfo)1 List (java.util.List)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1