Skip to content

Commit 86e8617

Browse files
committed
feat: allow disable auto-dump
1 parent bf69150 commit 86e8617

File tree

8 files changed

+56
-32
lines changed

8 files changed

+56
-32
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ import com.intellij.openapi.fileEditor.FileEditorManager
99
import kotlinx.coroutines.runBlocking
1010

1111
class RunDumpTokensCommandAction(
12-
val dumpService: DumperServiceInterface
13-
) : AnAction("Dump Tokens in Terminal", null, AllIcons.Actions.Execute) {
12+
val dumpService: DumperServiceInterface,
13+
title: String = "Dump Tokens",
14+
) : AnAction(title, null, AllIcons.Actions.Execute) {
1415
override fun actionPerformed(e: AnActionEvent) {
1516
val project = e.project ?: return
16-
println("project $project")
17+
// println("project $project")
1718
val editor = FileEditorManager.getInstance(project).selectedTextEditor ?: return
1819
val file = editor.virtualFile ?: return
19-
println("file $file")
20+
// println("file $file")
2021

2122
runBlocking { dumpService.dump(file) }
2223
}

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

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ class OpcodesTerminalPanel(
2929
val viewComponent: JComponent
3030
val service: OpcodesDumperService
3131
val state = DebugLevelState.getInstance(project)
32+
val consoleView = TextConsoleBuilderFactory.getInstance().createBuilder(project).console
3233

3334
init {
34-
val consoleView = TextConsoleBuilderFactory.getInstance().createBuilder(project).console
3535
viewComponent = consoleView.component
3636

3737
service = project.getService(OpcodesDumperService::class.java)
@@ -43,13 +43,36 @@ class OpcodesTerminalPanel(
4343

4444
private fun createToolBar() {
4545
val actionGroup = DefaultActionGroup().apply {
46-
add(RunDumpTokensCommandAction(service))
46+
add(RunDumpTokensCommandAction(service, "Dump Opcodes"))
47+
add(object : AnAction("Clear", "Clear console", AllIcons.Actions.GC) {
48+
override fun actionPerformed(e: AnActionEvent) {
49+
consoleView.clear()
50+
}
51+
})
52+
add(object : AnAction(
53+
"Enable Auto Refresh", "Turns on or off auto refresh of panel context",
54+
if (state.autoRefresh) AllIcons.Actions.RestartStop else AllIcons.Actions.RerunAutomatically
55+
) {
56+
override fun actionPerformed(e: AnActionEvent) {
57+
state.autoRefresh = !state.autoRefresh
58+
}
59+
60+
override fun update(e: AnActionEvent) {
61+
if (state.autoRefresh) {
62+
e.presentation.text = "Disable Auto Refresh"
63+
e.presentation.icon = AllIcons.Actions.RestartStop
64+
} else {
65+
e.presentation.text = "Enable Auto Refresh"
66+
e.presentation.icon = AllIcons.Actions.RerunAutomatically
67+
}
68+
}
69+
})
4770
addSeparator()
4871
add(DefaultActionGroup("Debug Level", true).apply {
4972
add(object : AnAction("Before Optimization") {
5073
override fun actionPerformed(e: AnActionEvent) {
5174
state.debugLevel = 1
52-
refresh(project)
75+
refresh(project, RefreshType.MANUAL)
5376
}
5477

5578
override fun update(e: AnActionEvent) {
@@ -62,7 +85,7 @@ class OpcodesTerminalPanel(
6285
add(object : AnAction("After Optimization") {
6386
override fun actionPerformed(e: AnActionEvent) {
6487
state.debugLevel = 2
65-
refresh(project)
88+
refresh(project, RefreshType.MANUAL)
6689
}
6790

6891
override fun update(e: AnActionEvent) {
@@ -88,7 +111,7 @@ class OpcodesTerminalPanel(
88111
.let { file ->
89112
state.preloadFile = file?.path
90113
}
91-
refresh(project)
114+
refresh(project, RefreshType.MANUAL)
92115
}
93116
})
94117
}
@@ -115,7 +138,10 @@ class OpcodesTerminalPanel(
115138
setContent(responsivePanel)
116139
}
117140

118-
override fun refresh(project: Project) {
141+
override fun refresh(project: Project, type: RefreshType) {
142+
if (type == RefreshType.AUTO && !state.autoRefresh) {
143+
return
144+
}
119145
val editor = FileEditorManager.getInstance(project).selectedTextEditor ?: return
120146
val virtualFile = editor.virtualFile ?: return
121147

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@ package com.github.xepozz.php_dump.panel
33
import com.intellij.openapi.project.Project
44

55
interface RefreshablePanel {
6-
fun refresh(project: Project)
6+
fun refresh(project: Project, type: RefreshType)
7+
}
8+
enum class RefreshType {
9+
AUTO, MANUAL
710
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class TokenTreePanel(private val project: Project) :
7272

7373
fun createToolbar() {
7474
val actionGroup = DefaultActionGroup().apply {
75-
add(RunDumpTokensCommandAction(service))
75+
add(RunDumpTokensCommandAction(service, "Dump Tree"))
7676
addSeparator()
7777
}
7878

@@ -176,7 +176,7 @@ class TokenTreePanel(private val project: Project) :
176176
return runBlocking as? TokensList ?: result
177177
}
178178

179-
override fun refresh(project: Project) {
179+
override fun refresh(project: Project, type: RefreshType) {
180180
refreshData()
181181
}
182182

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class TokensObjectTerminalPanel(
6161
setContent(responsivePanel)
6262
}
6363

64-
override fun refresh(project: Project) {
64+
override fun refresh(project: Project, type: RefreshType) {
6565
val editor = FileEditorManager.getInstance(project).selectedTextEditor ?: return
6666
val virtualFile = editor.virtualFile ?: return
6767

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class TokensTerminalPanel(
6161
setContent(responsivePanel)
6262
}
6363

64-
override fun refresh(project: Project) {
64+
override fun refresh(project: Project, type: RefreshType) {
6565
val editor = FileEditorManager.getInstance(project).selectedTextEditor ?: return
6666
val virtualFile = editor.virtualFile ?: return
6767

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

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,27 @@
11
package com.github.xepozz.php_dump.services
22

3-
import com.intellij.openapi.components.PersistentStateComponent
3+
import com.intellij.openapi.components.BaseState
44
import com.intellij.openapi.components.Service
5+
import com.intellij.openapi.components.SimplePersistentStateComponent
56
import com.intellij.openapi.components.State
67
import com.intellij.openapi.components.Storage
78
import com.intellij.openapi.project.Project
89

910
@Service(Service.Level.PROJECT)
1011
@State(
1112
name = "com.github.xepozz.php_dump.services.DebugLevelState",
12-
storages = [Storage("phpDumpDebugLevel.xml")]
13+
storages = [Storage("PhpDump.xml")]
1314
)
14-
class DebugLevelState : PersistentStateComponent<DebugLevelState.State> {
15-
data class State(
16-
var debugLevel: Int = 1,
17-
var preloadFile: String? = null,
18-
)
19-
20-
private var myState = State()
21-
22-
override fun getState(): State {
23-
return myState
24-
}
25-
26-
override fun loadState(state: State) {
27-
myState = state
15+
class DebugLevelState : SimplePersistentStateComponent<DebugLevelState.State>(State()) {
16+
class State : BaseState() {
17+
var debugLevel: Int by property(1)
18+
var preloadFile: String? by string(null)
19+
var autoRefresh: Boolean by property(true)
2820
}
2921

3022
var debugLevel: Int by state::debugLevel
3123
var preloadFile: String? by state::preloadFile
24+
var autoRefresh: Boolean by state::autoRefresh
3225

3326
companion object {
3427
fun getInstance(project: Project): DebugLevelState {

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

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

3+
import com.github.xepozz.php_dump.panel.RefreshType
34
import com.github.xepozz.php_dump.panel.RefreshablePanel
45
import com.intellij.openapi.fileEditor.FileEditorManagerEvent
56
import com.intellij.openapi.fileEditor.FileEditorManagerListener
@@ -27,7 +28,7 @@ class ProjectFileEditorListener(val project: Project) : FileEditorManagerListene
2728
?.component
2829
?.components
2930
?.mapNotNull { it as? RefreshablePanel }
30-
?.forEach { it.refresh(project) }
31+
?.forEach { it.refresh(project, RefreshType.AUTO) }
3132
?: return
3233

3334
}

0 commit comments

Comments
 (0)