Skip to content

Commit 66e43df

Browse files
committed
feat: add dump action
1 parent c5dc8ce commit 66e43df

File tree

3 files changed

+58
-5
lines changed

3 files changed

+58
-5
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
}

src/main/kotlin/com/github/xepozz/php_dump/toolWindow/panel/CustomTreePanel.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.xepozz.php_dump.toolWindow.panel
22

33
import com.github.xepozz.php_dump.actions.CollapseTreeAction
4+
import com.github.xepozz.php_dump.actions.EditPhpSnippetAction
45
import com.github.xepozz.php_dump.actions.ExpandTreeAction
56
import com.github.xepozz.php_dump.actions.OpenPhpSettingsAction
67
import com.github.xepozz.php_dump.actions.RefreshAction
@@ -44,8 +45,8 @@ import javax.swing.tree.DefaultTreeModel
4445
import javax.swing.tree.TreePath
4546

4647
class CustomTreePanel(
47-
private val tabConfig: CompositeWindowTabsState.TabConfig,
4848
private val project: Project,
49+
private val tabConfig: CompositeWindowTabsState.TabConfig,
4950
) :
5051
SimpleToolWindowPanel(false, false),
5152
RefreshablePanel, Disposable {
@@ -87,6 +88,9 @@ class CustomTreePanel(
8788
add(ExpandTreeAction(tree))
8889
add(CollapseTreeAction(tree))
8990
addSeparator()
91+
add(EditPhpSnippetAction(project, tabConfig) { snippet ->
92+
tabConfig.snippet = snippet
93+
})
9094
add(OpenPhpSettingsAction())
9195
}
9296

@@ -171,8 +175,8 @@ class CustomTreePanel(
171175
val virtualFile = editor.virtualFile ?: return result
172176

173177
service.phpSnippet = tabConfig.snippet
178+
174179
val runBlocking = service.dump(virtualFile)
175-
// println("result is $runBlocking")
176180

177181
return runBlocking as? TokensList ?: result
178182
}

src/main/kotlin/com/github/xepozz/php_dump/toolWindow/tabs/TabsUtil.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ object TabsUtil {
1111
// language=injectablephp
1212
private val SNIPPET = $$"""
1313
/**
14-
* $argv[0] – Program name
1514
* $argv[1] – File path
1615
*/
1716
@@ -47,11 +46,11 @@ object TabsUtil {
4746
val tabsState = CompositeWindowTabsState.getInstance(project)
4847
tabsState.state.tabs.add(tabConfig)
4948

50-
val panel = CustomTreePanel(tabConfig, project)
49+
val panel = CustomTreePanel(project, tabConfig)
5150
val content = contentFactory.createContent(panel, tabConfig.name, false)
5251

5352
contentManager.addContent(content)
54-
contentManager.addContentManagerListener(object : ContentManagerListener{
53+
contentManager.addContentManagerListener(object : ContentManagerListener {
5554
override fun contentRemoved(event: ContentManagerEvent) {
5655
if (event.content === content) {
5756
tabsState.state.tabs.remove(tabConfig)

0 commit comments

Comments
 (0)