Search in sources :

Example 11 with IPythonNature

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

the class PySetNextTarget method setNextToLine.

@Override
public boolean setNextToLine(IWorkbenchPart part, ISelection selection, ISuspendResume target) throws CoreException {
    // System.out.println("Run to line:"+target);
    PyStackFrame stack = null;
    if (target instanceof PyStackFrame) {
        stack = (PyStackFrame) target;
        target = stack.getThread();
    }
    if (!(part instanceof PyEdit)) {
        return false;
    }
    PyEdit pyEdit = (PyEdit) part;
    SimpleNode ast = pyEdit.getAST();
    if (ast == null) {
        IDocument doc = pyEdit.getDocument();
        SourceModule sourceModule;
        IPythonNature nature = null;
        try {
            nature = pyEdit.getPythonNature();
        } catch (MisconfigurationException e) {
            // Let's try to find a suitable nature
            File editorFile = pyEdit.getEditorFile();
            if (editorFile == null || !editorFile.exists()) {
                Log.log(e);
                return false;
            }
            nature = InterpreterManagersAPI.getInfoForFile(editorFile).o1;
        }
        if (nature == null) {
            Log.log("Unable to determine nature!");
            return false;
        }
        try {
            sourceModule = AbstractModule.createModuleFromDoc("", pyEdit.getEditorFile(), doc, nature, true);
        } catch (MisconfigurationException e) {
            Log.log(e);
            return false;
        }
        ast = sourceModule.getAst();
    }
    if (ast == null) {
        Log.log("Cannot determine context to run to.");
        return false;
    }
    if (target instanceof PyThread && selection instanceof ITextSelection) {
        ITextSelection textSelection = (ITextSelection) selection;
        PyThread pyThread = (PyThread) target;
        if (!pyThread.isPydevThread()) {
            int sourceLine = stack.getLineNumber();
            int targetLine = textSelection.getStartLine();
            if (!NodeUtils.isValidContextForSetNext(ast, sourceLine, targetLine)) {
                return false;
            }
            String functionName = NodeUtils.getContextName(targetLine, ast);
            if (functionName == null) {
                // global context
                functionName = "";
            } else {
                functionName = FullRepIterable.getLastPart(functionName).trim();
            }
            pyThread.setNextStatement(targetLine + 1, functionName);
            return true;
        }
    }
    return true;
}
Also used : SourceModule(org.python.pydev.ast.codecompletion.revisited.modules.SourceModule) MisconfigurationException(org.python.pydev.core.MisconfigurationException) IPythonNature(org.python.pydev.core.IPythonNature) File(java.io.File) PyEdit(org.python.pydev.editor.PyEdit) IDocument(org.eclipse.jface.text.IDocument) ITextSelection(org.eclipse.jface.text.ITextSelection) SimpleNode(org.python.pydev.parser.jython.SimpleNode)

Example 12 with IPythonNature

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

the class PyBreakpoint method getPythonNature.

/**
 * @return The nature to be used for this breakpoint or null if it cannot be determined.
 */
private IPythonNature getPythonNature() {
    IMarker marker = getMarker();
    IPythonNature nature = PythonNature.getPythonNature(marker.getResource());
    if (nature == null) {
        try {
            String externalPath = (String) marker.getAttribute(PyBreakpoint.PY_BREAK_EXTERNAL_PATH_ID);
            if (externalPath != null) {
                Tuple<IPythonNature, String> infoForFile = InterpreterManagersAPI.getInfoForFile(new File(externalPath));
                if (infoForFile != null) {
                    nature = infoForFile.o1;
                }
            }
        } catch (CoreException e) {
            throw new RuntimeException(e);
        }
    }
    return nature;
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) IPythonNature(org.python.pydev.core.IPythonNature) IMarker(org.eclipse.core.resources.IMarker) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Example 13 with IPythonNature

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

the class PyGlobalsBrowser method getFromSystemManager.

/**
 * @param selectedText the text that should be selected in the beginning (may be null)
 */
