Search in sources :

Example 1 with ICallbackListener

use of org.python.pydev.shared_core.callbacks.ICallbackListener in project Pydev by fabioz.

the class TddTestWorkbench method checkCreateNewModule3.

private void checkCreateNewModule3() throws CoreException, BadLocationException, MisconfigurationException {
    String mod1Contents;
    TddCodeGenerationQuickFixParticipant quickFix;
    PySelection ps;
    List<ICompletionProposalHandle> props;
    IFile mod2 = initFile.getParent().getParent().getParent().getFile(new Path("newpack/module_new.py"));
    final List<PyEdit> pyEditCreated = new ArrayList<PyEdit>();
    ICallbackListener<PyEdit> listener = new ICallbackListener<PyEdit>() {

        @Override
        public Object call(PyEdit obj) {
            pyEditCreated.add(obj);
            return null;
        }
    };
    PyEdit.onPyEditCreated.registerListener(listener);
    try {
        // Create module
        // give it a bit more time...
        goToManual(AnalysisRequestsTestWorkbench.TIME_FOR_ANALYSIS);
        mod1Contents = "" + "import newpack.module_new";
        setContentsAndWaitReparseAndError(mod1Contents);
        quickFix = new TddCodeGenerationQuickFixParticipant();
        int offset = mod1Contents.length();
        ps = new PySelection(editor.getDocument(), offset);
        assertTrue(quickFix.isValid(ps, "", editor, offset));
        props = waitForQuickFixProps(quickFix, ps, offset);
        assertTrue(!mod2.exists());
        findCompletion(props, "Create newpack.module_new module").apply(editor.getISourceViewer(), '\n', 0, offset);
        assertTrue(mod2.exists());
        assertEquals(1, pyEditCreated.size());
        PyEdit editCreated = pyEditCreated.get(0);
        // Create class at module
        mod1Contents = "" + "from newpack import module_new\n" + "module_new.NewClass";
        setContentsAndWaitReparseAndError(mod1Contents);
        quickFix = new TddCodeGenerationQuickFixParticipant();
        offset = mod1Contents.length();
        ps = new PySelection(editor.getDocument(), offset);
        assertTrue(quickFix.isValid(ps, "", editor, offset));
        props = waitForQuickFixProps(quickFix, ps, offset);
        findCompletion(props, "Create NewClass class at module_new.py").apply(editor.getISourceViewer(), '\n', 0, offset);
        String contents = editCreated.getDocument().get();
        assertContentsEqual("" + "class NewClass(object):\n" + "    pass\n" + "\n" + "\n" + "", contents);
        editCreated.getSite().getPage().saveEditor(editCreated, false);
        // Create __init__ at class.
        mod1Contents = "" + "'''\n" + "'''\n" + "" + "def bar():\n" + "    from newpack import module_new\n" + // the 'undefined param' will be the error
        "    module_new.NewClass(param)";
        setContentsAndWaitReparseAndError(mod1Contents);
        quickFix = new TddCodeGenerationQuickFixParticipant();
        offset = mod1Contents.length();
        ps = new PySelection(editor.getDocument(), offset);
        assertTrue(quickFix.isValid(ps, "", editor, offset));
        props = waitForQuickFixProps(quickFix, ps, offset);
        findCompletion(props, "Create NewClass __init__ (newpack.module_new)").apply(editor.getISourceViewer(), '\n', 0, offset);
        contents = editCreated.getDocument().get();
        assertContentsEqual("" + "class NewClass(object):\n" + "    \n" + "    \n" + "    def __init__(self, param):\n" + "        pass\n" + "    \n" + "    \n" + "\n" + "\n" + "\n" + "", contents);
    } finally {
        for (PyEdit edit : pyEditCreated) {
            edit.close(false);
        }
        PyEdit.onPyEditCreated.unregisterListener(listener);
        mod2.delete(true, null);
    }
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) ArrayList(java.util.ArrayList) ICompletionProposalHandle(org.python.pydev.shared_core.code_completion.ICompletionProposalHandle) PySelection(org.python.pydev.core.docutils.PySelection) ICallbackListener(org.python.pydev.shared_core.callbacks.ICallbackListener) PyEdit(org.python.pydev.editor.PyEdit)

