Skip to content

Commit d4b458c

Browse files
author
seal
committed
Add Anotation import class code block
1 parent 497f259 commit d4b458c

File tree

3 files changed

+81
-4
lines changed

3 files changed

+81
-4
lines changed

src/wu/seal/jsontokotlin/ConfigSettingDialog.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class ConfigSettingDialog(canBeParent: Boolean) : DialogWrapper(canBeParent), IC
4545

4646
tabbedPane.add("Target Json Lib", targetJsonLibConfigPanel)
4747

48-
tabbedPane.minimumSize = JBDimension(300, 180)
48+
tabbedPane.minimumSize = JBDimension(500, 300)
4949

5050
return tabbedPane
5151
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package wu.seal.jsontokotlin
2+
3+
import com.intellij.openapi.application.ApplicationManager
4+
import com.intellij.openapi.command.CommandProcessor
5+
import com.intellij.openapi.editor.Document
6+
import com.intellij.openapi.project.Project
7+
8+
/**
9+
* to be Import class declare code insert helper
10+
* Created by Seal.Wu on 2017/9/18.
11+
*/
12+
13+
14+
interface IImportClassWriter {
15+
16+
fun insertGsonImportClass(project: Project, editFile: Document)
17+
18+
19+
fun insertJackSonImportClass(project: Project, editFile: Document)
20+
21+
22+
fun insertFastJsonImportClass(project: Project, editFile: Document)
23+
24+
25+
fun insertImportClassCode(project: Project, editFile: Document)
26+
27+
28+
}
29+
30+
31+
object ImportClassWriter : IImportClassWriter {
32+
override fun insertImportClassCode(project: Project, editFile: Document) {
33+
34+
when (ConfigManager.targetJsonConverterLib) {
35+
36+
TargetJsonConverter.Gson -> insertGsonImportClass(project, editFile)
37+
TargetJsonConverter.FastJson -> insertFastJsonImportClass(project, editFile)
38+
TargetJsonConverter.Jackson -> insertFastJsonImportClass(project, editFile)
39+
40+
else -> {
41+
println("No need to import any Class code")
42+
}
43+
}
44+
}
45+
46+
47+
override fun insertFastJsonImportClass(project: Project, editFile: Document) {
48+
}
49+
50+
override fun insertJackSonImportClass(project: Project, editFile: Document) {
51+
}
52+
53+
override fun insertGsonImportClass(project: Project, editFile: Document) {
54+
val text = editFile.text
55+
if (GsonSupportor.gsonAnotationImportString !in text) {
56+
57+
val index = Math.max(text.lastIndexOf("import"), 0)
58+
val tobeInsertEndline = editFile.getLineNumber(index)
59+
val insertIndex = if (index == 0) index else editFile.getLineEndOffset(tobeInsertEndline)
60+
61+
CommandProcessor.getInstance().executeCommand(project, {
62+
ApplicationManager.getApplication().runWriteAction {
63+
editFile.insertString(insertIndex, "\n" + GsonSupportor.gsonAnotationImportString + "\n")
64+
65+
}
66+
}, "insertKotlin", "JsonToKotlin")
67+
68+
}
69+
}
70+
71+
}
72+
73+

src/wu/seal/jsontokotlin/MakeKotlinClassAction.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,10 @@ protected JComponent createScrollableTextComponent() {
120120
if (jsonString == null || jsonString.isEmpty()) {
121121
return;
122122
}
123-
final KotlinMaker maker = new KotlinMaker(className, jsonString);
124-
125123
final Document document = editor.getDocument();
124+
ImportClassWriter.INSTANCE.insertImportClassCode(project, document);
125+
126+
final KotlinMaker maker = new KotlinMaker(className, jsonString);
126127
final VirtualFile virtualFile = FileDocumentManager.getInstance().getFile(document);
127128

128129
CommandProcessor.getInstance().executeCommand(project, new Runnable() {
@@ -137,6 +138,9 @@ public void run() {
137138
if (caret != null) {
138139

139140
offset = caret.getOffset();
141+
if (offset == 0) {
142+
offset = document.getTextLength() - 1;
143+
}
140144
} else {
141145
offset = document.getTextLength() - 1;
142146
}
@@ -145,7 +149,7 @@ public void run() {
145149
}
146150
});
147151
}
148-
}, "insertKotlin", null);
152+
}, "insertKotlin", "JsonToKotlin");
149153
Messages.showMessageDialog(project, "Kotlin Code insert successfully!", "Information", Messages.getInformationIcon());
150154
}
151155
}

0 commit comments

Comments
 (0)