IDEA-113546 Suggest to create a plain file when creating a Java class with a suspicious name

This commit is contained in:
peter
2013-10-06 23:21:27 +02:00
parent c674398043
commit 46a5f0e96b
2 changed files with 42 additions and 8 deletions
@@ -105,8 +105,8 @@ public class CreateDirectoryOrPackageHandler implements InputValidatorEx {
boolean createFile = false;
if (StringUtil.countChars(subDirName, '.') == 1) {
FileType fileType = FileTypeManager.getInstance().getFileTypeByFileName(subDirName);
if (!(fileType instanceof UnknownFileType)) {
FileType fileType = findFileTypeBoundToName(subDirName);
if (fileType != null) {
String message = "The name you entered looks like a file name. Do you want to create a file named " + subDirName + " instead?";
int ec = Messages.showYesNoDialog(myProject, message,
"File Name Detected", "Yes, create file",
@@ -123,6 +123,12 @@ public class CreateDirectoryOrPackageHandler implements InputValidatorEx {
return myCreatedElement != null;
}
@Nullable
public static FileType findFileTypeBoundToName(String name) {
FileType fileType = FileTypeManager.getInstance().getFileTypeByFileName(name);
return fileType instanceof UnknownFileType ? null : fileType;
}
private void doCreateElement(final String subDirName, final boolean createFile) {
Runnable command = new Runnable() {
@Override
@@ -23,11 +23,15 @@ import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiNameIdentifierOwner;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -62,14 +66,35 @@ public abstract class CreateFromTemplateAction<T extends PsiElement> extends AnA
final CreateFileFromTemplateDialog.Builder builder = CreateFileFromTemplateDialog.createDialog(project);
buildDialog(project, dir, builder);
final Ref<T> createdElement = Ref.create(null);
final Ref<PsiFile> createdFile = Ref.create(null);
final Ref<String> selectedTemplateName = Ref.create(null);
final T createdElement =
builder.show(getErrorTitle(), getDefaultTemplateName(dir), new CreateFileFromTemplateDialog.FileCreator<T>() {
builder.show(getErrorTitle(), getDefaultTemplateName(dir), new CreateFileFromTemplateDialog.FileCreator<T>() {
@Override
public T createFile(@NotNull String name, @NotNull String templateName) {
if (StringUtil.countChars(name, '.') == 1) {
FileType fileType = CreateDirectoryOrPackageHandler.findFileTypeBoundToName(name);
if (fileType != null) {
String message = "The name you entered looks like a file name. Do you want to create a file named " + name + " instead?";
int ec = Messages.showYesNoDialog(project, message,
"File Name Detected",
"Yes, create " + name,
"No, create " + e.getPresentation().getText(),
fileType.getIcon());
if (ec == Messages.OK) {
PsiFile newFile = dir.createFile(name);
createdFile.set(newFile);
//noinspection unchecked
return (T)newFile;
}
}
}
selectedTemplateName.set(templateName);
return CreateFromTemplateAction.this.createFile(name, templateName, dir);
T created = CreateFromTemplateAction.this.createFile(name, templateName, dir);
createdElement.set(created);
return created;
}
@Override
@@ -78,9 +103,12 @@ public abstract class CreateFromTemplateAction<T extends PsiElement> extends AnA
return CreateFromTemplateAction.this.getActionName(dir, name, templateName);
}
});
if (createdElement != null) {
view.selectElement(createdElement);
postProcess(createdElement, selectedTemplateName.get(), builder.getCustomProperties());
if (!createdFile.isNull()) {
view.selectElement(createdFile.get());
}
else if (!createdElement.isNull()) {
view.selectElement(createdElement.get());
postProcess(createdElement.get(), selectedTemplateName.get(), builder.getCustomProperties());
}
}