|
| 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 | + |
0 commit comments