@@ -5,24 +5,67 @@ import com.intellij.openapi.actionSystem.ActionUpdateThread
5
5
import com.intellij.openapi.actionSystem.AnAction
6
6
import com.intellij.openapi.actionSystem.AnActionEvent
7
7
import com.intellij.openapi.actionSystem.CommonDataKeys
8
+ import com.intellij.openapi.actionSystem.PlatformDataKeys
9
+ import com.intellij.openapi.fileEditor.FileDocumentManager
10
+ import com.intellij.openapi.fileChooser.FileChooserFactory
11
+ import com.intellij.openapi.fileChooser.FileSaverDescriptor
12
+ import com.intellij.openapi.fileChooser.FileSaverDialog
13
+ import com.intellij.openapi.vfs.VirtualFileWrapper
14
+ import com.intellij.openapi.application.ApplicationManager
15
+ import com.intellij.openapi.ui.Messages
16
+ import com.intellij.openapi.vfs.LocalFileSystem
8
17
import com.intellij.psi.PsiElement
9
18
import com.intellij.psi.PsiManager
10
- import com.intellij.refactoring.copy.CopyHandler
19
+ import java.io.IOException
11
20
12
21
class AutoDevSaveFileAction : AnAction (AutoDevBundle .message("autodev.save.action")) {
13
22
override fun getActionUpdateThread (): ActionUpdateThread = ActionUpdateThread .BGT
14
23
15
24
override fun update (e : AnActionEvent ) {
16
- val project = e.project
17
- val virtualFile = project?.let { e.getData(CommonDataKeys .VIRTUAL_FILE ) }
18
- e.presentation.isEnabled = virtualFile != null
25
+ val editor = e.getData(PlatformDataKeys .EDITOR ) ? : return
26
+ val document = editor.document
27
+ val file = FileDocumentManager .getInstance().getFile(document)
28
+ e.presentation.isEnabled = file != null
19
29
}
20
30
21
31
override fun actionPerformed (e : AnActionEvent ) {
22
32
val project = e.project ? : return
23
- var virtualFile = e.getData(CommonDataKeys .VIRTUAL_FILE ) ? : return
24
- val element: PsiElement ? = PsiManager .getInstance(project).findFile(virtualFile)
25
- if (element == null ) return
26
- CopyHandler .doCopy(arrayOf<PsiElement ?>(element.containingFile), null )
33
+ val editor = e.getData(PlatformDataKeys .EDITOR ) ? : return
34
+ val document = editor.document
35
+ val virtualFile = FileDocumentManager .getInstance().getFile(document) ? : return
36
+ val content = virtualFile.contentsToByteArray().toString(Charsets .UTF_8 )
37
+
38
+ val descriptor = FileSaverDescriptor (
39
+ " Save File As" ,
40
+ " Choose location to save the file" ,
41
+ * arrayOf()
42
+ )
43
+
44
+ val dialog: FileSaverDialog = FileChooserFactory .getInstance().createSaveFileDialog(descriptor, project)
45
+ val dir = project.baseDir
46
+ val virtualFileWrapper: VirtualFileWrapper ? = dialog.save(dir, virtualFile.name)
47
+
48
+ if (virtualFileWrapper != null ) {
49
+ try {
50
+ ApplicationManager .getApplication().runWriteAction {
51
+ val file = virtualFileWrapper.file
52
+ file.writeText(content)
53
+
54
+ LocalFileSystem .getInstance().refreshAndFindFileByIoFile(file)
55
+
56
+ Messages .showInfoMessage(
57
+ project,
58
+ " File saved successfully to: ${file.absolutePath} " ,
59
+ " File Saved"
60
+ )
61
+ }
62
+ } catch (ex: IOException ) {
63
+ Messages .showErrorDialog(
64
+ project,
65
+ " Failed to save file: ${ex.message} " ,
66
+ " Error"
67
+ )
68
+ }
69
+ }
27
70
}
28
71
}
0 commit comments