Skip to content

Commit 46a62d0

Browse files
committed
feat: refresh active tab instead of a fixed one
1 parent 8c98be1 commit 46a62d0

File tree

8 files changed

+61
-38
lines changed

8 files changed

+61
-38
lines changed

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

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,17 @@ import com.intellij.icons.AllIcons
55
import com.intellij.openapi.actionSystem.ActionUpdateThread
66
import com.intellij.openapi.actionSystem.AnAction
77
import com.intellij.openapi.actionSystem.AnActionEvent
8-
import com.intellij.openapi.editor.EditorFactory
98
import com.intellij.openapi.fileEditor.FileEditorManager
109

1110
class RunDumpCommandAction() : AnAction("Dump Opcodes in Terminal", null, AllIcons.Actions.Execute) {
1211
override fun actionPerformed(e: AnActionEvent) {
1312
val project = e.project ?: return
1413
println("project $project")
15-
val basePath = project.basePath ?: return
16-
println("basePath $basePath")
17-
val editorFactory = EditorFactory.getInstance()
18-
val editors = editorFactory.allEditors
19-
println("editors $editors")
20-
2114
val editor = FileEditorManager.getInstance(project).selectedTextEditor ?: return
2215
val file = editor.virtualFile ?: return
2316
println("file $file")
2417

25-
val service = project.getService(OpcodesDumperService::class.java)
26-
27-
service.dump(file.path, {
28-
println("dumping $basePath")
29-
});
30-
18+
OpcodesDumperService.dump(file, project)
3119
}
3220

3321
override fun getActionUpdateThread() = ActionUpdateThread.BGT

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

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,17 @@ import com.intellij.icons.AllIcons
55
import com.intellij.openapi.actionSystem.ActionUpdateThread
66
import com.intellij.openapi.actionSystem.AnAction
77
import com.intellij.openapi.actionSystem.AnActionEvent
8-
import com.intellij.openapi.editor.EditorFactory
98
import com.intellij.openapi.fileEditor.FileEditorManager
109

1110
class RunDumpTokensCommandAction() : AnAction("Dump Tokens in Terminal", null, AllIcons.Actions.Execute) {
1211
override fun actionPerformed(e: AnActionEvent) {
1312
val project = e.project ?: return
1413
println("project $project")
15-
val basePath = project.basePath ?: return
16-
println("basePath $basePath")
17-
val editorFactory = EditorFactory.getInstance()
18-
val editors = editorFactory.allEditors
19-
println("editors $editors")
20-
2114
val editor = FileEditorManager.getInstance(project).selectedTextEditor ?: return
2215
val file = editor.virtualFile ?: return
2316
println("file $file")
2417

25-
val service = project.getService(TokensDumperService::class.java)
26-
27-
service.dump(file.path, {
28-
println("dumping $basePath")
29-
})
30-
18+
TokensDumperService.dump(file, project)
3119
}
3220

3321
override fun getActionUpdateThread() = ActionUpdateThread.BGT

src/main/kotlin/com/github/xepozz/php_dump/panel/OpcodesTerminalPanel.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.github.xepozz.php_dump.panel
22

33
import com.github.xepozz.php_dump.actions.RunDumpCommandAction
4+
import com.github.xepozz.php_dump.services.OpcodesDumperService
45
import com.intellij.openapi.actionSystem.ActionManager
56
import com.intellij.openapi.actionSystem.DefaultActionGroup
7+
import com.intellij.openapi.fileEditor.FileEditorManager
8+
import com.intellij.openapi.project.Project
69
import com.intellij.openapi.ui.SimpleToolWindowPanel
710
import java.awt.BorderLayout
811
import java.awt.GridLayout
@@ -13,7 +16,7 @@ import javax.swing.JPanel
1316

1417
class OpcodesTerminalPanel(
1518
val terminalViewComponent: JComponent,
16-
) : SimpleToolWindowPanel(false, false) {
19+
) : SimpleToolWindowPanel(false, false), RefreshablePanel {
1720
init {
1821
createToolBar()
1922
createContent()
@@ -46,4 +49,11 @@ class OpcodesTerminalPanel(
4649

4750
setContent(responsivePanel)
4851
}
52+
53+
override fun refresh(project: Project) {
54+
val editor = FileEditorManager.getInstance(project).selectedTextEditor ?: return
55+
val virtualFile = editor.virtualFile ?: return
56+
57+
OpcodesDumperService.dump(virtualFile, project)
58+
}
4959
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.github.xepozz.php_dump.panel
2+
3+
import com.intellij.openapi.project.Project
4+
5+
interface RefreshablePanel {
6+
fun refresh(project: Project)
7+
}

src/main/kotlin/com/github/xepozz/php_dump/panel/TokensTerminalPanel.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.github.xepozz.php_dump.panel
22

33
import com.github.xepozz.php_dump.actions.RunDumpTokensCommandAction
4+
import com.github.xepozz.php_dump.services.TokensDumperService
45
import com.intellij.openapi.actionSystem.ActionManager
56
import com.intellij.openapi.actionSystem.DefaultActionGroup
7+
import com.intellij.openapi.fileEditor.FileEditorManager
8+
import com.intellij.openapi.project.Project
69
import com.intellij.openapi.ui.SimpleToolWindowPanel
710
import java.awt.BorderLayout
811
import java.awt.GridLayout
@@ -13,7 +16,7 @@ import javax.swing.JPanel
1316

1417
class TokensTerminalPanel(
1518
val terminalViewComponent: JComponent,
16-
) : SimpleToolWindowPanel(false, false) {
19+
) : SimpleToolWindowPanel(false, false), RefreshablePanel {
1720
init {
1821
createToolBar()
1922
createContent()
@@ -46,4 +49,11 @@ class TokensTerminalPanel(
4649

4750
setContent(responsivePanel)
4851
}
52+
53+
override fun refresh(project: Project) {
54+
val editor = FileEditorManager.getInstance(project).selectedTextEditor ?: return
55+
val virtualFile = editor.virtualFile ?: return
56+
57+
TokensDumperService.dump(virtualFile, project)
58+
}
4959
}

src/main/kotlin/com/github/xepozz/php_dump/services/OpcodesDumperService.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,31 @@ import com.intellij.openapi.Disposable
1111
import com.intellij.openapi.components.Service
1212
import com.intellij.openapi.project.Project
1313
import com.intellij.openapi.util.Key
14-
import com.intellij.ui.jcef.JBCefBrowser
14+
import com.intellij.openapi.vfs.VirtualFile
1515
import com.jetbrains.php.config.PhpProjectConfigurationFacade
1616
import com.jetbrains.php.config.interpreters.PhpInterpretersManagerImpl
1717
import kotlinx.coroutines.CoroutineScope
1818
import kotlinx.coroutines.Dispatchers
1919
import kotlinx.coroutines.launch
2020
import kotlinx.coroutines.withContext
21-
import kotlin.math.log
2221

2322
@Service(Service.Level.PROJECT)
2423
class OpcodesDumperService(var project: Project) : Disposable {
2524
var consoleView: ConsoleView? = null
2625

26+
companion object {
27+
fun dump(file: VirtualFile, project: Project) {
28+
val service = project.getService(OpcodesDumperService::class.java)
29+
30+
service.dump(file.path)
31+
}
32+
}
33+
2734
override fun dispose() {
2835
consoleView?.dispose()
2936
}
3037

31-
fun dump(file: String, callback: () -> Unit) {
38+
fun dump(file: String, callback: () -> Unit = {}) {
3239
val interpretersManager = PhpInterpretersManagerImpl.getInstance(project)
3340
val interpreter = PhpProjectConfigurationFacade.getInstance(project).interpreter
3441
?: interpretersManager.interpreters.firstOrNull() ?: return

src/main/kotlin/com/github/xepozz/php_dump/services/TokensDumperService.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import com.intellij.openapi.Disposable
1111
import com.intellij.openapi.components.Service
1212
import com.intellij.openapi.project.Project
1313
import com.intellij.openapi.util.Key
14+
import com.intellij.openapi.vfs.VirtualFile
1415
import com.jetbrains.php.config.PhpProjectConfigurationFacade
1516
import com.jetbrains.php.config.interpreters.PhpInterpretersManagerImpl
1617
import kotlinx.coroutines.CoroutineScope
@@ -22,11 +23,19 @@ import kotlinx.coroutines.withContext
2223
class TokensDumperService(var project: Project) : Disposable {
2324
var consoleView: ConsoleView? = null
2425

26+
companion object {
27+
fun dump(file: VirtualFile, project: Project) {
28+
val service = project.getService(TokensDumperService::class.java)
29+
30+
service.dump(file.path)
31+
}
32+
}
33+
2534
override fun dispose() {
2635
consoleView?.dispose()
2736
}
2837

29-
fun dump(file: String, callback: () -> Unit) {
38+
fun dump(file: String, callback: () -> Unit = {}) {
3039
val interpretersManager = PhpInterpretersManagerImpl.getInstance(project)
3140
val interpreter = PhpProjectConfigurationFacade.getInstance(project).interpreter
3241
?: interpretersManager.interpreters.firstOrNull() ?: return

src/main/kotlin/com/github/xepozz/php_dump/startup/ProjectFileEditorListener.kt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package com.github.xepozz.php_dump.startup
22

3-
import com.github.xepozz.php_dump.services.OpcodesDumperService
3+
import com.github.xepozz.php_dump.panel.RefreshablePanel
44
import com.intellij.openapi.fileEditor.FileEditorManagerEvent
55
import com.intellij.openapi.fileEditor.FileEditorManagerListener
66
import com.intellij.openapi.project.Project
7+
import com.intellij.openapi.wm.ToolWindowManager
78

89
class ProjectFileEditorListener(val project: Project) : FileEditorManagerListener {
910
// override fun fileClosed(source: FileEditorManager, file: VirtualFile) {
@@ -19,12 +20,15 @@ class ProjectFileEditorListener(val project: Project) : FileEditorManagerListene
1920
super.selectionChanged(event)
2021
println("selection changed $event")
2122

22-
val virtualFile = event.newFile ?: return
23+
val toolWindowManager = ToolWindowManager.getInstance(project)
24+
val toolWindow = toolWindowManager.getToolWindow("PHP Dump")
2325

24-
val service = project.getService(OpcodesDumperService::class.java)
26+
toolWindow
27+
?.component
28+
?.components
29+
?.mapNotNull { it as? RefreshablePanel }
30+
?.forEach { it.refresh(project) }
31+
?: return
2532

26-
service.dump(virtualFile.path) {
27-
println("dump in selection changed ")
28-
}
2933
}
3034
}

0 commit comments

Comments
 (0)