Skip to content

Commit 89c5161

Browse files
committed
feat: play with temporary files instead of scratches
1 parent 87b4a68 commit 89c5161

File tree

2 files changed

+24
-29
lines changed

2 files changed

+24
-29
lines changed

src/main/kotlin/com/github/xepozz/php_dump/actions/EditPhpSnippetAction.kt

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,44 @@ package com.github.xepozz.php_dump.actions
22

33
import com.github.xepozz.php_dump.toolWindow.tabs.CompositeWindowTabsState
44
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
85
import com.intellij.lang.Language
96
import com.intellij.openapi.actionSystem.AnAction
107
import com.intellij.openapi.actionSystem.AnActionEvent
8+
import com.intellij.openapi.editor.event.DocumentEvent
9+
import com.intellij.openapi.editor.event.DocumentListener
10+
import com.intellij.openapi.fileEditor.FileEditorManager
1111
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
12+
import com.intellij.psi.PsiDocumentManager
13+
import com.intellij.psi.PsiManager
14+
import com.intellij.testFramework.LightVirtualFile
1715

1816
class EditPhpSnippetAction(
1917
val project: Project,
2018
val tabConfig: CompositeWindowTabsState.TabConfig,
2119
val onUpdate: (String) -> Unit,
2220
) :
2321
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
3022

31-
val psiFile = ScratchFileActions.doCreateNewScratch(project, context) ?: return
32-
val virtualFile = psiFile.virtualFile
23+
val documentManager = PsiDocumentManager.getInstance(project)
3324

34-
val virtualFileManager = VirtualFileManager.getInstance()
25+
override fun actionPerformed(e: AnActionEvent) {
26+
val language = Language.findLanguageByID("InjectablePHP") ?: return
3527

36-
virtualFileManager.addAsyncFileListener(object :AsyncFileListener{
37-
override fun prepareChange(events: List<VFileEvent>): AsyncFileListener.ChangeApplier? {
38-
events.find { it.file == virtualFile } ?: return null
28+
val virtualFile = LightVirtualFile(
29+
tabConfig.name,
30+
language,
31+
tabConfig.snippet,
32+
)
33+
val psiFile = PsiManager.getInstance(project).findFile(virtualFile)!!
3934

40-
return object : AsyncFileListener.ChangeApplier {
41-
override fun afterVfsChange() {
42-
val newText= virtualFile.readText()
43-
println("newText: $newText")
44-
onUpdate(newText)
45-
}
46-
}
35+
val document = documentManager.getDocument(psiFile)!!
36+
document.addDocumentListener(object : DocumentListener {
37+
override fun documentChanged(event: DocumentEvent) {
38+
val newText = event.document.text
39+
onUpdate(newText)
4740
}
48-
}) {}
41+
})
42+
43+
FileEditorManager.getInstance(project).openFile(virtualFile, true)
4944
}
5045
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ object TabsUtil {
3737
name = tabName,
3838
snippet = SNIPPET,
3939
)
40+
tabsState.state.tabs.add(tabConfig)
4041

4142
createTab(project, contentManager, tabConfig)
4243
}
4344

4445
fun createTab(project: Project, contentManager: ContentManager, tabConfig: CompositeWindowTabsState.TabConfig) {
4546
val contentFactory = ContentFactory.getInstance()
4647
val tabsState = CompositeWindowTabsState.getInstance(project)
47-
tabsState.state.tabs.add(tabConfig)
4848

4949
val panel = CustomTreePanel(project, tabConfig)
5050
val content = contentFactory.createContent(panel, tabConfig.name, false)

0 commit comments

Comments
 (0)