Search in sources :

Example 6 with AbstractAdditionalTokensInfo

use of com.python.pydev.analysis.additionalinfo.AbstractAdditionalTokensInfo 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 7 with AbstractAdditionalTokensInfo

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

the class NatureGroup method calculateChildren.

@Override
protected void calculateChildren() throws MisconfigurationException {
    ICodeCompletionASTManager astManager = nature.getAstManager();
    if (astManager == null) {
        addLeaf("AST manager == null (should happen only in the plugin initialization) -- skipping other checks.");
        return;
    }
    IModulesManager projectModulesManager = astManager.getModulesManager();
    if (projectModulesManager == null) {
        addLeaf("Modules manager == null (should happen only in the plugin initialization) -- skipping other checks.");
        return;
    }
    PythonPathHelper pythonPathHelper = (PythonPathHelper) projectModulesManager.getPythonPathHelper();
    if (pythonPathHelper == null) {
        addLeaf("PythonPathHelper == null (should happen only in the plugin initialization) -- skipping other checks.");
        return;
    }
    List<String> pythonpath = pythonPathHelper.getPythonpath();
    for (String s : pythonpath) {
        addLeaf("PYTHONPATH: " + s);
    }
    HashSet<ModulesKey> expectedModuleNames = new HashSet<ModulesKey>();
    for (String string : pythonpath) {
        File file = new File(string);
        if (file.isDirectory()) {
            // TODO: Handle zip file modules!
            Collection<PyFileInfo> modulesBelow = PythonPathHelper.getModulesBelow(file, new NullProgressMonitor(), pythonpath).getFoundPyFileInfos();
            for (PyFileInfo fileInfo : modulesBelow) {
                File moduleFile = fileInfo.getFile();
                String modName = pythonPathHelper.resolveModule(FileUtils.getFileAbsolutePath(moduleFile), true, nature.getProject());
                if (modName != null) {
                    expectedModuleNames.add(new ModulesKey(modName, moduleFile));
                } else {
                    if (PythonPathHelper.isValidModuleLastPart(StringUtils.stripExtension((moduleFile.getName())))) {
                        addLeaf(StringUtils.format("Unable to resolve module: %s (gotten null module name)", moduleFile));
                    }
                }
            }
        } else {
            if (!file.exists()) {
                addLeaf(StringUtils.format("File %s is referenced in the pythonpath but does not exist.", file));
            } else {
                addLeaf(org.python.pydev.shared_core.string.StringUtils.format("File %s not handled (TODO: Fix zip files support in the viewer).", file));
            }
        }
    }
    IntegrityInfo info = new IntegrityInfo();
    info.nature = nature;
    ModulesKey[] onlyDirectModules = projectModulesManager.getOnlyDirectModules();
    TreeSet<ModulesKey> inModulesManager = new TreeSet<ModulesKey>(Arrays.asList(onlyDirectModules));
    Set<String> allAdditionalInfoModuleNames = new TreeSet<String>();
    List<Tuple<AbstractAdditionalTokensInfo, IPythonNature>> additionalInfoAndNature = AdditionalProjectInterpreterInfo.getAdditionalInfoAndNature(nature, false, false, false);
    AbstractAdditionalTokensInfo additionalProjectInfo;
    if (additionalInfoAndNature.size() == 0) {
        addChild(new LeafElement(this, "No additional infos found (1 expected) -- skipping other checks."));
        return;
    } else {
        if (additionalInfoAndNature.size() > 1) {
            addChild(new LeafElement(this, StringUtils.format("%s additional infos found (only 1 expected) -- continuing checks but analysis may be wrong.", additionalInfoAndNature.size())));
        }
        additionalProjectInfo = additionalInfoAndNature.get(0).o1;
        allAdditionalInfoModuleNames.addAll(additionalProjectInfo.getAllModulesWithTokens());
    }
    for (ModulesKey key : inModulesManager) {
        if (!expectedModuleNames.contains(key)) {
            info.modulesNotInDisk.add(key);
            addChild(new LeafElement(this, StringUtils.format("%s exists in memory but not in the disk.", key)));
        }
    }
    ModulesKey tempKey = new ModulesKey(null, null);
    for (String s : allAdditionalInfoModuleNames) {
        tempKey.name = s;
        if (!expectedModuleNames.contains(tempKey)) {
            info.additionalModulesNotInDisk.add(s);
            addChild(new LeafElement(this, StringUtils.format("%s exists in the additional info but not in the disk.", s)));
        }
    }
    for (ModulesKey key : expectedModuleNames) {
        boolean isInModulesManager = inModulesManager.contains(key);
        if (!isInModulesManager) {
            info.modulesNotInMemory.add(key);
            addChild(new LeafElement(this, StringUtils.format("%s exists in the disk but not in memory.", key)));
        }
        if (!allAdditionalInfoModuleNames.contains(key.name)) {
            try {
                AbstractModule mod = AbstractModule.createModule(key.name, key.file, info.nature, true);
                if (!(mod instanceof SourceModule)) {
                    continue;
                }
                SourceModule module = (SourceModule) mod;
                if (module == null || module.getAst() == null) {
                    addChild(new LeafElement(this, StringUtils.format("Warning: cannot parse: %s - %s (so, it's ok not having additional info on it)", key.name, key.file)));
                } else {
                    try {
                        Iterator<ASTEntry> innerEntriesForAST = AbstractAdditionalDependencyInfo.getInnerEntriesForAST(module.getAst()).o2;
                        if (innerEntriesForAST.hasNext()) {
                            info.moduleNotInAdditionalInfo.add(module);
                            addChild(new LeafElement(this, StringUtils.format("The additional info index of the module: %s is not updated.", key.name)));
                        }
                    } catch (Exception e) {
                        addChild(new LeafElement(this, StringUtils.format("Unexpected error happened on: %s - %s: %s", key.name, key.file, e.getMessage())));
                    }
                }
            } catch (IOException e) {
                // OK, it cannot be parsed, so, we cannot generate its info
                addChild(new LeafElement(this, StringUtils.format("Warning: cannot parse: %s - %s (so, it's ok not having additional info on it)", key.name, key.file)));
            }
        }
    }
    // modules manager
    if (info.modulesNotInDisk.size() > 0) {
        for (ModulesKey m : info.modulesNotInDisk) {
            addChild(new LeafElement(this, StringUtils.format("FIX: Removing from modules manager: %s", m)));
        }
        projectModulesManager.removeModules(info.modulesNotInDisk);
    }
    for (ModulesKey key : info.modulesNotInMemory) {
        addChild(new LeafElement(this, "FIX: Adding to modules manager: " + key));
        projectModulesManager.addModule(key);
    }
    // additional info
    for (String s : info.additionalModulesNotInDisk) {
        addChild(new LeafElement(this, StringUtils.format("FIX: Removing from additional info: %s", s)));
        additionalProjectInfo.removeInfoFromModule(s, true);
    }
    for (SourceModule mod : info.moduleNotInAdditionalInfo) {
        addChild(new LeafElement(this, StringUtils.format("FIX: Adding to additional info: %s", mod.getName())));
        additionalProjectInfo.addAstInfo(mod.getAst(), mod.getModulesKey(), true);
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) TreeSet(java.util.TreeSet) ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) PyFileInfo(org.python.pydev.ast.listing_utils.PyFileListing.PyFileInfo) HashSet(java.util.HashSet) SourceModule(org.python.pydev.ast.codecompletion.revisited.modules.SourceModule) IModulesManager(org.python.pydev.core.IModulesManager) IOException(java.io.IOException) IOException(java.io.IOException) MisconfigurationException(org.python.pydev.core.MisconfigurationException) PythonPathHelper(org.python.pydev.ast.codecompletion.revisited.PythonPathHelper) AbstractModule(org.python.pydev.ast.codecompletion.revisited.modules.AbstractModule) ICodeCompletionASTManager(org.python.pydev.core.ICodeCompletionASTManager) ModulesKey(org.python.pydev.core.ModulesKey) AbstractAdditionalTokensInfo(com.python.pydev.analysis.additionalinfo.AbstractAdditionalTokensInfo) File(java.io.File) Tuple(org.python.pydev.shared_core.structure.Tuple)

