Search in sources :

Example 46 with PyEdit

use of org.python.pydev.editor.PyEdit in project Pydev by fabioz.

the class StepIntoEditorSelectionHandler method execute.

/* (non-Javadoc)
     * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
     */
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IEditorPart editor = HandlerUtil.getActiveEditor(event);
    if (editor instanceof PyEdit) {
        PyEdit pyEdit = (PyEdit) editor;
        StepIntoSelectionHyperlinkDetector stepIntoSelectionHyperlinkDetector = new StepIntoSelectionHyperlinkDetector();
        ISelection selection = editor.getEditorSite().getSelectionProvider().getSelection();
        if (selection instanceof ITextSelection) {
            stepIntoSelectionHyperlinkDetector.setContext(pyEdit);
            ITextSelection iTextSelection = (ITextSelection) selection;
            IHyperlink[] detectHyperlinks = stepIntoSelectionHyperlinkDetector.detectHyperlinks(pyEdit.getISourceViewer(), new Region(iTextSelection.getOffset(), iTextSelection.getLength()), false);
            if (detectHyperlinks != null && detectHyperlinks.length > 0) {
                detectHyperlinks[0].open();
            }
        }
    }
    return null;
}
Also used : IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) ISelection(org.eclipse.jface.viewers.ISelection) Region(org.eclipse.jface.text.Region) IEditorPart(org.eclipse.ui.IEditorPart) PyEdit(org.python.pydev.editor.PyEdit) ITextSelection(org.eclipse.jface.text.ITextSelection)

Example 47 with PyEdit

use of org.python.pydev.editor.PyEdit in project Pydev by fabioz.

the class PyRunToLineTarget method runToLine.

@Override
public void runToLine(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;
    }
    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;
            }
            nature = InterpreterManagersAPI.getInfoForFile(editorFile).o1;
        }
        if (nature == null) {
            Log.log("Unable to determine nature!");
            return;
        }
        try {
            sourceModule = AbstractModule.createModuleFromDoc("", pyEdit.getEditorFile(), doc, nature, true);
        } catch (MisconfigurationException e) {
            Log.log(e);
            return;
        }
        ast = sourceModule.getAst();
    }
    if (ast == null) {
        Log.log("Cannot determine context to run to.");
        return;
    }
    if (target instanceof PyThread && selection instanceof ITextSelection) {
        ITextSelection textSelection = (ITextSelection) selection;
        PyThread pyThread = (PyThread) target;
        if (!pyThread.isPydevThread()) {
            int line = textSelection.getStartLine();
            String functionName = NodeUtils.getContextName(line, ast);
            if (functionName == null) {
                // global context
                functionName = "";
            } else {
                functionName = FullRepIterable.getLastPart(functionName).trim();
            }
            pyThread.runToLine(line + 1, functionName);
            return;
        }
    }
}
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 48 with PyEdit

use of org.python.pydev.editor.PyEdit in project Pydev by fabioz.

the class EvaluateActionSetter method onCreateActions.

/**
 * This method associates Ctrl+new line with the evaluation of commands in the console.
 */
@Override
public void onCreateActions(ListResourceBundle resources, final BaseEditor baseEditor, IProgressMonitor monitor) {
    final PyEdit edit = (PyEdit) baseEditor;
    final EvaluateAction evaluateAction = new EvaluateAction(edit);
    evaluateAction.setActionDefinitionId(IInteractiveConsoleConstants.EVALUATE_ACTION_ID);
    evaluateAction.setId(IInteractiveConsoleConstants.EVALUATE_ACTION_ID);
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            if (!edit.isDisposed()) {
                edit.setAction(IInteractiveConsoleConstants.EVALUATE_ACTION_ID, evaluateAction);
            }
        }
    };
    Display.getDefault().syncExec(runnable);
}
Also used : PyEdit(org.python.pydev.editor.PyEdit)

Example 49 with PyEdit

use of org.python.pydev.editor.PyEdit in project Pydev by fabioz.

the class DjangoActionCreatorPyEditListener method onCreateActions.

@Override
public void onCreateActions(ListResourceBundle resources, BaseEditor baseEditor, IProgressMonitor monitor) {
    PyEdit edit = (PyEdit) baseEditor;
    edit.addOfflineActionListener("dj", new PyDjangoOfflineAction(edit), "Execute django action", true);
}
Also used : PyEdit(org.python.pydev.editor.PyEdit)

Example 50 with PyEdit

use of org.python.pydev.editor.PyEdit in project Pydev by fabioz.

the class PydevIProcessFactory method createInteractiveLaunch.

/**
 * Creates a launch (and its associated IProcess) for the xml-rpc server to be used in the interactive console.
 *
 * It'll ask the user how to create it:
 * - editor
 * - python interpreter
 * - jython interpreter
 *
 * @return the Launch, the Process created and the port that'll be used for the server to call back into
 * this client for requesting input.
 *
 * @throws UserCanceledException
 * @throws Exception
 */
