public MkDirs(@NotNull String newName, @NotNull PsiDirectory directory) { if (SystemInfo.isWindows) { newName = newName.replace('\\', '/'); } if (newName.contains("/")) { final List<String> subDirs = StringUtil.split(newName, "/"); newName = subDirs.remove(subDirs.size() - 1); boolean firstToken = true; for (String dir : subDirs) { if (firstToken && "~".equals(dir)) { final VirtualFile userHomeDir = VfsUtil.getUserHomeDir(); if (userHomeDir == null) throw new IncorrectOperationException("User home directory not found"); final PsiDirectory directory1 = directory.getManager().findDirectory(userHomeDir); if (directory1 == null) throw new IncorrectOperationException("User home directory not found"); directory = directory1; } else if ("..".equals(dir)) { final PsiDirectory parentDirectory = directory.getParentDirectory(); if (parentDirectory == null) throw new IncorrectOperationException("Not a valid directory"); directory = parentDirectory; } else if (!".".equals(dir)) { final PsiDirectory sub = directory.findSubdirectory(dir); directory = sub == null ? directory.createSubdirectory(dir) : sub; } firstToken = false; } } this.newName = newName; this.directory = directory; }
@Override public boolean checkInput(String inputString) { final StringTokenizer tokenizer = new StringTokenizer(inputString, "\\/"); VirtualFile vFile = getDirectory().getVirtualFile(); boolean firstToken = true; while (tokenizer.hasMoreTokens()) { final String token = tokenizer.nextToken(); if ((token.equals(".") || token.equals("..")) && !tokenizer.hasMoreTokens()) { myErrorText = "Can't create file with name '" + token + "'"; return false; } if (vFile != null) { if (firstToken && "~".equals(token)) { final VirtualFile userHomeDir = VfsUtil.getUserHomeDir(); if (userHomeDir == null) { myErrorText = "User home directory not found"; return false; } vFile = userHomeDir; } else if ("..".equals(token)) { vFile = vFile.getParent(); if (vFile == null) { myErrorText = "Not a valid directory"; return false; } } else if (!".".equals(token)) { final VirtualFile child = vFile.findChild(token); if (child != null) { if (!child.isDirectory()) { myErrorText = "A file with name '" + token + "' already exists"; return false; } else if (!tokenizer.hasMoreTokens()) { myErrorText = "A directory with name '" + token + "' already exists"; return false; } } vFile = child; } } if (FileTypeManager.getInstance().isFileIgnored(getFileName(token))) { myErrorText = "'" + token + "' is an ignored name (Settings | Editor | File Types | Ignore files and folders)"; return true; } firstToken = false; } myErrorText = null; return true; }
@Nullable private static PsiDirectory tryNotNullizeDirectory( @NotNull Project project, @Nullable PsiDirectory defaultTargetDirectory) { if (defaultTargetDirectory == null) { VirtualFile root = ArrayUtil.getFirstElement(ProjectRootManager.getInstance(project).getContentRoots()); if (root == null) root = project.getBaseDir(); if (root == null) root = VfsUtil.getUserHomeDir(); defaultTargetDirectory = root != null ? PsiManager.getInstance(project).findDirectory(root) : null; if (defaultTargetDirectory == null) { LOG.warn("No directory found for project: " + project.getName() + ", root: " + root); } } return defaultTargetDirectory; }