Skip to content

Commit 18f5fae

Browse files
committed
feat:add auto import
1 parent 1d94624 commit 18f5fae

5 files changed

Lines changed: 89 additions & 44 deletions

File tree

src/main/java/cn/sp/constant/CodeConstants.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ public class CodeConstants {
4242
public static final Map<ActionTypeEnum, String> CODE_TEMPLATE_MAP = new HashMap<>();
4343

4444
static {
45-
String str = "Map<$fieldType,$simpleClassName> $newVariableName = $variableName.stream().collect(Collectors.toMap($simpleClassName::$fieldGetterMethodName, r -> r));";
45+
String str = "Map<$fieldType,$simpleClassName> $newVariableName = $variableName.stream().collect(Collectors.toMap(r -> r.$fieldGetterMethodName(), r -> r));";
4646
CODE_TEMPLATE_MAP.put(ActionTypeEnum.LIST_TO_MAP, str);
4747

48-
String groupByCodeStr = "Map<$fieldType,List<$simpleClassName>> $newVariableName = $variableName.stream().collect(Collectors.groupingBy($simpleClassName::$fieldGetterMethodName));";
48+
String groupByCodeStr = "Map<$fieldType,List<$simpleClassName>> $newVariableName = $variableName.stream().collect(Collectors.groupingBy(r -> r.$fieldGetterMethodName()));";
4949
CODE_TEMPLATE_MAP.put(ActionTypeEnum.GROUP_BY_LIST_TO_MAP, groupByCodeStr);
5050

51-
String toNewListStr = "List<$fieldType> $newVariableName = $variableName.stream().map($simpleClassName::$fieldGetterMethodName).distinct().collect(Collectors.toList());";
51+
String toNewListStr = "List<$fieldType> $newVariableName = $variableName.stream().map(r -> r.$fieldGetterMethodName()).distinct().collect(Collectors.toList());";
5252
CODE_TEMPLATE_MAP.put(ActionTypeEnum.TO_NEW_LIST, toNewListStr);
5353

5454
String returnIfEmptyStr = "${splitText}if (CollectionUtils.isEmpty($variableName)) {" +

src/main/java/cn/sp/service/impl/ConvertToNewListCodeGenerator.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
import com.intellij.psi.*;
1818
import com.intellij.psi.search.GlobalSearchScope;
1919

20+
import java.util.Arrays;
21+
import java.util.HashSet;
22+
import java.util.Set;
23+
import java.util.stream.Collectors;
24+
2025
/**
2126
* @Author: Ship
2227
* @Description:
@@ -112,6 +117,17 @@ private void generateCode(GenerateContext generateContext, GenerateCodeInfo code
112117
psiDocumentManager.commitDocument(generateContext.getDocument());
113118
});
114119
});
120+
121+
// 自动导包
122+
Set<String> importSet = new HashSet<>();
123+
importSet.add("java.util.stream.Collectors");
124+
125+
application.runWriteAction(() -> {
126+
WriteCommandAction.runWriteCommandAction(generateContext.getProject(), () -> {
127+
PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(generateContext.getProject());
128+
CodeUtils.addImportToFile(psiDocumentManager, (PsiJavaFile) generateContext.getPsiFile(), generateContext.getDocument(), importSet);
129+
});
130+
});
115131
}
116132

117133
/**

src/main/java/cn/sp/service/impl/GroupByListToMapCodeGenerator.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
import com.intellij.psi.*;
1818
import com.intellij.psi.search.GlobalSearchScope;
1919

20+
import java.util.HashSet;
21+
import java.util.Set;
22+
2023
/**
2124
* @Author: Ship
2225
* @Description:
@@ -53,6 +56,18 @@ private void generateCode(GenerateContext generateContext, GenerateCodeInfo code
5356
psiDocumentManager.commitDocument(generateContext.getDocument());
5457
});
5558
});
59+
60+
// 自动导包
61+
Set<String> importSet = new HashSet<>();
62+
importSet.add("java.util.stream.Collectors");
63+
importSet.add("java.util.Map");
64+
65+
application.runWriteAction(() -> {
66+
WriteCommandAction.runWriteCommandAction(generateContext.getProject(), () -> {
67+
PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(generateContext.getProject());
68+
CodeUtils.addImportToFile(psiDocumentManager, (PsiJavaFile) generateContext.getPsiFile(), generateContext.getDocument(), importSet);
69+
});
70+
});
5671
}
5772

5873
/**

src/main/java/cn/sp/service/impl/ListToMapCodeGenerator.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
import com.intellij.psi.*;
1818
import com.intellij.psi.search.GlobalSearchScope;
1919

20+
import java.util.HashSet;
21+
import java.util.Set;
22+
2023
/**
2124
* @Author: Ship
2225
* @Description:
@@ -105,6 +108,18 @@ private void generateCode(GenerateContext generateContext, GenerateCodeInfo code
105108
psiDocumentManager.commitDocument(generateContext.getDocument());
106109
});
107110
});
111+
112+
// 自动导包
113+
Set<String> importSet = new HashSet<>();
114+
importSet.add("java.util.stream.Collectors");
115+
importSet.add("java.util.Map");
116+
117+
application.runWriteAction(() -> {
118+
WriteCommandAction.runWriteCommandAction(generateContext.getProject(), () -> {
119+
PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(generateContext.getProject());
120+
CodeUtils.addImportToFile(psiDocumentManager, (PsiJavaFile) generateContext.getPsiFile(), generateContext.getDocument(), importSet);
121+
});
122+
});
108123
}
109124

110125

src/main/resources/META-INF/plugin.xml

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,60 +24,59 @@
2424
<action id="CodeFaster.ListToMapAction" class="cn.sp.action.ListToMapAction" text="Convert List To Map"
2525
description="generate code of list to map">
2626
<add-to-group group-id="GenerateGroup" anchor="last"/>
27-
<!-- <keyboard-shortcut-->
28-
<!-- keymap="$default"-->
29-
<!-- first-keystroke="control shift T"/>-->
27+
<keyboard-shortcut
28+
keymap="$default"
29+
first-keystroke="control shift T"/>
3030

31-
<!-- &lt;!&ndash; ...except the "Mac OS X" keymap and its children. &ndash;&gt;-->
32-
<!-- <keyboard-shortcut-->
33-
<!-- keymap="Mac OS X"-->
34-
<!-- first-keystroke="control shift T"-->
35-
<!-- remove="true"/>-->
31+
<!-- ...except the "Mac OS X" keymap and its children. -->
32+
<keyboard-shortcut
33+
keymap="Mac OS X"
34+
first-keystroke="control shift T"
35+
remove="true"/>
3636

37-
<!-- &lt;!&ndash; The "Mac OS X 10.5+" keymap and its children will have only-->
38-
<!-- this keyboard shortcut for this action. &ndash;&gt;-->
39-
<!-- <keyboard-shortcut-->
40-
<!-- keymap="Mac OS X 10.5+"-->
41-
<!-- first-keystroke="control shift T"-->
42-
<!-- replace-all="true"/>-->
37+
<!-- The "Mac OS X 10.5+" keymap and its children will have only
38+
this keyboard shortcut for this action. -->
39+
<keyboard-shortcut
40+
keymap="Mac OS X 10.5+"
41+
first-keystroke="control shift T"
42+
replace-all="true"/>
4343
</action>
4444
<action id="CodeFaster.GroupByListToMapAction" class="cn.sp.action.GroupByListToMapAction"
4545
text="Group By List To Map" description="generate code of group by list to map">
4646
<add-to-group group-id="GenerateGroup" anchor="last"/>
47-
<!-- <keyboard-shortcut-->
48-
<!-- keymap="$default"-->
49-
<!-- first-keystroke="control shift Y"/>-->
47+
<keyboard-shortcut
48+
keymap="$default"
49+
first-keystroke="control shift Y"/>
5050

51-
<!-- &lt;!&ndash; ...except the "Mac OS X" keymap and its children. &ndash;&gt;-->
52-
<!-- <keyboard-shortcut-->
53-
<!-- keymap="Mac OS X"-->
54-
<!-- first-keystroke="control shift Y"-->
55-
<!-- remove="true"/>-->
51+
<!-- ...except the "Mac OS X" keymap and its children. -->
52+
<keyboard-shortcut
53+
keymap="Mac OS X"
54+
first-keystroke="control shift Y"
55+
remove="true"/>
5656

57-
<!-- &lt;!&ndash; The "Mac OS X 10.5+" keymap and its children will have only-->
58-
<!-- this keyboard shortcut for this action. &ndash;&gt;-->
59-
<!-- <keyboard-shortcut-->
60-
<!-- keymap="Mac OS X 10.5+"-->
61-
<!-- first-keystroke="control shift Y"-->
62-
<!-- replace-all="true"/>-->
57+
<!-- The "Mac OS X 10.5+" keymap and its children will have only
58+
this keyboard shortcut for this action. -->
59+
<keyboard-shortcut
60+
keymap="Mac OS X 10.5+"
61+
first-keystroke="control shift Y"
62+
replace-all="true"/>
6363
</action>
6464
<action id="CodeFaster.ConvertToNewListAction" class="cn.sp.action.ConvertToNewListAction"
6565
text="Convert To New List" description="convert current list to new List by field">
6666
<add-to-group group-id="GenerateGroup" anchor="last"/>
67-
<!-- <keyboard-shortcut keymap="$default" first-keystroke="shift ctrl U"/>-->
67+
<keyboard-shortcut keymap="$default" first-keystroke="shift ctrl K"/>
68+
<!-- ...except the "Mac OS X" keymap and its children. -->
69+
<keyboard-shortcut
70+
keymap="Mac OS X"
71+
first-keystroke="control shift K"
72+
remove="true"/>
6873

69-
<!-- &lt;!&ndash; ...except the "Mac OS X" keymap and its children. &ndash;&gt;-->
70-
<!-- <keyboard-shortcut-->
71-
<!-- keymap="Mac OS X"-->
72-
<!-- first-keystroke="control shift U"-->
73-
<!-- remove="true"/>-->
74-
75-
<!-- &lt;!&ndash; The "Mac OS X 10.5+" keymap and its children will have only-->
76-
<!-- this keyboard shortcut for this action. &ndash;&gt;-->
77-
<!-- <keyboard-shortcut-->
78-
<!-- keymap="Mac OS X 10.5+"-->
79-
<!-- first-keystroke="control shift U"-->
80-
<!-- replace-all="true"/>-->
74+
<!-- The "Mac OS X 10.5+" keymap and its children will have only
75+
this keyboard shortcut for this action. -->
76+
<keyboard-shortcut
77+
keymap="Mac OS X 10.5+"
78+
first-keystroke="control shift K"
79+
replace-all="true"/>
8180
</action>
8281
<action id="CodeFaster.ReturnIfEmptyAction" class="cn.sp.action.ReturnIfEmptyAction" text="Return If List Empty"
8382
description="return a empty list if current list is empty">

0 commit comments

Comments
 (0)