mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
create file/class with dir/package prefix
This commit is contained in:
@@ -32,6 +32,8 @@ import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.PlatformIcons;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -65,8 +67,18 @@ public class CreateClassAction extends JavaCreateTemplateInPackageAction<PsiClas
|
||||
builder.setValidator(new InputValidatorEx() {
|
||||
@Override
|
||||
public String getErrorText(String inputString) {
|
||||
if (inputString.length() > 0 && !JavaPsiFacade.getInstance(project).getNameHelper().isQualifiedName(inputString)) {
|
||||
return "This is not a valid Java qualified name";
|
||||
if (inputString.length() > 0) {
|
||||
final List<String> tokens = inputString.contains(".") ? StringUtil.split(inputString, ".") : Arrays.asList(inputString);
|
||||
for (String classOrPackageName : tokens) {
|
||||
if (!JavaPsiFacade.getInstance(project).getNameHelper().isQualifiedName(classOrPackageName)) {
|
||||
return classOrPackageName.isEmpty()
|
||||
|| ".".equals(classOrPackageName)
|
||||
|| classOrPackageName.startsWith(".")
|
||||
|| classOrPackageName.endsWith(".")
|
||||
? "Class or Package name should not be empty"
|
||||
: "'" + classOrPackageName + "' is not a valid Java qualified name";
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -95,6 +107,14 @@ public class CreateClassAction extends JavaCreateTemplateInPackageAction<PsiClas
|
||||
}
|
||||
|
||||
protected final PsiClass doCreate(PsiDirectory dir, String className, String templateName) throws IncorrectOperationException {
|
||||
if (className.contains(".")) {
|
||||
final List<String> packages = StringUtil.split(className, ".");
|
||||
className = packages.remove(packages.size() - 1);
|
||||
|
||||
for (String aPackage : packages) {
|
||||
dir = dir.createSubdirectory(aPackage);
|
||||
}
|
||||
}
|
||||
return JavaDirectoryService.getInstance().createClass(dir, className, templateName, true);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2000-2009 JetBrains s.r.o.
|
||||
* Copyright 2000-2012 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.
|
||||
@@ -26,7 +26,9 @@ import com.intellij.openapi.project.DumbAware;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.ui.InputValidatorEx;
|
||||
import com.intellij.openapi.ui.Messages;
|
||||
import com.intellij.openapi.util.SystemInfo;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.PsiDirectory;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -34,6 +36,7 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
public class CreateFileAction extends CreateElementActionBase implements DumbAware {
|
||||
|
||||
@@ -70,6 +73,17 @@ public class CreateFileAction extends CreateElementActionBase implements DumbAwa
|
||||
|
||||
@NotNull
|
||||
protected PsiElement[] create(String newName, PsiDirectory directory) throws Exception {
|
||||
if (SystemInfo.isWindows) {
|
||||
newName = newName.replace('\\', '/');
|
||||
}
|
||||
if (newName.contains("/")) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
return new PsiElement[]{directory.createFile(getFileName(newName))};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user