Search in sources :

Example 1 with PydevConsoleInterpreter

use of org.python.pydev.debug.newconsole.PydevConsoleInterpreter in project Pydev by fabioz.

the class ConsoleCompletionsPageParticipant method init.

/**
 * When a console page is initialized,
 */
@Override
public void init(IPageBookViewPage page, final IConsole console) {
    if (!(console instanceof ProcessConsole)) {
        return;
    }
    ProcessConsole processConsole = (ProcessConsole) console;
    IProcess process = processConsole.getProcess();
    if (process == null) {
        return;
    }
    if (!PyCodeCompletionPreferences.useCodeCompletion() || !PyCodeCompletionPreferences.useCodeCompletionOnDebug()) {
        return;
    }
    String attribute = process.getAttribute(Constants.PYDEV_DEBUG_IPROCESS_ATTR);
    if (!Constants.PYDEV_DEBUG_IPROCESS_ATTR_TRUE.equals(attribute)) {
        // Only provide code-completion for pydev debug processes.
        return;
    }
    Control control = page.getControl();
    if (page instanceof IOConsolePage) {
        // Note that completions on "all letters and '_'" are already activated just by installing
        // the content assist, but the completions on the default keybinding is not, so, we have to
        // call it ourselves here.
        control.addKeyListener(new KeyListener() {

            @Override
            public void keyPressed(KeyEvent e) {
                if (KeyBindingHelper.matchesContentAssistKeybinding(e)) {
                    contentAssist.showPossibleCompletions();
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
            }
        });
        final CurrentPyStackFrameForConsole currentPyStackFrameForConsole = new CurrentPyStackFrameForConsole(console);
        IOConsolePage consolePage = (IOConsolePage) page;
        TextConsoleViewer viewer = consolePage.getViewer();
        PydevConsoleInterpreter interpreter = new PydevConsoleInterpreter();
        interpreter.setLaunchAndRelatedInfo(process.getLaunch());
        interpreter.setConsoleCommunication(new GetCompletionsInDebug(currentPyStackFrameForConsole));
        contentAssist = new PyContentAssistant() {

            @Override
            public String showPossibleCompletions() {
                // Only show completions if we're in a suspended console.
                if (currentPyStackFrameForConsole.getLastSelectedFrame() == null) {
                    return null;
                }
                return super.showPossibleCompletions();
            }
        };
        contentAssist.setInformationControlCreator(new PyInformationControlCreator());
        contentAssist.install(new ScriptConsoleViewerWrapper(viewer, interpreter.getInterpreterInfo()));
        IContentAssistProcessor processor = new PydevConsoleCompletionProcessor(interpreter, contentAssist);
        contentAssist.setContentAssistProcessor(processor, IOConsolePartition.INPUT_PARTITION_TYPE);
        contentAssist.setContentAssistProcessor(processor, IOConsolePartition.OUTPUT_PARTITION_TYPE);
        contentAssist.enableAutoActivation(true);
        contentAssist.enableAutoInsert(false);
        contentAssist.setAutoActivationDelay(PyCodeCompletionPreferences.getAutocompleteDelay());
    }
}
Also used : CurrentPyStackFrameForConsole(org.python.pydev.debug.newconsole.CurrentPyStackFrameForConsole) PydevConsoleInterpreter(org.python.pydev.debug.newconsole.PydevConsoleInterpreter) IOConsolePage(org.eclipse.ui.internal.console.IOConsolePage) PyContentAssistant(org.python.pydev.editor.codecompletion.PyContentAssistant) ProcessConsole(org.eclipse.debug.internal.ui.views.console.ProcessConsole) IContentAssistProcessor(org.eclipse.jface.text.contentassist.IContentAssistProcessor) PyInformationControlCreator(org.python.pydev.editor.PyInformationControlCreator) KeyEvent(org.eclipse.swt.events.KeyEvent) Control(org.eclipse.swt.widgets.Control) TextConsoleViewer(org.eclipse.ui.console.TextConsoleViewer) PydevConsoleCompletionProcessor(org.python.pydev.debug.newconsole.PydevConsoleCompletionProcessor) KeyListener(org.eclipse.swt.events.KeyListener) IProcess(org.eclipse.debug.core.model.IProcess)

Example 2 with PydevConsoleInterpreter

use of org.python.pydev.debug.newconsole.PydevConsoleInterpreter in project Pydev by fabioz.

the class DjangoShell method run.

@Override
public void run(IAction action) {
    try {
        // this.launchDjangoCommand("shell", false);
        PythonNature nature = PythonNature.getPythonNature(selectedProject);
        if (nature == null) {
            MessageDialog.openError(EditorUtils.getShell(), "PyDev nature not found", "Unable to perform action because the Pydev nature is not properly set.");
            return;
        }
        IPythonPathNature pythonPathNature = nature.getPythonPathNature();
        String settingsModule = null;
        Map<String, String> variableSubstitution = null;
        try {
            variableSubstitution = pythonPathNature.getVariableSubstitution();
            settingsModule = variableSubstitution.get(DjangoConstants.DJANGO_SETTINGS_MODULE);
        } catch (Exception e1) {
            throw new RuntimeException(e1);
        }
        if (settingsModule == null) {
            InputDialog d = new InputDialog(EditorUtils.getShell(), "Settings module", "Please enter the settings module to be used.\n" + "\n" + "Note that it can be edited later in:\np" + "roject properties > pydev pythonpath > string substitution variables.", selectedProject.getName() + ".settings", new IInputValidator() {

                @Override
                public String isValid(String newText) {
                    if (newText.length() == 0) {
                        return "Text must be entered.";
                    }
                    for (char c : newText.toCharArray()) {
                        if (c == ' ') {
                            return "Whitespaces not accepted";
                        }
                        if (c != '.' && !Character.isJavaIdentifierPart(c)) {
                            return "Invalid char: " + c;
                        }
                    }
                    return null;
                }
            });
            int retCode = d.open();
            if (retCode == InputDialog.OK) {
                settingsModule = d.getValue();
                variableSubstitution.put(DjangoConstants.DJANGO_SETTINGS_MODULE, settingsModule);
                try {
                    pythonPathNature.setVariableSubstitution(variableSubstitution);
                } catch (Exception e) {
                    Log.log(e);
                }
            }
            if (settingsModule == null) {
                return;
            }
        }
        List<IPythonNature> natures = Collections.singletonList((IPythonNature) nature);
        PydevConsoleFactory consoleFactory = new PydevConsoleFactory();
        PydevConsoleLaunchInfo launchInfo = new PydevIProcessFactory().createLaunch(nature.getRelatedInterpreterManager(), nature.getProjectInterpreter(), nature.getPythonPathNature().getCompleteProjectPythonPath(nature.getProjectInterpreter(), nature.getRelatedInterpreterManager()), nature, natures);
        PydevConsoleInterpreter interpreter = PydevConsoleFactory.createPydevInterpreter(launchInfo, natures, launchInfo.encoding);
        String djangoAdditionalCommands = PydevDebugPlugin.getDefault().getPreferenceStore().getString(PydevConsoleConstants.DJANGO_INTERPRETER_CMDS);
        djangoAdditionalCommands = djangoAdditionalCommands.replace("${" + DjangoConstants.DJANGO_SETTINGS_MODULE + "}", settingsModule);
        // os.environ.setdefault("DJANGO_SETTINGS_MODULE", "fooproject.settings")
        consoleFactory.createConsole(interpreter, djangoAdditionalCommands);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : InputDialog(org.eclipse.jface.dialogs.InputDialog) PydevConsoleInterpreter(org.python.pydev.debug.newconsole.PydevConsoleInterpreter) IPythonNature(org.python.pydev.core.IPythonNature) PythonNature(org.python.pydev.plugin.nature.PythonNature) PydevConsoleFactory(org.python.pydev.debug.newconsole.PydevConsoleFactory) IPythonPathNature(org.python.pydev.core.IPythonPathNature) IPythonNature(org.python.pydev.core.IPythonNature) PydevConsoleLaunchInfo(org.python.pydev.debug.newconsole.env.PydevIProcessFactory.PydevConsoleLaunchInfo) PydevIProcessFactory(org.python.pydev.debug.newconsole.env.PydevIProcessFactory) IInputValidator(org.eclipse.jface.dialogs.IInputValidator)

Aggregations

PydevConsoleInterpreter (org.python.pydev.debug.newconsole.PydevConsoleInterpreter)2 IProcess (org.eclipse.debug.core.model.IProcess)1 ProcessConsole (org.eclipse.debug.internal.ui.views.console.ProcessConsole)1 IInputValidator (org.eclipse.jface.dialogs.IInputValidator)1 InputDialog (org.eclipse.jface.dialogs.InputDialog)1 IContentAssistProcessor (org.eclipse.jface.text.contentassist.IContentAssistProcessor)1 KeyEvent (org.eclipse.swt.events.KeyEvent)1 KeyListener (org.eclipse.swt.events.KeyListener)1 Control (org.eclipse.swt.widgets.Control)1 TextConsoleViewer (org.eclipse.ui.console.TextConsoleViewer)1 IOConsolePage (org.eclipse.ui.internal.console.IOConsolePage)1 IPythonNature (org.python.pydev.core.IPythonNature)1 IPythonPathNature (org.python.pydev.core.IPythonPathNature)1 CurrentPyStackFrameForConsole (org.python.pydev.debug.newconsole.CurrentPyStackFrameForConsole)1 PydevConsoleCompletionProcessor (org.python.pydev.debug.newconsole.PydevConsoleCompletionProcessor)1 PydevConsoleFactory (org.python.pydev.debug.newconsole.PydevConsoleFactory)1 PydevIProcessFactory (org.python.pydev.debug.newconsole.env.PydevIProcessFactory)1 PydevConsoleLaunchInfo (org.python.pydev.debug.newconsole.env.PydevIProcessFactory.PydevConsoleLaunchInfo)1 PyInformationControlCreator (org.python.pydev.editor.PyInformationControlCreator)1 PyContentAssistant (org.python.pydev.editor.codecompletion.PyContentAssistant)1