@@ -32,20 +32,20 @@ class AutoDevRunAction : AnAction(AutoDevBundle.message("autodev.run.action")) {
32
32
val document = editor.document
33
33
val file = FileDocumentManager .getInstance().getFile(document)
34
34
35
- if (file != null ) {
36
- val lightFile = file as ? LightVirtualFile
37
- if (lightFile?.language == JsonLanguage .INSTANCE ) {
38
- val virtualFile = AutoDevContainer .updateForDevContainer(project, file, document.text)
39
- ? : lightFile
40
- e.presentation.isEnabled = RunService .provider(project, virtualFile) != null
41
- return
42
- }
35
+ if (file == null ) {
36
+ e.presentation.isEnabled = false
37
+ return
38
+ }
43
39
44
- e.presentation.isEnabled = RunService .provider(project, file) != null
40
+ val lightFile = file as ? LightVirtualFile
41
+ if (lightFile?.language == JsonLanguage .INSTANCE ) {
42
+ val virtualFile = AutoDevContainer .updateForDevContainer(project, file, document.text)
43
+ ? : lightFile
44
+ e.presentation.isEnabled = RunService .provider(project, virtualFile) != null
45
45
return
46
46
}
47
47
48
- e.presentation.isEnabled = false
48
+ e.presentation.isEnabled = RunService .provider(project, file) != null
49
49
}
50
50
51
51
override fun actionPerformed (e : AnActionEvent ) {
@@ -60,23 +60,28 @@ class AutoDevRunAction : AnAction(AutoDevBundle.message("autodev.run.action")) {
60
60
var scratchFile: VirtualFile ? = ScratchRootType .getInstance()
61
61
.createScratchFile(project, file.name, originPsiFile.language, document.text)
62
62
63
- if (scratchFile?.extension == " Dockerfile" ) {
64
- scratchFile = createDockerFile(project, document.text) ? : scratchFile
65
- } else if (scratchFile?.extension?.lowercase() == " json" ) {
66
- scratchFile = AutoDevContainer .updateForDevContainer(project, file as LightVirtualFile , document.text) ? : scratchFile
63
+ val extension = scratchFile?.extension
64
+ when {
65
+ extension == " Dockerfile" -> {
66
+ scratchFile = createDockerFile(project, document.text) ? : scratchFile
67
+ }
68
+
69
+ extension?.lowercase() == " json" -> {
70
+ scratchFile = AutoDevContainer .updateForDevContainer(project, file as LightVirtualFile , document.text)
71
+ ? : scratchFile
72
+ }
73
+
74
+ scratchFile?.extension == " sh" -> {
75
+ File (scratchFile.path).setExecutable(true )
76
+ }
67
77
}
68
78
69
79
if (scratchFile == null ) {
70
80
AutoDevNotifications .warn(project, " Cannot create scratch file" )
71
81
return
72
82
}
73
83
74
- if (scratchFile.extension == " sh" ) {
75
- File (scratchFile.path).setExecutable(true )
76
- }
77
-
78
- var psiFile = PsiManager .getInstance(project).findFile(scratchFile)
79
- ? : return
84
+ var psiFile = PsiManager .getInstance(project).findFile(scratchFile) ? : return
80
85
81
86
try {
82
87
RunService .provider(project, scratchFile)
@@ -88,25 +93,18 @@ class AutoDevRunAction : AnAction(AutoDevBundle.message("autodev.run.action")) {
88
93
}
89
94
}
90
95
91
- private fun createDockerFile (project : Project , text : @NlsSafe String ): VirtualFile ? {
92
- val projectDir = project.guessProjectDir()
93
- if (projectDir == null ) {
94
- return null
95
- }
96
-
97
- return runWriteAction {
98
- try {
99
- // 在项目根目录创建名为 dockerfile 的文件
100
- var dockerfile = projectDir.findChild(" Dockerfile" )
101
- if (dockerfile == null ) {
102
- dockerfile = projectDir.createChildData(null , " Dockerfile" )
103
- }
104
-
105
- dockerfile.setBinaryContent(text.toByteArray())
106
- return @runWriteAction dockerfile
107
- } catch (e: IOException ) {
108
- return @runWriteAction null
96
+ private fun createDockerFile (project : Project , text : String ): VirtualFile ? = runWriteAction {
97
+ val projectDir = project.guessProjectDir()!!
98
+ try {
99
+ var dockerfile = projectDir.findChild(" Dockerfile" )
100
+ if (dockerfile == null ) {
101
+ dockerfile = projectDir.createChildData(null , " Dockerfile" )
109
102
}
103
+
104
+ dockerfile.setBinaryContent(text.toByteArray())
105
+ return @runWriteAction dockerfile
106
+ } catch (e: IOException ) {
107
+ return @runWriteAction null
110
108
}
111
109
}
112
110
}
0 commit comments