private void getFromSystemManager(String selectedText) {
    // is null
    File editorFile = getPyEdit().getEditorFile();
    Tuple<IPythonNature, String> infoForFile;
    if (editorFile != null) {
        infoForFile = InterpreterManagersAPI.getInfoForFile(editorFile);
    } else {
        infoForFile = null;
    }
    if (infoForFile != null) {
        IPythonNature systemPythonNature = infoForFile.o1;
        if (systemPythonNature == null) {
            getFromWorkspace(selectedText);
            return;
        }
        IInterpreterManager manager = infoForFile.o1.getRelatedInterpreterManager();
        getFromManagerAndRelatedNatures(selectedText, manager);
    } else {
        getFromWorkspace(selectedText);
    }
}
Also used : IPythonNature(org.python.pydev.core.IPythonNature) File(java.io.File) IInterpreterManager(org.python.pydev.core.IInterpreterManager)

Example 14 with IPythonNature

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

the class PyGlobalsBrowser method run.

@Override
public void run(IAction action) {
    IPythonNature pythonNature;
    try {
        pythonNature = getPyEdit().getPythonNature();
    } catch (MisconfigurationException e1) {
        handle(e1);
        return;
    }
    PySelection ps = PySelectionFromEditor.createPySelectionFromEditor(this.getPyEdit());
    String selectedText;
    try {
        selectedText = ps.getSelectedText();
    } catch (BadLocationException e1) {
        selectedText = null;
    }
    if (selectedText == null || selectedText.length() == 0) {
        try {
            selectedText = ps.getCurrToken().o1;
        } catch (BadLocationException e) {
        // ignore
        }
    }
    if (pythonNature != null) {
        IInterpreterManager manager = pythonNature.getRelatedInterpreterManager();
        getFromManagerAndRelatedNatures(selectedText, manager);
    } else {
        getFromSystemManager(selectedText);
    }
}
Also used : MisconfigurationException(org.python.pydev.core.MisconfigurationException) IPythonNature(org.python.pydev.core.IPythonNature) PySelection(org.python.pydev.core.docutils.PySelection) IInterpreterManager(org.python.pydev.core.IInterpreterManager) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 15 with IPythonNature

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

the class PyGlobalsBrowser method getFromManagerAndRelatedNatures.

/**
 * Gets it using all the natures that match a given interpreter manager.
 * @throws MisconfigurationException
 */
private static void getFromManagerAndRelatedNatures(String selectedText, IInterpreterManager useManager) {
    AbstractAdditionalTokensInfo additionalSystemInfo;
    try {
        additionalSystemInfo = AdditionalSystemInterpreterInfo.getAdditionalSystemInfo(useManager, useManager.getDefaultInterpreterInfo(true).getExecutableOrJar());
    } catch (MisconfigurationException e) {
        MessageDialog.openError(EditorUtils.getShell(), "Error", "Additional info is not available (default interpreter not configured).");
        handle(e);
        return;
    }
    List<AbstractAdditionalTokensInfo> additionalInfo = new ArrayList<AbstractAdditionalTokensInfo>();
    additionalInfo.add(additionalSystemInfo);
    List<IPythonNature> natures = PythonNature.getPythonNaturesRelatedTo(useManager.getInterpreterType());
    for (IPythonNature nature : natures) {
        AbstractAdditionalDependencyInfo info;
        try {
            info = AdditionalProjectInterpreterInfo.getAdditionalInfoForProject(nature);
            if (info != null) {
                additionalInfo.add(info);
            }
        } catch (MisconfigurationException e) {
            // just go on to the next nature if one is not properly configured.
            handle(e);
        }
    }
    doSelect(natures, additionalInfo, selectedText);
}
Also used : MisconfigurationException(org.python.pydev.core.MisconfigurationException) ArrayList(java.util.ArrayList) IPythonNature(org.python.pydev.core.IPythonNature) AbstractAdditionalTokensInfo(com.python.pydev.analysis.additionalinfo.AbstractAdditionalTokensInfo) AbstractAdditionalDependencyInfo(com.python.pydev.analysis.additionalinfo.AbstractAdditionalDependencyInfo)

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