Merge remote-tracking branch 'origin/master'

This commit is contained in:
makejava 2020-04-10 14:02:37 +08:00
commit b9841a329a
9 changed files with 145 additions and 14 deletions

View File

@ -30,7 +30,8 @@ repositories {
dependencies {
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.6'
testCompile group: 'junit', name: 'junit', version: '4.12'
compileOnly "org.projectlombok:lombok:1.18.0"
compileOnly "org.projectlombok:lombok:1.18.10"
annotationProcessor "org.projectlombok:lombok:1.18.10"
}
intellij {

View File

@ -80,7 +80,7 @@ public class GlobalTool extends NameUtils {
}
/**
* 创建无Map
* 创建无Map
*
* @return map对象
*/

View File

@ -38,7 +38,8 @@ public class NameUtils {
/**
* 转驼峰命名正则匹配规则
*/
private final Pattern TO_HUMP_PATTERN = Pattern.compile("[-_]([a-z0-9])");
private static final Pattern TO_HUMP_PATTERN = Pattern.compile("[-_]([a-z0-9])");
private static final Pattern TO_LINE_PATTERN = Pattern.compile("[A-Z]+");
/**
* 首字母大写方法
@ -60,6 +61,29 @@ public class NameUtils {
return StringUtils.uncapitalize(name);
}
/**
* 驼峰转下划线全小写
*
* @param str 驼峰字符串
* @return 下划线字符串
*/
public String hump2Underline(String str) {
if (StringUtils.isEmpty(str)) {
return str;
}
Matcher matcher = TO_LINE_PATTERN.matcher(str);
StringBuffer buffer = new StringBuffer();
while (matcher.find()) {
if (matcher.start() > 0) {
matcher.appendReplacement(buffer, "_" + matcher.group(0).toLowerCase());
} else {
matcher.appendReplacement(buffer, matcher.group(0).toLowerCase());
}
}
matcher.appendTail(buffer);
return buffer.toString();
}
/**
* 通过java全名获取类名
*

View File

@ -0,0 +1,49 @@
package com.sjhy.plugin.tool;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.wm.WindowManager;
import java.awt.*;
/**
* IDEA项目相关工具
*
* @author tangcent
* @version 1.0.0
* @since 2020/02/14 18:35
*/
public class ProjectUtils {
/**
* 获取当前项目对象
*
* @return 当前项目对象
*/
public static Project getCurrProject() {
ProjectManager projectManager = ProjectManager.getInstance();
Project[] openProjects = projectManager.getOpenProjects();
if (openProjects.length == 0) {
return projectManager.getDefaultProject();//正常情况下不会发生
} else if (openProjects.length == 1) {
// 只存在一个打开的项目则使用打开的项目
return openProjects[0];
}
//如果有项目窗口处于激活状态
try {
WindowManager wm = WindowManager.getInstance();
for (Project project : openProjects) {
Window window = wm.suggestParentWindow(project);
if (window != null && window.isActive()) {
return project;
}
}
} catch (Exception ignored) {
}
//否则使用默认项目
return projectManager.getDefaultProject();
}
}

View File

@ -12,6 +12,7 @@ import com.sjhy.plugin.entity.GlobalConfig;
import com.sjhy.plugin.entity.GlobalConfigGroup;
import com.sjhy.plugin.tool.CloneUtils;
import com.sjhy.plugin.config.Settings;
import com.sjhy.plugin.tool.ProjectUtils;
import com.sjhy.plugin.ui.base.BaseGroupPanel;
import com.sjhy.plugin.ui.base.BaseItemSelectPanel;
import com.sjhy.plugin.ui.base.TemplateEditor;
@ -88,11 +89,8 @@ public class GlobalConfigSettingPanel implements Configurable {
* 默认构造方法
*/
GlobalConfigSettingPanel() {
// 存在打开的项目则使用打开的项目否则使用默认项目
ProjectManager projectManager = ProjectManager.getInstance();
Project[] openProjects = projectManager.getOpenProjects();
// 项目对象
this.project = openProjects.length > 0 ? openProjects[0] : projectManager.getDefaultProject();
this.project = ProjectUtils.getCurrProject();
// 配置服务实例化
this.settings = Settings.getInstance();
// 克隆对象

View File

@ -7,7 +7,6 @@ import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.options.UnnamedConfigurable;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.ui.*;
import com.intellij.openapi.ui.ex.MultiLineLabel;
import com.intellij.util.ExceptionUtil;
@ -17,6 +16,7 @@ import com.sjhy.plugin.constants.StrState;
import com.sjhy.plugin.entity.*;
import com.sjhy.plugin.tool.CollectionUtil;
import com.sjhy.plugin.tool.HttpUtils;
import com.sjhy.plugin.tool.ProjectUtils;
import com.sjhy.plugin.tool.StringUtils;
import com.sjhy.plugin.ui.base.ListCheckboxPanel;
import org.jetbrains.annotations.Nls;
@ -91,8 +91,8 @@ public class MainSetting implements Configurable, Configurable.Composite {
* 默认构造方法
*/
public MainSetting() {
// 获取默认项目
Project project = ProjectManager.getInstance().getDefaultProject();
// 获取当前项目
Project project = ProjectUtils.getCurrProject();
init();
//初始化事件

View File

@ -41,6 +41,7 @@ import com.sjhy.plugin.service.CodeGenerateService;
import com.sjhy.plugin.service.TableInfoService;
import com.sjhy.plugin.tool.CloneUtils;
import com.sjhy.plugin.tool.CollectionUtil;
import com.sjhy.plugin.tool.ProjectUtils;
import com.sjhy.plugin.ui.base.BaseGroupPanel;
import com.sjhy.plugin.ui.base.BaseItemSelectPanel;
import com.sjhy.plugin.ui.base.TemplateEditor;
@ -117,11 +118,8 @@ public class TemplateSettingPanel implements Configurable {
private Project project;
TemplateSettingPanel() {
// 存在打开的项目则使用打开的项目否则使用默认项目
ProjectManager projectManager = ProjectManager.getInstance();
Project[] openProjects = projectManager.getOpenProjects();
// 项目对象
this.project = openProjects.length > 0 ? openProjects[0] : projectManager.getDefaultProject();
this.project = ProjectUtils.getCurrProject();
// 配置服务实例化
this.settings = Settings.getInstance();
// 克隆对象
@ -151,6 +149,8 @@ public class TemplateSettingPanel implements Configurable {
// 创建主面板
JPanel mainPanel = new JPanel(new BorderLayout());
this.currGroupName = findExistedGroupName(this.currGroupName);
// 实例化分组面板
this.baseGroupPanel = new BaseGroupPanel(new ArrayList<>(group.keySet()), this.currGroupName) {
@Override
@ -254,6 +254,25 @@ public class TemplateSettingPanel implements Configurable {
return mainPanel;
}
/**
* 获取存在的分组名
*
* @param groupName 分组名
* @return 存在的分组名
*/
private String findExistedGroupName(String groupName) {
//如果groupName不存在
if (!group.containsKey(groupName)) {
if (group.containsKey(Settings.DEFAULT_NAME)) {//尝试使用默认分组
return Settings.DEFAULT_NAME;
} else {
//获取第一个分组
return group.keySet().stream().findFirst().orElse(Settings.DEFAULT_NAME);
}
}
return groupName;
}
/**
* 添加调试面板
*/
@ -414,6 +433,7 @@ public class TemplateSettingPanel implements Configurable {
// 防止对象篡改需要进行克隆
this.group = CloneUtils.cloneByJson(settings.getTemplateGroupMap(), new TypeReference<Map<String, TemplateGroup>>() {});
this.currGroupName = settings.getCurrTemplateGroupName();
this.currGroupName = findExistedGroupName(settings.getCurrTemplateGroupName());
if (baseGroupPanel == null) {
return;
}

View File

@ -45,6 +45,7 @@
getClsNameByFullName(String fullName) 通过包全名获取类名
getJavaName(String name) 将下划线分割字符串转驼峰命名(属性名)
getClassName(String name) 将下划线分割字符串转驼峰命名(类名)
hump2Underline(String str) 将驼峰字符串转下划线字符串
append(Object... objs) 多个数据进行拼接
newHashSet(Object... objs) 创建一个HashSet对象
newArrayList(Object... objs) 创建一个ArrayList对象

View File

@ -0,0 +1,38 @@
package com.sjhy.plugin.tool;
import org.junit.Assert;
import org.junit.Test;
/**
* GlobalTool相关测试
**/
public class GlobalToolTest {
@Test
public void hump2Underline() {
GlobalTool tool = GlobalTool.getInstance();
String out;
//正常
System.out.println(out = tool.hump2Underline("asianInfrastructureInvestmentBank"));
Assert.assertEquals("asian_infrastructure_investment_bank", out);
//首字母大写
System.out.println(out = tool.hump2Underline("AsianInfrastructureInvestmentBank"));
Assert.assertEquals("asian_infrastructure_investment_bank", out);
//已经是下划线字符串
System.out.println(out = tool.hump2Underline("asian_infrastructure_investment_bank"));
Assert.assertEquals("asian_infrastructure_investment_bank", out);
//包含重复的大写字符
System.out.println(out = tool.hump2Underline("AAsianIInfrastructureInvestmentBank"));
Assert.assertEquals("aasian_iinfrastructure_investment_bank", out);
//空字符串
System.out.println(out = tool.hump2Underline(""));
Assert.assertEquals("", out);
//null
System.out.println(out = tool.hump2Underline(null));
Assert.assertEquals(null, out);
//全大写字符串
System.out.println(out = tool.hump2Underline("AASIANINFRASTRUCTUREINVESTMENTBANK"));
Assert.assertEquals("aasianinfrastructureinvestmentbank", out);
}
}