Example 8 with AbstractAdditionalTokensInfo

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

the class StuctureCreationTest method testSetup2.

public void testSetup2() throws Exception {
    AbstractAdditionalTokensInfo additionalInfo = AdditionalProjectInterpreterInfo.getAdditionalInfoForProject(nature);
    assertTrue(additionalInfo.getAllTokens().size() > 0);
    Collection<IInfo> tokensStartingWith = additionalInfo.getTokensStartingWith("MyInvalidClassInInvalidFil", AbstractAdditionalTokensInfo.TOP_LEVEL);
    assertEquals("Expecting no tokens. Found: " + tokensStartingWith, 0, tokensStartingWith.size());
}
Also used : IInfo(org.python.pydev.core.IInfo) AbstractAdditionalTokensInfo(com.python.pydev.analysis.additionalinfo.AbstractAdditionalTokensInfo)

Example 9 with AbstractAdditionalTokensInfo

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

the class StuctureCreationTest method testSetup.

// ------------------------------------------------------------------------------------------------- tests
public void testSetup() {
    // fails on Python >= 2.7 because unittest became a dir instead of one file.
    if (SharedCorePlugin.skipKnownFailures()) {
        return;
    }
    AbstractAdditionalTokensInfo additionalSystemInfo;
    try {
        additionalSystemInfo = AdditionalSystemInterpreterInfo.getAdditionalSystemInfo(getInterpreterManager(), getInterpreterManager().getDefaultInterpreterInfo(false).getExecutableOrJar(), true);
    } catch (MisconfigurationException e) {
        throw new RuntimeException(e);
    }
    assertTrue(additionalSystemInfo.getAllTokens().size() > 0);
    Collection<IInfo> tokensStartingWith = additionalSystemInfo.getTokensStartingWith("TestC", AbstractAdditionalTokensInfo.TOP_LEVEL);
    assertIsIn("TestCase", "unittest", tokensStartingWith);
}
Also used : IInfo(org.python.pydev.core.IInfo) MisconfigurationException(org.python.pydev.core.MisconfigurationException) AbstractAdditionalTokensInfo(com.python.pydev.analysis.additionalinfo.AbstractAdditionalTokensInfo)

