PyClass (final Project project, PyClass newClass, @NotNull final PyClass clazz, final String targetFile) { VirtualFile file = VirtualFileManager.getInstance() .findFileByUrl(ApplicationManager.getApplication().isUnitTestMode() ? targetFile : VfsUtilCore.pathToUrl(targetFile)); // file is the same as the source if (Comparing.equal(file, clazz.getContainingFile().getVirtualFile()) || isRefactoredClassInBackedFile(file, clazz)) { return (PyClass)clazz.getParent().addBefore(newClass, clazz); } PsiFile psiFile = null; try { if (file == null) { // file does not exist final String filename; final String path; if (targetFile.endsWith(PythonFileType.INSTANCE.getDefaultExtension())) { path = PathUtil.getParentPath(targetFile); filename = PathUtil.getFileName(targetFile); } else { path = targetFile; filename = PyNames.INIT_DOT_PY; // user requested putting the class into this package directly } psiFile = placeFile(project, path, filename); } else if (file.isDirectory()) { // existing directory psiFile = placeFile(project, file.getPath(), PyNames.INIT_DOT_PY); } else { // existing file psiFile = PsiManager.getInstance(project).findFile(file); } } catch (IOException e) { LOG.error(e); } LOG.assertTrue(psiFile != null); if (psiFile.getLastChild() != null) { // TODO: make the number of newlines depend on style setting psiFile.add(PyElementGenerator.getInstance(project).createFromText(LanguageLevel.PYTHON27, PsiWhiteSpace.class, "\n\n")); } newClass = (PyClass)psiFile.add(newClass); PyPsiRefactoringUtil.insertImport(clazz, Collections.singleton(newClass)); return newClass; }