Search in sources :

Example 1 with PyEdit

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

the class TddRefactorCompletionInInexistentModule method apply.

@Override
public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
    // Now, we need to go on and create the module
    List<File> parents = new ArrayList<File>();
    File f = module.getParentFile();
    while (f != null && !f.exists()) {
        parents.add(f);
        f = f.getParentFile();
    }
    IProject project = null;
    if (edit != null) {
        project = edit.getProject();
    }
    IContainer container = FindWorkspaceFiles.getContainerForLocation(Path.fromOSString(f.getAbsolutePath()), project);
    if (container == null) {
        return;
    }
    Collections.reverse(parents);
    int size = parents.size();
    for (int i = 0; i < size; i++) {
        File parent = parents.get(i);
        // create folder with __init__.
        IFolder folder = container.getFolder(new Path(parent.getName()));
        if (!folder.exists()) {
            try {
                folder.create(true, true, null);
            } catch (CoreException e) {
                Log.log(e);
            }
        }
        container = folder;
        IFile file = container.getFile(new Path("__init__" + FileTypesPreferences.getDefaultDottedPythonExtension()));
        if (!file.exists()) {
            try {
                file.create(new ByteArrayInputStream(new byte[0]), true, null);
            } catch (CoreException e) {
                Log.log(e);
            }
        }
    }
    // Now that the package structure is created, create the actual module.
    IFile file = container.getFile(new Path(module.getName()));
    if (!file.exists()) {
        try {
            file.create(new ByteArrayInputStream(new byte[0]), true, null);
        } catch (CoreException e) {
            Log.log(e);
        }
    }
    // Upon creation, opens the new editor and creates the class.
    PyOpenAction openAction = new PyOpenAction();
    openAction.run(new ItemPointer(file));
    PyEdit pyEdit = (PyEdit) openAction.editor;
    TddRefactorCompletion completion = new TddRefactorCompletion(fReplacementString, fImage, fDisplayString, fContextInformation, fAdditionalProposalInfo, 0, pyEdit, PyCreateClass.LOCATION_STRATEGY_END, parametersAfterCall, pyCreateAction, ps);
    completion.apply(pyEdit.getEditorSourceViewer(), '\n', 0, 0);
    // As the change was done in another module, let's ask for a new code analysis for the current editor,
    // as the new contents should fix the marker which we used for the fix.
    forceReparseInBaseEditorAnd(pyEdit);
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) PyOpenAction(org.python.pydev.editor.actions.PyOpenAction) ArrayList(java.util.ArrayList) IProject(org.eclipse.core.resources.IProject) Point(org.eclipse.swt.graphics.Point) CoreException(org.eclipse.core.runtime.CoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) IContainer(org.eclipse.core.resources.IContainer) File(java.io.File) IFile(org.eclipse.core.resources.IFile) PyEdit(org.python.pydev.editor.PyEdit) IFolder(org.eclipse.core.resources.IFolder) ItemPointer(org.python.pydev.ast.item_pointer.ItemPointer)

Example 2 with PyEdit

use of org.python.pydev.editor.PyEdit 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 3 with PyEdit

use of org.python.pydev.editor.PyEdit 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 4 with PyEdit

use of org.python.pydev.editor.PyEdit 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 5 with PyEdit

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

the class TddTestWorkbench method checkCreateClassAtOtherModule.

private void checkCreateClassAtOtherModule() throws CoreException, BadLocationException, MisconfigurationException {
    String mod1Contents;
    TddCodeGenerationQuickFixParticipant quickFix;
    PySelection ps;
    List<ICompletionProposalHandle> props;
    IFile mod2 = initFile.getParent().getFile(new Path("other_module.py"));
    mod2.create(new ByteArrayInputStream("".getBytes()), true, null);
    PyEdit editor2 = (PyEdit) PyOpenEditor.doOpenEditor(mod2);
    try {
        // give it a bit more time...
        goToManual(AnalysisRequestsTestWorkbench.TIME_FOR_ANALYSIS);
        mod1Contents = "" + "import other_module\n" + "other_module.Foo";
        setContentsAndWaitReparseAndError(mod1Contents);
        quickFix = new TddCodeGenerationQuickFixParticipant();
        int offset = mod1Contents.length() - 1;
        ps = new PySelection(editor.getDocument(), offset);
        assertTrue(quickFix.isValid(ps, "", editor, offset));
        props = waitForQuickFixProps(quickFix, ps, offset);
        findCompletion(props, "Create Foo class at other_module.py").apply(editor.getISourceViewer(), '\n', 0, offset);
        assertContentsEqual("" + "class Foo(object):\n" + "    pass\n" + "\n" + "\n" + "", editor2.getDocument().get());
    } finally {
        editor2.close(false);
        mod2.delete(true, null);
    }
}
Also used : Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) ByteArrayInputStream(java.io.ByteArrayInputStream) ICompletionProposalHandle(org.python.pydev.shared_core.code_completion.ICompletionProposalHandle) PySelection(org.python.pydev.core.docutils.PySelection) 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