Search in sources :

Example 6 with ModulesKey

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

the class RefactoringRenameTestBase method setUp.

/**
 * In the setUp, it initializes the files in the refactoring project
 * @see com.python.pydev.analysis.refactoring.refactorer.refactorings.renamelocal.RefactoringLocalTestBase#setUp()
 */
@Override
public void setUp() throws Exception {
    super.setUp();
    if (filesInRefactoringProject == null) {
        filesInRefactoringProject = PyFileListing.getPyFilesBelow(new File(TestDependent.TEST_COM_REFACTORING_PYSRC_LOC), new NullProgressMonitor(), true).getFoundPyFileInfos();
        ArrayList<Tuple<List<ModulesKey>, IPythonNature>> iFiles = new ArrayList<Tuple<List<ModulesKey>, IPythonNature>>();
        List<ModulesKey> modules = new ArrayList<ModulesKey>();
        iFiles.add(new Tuple<List<ModulesKey>, IPythonNature>(modules, natureRefactoring));
        FastStringBuffer tempBuf = new FastStringBuffer();
        for (PyFileInfo info : filesInRefactoringProject) {
            File f = info.getFile();
            String modName = info.getModuleName(tempBuf);
            ModulesKey modulesKey = new ModulesKey(modName, f);
            modules.add(modulesKey);
            SourceModule mod = (SourceModule) AbstractModule.createModule(modName, f, natureRefactoring, true);
            // also create the additional info so that it can be used for finds
            AbstractAdditionalTokensInfo additionalInfo = AdditionalProjectInterpreterInfo.getAdditionalInfoForProject(natureRefactoring);
            additionalInfo.addAstInfo(mod.getAst(), modulesKey, false);
        }
    // RefactorerFindReferences.FORCED_RETURN = iFiles;
    }
    setUpConfigWorkspaceFiles();
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) SourceModule(org.python.pydev.ast.codecompletion.revisited.modules.SourceModule) ASTEntryWithSourceModule(org.python.pydev.ast.codecompletion.revisited.modules.ASTEntryWithSourceModule) FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer) ArrayList(java.util.ArrayList) IPythonNature(org.python.pydev.core.IPythonNature) ModulesKey(org.python.pydev.core.ModulesKey) List(java.util.List) ArrayList(java.util.ArrayList) PyFileInfo(org.python.pydev.ast.listing_utils.PyFileListing.PyFileInfo) AbstractAdditionalTokensInfo(com.python.pydev.analysis.additionalinfo.AbstractAdditionalTokensInfo) IFile(org.eclipse.core.resources.IFile) File(java.io.File) Tuple(org.python.pydev.shared_core.structure.Tuple)

Example 7 with ModulesKey

use of org.python.pydev.core.ModulesKey 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 8 with ModulesKey

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

the class PyOrganizeImports method organizeImports.

