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);
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations