1+ package com.github.xepozz.php_dump.actions
2+
3+ import com.github.xepozz.php_dump.toolWindow.tabs.CompositeWindowTabsState
4+ import com.intellij.icons.AllIcons
5+ import com.intellij.ide.scratch.ScratchFileActions
6+ import com.intellij.ide.scratch.ScratchFileCreationHelper
7+ import com.intellij.ide.scratch.ScratchFileService
8+ import com.intellij.lang.Language
9+ import com.intellij.openapi.actionSystem.AnAction
10+ import com.intellij.openapi.actionSystem.AnActionEvent
11+ import com.intellij.openapi.project.Project
12+ import com.intellij.openapi.util.IntellijInternalApi
13+ import com.intellij.openapi.vfs.AsyncFileListener
14+ import com.intellij.openapi.vfs.VirtualFileManager
15+ import com.intellij.openapi.vfs.newvfs.events.VFileEvent
16+ import com.intellij.openapi.vfs.readText
17+
18+ class EditPhpSnippetAction (
19+ val project : Project ,
20+ val tabConfig : CompositeWindowTabsState .TabConfig ,
21+ val onUpdate : (String ) -> Unit ,
22+ ) :
23+ AnAction (" Edit PHP Snippet" , " Open PHP snippet in a temporary file" , AllIcons .General .ExternalTools ) {
24+ @OptIn(IntellijInternalApi ::class )
25+ override fun actionPerformed (e : AnActionEvent ) {
26+ val context = ScratchFileCreationHelper .Context ()
27+ context.text = tabConfig.snippet
28+ context.language = Language .findLanguageByID(" InjectablePHP" )
29+ context.createOption = ScratchFileService .Option .create_if_missing
30+
31+ val psiFile = ScratchFileActions .doCreateNewScratch(project, context) ? : return
32+ val virtualFile = psiFile.virtualFile
33+
34+ val virtualFileManager = VirtualFileManager .getInstance()
35+
36+ virtualFileManager.addAsyncFileListener(object : AsyncFileListener {
37+ override fun prepareChange (events : List <VFileEvent >): AsyncFileListener .ChangeApplier ? {
38+ events.find { it.file == virtualFile } ? : return null
39+
40+ return object : AsyncFileListener .ChangeApplier {
41+ override fun afterVfsChange () {
42+ val newText= virtualFile.readText()
43+ println (" newText: $newText " )
44+ onUpdate(newText)
45+ }
46+ }
47+ }
48+ }) {}
49+ }
50+ }
0 commit comments