Search in sources :

Example 16 with SystemPythonNature

use of org.python.pydev.plugin.nature.SystemPythonNature in project Pydev by fabioz.

the class PyCodeCompletion2Test method testSelfOrClsCompletion.

public void testSelfOrClsCompletion() throws Exception {
    String s = "" + "class B:\n" + "    def m2(self):\n" + "        pass\n" + "\n" + "class A:\n" + "    m1 = B()\n" + "    def foo(self):\n" + "        self.m1." + "";
    SystemPythonNature nature = new SystemPythonNature(PyCodeCompletion2Test.nature.getRelatedInterpreterManager());
    PySelection ps = new PySelection(new Document(s), s.length() - 1);
    ICompletionState state = new CompletionState(ps.getStartLineIndex(), ps.getAbsoluteCursorOffset() - ps.getStartLine().getOffset(), null, nature, "");
    CompletionRequest request = new CompletionRequest(null, nature, ps.getDoc(), "self.m1", ps.getAbsoluteCursorOffset(), 0, new PyCodeCompletion(), "", false);
    TokensList selfCompletions = new TokensList();
    PyCodeCompletion.getSelfOrClsCompletions(request, selfCompletions, state, false, false, "self.m1");
    assertEquals(1, selfCompletions.size());
    assertEquals("m2", selfCompletions.getFirst().getRepresentation());
}
Also used : ICompletionState(org.python.pydev.core.ICompletionState) SystemPythonNature(org.python.pydev.plugin.nature.SystemPythonNature) ICompletionState(org.python.pydev.core.ICompletionState) CompletionState(org.python.pydev.ast.codecompletion.revisited.CompletionState) PySelection(org.python.pydev.core.docutils.PySelection) Document(org.eclipse.jface.text.Document) TokensList(org.python.pydev.core.TokensList)

Example 17 with SystemPythonNature

use of org.python.pydev.plugin.nature.SystemPythonNature in project Pydev by fabioz.

the class InterpreterConfigHelpers method createPipenvInterpreter.

public static ObtainInterpreterInfoOperation createPipenvInterpreter(IInterpreterInfo[] interpreterInfos, Shell shell, PrintWriter logger, Map<String, IInterpreterInfo> nameToInfo, String defaultProjectLocation, IInterpreterManager interpreterManager) throws Exception {
    if (logger == null) {
        logger = new PrintWriter(new ByteArrayOutputStream());
    }
    if (interpreterInfos == null || interpreterInfos.length == 0) {
        PyDialogHelpers.openCritical("Unable to configure with pipenv", "Cannot configure with pipenv without a base interpreter configured. Please configure the base interpreter first.");
        return null;
    }
    PipenvDialog pipenvDialog = new PipenvDialog(shell, interpreterInfos, null, defaultProjectLocation, interpreterManager, "New Pipenv interpreter", true);
    if (pipenvDialog.open() == Dialog.OK) {
        final IInterpreterInfo baseInterpreter = pipenvDialog.getBaseInterpreter();
        final String executableOrJar = baseInterpreter.getExecutableOrJar();
        final String pipenvLocation = pipenvDialog.getPipenvLocation();
        final String projectLocation = pipenvDialog.getProjectLocation();
        final SystemPythonNature nature = new SystemPythonNature(interpreterManager, baseInterpreter);
        File pythonVenvFromLocation = PipenvHelper.getPythonExecutableFromProjectLocationWithPipenv(pipenvLocation, new File(projectLocation));
        if (pythonVenvFromLocation == null) {
            PipenvPackageManager.create(executableOrJar, pipenvLocation, projectLocation, nature);
            // Get the one just created.
            pythonVenvFromLocation = PipenvHelper.getPythonExecutableFromProjectLocationWithPipenv(pipenvLocation, new File(projectLocation));
        }
        if (pythonVenvFromLocation != null) {
            NameAndExecutable interpreterNameAndExecutable = new NameAndExecutable(new File(projectLocation).getName() + " (pipenv)", pythonVenvFromLocation.getAbsolutePath());
            boolean foundError = canAddNameAndExecutable(logger, interpreterNameAndExecutable, nameToInfo, shell);
            if (foundError) {
                return null;
            }
            logger.println("- Chosen interpreter (name and file):'" + interpreterNameAndExecutable);
            if (interpreterNameAndExecutable != null && interpreterNameAndExecutable.o2 != null) {
                // ok, now that we got the file, let's see if it is valid and get the library info.
                ObtainInterpreterInfoOperation ret = tryInterpreter(interpreterNameAndExecutable, interpreterManager, false, true, logger, shell);
                if (ret != null && ret.result != null) {
                    ret.result.setPipenvTargetDir(projectLocation);
                }
                return ret;
            }
        }
    }
    return null;
}
Also used : IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) SystemPythonNature(org.python.pydev.plugin.nature.SystemPythonNature) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ZipFile(java.util.zip.ZipFile) File(java.io.File) PrintWriter(java.io.PrintWriter)

Aggregations

SystemPythonNature (org.python.pydev.plugin.nature.SystemPythonNature)17 IPythonNature (org.python.pydev.core.IPythonNature)8 MisconfigurationException (org.python.pydev.core.MisconfigurationException)8 IInterpreterManager (org.python.pydev.core.IInterpreterManager)7 ArrayList (java.util.ArrayList)6 File (java.io.File)5 CoreException (org.eclipse.core.runtime.CoreException)5 IInterpreterInfo (org.python.pydev.core.IInterpreterInfo)5 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)4 IProject (org.eclipse.core.resources.IProject)3 SimplePythonRunner (org.python.pydev.ast.runners.SimplePythonRunner)3 SimpleRunner (org.python.pydev.ast.runners.SimpleRunner)3 Tuple (org.python.pydev.shared_core.structure.Tuple)3 AbstractAdditionalTokensInfo (com.python.pydev.analysis.additionalinfo.AbstractAdditionalTokensInfo)2 AdditionalProjectInterpreterInfo (com.python.pydev.analysis.additionalinfo.AdditionalProjectInterpreterInfo)2 IOException (java.io.IOException)2 ZipFile (java.util.zip.ZipFile)2 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)2 Path (org.eclipse.core.runtime.Path)2 SelectionDialog (org.eclipse.ui.dialogs.SelectionDialog)2