Example 2 with ICallbackListener

use of org.python.pydev.shared_core.callbacks.ICallbackListener in project Pydev by fabioz.

the class TddTestWorkbench method checkCreateNewModuleWithClass3.

private void checkCreateNewModuleWithClass3() throws CoreException, BadLocationException, MisconfigurationException {
    String mod1Contents;
    TddCodeGenerationQuickFixParticipant quickFix;
    PySelection ps;
    List<ICompletionProposalHandle> props;
    IFile mod2 = initFile.getParent().getParent().getParent().getFile(new Path("newpack2/module_new9.py"));
    final List<PyEdit> pyEditCreated = new ArrayList<PyEdit>();
    ICallbackListener<PyEdit> listener = new ICallbackListener<PyEdit>() {

        @Override
        public Object call(PyEdit obj) {
            pyEditCreated.add(obj);
            return null;
        }
    };
    PyEdit.onPyEditCreated.registerListener(listener);
    try {
        // give it a bit more time...
        goToManual(AnalysisRequestsTestWorkbench.TIME_FOR_ANALYSIS);
        mod1Contents = "" + "class Foo:\n    from newpack2.module_new9 import Foo";
        setContentsAndWaitReparseAndError(mod1Contents);
        quickFix = new TddCodeGenerationQuickFixParticipant();
        int offset = mod1Contents.length();
        ps = new PySelection(editor.getDocument(), offset);
        assertTrue(quickFix.isValid(ps, "", editor, offset));
        props = waitForQuickFixProps(quickFix, ps, offset);
        assertTrue(!mod2.exists());
        findCompletion(props, "Create Foo class at new module newpack2.module_new9").apply(editor.getISourceViewer(), '\n', 0, offset);
        assertTrue("Expected: " + mod2 + " to exist.", mod2.exists());
        assertEquals(1, pyEditCreated.size());
    } finally {
        for (PyEdit edit : pyEditCreated) {
            edit.close(false);
        }
        PyEdit.onPyEditCreated.unregisterListener(listener);
        mod2.delete(true, null);
    }
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) ArrayList(java.util.ArrayList) ICompletionProposalHandle(org.python.pydev.shared_core.code_completion.ICompletionProposalHandle) PySelection(org.python.pydev.core.docutils.PySelection) ICallbackListener(org.python.pydev.shared_core.callbacks.ICallbackListener) PyEdit(org.python.pydev.editor.PyEdit)

Example 3 with ICallbackListener

use of org.python.pydev.shared_core.callbacks.ICallbackListener in project Pydev by fabioz.

the class TddTestWorkbench method checkCreateNewModule.

