Search in sources :

Example 26 with IPythonNature

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

the class PythonSourceFolderWizard method doCreateNew.

@Override
protected IFile doCreateNew(IProgressMonitor monitor) throws CoreException {
    IProject project = filePage.getValidatedProject();
    String name = filePage.getValidatedName();
    if (project == null || !project.exists()) {
        throw new RuntimeException("The project selected does not exist in the workspace.");
    }
    IPythonPathNature pathNature = PythonNature.getPythonPathNature(project);
    if (pathNature == null) {
        IPythonNature nature = PythonNature.addNature(project, monitor, null, null, null, null, null);
        pathNature = nature.getPythonPathNature();
        if (pathNature == null) {
            throw new RuntimeException("Unable to add the nature to the seleted project.");
        }
    }
    IFolder folder = project.getFolder(name);
    if (folder.exists()) {
        Log.log("Source folder already exists. Nothing new was created");
        return null;
    }
    folder.create(true, true, monitor);
    String newPath = folder.getFullPath().toString();
    String curr = pathNature.getProjectSourcePath(false);
    if (curr == null) {
        curr = "";
    }
    if (curr.endsWith("|")) {
        curr = curr.substring(0, curr.length() - 1);
    }
    String newPathRel = PyStructureConfigHelpers.convertToProjectRelativePath(project.getFullPath().toString(), newPath);
    if (curr.length() > 0) {
        // there is already some path
        Set<String> projectSourcePathSet = pathNature.getProjectSourcePathSet(true);
        if (!projectSourcePathSet.contains(newPath)) {
            // only add to the path if it doesn't already contain the new path
            curr += "|" + newPathRel;
        }
    } else {
        // there is still no other path
        curr = newPathRel;
    }
    pathNature.setProjectSourcePath(curr);
    PythonNature.getPythonNature(project).rebuildPath();
    return null;
}
Also used : IPythonPathNature(org.python.pydev.core.IPythonPathNature) IPythonNature(org.python.pydev.core.IPythonNature) IProject(org.eclipse.core.resources.IProject) IFolder(org.eclipse.core.resources.IFolder)

Example 27 with IPythonNature

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

the class RefactorerFindDefinition method findDefinition.

/**
 * This function is used to find the definition for some token.
 * It may return a list of ItemPointer because the actual definition may not be
 * easy to find (so, multiple places that could be the definitions for
 * the given token may be returned... and it may be up to the user to actually
 * choose the best match).
 * @throws BadLocationException
 *
 * @see org.python.pydev.ast.refactoring.IPyRefactoring#findDefinition(org.python.pydev.ast.refactoring.RefactoringRequest)
 */
