mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-113546 Suggest to create a plain file when creating a Java class with a suspicious name
This commit is contained in:
+8
-2
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user