@NotNull
protected PsiElement[] create(String newName, PsiDirectory directory) throws Exception {
HaskellFileType type = HaskellFileType.INSTANCE;
String ext = type.getDefaultExtension();
Project project = directory.getProject();
PsiDirectory moduleDir = directory;
int length = newName.length() - 1;
while (newName.charAt(length) == '.') {
newName = newName.substring(0, length);
length--;
}
String suffix = "." + ext;
if (newName.toLowerCase().endsWith(suffix)) {
newName = newName.substring(0, newName.length() - suffix.length());
}
String[] fileNames = newName.split("\\.");
int depth = fileNames.length;
String moduleName = fileNames[depth - 1];
boolean needsModuleName = Character.isUpperCase(moduleName.charAt(0));
String parentPackages = ProjectRootManager.getInstance(project).getFileIndex().getPackageNameByDirectory(directory.getVirtualFile());
StringBuilder packages = new StringBuilder(
"".equals(parentPackages) || !needsModuleName
? ""
: parentPackages + "."
);
for (int i = 0; i < depth - 1; i++) {
String fileName = fileNames[i];
// todo: check before create
// todo: check correct module names
if ("".equals(fileName)) {
throw new IncorrectOperationException("File name cannot be empty");
}
char oldChar = fileName.charAt(0);
String dirName = fileName.replace(oldChar, Character.toUpperCase(oldChar));
PsiDirectory subDir = moduleDir.findSubdirectory(dirName);
moduleDir = subDir == null ? moduleDir.createSubdirectory(dirName) : subDir;
if (needsModuleName) {
packages.append(dirName).append('.');
}
}