Skip to content

Commit b9841a3

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 1b4a5da + f5c0853 commit b9841a3

File tree

9 files changed

+145
-14
lines changed

9 files changed

+145
-14
lines changed

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ repositories {
3030
dependencies {
3131
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.6'
3232
testCompile group: 'junit', name: 'junit', version: '4.12'
33-
compileOnly "org.projectlombok:lombok:1.18.0"
33+
compileOnly "org.projectlombok:lombok:1.18.10"
34+
annotationProcessor "org.projectlombok:lombok:1.18.10"
3435
}
3536

3637
intellij {

src/main/java/com/sjhy/plugin/tool/GlobalTool.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public List<?> newArrayList(Object... items) {
8080
}
8181

8282
/**
83-
* 创建无须Map
83+
* 创建无序Map
8484
*
8585
* @return map对象
8686
*/

src/main/java/com/sjhy/plugin/tool/NameUtils.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public static NameUtils getInstance() {
3838
/**
3939
* 转驼峰命名正则匹配规则
4040
*/
41-
private final Pattern TO_HUMP_PATTERN = Pattern.compile("[-_]([a-z0-9])");
41+
private static final Pattern TO_HUMP_PATTERN = Pattern.compile("[-_]([a-z0-9])");
42+
private static final Pattern TO_LINE_PATTERN = Pattern.compile("[A-Z]+");
4243

4344
/**
4445
* 首字母大写方法
@@ -60,6 +61,29 @@ public String firstLowerCase(String name) {
6061
return StringUtils.uncapitalize(name);
6162
}
6263

64+
/**
65+
* 驼峰转下划线,全小写
66+
*
67+
* @param str 驼峰字符串
68+
* @return 下划线字符串
69+
*/
70+
public String hump2Underline(String str) {
71+
if (StringUtils.isEmpty(str)) {
72+
return str;
73+
}
74+
Matcher matcher = TO_LINE_PATTERN.matcher(str);
75+
StringBuffer buffer = new StringBuffer();
76+
while (matcher.find()) {
77+
if (matcher.start() > 0) {
78+
matcher.appendReplacement(buffer, "_" + matcher.group(0).toLowerCase());
79+
} else {
80+
matcher.appendReplacement(buffer, matcher.group(0).toLowerCase());
81+
}
82+
}
83+
matcher.appendTail(buffer);
84+
return buffer.toString();
85+
}
86+
6387
/**
6488
* 通过java全名获取类名
6589
*
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.sjhy.plugin.tool;
2+
3+
import com.intellij.openapi.project.Project;
4+
import com.intellij.openapi.project.ProjectManager;
5+
import com.intellij.openapi.wm.WindowManager;
6+
7+
import java.awt.*;
8+
9+
/**
10+
* IDEA项目相关工具
11+
*
12+
* @author tangcent
13+
* @version 1.0.0
14+
* @since 2020/02/14 18:35
15+
*/
16+
public class ProjectUtils {
17+
18+
/**
19+
* 获取当前项目对象
20+
*
21+
* @return 当前项目对象
22+
*/
23+
public static Project getCurrProject() {
24+
25+
ProjectManager projectManager = ProjectManager.getInstance();
26+
Project[] openProjects = projectManager.getOpenProjects();
27+
if (openProjects.length == 0) {
28+
return projectManager.getDefaultProject();//正常情况下不会发生
29+
} else if (openProjects.length == 1) {
30+
// 只存在一个打开的项目则使用打开的项目
31+
return openProjects[0];
32+
}
33+
34+
//如果有项目窗口处于激活状态
35+
try {
36+
WindowManager wm = WindowManager.getInstance();
37+
for (Project project : openProjects) {
38+
Window window = wm.suggestParentWindow(project);
39+
if (window != null && window.isActive()) {
40+
return project;
41+
}
42+
}
43+
} catch (Exception ignored) {
44+
}
45+
46+
//否则使用默认项目
47+
return projectManager.getDefaultProject();
48+
}
49+
}

src/main/java/com/sjhy/plugin/ui/GlobalConfigSettingPanel.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.sjhy.plugin.entity.GlobalConfigGroup;
1313
import com.sjhy.plugin.tool.CloneUtils;
1414
import com.sjhy.plugin.config.Settings;
15+
import com.sjhy.plugin.tool.ProjectUtils;
1516
import com.sjhy.plugin.ui.base.BaseGroupPanel;
1617
import com.sjhy.plugin.ui.base.BaseItemSelectPanel;
1718
import com.sjhy.plugin.ui.base.TemplateEditor;
@@ -88,11 +89,8 @@ public class GlobalConfigSettingPanel implements Configurable {
8889
* 默认构造方法
8990
*/
9091
GlobalConfigSettingPanel() {
91-
// 存在打开的项目则使用打开的项目,否则使用默认项目
92-
ProjectManager projectManager = ProjectManager.getInstance();
93-
Project[] openProjects = projectManager.getOpenProjects();
9492
// 项目对象
95-
this.project = openProjects.length > 0 ? openProjects[0] : projectManager.getDefaultProject();
93+
this.project = ProjectUtils.getCurrProject();
9694
// 配置服务实例化
9795
this.settings = Settings.getInstance();
9896
// 克隆对象

src/main/java/com/sjhy/plugin/ui/MainSetting.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import com.intellij.openapi.options.ConfigurationException;
88
import com.intellij.openapi.options.UnnamedConfigurable;
99
import com.intellij.openapi.project.Project;
10-
import com.intellij.openapi.project.ProjectManager;
1110
import com.intellij.openapi.ui.*;
1211
import com.intellij.openapi.ui.ex.MultiLineLabel;
1312
import com.intellij.util.ExceptionUtil;
@@ -17,6 +16,7 @@
1716
import com.sjhy.plugin.entity.*;
1817
import com.sjhy.plugin.tool.CollectionUtil;
1918
import com.sjhy.plugin.tool.HttpUtils;
19+
import com.sjhy.plugin.tool.ProjectUtils;
2020
import com.sjhy.plugin.tool.StringUtils;
2121
import com.sjhy.plugin.ui.base.ListCheckboxPanel;
2222
import org.jetbrains.annotations.Nls;
@@ -91,8 +91,8 @@ public class MainSetting implements Configurable, Configurable.Composite {
9191
* 默认构造方法
9292
*/
9393
public MainSetting() {
94-
// 获取默认项目
95-
Project project = ProjectManager.getInstance().getDefaultProject();
94+
// 获取当前项目
95+
Project project = ProjectUtils.getCurrProject();
9696
init();
9797

9898
//初始化事件

src/main/java/com/sjhy/plugin/ui/TemplateSettingPanel.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import com.sjhy.plugin.service.TableInfoService;
4242
import com.sjhy.plugin.tool.CloneUtils;
4343
import com.sjhy.plugin.tool.CollectionUtil;
44+
import com.sjhy.plugin.tool.ProjectUtils;
4445
import com.sjhy.plugin.ui.base.BaseGroupPanel;
4546
import com.sjhy.plugin.ui.base.BaseItemSelectPanel;
4647
import com.sjhy.plugin.ui.base.TemplateEditor;
@@ -117,11 +118,8 @@ public class TemplateSettingPanel implements Configurable {
117118
private Project project;
118119

119120
TemplateSettingPanel() {
120-
// 存在打开的项目则使用打开的项目,否则使用默认项目
121-
ProjectManager projectManager = ProjectManager.getInstance();
122-
Project[] openProjects = projectManager.getOpenProjects();
123121
// 项目对象
124-
this.project = openProjects.length > 0 ? openProjects[0] : projectManager.getDefaultProject();
122+
this.project = ProjectUtils.getCurrProject();
125123
// 配置服务实例化
126124
this.settings = Settings.getInstance();
127125
// 克隆对象
@@ -151,6 +149,8 @@ public JComponent createComponent() {
151149
// 创建主面板
152150
JPanel mainPanel = new JPanel(new BorderLayout());
153151

152+
this.currGroupName = findExistedGroupName(this.currGroupName);
153+
154154
// 实例化分组面板
155155
this.baseGroupPanel = new BaseGroupPanel(new ArrayList<>(group.keySet()), this.currGroupName) {
156156
@Override
@@ -254,6 +254,25 @@ protected void selectedItem(Template item) {
254254
return mainPanel;
255255
}
256256

257+
/**
258+
* 获取存在的分组名
259+
*
260+
* @param groupName 分组名
261+
* @return 存在的分组名
262+
*/
263+
private String findExistedGroupName(String groupName) {
264+
//如果groupName不存在
265+
if (!group.containsKey(groupName)) {
266+
if (group.containsKey(Settings.DEFAULT_NAME)) {//尝试使用默认分组
267+
return Settings.DEFAULT_NAME;
268+
} else {
269+
//获取第一个分组
270+
return group.keySet().stream().findFirst().orElse(Settings.DEFAULT_NAME);
271+
}
272+
}
273+
return groupName;
274+
}
275+
257276
/**
258277
* 添加调试面板
259278
*/
@@ -414,6 +433,7 @@ public void reset() {
414433
// 防止对象篡改,需要进行克隆
415434
this.group = CloneUtils.cloneByJson(settings.getTemplateGroupMap(), new TypeReference<Map<String, TemplateGroup>>() {});
416435
this.currGroupName = settings.getCurrTemplateGroupName();
436+
this.currGroupName = findExistedGroupName(settings.getCurrTemplateGroupName());
417437
if (baseGroupPanel == null) {
418438
return;
419439
}

src/main/resources/description/templateDescription.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
getClsNameByFullName(String fullName) 通过包全名获取类名
4646
getJavaName(String name) 将下划线分割字符串转驼峰命名(属性名)
4747
getClassName(String name) 将下划线分割字符串转驼峰命名(类名)
48+
hump2Underline(String str) 将驼峰字符串转下划线字符串
4849
append(Object... objs) 多个数据进行拼接
4950
newHashSet(Object... objs) 创建一个HashSet对象
5051
newArrayList(Object... objs) 创建一个ArrayList对象
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.sjhy.plugin.tool;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
/**
7+
* GlobalTool相关测试
8+
**/
9+
public class GlobalToolTest {
10+
11+
@Test
12+
public void hump2Underline() {
13+
GlobalTool tool = GlobalTool.getInstance();
14+
String out;
15+
//正常
16+
System.out.println(out = tool.hump2Underline("asianInfrastructureInvestmentBank"));
17+
Assert.assertEquals("asian_infrastructure_investment_bank", out);
18+
//首字母大写
19+
System.out.println(out = tool.hump2Underline("AsianInfrastructureInvestmentBank"));
20+
Assert.assertEquals("asian_infrastructure_investment_bank", out);
21+
//已经是下划线字符串
22+
System.out.println(out = tool.hump2Underline("asian_infrastructure_investment_bank"));
23+
Assert.assertEquals("asian_infrastructure_investment_bank", out);
24+
//包含重复的大写字符
25+
System.out.println(out = tool.hump2Underline("AAsianIInfrastructureInvestmentBank"));
26+
Assert.assertEquals("aasian_iinfrastructure_investment_bank", out);
27+
//空字符串
28+
System.out.println(out = tool.hump2Underline(""));
29+
Assert.assertEquals("", out);
30+
//null
31+
System.out.println(out = tool.hump2Underline(null));
32+
Assert.assertEquals(null, out);
33+
//全大写字符串
34+
System.out.println(out = tool.hump2Underline("AASIANINFRASTRUCTUREINVESTMENTBANK"));
35+
Assert.assertEquals("aasianinfrastructureinvestmentbank", out);
36+
}
37+
38+
}

0 commit comments

Comments
 (0)