private void checkCreateNewModule() throws CoreException, BadLocationException, MisconfigurationException {
    String mod1Contents;
    TddCodeGenerationQuickFixParticipant quickFix;
    PySelection ps;
    IFile mod2 = initFile.getParent().getFile(new Path("module_new.py"));
    final List<PyEdit> pyEditCreated = new ArrayList<PyEdit>();
    ICallbackListener<PyEdit> listener = new ICallbackListener<PyEdit>() {

        @Override
        public Object call(PyEdit obj) {
            pyEditCreated.add(obj);
            return null;
        }
    };
    PyEdit.onPyEditCreated.registerListener(listener);
    try {
        // give it a bit more time...
        goToManual(AnalysisRequestsTestWorkbench.TIME_FOR_ANALYSIS);
        mod1Contents = "" + "import pack1.pack2.module_new";
        setContentsAndWaitReparseAndError(mod1Contents);
        quickFix = new TddCodeGenerationQuickFixParticipant();
        int offset = mod1Contents.length();
        ps = new PySelection(editor.getDocument(), offset);
        assertTrue(quickFix.isValid(ps, "", editor, offset));
        assertTrue(!mod2.exists());
        waitForQuickFixProps(quickFix, ps, offset, "Create pack1.pack2.module_new module").apply(editor.getISourceViewer(), '\n', 0, offset);
        assertTrue(mod2.exists());
        assertEquals(1, pyEditCreated.size());
    } finally {
        for (PyEdit edit : pyEditCreated) {
            edit.close(false);
        }
        PyEdit.onPyEditCreated.unregisterListener(listener);
        mod2.delete(true, null);
    }
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) ArrayList(java.util.ArrayList) PySelection(org.python.pydev.core.docutils.PySelection) ICallbackListener(org.python.pydev.shared_core.callbacks.ICallbackListener) PyEdit(org.python.pydev.editor.PyEdit)

Example 4 with ICallbackListener

use of org.python.pydev.shared_core.callbacks.ICallbackListener in project Pydev by fabioz.

the class TddTestWorkbench method checkCreateNewModule4.

private void checkCreateNewModule4() throws CoreException, BadLocationException, MisconfigurationException {
    String mod1Contents;
    TddCodeGenerationQuickFixParticipant quickFix;
    PySelection ps;
    IFile mod3 = initFile.getParent().getFile(new Path("module_new3.py"));
    final List<PyEdit> pyEditCreated = new ArrayList<PyEdit>();
    ICallbackListener<PyEdit> listener = new ICallbackListener<PyEdit>() {

        @Override
        public Object call(PyEdit obj) {
            pyEditCreated.add(obj);
            return null;
        }
    };
    PyEdit.onPyEditCreated.registerListener(listener);
    try {
        // give it a bit more time...
        goToManual(AnalysisRequestsTestWorkbench.TIME_FOR_ANALYSIS);
        mod1Contents = "" + "from pack1.pack2 import module_new3";
        setContentsAndWaitReparseAndError(mod1Contents);
        quickFix = new TddCodeGenerationQuickFixParticipant();
        int offset = mod1Contents.length();
        ps = new PySelection(editor.getDocument(), offset);
        assertTrue(quickFix.isValid(ps, "", editor, offset));
        assertTrue(!mod3.exists());
        waitForQuickFixProps(quickFix, ps, offset, "Create module_new3 module").apply(editor.getISourceViewer(), '\n', 0, offset);
        assertTrue(mod3.exists());
        assertEquals(1, pyEditCreated.size());
    } finally {
        for (PyEdit edit : pyEditCreated) {
            edit.close(false);
        }
        PyEdit.onPyEditCreated.unregisterListener(listener);
        mod3.delete(true, null);
    }
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) ArrayList(java.util.ArrayList) PySelection(org.python.pydev.core.docutils.PySelection) ICallbackListener(org.python.pydev.shared_core.callbacks.ICallbackListener) PyEdit(org.python.pydev.editor.PyEdit)

Example 5 with ICallbackListener

use of org.python.pydev.shared_core.callbacks.ICallbackListener in project Pydev by fabioz.

the class InterativeConsoleCommandsPreferencesEditorTest method testInteractiveConsoleCommandsPreferencesEditor.