@SuppressWarnings("unchecked")
private void organizeImports(PyEdit edit, final IDocument doc, IFile f, PySelection ps) throws MisconfigurationException, PythonNatureWithoutProjectException {
    DocumentRewriteSession session = null;
    String endLineDelim = ps.getEndLineDelim();
    List<IOrganizeImports> participants = null;
    if (f == null && !automatic) {
        // organizing single file ...
        // let's see if someone wants to make a better implementation in another plugin...
        participants = ExtensionHelper.getParticipants(ExtensionHelper.PYDEV_ORGANIZE_IMPORTS);
        for (IOrganizeImports organizeImports : participants) {
            if (!organizeImports.beforePerformArrangeImports(ps, edit, f)) {
                return;
            }
        }
    }
    String fileContents = doc.get();
    if (fileContents.contains("isort:skip_file") || fileContents.length() == 0) {
        return;
    }
    IAdaptable projectAdaptable = edit != null ? edit : f;
    String indentStr = edit != null ? edit.getIndentPrefs().getIndentationString() : DefaultIndentPrefs.get(f).getIndentationString();
    session = TextSelectionUtils.startWrite(doc);
    try {
        // Important: the remove and later update have to be done in the same session (since the remove
        // will just remove some names and he actual perform will remove the remaining if needed).
        // i.e.: from a import b <-- b will be removed by the OrganizeImportsFixesUnused and the
        // from a will be removed in the performArrangeImports later on.
        boolean removeUnusedImports = false;
        if (!automatic) {
            // Only go through the removal of unused imports if it's manually activated (not on automatic mode).
            removeUnusedImports = ImportsPreferencesPage.getDeleteUnusedImports(projectAdaptable);
            if (removeUnusedImports) {
                new OrganizeImportsFixesUnused().beforePerformArrangeImports(ps, edit, f);
            }
        }
        Set<String> knownThirdParty = new HashSet<String>();
        // isort itself already has a reasonable stdLib, so, don't do our own.
        String importEngine = ImportsPreferencesPage.getImportEngine(projectAdaptable);
        if (edit != null) {
            IPythonNature pythonNature = edit.getPythonNature();
            if (pythonNature != null) {
                IInterpreterInfo projectInterpreter = pythonNature.getProjectInterpreter();
                ISystemModulesManager modulesManager = projectInterpreter.getModulesManager();
                ModulesKey[] onlyDirectModules = modulesManager.getOnlyDirectModules();
                Set<String> stdLib = new HashSet<>();
                for (ModulesKey modulesKey : onlyDirectModules) {
                    if (modulesKey.file == null) {
                        int i = modulesKey.name.indexOf('.');
                        String name;
                        if (i < 0) {
                            name = modulesKey.name;
                        } else {
                            name = modulesKey.name.substring(0, i);
                        }
                        // Add all names to std lib
                        stdLib.add(name);
                    }
                }
                for (ModulesKey modulesKey : onlyDirectModules) {
                    int i = modulesKey.name.indexOf('.');
                    String name;
                    if (i < 0) {
                        name = modulesKey.name;
                    } else {
                        name = modulesKey.name.substring(0, i);
                    }
                    // Consider all in site-packages to be third party.
                    if (modulesKey.file != null && modulesKey.file.toString().contains("site-packages")) {
                        stdLib.remove(name);
                        knownThirdParty.add(name);
                    }
                }
            }
        }
        switch(importEngine) {
            case ImportsPreferencesPage.IMPORT_ENGINE_ISORT:
                if (fileContents.length() > 0) {
                    File targetFile = edit != null ? edit.getEditorFile() : null;
                    if (targetFile == null) {
                        if (f != null) {
                            IPath location = f.getLocation();
                            if (location != null) {
                                targetFile = location.toFile();
                            }
                        }
                    }
                    String isortResult = JythonModules.makeISort(fileContents, targetFile, knownThirdParty);
                    if (isortResult != null) {
                        try {
                            DocUtils.updateDocRangeWithContents(doc, fileContents, isortResult.toString());
                        } catch (Exception e) {
                            Log.log(StringUtils.format("Error trying to apply isort result. Curr doc:\n>>>%s\n<<<.\nNew doc:\\n>>>%s\\n<<<.", fileContents, isortResult.toString()), e);
                        }
                    }
                }
                break;
            case ImportsPreferencesPage.IMPORT_ENGINE_REGULAR_SORT:
                performArrangeImports(doc, removeUnusedImports, endLineDelim, indentStr, automatic, edit);
                break;
            default:
                // case ImportsPreferencesPage.IMPORT_ENGINE_PEP_8:
                if (f == null) {
                    f = edit.getIFile();
                }
                IProject p = f != null ? f.getProject() : null;
                pep8PerformArrangeImports(doc, removeUnusedImports, endLineDelim, p, indentStr, automatic, edit);
                break;
        }
        if (participants != null) {
            for (IOrganizeImports organizeImports : participants) {
                organizeImports.afterPerformArrangeImports(ps, edit);
            }
        }
    } finally {
        TextSelectionUtils.endWrite(doc, session);
    }
}
Also used : IAdaptable(org.eclipse.core.runtime.IAdaptable) IPath(org.eclipse.core.runtime.IPath) IPythonNature(org.python.pydev.core.IPythonNature) SyntaxErrorException(org.python.pydev.core.docutils.SyntaxErrorException) PythonNatureWithoutProjectException(org.python.pydev.core.PythonNatureWithoutProjectException) MisconfigurationException(org.python.pydev.core.MisconfigurationException) IProject(org.eclipse.core.resources.IProject) ISystemModulesManager(org.python.pydev.core.ISystemModulesManager) DocumentRewriteSession(org.eclipse.jface.text.DocumentRewriteSession) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) ModulesKey(org.python.pydev.core.ModulesKey) IFile(org.eclipse.core.resources.IFile) File(java.io.File) HashSet(java.util.HashSet)

