1+ package com.dengzii.plugin.auc
2+
3+ import com.intellij.openapi.actionSystem.AnActionEvent
4+ import com.intellij.openapi.actionSystem.PlatformDataKeys
5+ import com.intellij.openapi.module.Module
6+ import com.intellij.openapi.module.ModuleManager
7+ import com.intellij.openapi.project.Project
8+ import com.intellij.openapi.vfs.VirtualFile
9+ import com.intellij.psi.PsiFile
10+
11+ /* *
12+ * <pre>
13+ * author : dengzi
14+ * e-mail : denua@foxmail.com
15+ * github : https://github.com/MrDenua
16+ * time : 2019/12/31
17+ * desc :
18+ * </pre>
19+ */
20+ class PluginKit private constructor(e : AnActionEvent ) {
21+
22+ private val event = e
23+ lateinit var project: Project
24+
25+ init {
26+ if (e.project != null ) {
27+ project = e.project!!
28+ }
29+ }
30+
31+ fun isProjectValid (): Boolean {
32+ return project.isOpen && project.isInitialized
33+ }
34+
35+ fun getModules (): Array <Module > {
36+ return ModuleManager .getInstance(project).modules
37+ }
38+
39+ fun getPsiFile (): PsiFile ? {
40+ return event.getData(PlatformDataKeys .PSI_FILE )
41+ }
42+
43+ fun getVirtualFile (): VirtualFile ? {
44+ return event.getData(PlatformDataKeys .VIRTUAL_FILE )
45+ }
46+
47+ fun createDir (name : String , vf : VirtualFile ? = getVirtualFile()) {
48+ if (checkCreateFile(name, vf)) {
49+ return
50+ }
51+ vf!! .createChildDirectory(null , name)
52+ }
53+
54+ fun createFile (name : String , vf : VirtualFile ? = getVirtualFile()) {
55+ if (checkCreateFile(name, vf)) {
56+ return
57+ }
58+ vf!! .createChildData(null , name)
59+ }
60+
61+ private fun checkCreateFile (name : String , vf : VirtualFile ? ): Boolean {
62+ if (vf == null || ! vf.isDirectory) {
63+ Logger .e(TAG , " target is null or is not a directory." )
64+ return false
65+ }
66+ if (vf.findChild(name)?.exists() == true ) {
67+ Logger .e(TAG , " directory already exists." )
68+ return false
69+ }
70+ return true
71+ }
72+
73+ companion object {
74+
75+ private val TAG = PluginKit ::class .java.simpleName;
76+
77+ fun get (e : AnActionEvent ): PluginKit {
78+ return PluginKit (e)
79+ }
80+ }
81+ }
0 commit comments