mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-117245 (New file allow creating in parent folder)
This commit is contained in:
+28
-11
@@ -41,6 +41,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
public class CreateDirectoryOrPackageHandler implements InputValidatorEx {
|
||||
@Nullable private final Project myProject;
|
||||
@@ -72,22 +73,38 @@ public class CreateDirectoryOrPackageHandler implements InputValidatorEx {
|
||||
|
||||
@Override
|
||||
public boolean checkInput(String inputString) {
|
||||
boolean firstToken = true;
|
||||
for (String token : StringUtil.tokenize(inputString, myDelimiters)) {
|
||||
if (token.equals(".") || token.equals("..")) {
|
||||
final StringTokenizer tokenizer = new StringTokenizer(inputString, myDelimiters);
|
||||
VirtualFile vFile = myDirectory.getVirtualFile();
|
||||
|
||||
while (tokenizer.hasMoreTokens()) {
|
||||
final String token = tokenizer.nextToken();
|
||||
if (!tokenizer.hasMoreTokens() && (token.equals(".") || token.equals(".."))) {
|
||||
myErrorText = "Can't create a directory with name '" + token + "'";
|
||||
return false;
|
||||
}
|
||||
if (firstToken) {
|
||||
final VirtualFile vFile = myDirectory.getVirtualFile();
|
||||
final VirtualFile child = vFile.findChild(token);
|
||||
if (child != null) {
|
||||
myErrorText = "A " + (child.isDirectory() ? "directory" : "file") +
|
||||
" with name '" + token + "' already exists";
|
||||
return false;
|
||||
if (vFile != null) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
firstToken = false;
|
||||
if (FileTypeManager.getInstance().isFileIgnored(token)) {
|
||||
myErrorText = "Trying to create a " + (myIsDirectory ? "directory" : "package") +
|
||||
" with an ignored name, the result will not be visible";
|
||||
|
||||
@@ -34,6 +34,7 @@ import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.openapi.vfs.newvfs.impl.FakeVirtualFile;
|
||||
import com.intellij.psi.PsiDirectory;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -95,8 +96,15 @@ public class CreateFileAction extends CreateElementActionBase implements DumbAwa
|
||||
final List<String> subDirs = StringUtil.split(newName, "/");
|
||||
newName = subDirs.remove(subDirs.size() - 1);
|
||||
for (String dir : subDirs) {
|
||||
final PsiDirectory sub = directory.findSubdirectory(dir);
|
||||
directory = sub == null ? directory.createSubdirectory(dir) : sub;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,25 +150,36 @@ public class CreateFileAction extends CreateElementActionBase implements DumbAwa
|
||||
@Override
|
||||
public boolean checkInput(String inputString) {
|
||||
final StringTokenizer tokenizer = new StringTokenizer(inputString, "\\/");
|
||||
boolean firstToken = true;
|
||||
VirtualFile vFile = getDirectory().getVirtualFile();
|
||||
while (tokenizer.hasMoreTokens()) {
|
||||
final String token = tokenizer.nextToken();
|
||||
if (token.equals(".") || token.equals("..")) {
|
||||
myErrorText = tokenizer.hasMoreTokens()
|
||||
? "Can't create directory with name '" + token + "'"
|
||||
: "Can't create file with name '" + token + "'";
|
||||
if ((token.equals(".") || token.equals("..")) && !tokenizer.hasMoreTokens()) {
|
||||
myErrorText = "Can't create file with name '" + token + "'";
|
||||
return false;
|
||||
}
|
||||
if (firstToken) {
|
||||
final VirtualFile vFile = getDirectory().getVirtualFile();
|
||||
final VirtualFile child = vFile.findChild(token);
|
||||
if (child != null) {
|
||||
myErrorText = "A " + (child.isDirectory() ? "directory" : "file") +
|
||||
" with name '" + token + "' already exists";
|
||||
return false;
|
||||
if (vFile != null) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
firstToken = false;
|
||||
if (FileTypeManager.getInstance().isFileIgnored(getFileName(token))) {
|
||||
myErrorText = "'" + token + "' is an ignored name (Settings | Editor | File Types | Ignore files and folders)";
|
||||
return true;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2000-2009 JetBrains s.r.o.
|
||||
* Copyright 2000-2015 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -97,15 +97,23 @@ public class DirectoryUtil {
|
||||
StringTokenizer tokenizer = new StringTokenizer(subDirName, delim);
|
||||
PsiDirectory dir = baseDirectory;
|
||||
while (tokenizer.hasMoreTokens()) {
|
||||
String packName = tokenizer.nextToken();
|
||||
String dirName = tokenizer.nextToken();
|
||||
if (tokenizer.hasMoreTokens()) {
|
||||
PsiDirectory existingDir = dir.findSubdirectory(packName);
|
||||
if ("..".equals(dirName)) {
|
||||
dir = dir.getParentDirectory();
|
||||
if (dir == null) throw new IncorrectOperationException("Not a valid directory");
|
||||
continue;
|
||||
}
|
||||
else if (".".equals(dirName)) {
|
||||
continue;
|
||||
}
|
||||
PsiDirectory existingDir = dir.findSubdirectory(dirName);
|
||||
if (existingDir != null) {
|
||||
dir = existingDir;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
dir = dir.createSubdirectory(packName);
|
||||
dir = dir.createSubdirectory(dirName);
|
||||
}
|
||||
return dir;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user