public PydevConsoleLaunchInfo createInteractiveLaunch() throws UserCanceledException, Exception {
    IWorkbenchWindow workbenchWindow = EditorUtils.getActiveWorkbenchWindow();
    IEditorPart activeEditor = EditorUtils.getActiveEditor();
    PyEdit edit = null;
    if (activeEditor instanceof PyEdit) {
        edit = (PyEdit) activeEditor;
    }
    ChooseProcessTypeDialog dialog = new ChooseProcessTypeDialog(getShell(), edit);
    String rep = InteractiveConsolePrefs.getDefaultInteractiveConsole();
    boolean hasPrefs = false;
    if (!rep.isEmpty()) {
        dialog.create();
        hasPrefs = dialog.setInteractiveConsoleInterpreterPref(rep);
        if (hasPrefs) {
            dialog.okPressed();
        }
    }
    if (hasPrefs || dialog.open() == ChooseProcessTypeDialog.OK) {
        PyStackFrame selectedFrame = dialog.getSelectedFrame();
        if (selectedFrame != null) {
            // Interpreter not required for Debug Console
            String encoding = getEncodingFromFrame(selectedFrame);
            return new PydevConsoleLaunchInfo(null, null, 0, null, selectedFrame, new String[] { "Debug connection (no command line)" }, null, encoding);
        }
        IInterpreterManager interpreterManager = dialog.getInterpreterManager();
        if (interpreterManager == null) {
            MessageDialog.openError(workbenchWindow.getShell(), "No interpreter manager for creating console", "No interpreter manager was available for creating a console.");
        }
        IInterpreterInfo[] interpreters = interpreterManager.getInterpreterInfos();
        if (interpreters == null || interpreters.length == 0) {
            MessageDialog.openError(workbenchWindow.getShell(), "No interpreters for creating console", "No interpreter available for creating a console.");
            return null;
        }
        IInterpreterInfo interpreter = null;
        if (interpreters.length == 1) {
            // We just have one, so, no point in asking about which one should be there.
            interpreter = interpreters[0];
        }
        if (interpreter == null) {
            if (PydevConsoleConstants.ACTIVE_EDITOR_INTERPRETER_REPRESENTATION.equals(dialog.getSelectedInteractiveConsoleInterpreterRep())) {
                if (edit != null) {
                    PythonNature pythonNature = PythonNature.getPythonNature(edit.getProject());
                    if (pythonNature != null) {
                        interpreter = pythonNature.getProjectInterpreter();
                    }
                }
            }
            if (interpreter == null) {
                SelectionDialog listDialog = AbstractInterpreterPreferencesPage.createChooseIntepreterInfoDialog(workbenchWindow, interpreters, "Select interpreter to be used.", false);
                int open = listDialog.open();
                if (open != ListDialog.OK || listDialog.getResult().length > 1) {
                    return null;
                }
                Object[] result = listDialog.getResult();
                if (result == null || result.length == 0) {
                    interpreter = interpreters[0];
                } else {
                    interpreter = ((IInterpreterInfo) result[0]);
                }
            }
        }
        if (interpreter == null) {
            return null;
        }
        Tuple<Collection<String>, IPythonNature> pythonpathAndNature = dialog.getPythonpathAndNature(interpreter);
        if (pythonpathAndNature == null) {
            return null;
        }
        return createLaunch(interpreterManager, interpreter, pythonpathAndNature.o1, pythonpathAndNature.o2, dialog.getNatures());
    }
    return null;
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) PyStackFrame(org.python.pydev.debug.model.PyStackFrame) IPythonNature(org.python.pydev.core.IPythonNature) PythonNature(org.python.pydev.plugin.nature.PythonNature) IPythonNature(org.python.pydev.core.IPythonNature) IEditorPart(org.eclipse.ui.IEditorPart) IInterpreterManager(org.python.pydev.core.IInterpreterManager) SelectionDialog(org.eclipse.ui.dialogs.SelectionDialog) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) Collection(java.util.Collection) PyEdit(org.python.pydev.editor.PyEdit)

Aggregations

PyEdit (org.python.pydev.editor.PyEdit)64 PySelection (org.python.pydev.core.docutils.PySelection)22 IFile (org.eclipse.core.resources.IFile)15 ArrayList (java.util.ArrayList)14 BadLocationException (org.eclipse.jface.text.BadLocationException)13 Path (org.eclipse.core.runtime.Path)12 IDocument (org.eclipse.jface.text.IDocument)12 MisconfigurationException (org.python.pydev.core.MisconfigurationException)9 ICompletionProposalHandle (org.python.pydev.shared_core.code_completion.ICompletionProposalHandle)9 IPythonNature (org.python.pydev.core.IPythonNature)8 File (java.io.File)7 ICallbackListener (org.python.pydev.shared_core.callbacks.ICallbackListener)7 ByteArrayInputStream (java.io.ByteArrayInputStream)5 IRegion (org.eclipse.jface.text.IRegion)5 ITextSelection (org.eclipse.jface.text.ITextSelection)5 IEditorInput (org.eclipse.ui.IEditorInput)5 SimpleNode (org.python.pydev.parser.jython.SimpleNode)5 CoreException (org.eclipse.core.runtime.CoreException)4 RefactoringRequest (org.python.pydev.ast.refactoring.RefactoringRequest)4 IInterpreterManager (org.python.pydev.core.IInterpreterManager)4