public void testInteractiveConsoleCommandsPreferencesEditor() throws Exception {
    InterativeConsoleCommandsPreferencesEditor editor = new InterativeConsoleCommandsPreferencesEditor();
    Display display = Display.getCurrent();
    if (display == null) {
        display = Display.getDefault();
    }
    Shell parent = new Shell(display);
    parent.setLayout(new FillLayout());
    editor.createContents(parent);
    InteractiveConsoleCommand cmd = new InteractiveConsoleCommand("Plot");
    cmd.keybinding = "F3";
    cmd.commandText = "some text";
    editor.addCommand(cmd);
    assertEquals(editor.getCombo().getItemCount(), 1);
    editor.performSave();
    editor.loadCommands();
    assertEquals(editor.getCombo().getItemCount(), 1);
    assertEquals("Plot", editor.getCombo().getText());
    editor.removeSelectedCommand();
    assertEquals("", editor.getCombo().getText());
    assertEquals(editor.getCombo().getItemCount(), 0);
    cmd = new InteractiveConsoleCommand("Plot");
    editor.addCommand(cmd);
    assertEquals("Plot", editor.getCombo().getText());
    editor.setCommandText("Plot0");
    editor.setKeybindingText("F2");
    assertEquals("Plot0", cmd.commandText);
    assertEquals("F2", cmd.keybinding);
    cmd = new InteractiveConsoleCommand("Plot2");
    editor.addCommand(cmd);
    assertEquals("Plot2", editor.getCombo().getText());
    editor.setCommandText("Plot it");
    editor.setKeybindingText("F1");
    editor.selectComboText("Plot");
    assertEquals("Plot0", editor.getCommandText());
    assertEquals("F2", editor.getCommandKeybinding());
    editor.selectComboText("Plot2");
    editor.removeSelectedCommand();
    assertEquals("Plot", editor.getCombo().getText());
    final ArrayList<Object> lst = new ArrayList<>();
    ICallbackListener<Object> iCallbackListener = new ICallbackListener<Object>() {

        @Override
        public Object call(Object obj) {
            synchronized (lst) {
                lst.add(1);
            }
            return null;
        }
    };
    InteractiveConsoleCommand.registerOnCommandsChangedCallback(iCallbackListener);
    editor.performSave();
    for (int i = 0; i < 10; i++) {
        synchronized (lst) {
            if (lst.size() > 0) {
                break;
            }
        }
        synchronized (this) {
            this.wait(50);
        }
        if (i == 9) {
            fail("Did not get notification that the commands changed.");
        }
    }
    InteractiveConsoleCommand.unregisterOnCommandsChangedCallback(iCallbackListener);
// Uncomment below to see results.
// goToManual(display, parent);
}
Also used : Shell(org.eclipse.swt.widgets.Shell) ArrayList(java.util.ArrayList) FillLayout(org.eclipse.swt.layout.FillLayout) ICallbackListener(org.python.pydev.shared_core.callbacks.ICallbackListener) Display(org.eclipse.swt.widgets.Display)

Aggregations

ICallbackListener (org.python.pydev.shared_core.callbacks.ICallbackListener)10 ArrayList (java.util.ArrayList)9 IFile (org.eclipse.core.resources.IFile)7 Path (org.eclipse.core.runtime.Path)7 PySelection (org.python.pydev.core.docutils.PySelection)7 PyEdit (org.python.pydev.editor.PyEdit)7 ICompletionProposalHandle (org.python.pydev.shared_core.code_completion.ICompletionProposalHandle)5 InterpreterInfoBuilder (com.python.pydev.analysis.system_info_builder.InterpreterInfoBuilder)1 HashSet (java.util.HashSet)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 FillLayout (org.eclipse.swt.layout.FillLayout)1 Display (org.eclipse.swt.widgets.Display)1 Shell (org.eclipse.swt.widgets.Shell)1 SourceToken (org.python.pydev.ast.codecompletion.revisited.modules.SourceToken)1 InterpreterInfo (org.python.pydev.ast.interpreter_managers.InterpreterInfo)1 IInterpreterManager (org.python.pydev.core.IInterpreterManager)1 ModulesKey (org.python.pydev.core.ModulesKey)1 ClassDef (org.python.pydev.parser.jython.ast.ClassDef)1 FunctionDef (org.python.pydev.parser.jython.ast.FunctionDef)1 Name (org.python.pydev.parser.jython.ast.Name)1