public ItemPointer[] findDefinition(RefactoringRequest request) throws BadLocationException {
    try {
        request.getMonitor().beginTask("Find definition", 100);
        List<ItemPointer> pointers = new ArrayList<ItemPointer>();
        CompletionState completionState = new CompletionState();
        completionState.setAcceptTypeshed(request.acceptTypeshed);
        ArrayList<IDefinition> selected = new ArrayList<IDefinition>();
        String[] tokenAndQual;
        try {
            tokenAndQual = PyRefactoringFindDefinition.findActualDefinition(request, completionState, selected);
        } catch (CompletionRecursionException e1) {
            Log.log(e1);
            return new ItemPointer[0];
        }
        if (tokenAndQual == null) {
            return new ItemPointer[0];
        }
        PyRefactoringFindDefinition.getAsPointers(pointers, selected.toArray(new Definition[0]));
        if (pointers.size() == 0 && ((Boolean) request.getAdditionalInfo(RefactoringRequest.FIND_DEFINITION_IN_ADDITIONAL_INFO, true))) {
            String lookForInterface = tokenAndQual[1];
            List<IInfo> tokensEqualTo;
            try {
                tokensEqualTo = AdditionalProjectInterpreterInfo.getTokensEqualTo(lookForInterface, request.nature, AbstractAdditionalTokensInfo.TOP_LEVEL | AbstractAdditionalTokensInfo.INNER);
                ICodeCompletionASTManager manager = request.nature.getAstManager();
                if (manager == null) {
                    return new ItemPointer[0];
                }
                if (tokensEqualTo.size() > 50) {
                    // too many matches for that...
                    throw new TooManyMatchesException("Too Many matches (" + tokensEqualTo.size() + ") were found for the requested token:" + lookForInterface, tokensEqualTo.size());
                }
                request.communicateWork(StringUtils.format("Found: %s possible matches.", tokensEqualTo.size()));
                IPythonNature nature = request.nature;
                for (IInfo info : tokensEqualTo) {
                    AnalysisPlugin.getDefinitionFromIInfo(pointers, manager, nature, info, completionState, true, true);
                    request.checkCancelled();
                }
            } catch (MisconfigurationException e) {
                Log.log(e);
                return new ItemPointer[0];
            }
        }
        request.communicateWork(StringUtils.format("Found: %s matches.", pointers.size()));
        return pointers.toArray(new ItemPointer[0]);
    } catch (BadLocationException e) {
        throw e;
    } catch (OperationCanceledException e) {
        // that's ok... it was cancelled
        throw e;
    } finally {
        request.getMonitor().done();
    }
}
Also used : MisconfigurationException(org.python.pydev.core.MisconfigurationException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ArrayList(java.util.ArrayList) IDefinition(org.python.pydev.core.IDefinition) Definition(org.python.pydev.ast.codecompletion.revisited.visitors.Definition) PyRefactoringFindDefinition(org.python.pydev.ast.refactoring.PyRefactoringFindDefinition) IPythonNature(org.python.pydev.core.IPythonNature) CompletionState(org.python.pydev.ast.codecompletion.revisited.CompletionState) IDefinition(org.python.pydev.core.IDefinition) IInfo(org.python.pydev.core.IInfo) ICodeCompletionASTManager(org.python.pydev.core.ICodeCompletionASTManager) TooManyMatchesException(org.python.pydev.ast.refactoring.TooManyMatchesException) CompletionRecursionException(org.python.pydev.core.structure.CompletionRecursionException) BadLocationException(org.eclipse.jface.text.BadLocationException) ItemPointer(org.python.pydev.ast.item_pointer.ItemPointer)

Example 28 with IPythonNature

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

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

the class ImportsCompletionParticipant method computeConsoleCompletions.

// Console completions ---------------------------------------------------------------------------------------------
@Override
public Collection<ICompletionProposalHandle> computeConsoleCompletions(ActivationTokenAndQualifier tokenAndQual, Set<IPythonNature> naturesUsed, IScriptConsoleViewer viewer, int requestOffset) {
    ArrayList<ICompletionProposalHandle> completions = new ArrayList<ICompletionProposalHandle>();
    if (tokenAndQual.activationToken != null && tokenAndQual.activationToken.length() > 0) {
        // we only want
        return completions;
    }
    String qual = tokenAndQual.qualifier;
    if (qual.length() >= PyCodeCompletionPreferences.getCharsForContextInsensitiveModulesCompletion() && naturesUsed != null && naturesUsed.size() > 0) {
        // at least n characters required...
        int qlen = qual.length();
        boolean addAutoImport = AnalysisPreferences.doAutoImport(null);
        for (IPythonNature nature : naturesUsed) {
            fillCompletions(requestOffset, completions, qual, nature, qlen, addAutoImport, viewer);
        }
    }
    return completions;
}
Also used : ArrayList(java.util.ArrayList) IPythonNature(org.python.pydev.core.IPythonNature) ICompletionProposalHandle(org.python.pydev.shared_core.code_completion.ICompletionProposalHandle)

Example 30 with IPythonNature

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

the class AbstractManageEnvEditorAction method run.

@Override
public void run() {
    try {
        final IPythonNature pythonNature = edit.getPythonNature();
        if (pythonNature == null) {
            PyDialogHelpers.openCritical("Unable to execute pip", "The related editor does not have an associated python nature.");
            return;
        }
        IInterpreterInfo projectInterpreter = pythonNature.getProjectInterpreter();
        if (projectInterpreter == null) {
            PyDialogHelpers.openCritical("Unable to execute pip", "The related editor does not have an associated interpreter.");
            return;
        }
        doRun(pythonNature, projectInterpreter);
    } catch (MisconfigurationException | PythonNatureWithoutProjectException e) {
        Log.log(e);
    }
}
Also used : IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) MisconfigurationException(org.python.pydev.core.MisconfigurationException) IPythonNature(org.python.pydev.core.IPythonNature) PythonNatureWithoutProjectException(org.python.pydev.core.PythonNatureWithoutProjectException)

Aggregations

IPythonNature (org.python.pydev.core.IPythonNature)85 MisconfigurationException (org.python.pydev.core.MisconfigurationException)33 ArrayList (java.util.ArrayList)30 File (java.io.File)26 IProject (org.eclipse.core.resources.IProject)20 IFile (org.eclipse.core.resources.IFile)18 CoreException (org.eclipse.core.runtime.CoreException)17 Tuple (org.python.pydev.shared_core.structure.Tuple)14 IDocument (org.eclipse.jface.text.IDocument)12 SourceModule (org.python.pydev.ast.codecompletion.revisited.modules.SourceModule)12 ICodeCompletionASTManager (org.python.pydev.core.ICodeCompletionASTManager)11 IModule (org.python.pydev.core.IModule)10 TokensList (org.python.pydev.core.TokensList)10 HashSet (java.util.HashSet)9 List (java.util.List)9 BadLocationException (org.eclipse.jface.text.BadLocationException)9 IInterpreterInfo (org.python.pydev.core.IInterpreterInfo)9 SimpleNode (org.python.pydev.parser.jython.SimpleNode)9 PyEdit (org.python.pydev.editor.PyEdit)8 SystemPythonNature (org.python.pydev.plugin.nature.SystemPythonNature)8