Example 10 with AbstractAdditionalTokensInfo

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

the class ParameterCompletionTest method testSetup.

// ------------------------------------------------------------------------------------------------- tests
public void testSetup() throws MisconfigurationException {
    AbstractAdditionalTokensInfo additionalInfo = AdditionalProjectInterpreterInfo.getAdditionalInfoForProject(nature);
    assertTrue(additionalInfo.getAllTokens().size() > 0);
    Collection<IInfo> tokensStartingWith = additionalInfo.getTokensStartingWith("existingM", AbstractAdditionalTokensInfo.INNER);
    assertTrue(tokensStartingWith.size() == 1);
    assertIsIn("existingMethod", "testAssist.assist", tokensStartingWith);
}
Also used : IInfo(org.python.pydev.core.IInfo) AbstractAdditionalTokensInfo(com.python.pydev.analysis.additionalinfo.AbstractAdditionalTokensInfo)

Aggregations

AbstractAdditionalTokensInfo (com.python.pydev.analysis.additionalinfo.AbstractAdditionalTokensInfo)13 MisconfigurationException (org.python.pydev.core.MisconfigurationException)7 ArrayList (java.util.ArrayList)6 IInfo (org.python.pydev.core.IInfo)6 IPythonNature (org.python.pydev.core.IPythonNature)5 ModulesKey (org.python.pydev.core.ModulesKey)4 Tuple (org.python.pydev.shared_core.structure.Tuple)4 HashSet (java.util.HashSet)3 List (java.util.List)3 SourceModule (org.python.pydev.ast.codecompletion.revisited.modules.SourceModule)3 IModulesManager (org.python.pydev.core.IModulesManager)3 AdditionalProjectInterpreterInfo (com.python.pydev.analysis.additionalinfo.AdditionalProjectInterpreterInfo)2 File (java.io.File)2 IProject (org.eclipse.core.resources.IProject)2 IResource (org.eclipse.core.resources.IResource)2 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)2 PyFileInfo (org.python.pydev.ast.listing_utils.PyFileListing.PyFileInfo)2 ICodeCompletionASTManager (org.python.pydev.core.ICodeCompletionASTManager)2 FastStringBuffer (org.python.pydev.shared_core.string.FastStringBuffer)2