Example 9 with ModulesKey

use of org.python.pydev.core.ModulesKey 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 10 with ModulesKey

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

the class AbstractASTManager method getAbsoluteImportTokens.

/**
 * @param moduleToGetTokensFrom the string that represents the token from where we are getting the imports
 * @param set the set where the tokens should be added
 * @param importInfo if null, only the 1st element of the module will be added, otherwise, it'll check the info
 * to see if it should add only the 1st element of the module or the complete module (e.g.: add only xml or
 * xml.dom and other submodules too)
 */
public void getAbsoluteImportTokens(String moduleToGetTokensFrom, Set<IToken> inputOutput, int type, boolean onlyFilesOnSameLevel, ImportInfo importInfo, boolean onlyGetDirectModules) {
    // boolean getSubModules = false;
    // if(importInfo != null){
    // //we only want to get submodules if we're in:
    // //from xxx
    // //import xxx
    // //
    // //We do NOT want to get it on:
    // //from xxx import yyy
    // if(importInfo.hasFromSubstring != importInfo.hasImportSubstring){
    // getSubModules = true;
    // }
    // }
    HashMap<String, IToken> temp = new HashMap<String, IToken>();
    SortedMap<ModulesKey, ModulesKey> modulesStartingWith;
    if (onlyGetDirectModules) {
        modulesStartingWith = modulesManager.getAllDirectModulesStartingWith(moduleToGetTokensFrom);
    } else {
        modulesStartingWith = modulesManager.getAllModulesStartingWith(moduleToGetTokensFrom);
    }
    Iterator<ModulesKey> itModules = modulesStartingWith.keySet().iterator();
    while (itModules.hasNext()) {
        ModulesKey key = itModules.next();
        String element = key.name;
        // if (element.startsWith(moduleToGetTokensFrom)) { we don't check that anymore because we get all the modules starting with it already
        if (onlyFilesOnSameLevel && key.file != null && key.file.isDirectory()) {
            // we only want those that are in the same directory, and not in other directories...
            continue;
        }
        element = element.substring(moduleToGetTokensFrom.length());
        // we only check this if we only want file modules (in
        if (onlyFilesOnSameLevel && StringUtils.countChars('.', element) > 1) {
            continue;
        }
        boolean goForIt = false;
        // if we have xml token (not using the qualifier here)
        if (moduleToGetTokensFrom.length() != 0) {
            if (element.length() > 0 && element.charAt(0) == ('.')) {
                element = element.substring(1);
                goForIt = true;
            }
        } else {
            goForIt = true;
        }
        if (element.length() > 0 && goForIt) {
            List<String> splitted = StringUtils.dotSplit(element);
            if (splitted.size() > 0) {
                String strToAdd;
                strToAdd = splitted.get(0);
                // if(!getSubModules){
                // }else{
                // if(element.endsWith(".__init__")){
                // strToAdd = element.substring(0, element.length()-9);
                // }else{
                // strToAdd = element;
                // }
                // }
                // this is the completion
                temp.put(strToAdd, new ConcreteToken(strToAdd, "", "", moduleToGetTokensFrom, type, modulesManager.getNature()));
            }
        }
    // }
    }
    inputOutput.addAll(temp.values());
}
Also used : IToken(org.python.pydev.core.IToken) HashMap(java.util.HashMap) ModulesKey(org.python.pydev.core.ModulesKey)

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