Skip to content

Commit 86895e0

Browse files
committed
feat: add optimization level selector
1 parent e16400a commit 86895e0

File tree

6 files changed

+90
-88
lines changed

6 files changed

+90
-88
lines changed

playground/simple.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
<?php
22

3-
echo 1;
3+
$var = 1;
4+
echo $var;

src/main/kotlin/com/github/xepozz/php_dump/CompositeWindowFactory.kt

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package com.github.xepozz.php_dump
33
import com.github.xepozz.php_dump.panel.OpcodesTerminalPanel
44
import com.github.xepozz.php_dump.panel.TokenTreePanel
55
import com.github.xepozz.php_dump.panel.TokensObjectTerminalPanel
6-
import com.github.xepozz.php_dump.panel.TokensObjectTreeTerminalPanel
76
import com.github.xepozz.php_dump.panel.TokensTerminalPanel
87
import com.intellij.openapi.project.DumbAware
98
import com.intellij.openapi.project.Project
@@ -19,9 +18,7 @@ open class CompositeWindowFactory : ToolWindowFactory, DumbAware {
1918
val opcodesTerminalLayout = OpcodesTerminalPanel(project)
2019
val tokensTerminalLayout = TokensTerminalPanel(project)
2120
val tokensObjectTerminalLayout = TokensObjectTerminalPanel(project)
22-
val tokensObjectTreeTerminalLayout = TokensObjectTreeTerminalPanel(project)
23-
24-
val treeWindow = TokenTreePanel(project)
21+
val tokenTreeLayout = TokenTreePanel(project)
2522

2623
contentFactory.apply {
2724
this.createContent(opcodesTerminalLayout, "Opcodes", false).apply {
@@ -48,15 +45,7 @@ open class CompositeWindowFactory : ToolWindowFactory, DumbAware {
4845
}
4946
)
5047
}
51-
this.createContent(tokensObjectTreeTerminalLayout, "Tokens Object Tree", false).apply {
52-
contentManager.addContent(
53-
this.apply {
54-
this.isPinnable = true
55-
this.isCloseable = false
56-
}
57-
)
58-
}
59-
this.createContent(treeWindow.component, "Tree panel", false).apply {
48+
this.createContent(tokenTreeLayout.component, "Tree panel", false).apply {
6049
contentManager.addContent(
6150
this.apply {
6251
this.isPinnable = true

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

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
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.DebugLevelState
45
import com.github.xepozz.php_dump.services.OpcodesDumperService
56
import com.intellij.execution.filters.TextConsoleBuilderFactory
7+
import com.intellij.icons.AllIcons
68
import com.intellij.openapi.actionSystem.ActionManager
9+
import com.intellij.openapi.actionSystem.AnAction
10+
import com.intellij.openapi.actionSystem.AnActionEvent
711
import com.intellij.openapi.actionSystem.DefaultActionGroup
812
import com.intellij.openapi.fileEditor.FileEditorManager
913
import com.intellij.openapi.project.Project
1014
import com.intellij.openapi.ui.SimpleToolWindowPanel
15+
import com.intellij.ui.JBColor
16+
import com.intellij.util.IconUtil
1117
import kotlinx.coroutines.runBlocking
1218
import java.awt.BorderLayout
1319
import java.awt.GridLayout
@@ -21,6 +27,7 @@ class OpcodesTerminalPanel(
2127
) : SimpleToolWindowPanel(false, false), RefreshablePanel {
2228
val viewComponent: JComponent
2329
val service: OpcodesDumperService
30+
val state = DebugLevelState.getInstance(project)
2431

2532
init {
2633
val consoleView = TextConsoleBuilderFactory.getInstance().createBuilder(project).console
@@ -37,7 +44,34 @@ class OpcodesTerminalPanel(
3744
val actionGroup = DefaultActionGroup()
3845
actionGroup.add(RunDumpTokensCommandAction(service))
3946
actionGroup.addSeparator()
40-
// actionGroup.add(OpenSettingsAction())
47+
actionGroup.add(DefaultActionGroup("Debug Level", true).apply {
48+
add(object : AnAction("Before Optimization") {
49+
override fun actionPerformed(e: AnActionEvent) {
50+
state.setDebugLevel(1)
51+
refresh(project)
52+
}
53+
override fun update(e: AnActionEvent) {
54+
e.presentation.icon = when (state.getDebugLevel()) {
55+
1 -> IconUtil.colorize(AllIcons.General.GreenCheckmark, JBColor.GRAY)
56+
else -> null
57+
}
58+
}
59+
})
60+
add(object : AnAction("After Optimization") {
61+
override fun actionPerformed(e: AnActionEvent) {
62+
state.setDebugLevel(2)
63+
refresh(project)
64+
}
65+
66+
override fun update(e: AnActionEvent) {
67+
e.presentation.icon = when (state.getDebugLevel()) {
68+
2 -> IconUtil.colorize(AllIcons.General.GreenCheckmark, JBColor.GRAY)
69+
else -> null
70+
}
71+
}
72+
})
73+
templatePresentation.icon = AllIcons.Actions.ToggleVisibility
74+
})
4175

4276
val actionToolbar = ActionManager.getInstance().createActionToolbar("Opcodes Toolbar", actionGroup, false)
4377
actionToolbar.targetComponent = this
@@ -67,4 +101,4 @@ class OpcodesTerminalPanel(
67101

68102
runBlocking { service.dump(virtualFile) }
69103
}
70-
}
104+
}

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

Lines changed: 0 additions & 70 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.github.xepozz.php_dump.services
2+
3+
import com.intellij.openapi.components.PersistentStateComponent
4+
import com.intellij.openapi.components.Service
5+
import com.intellij.openapi.components.State
6+
import com.intellij.openapi.components.Storage
7+
import com.intellij.openapi.project.Project
8+
9+
@Service(Service.Level.PROJECT)
10+
@State(
11+
name = "com.github.xepozz.php_dump.services.DebugLevelState",
12+
storages = [Storage("phpDumpDebugLevel.xml")]
13+
)
14+
class DebugLevelState : PersistentStateComponent<DebugLevelState.State> {
15+
data class State(
16+
var debugLevel: Int = 1
17+
)
18+
19+
private var myState = State()
20+
21+
override fun getState(): State {
22+
return myState
23+
}
24+
25+
override fun loadState(state: State) {
26+
myState = state
27+
}
28+
29+
fun getDebugLevel(): Int {
30+
return myState.debugLevel
31+
}
32+
33+
fun setDebugLevel(level: Int) {
34+
myState.debugLevel = level
35+
}
36+
37+
companion object {
38+
fun getInstance(project: Project): DebugLevelState {
39+
return project.getService(DebugLevelState::class.java)
40+
}
41+
}
42+
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,19 @@ import com.intellij.openapi.project.Project
1313
import com.intellij.openapi.util.Key
1414
import com.jetbrains.php.config.PhpProjectConfigurationFacade
1515
import com.jetbrains.php.config.interpreters.PhpInterpretersManagerImpl
16+
import fleet.util.max
1617
import kotlinx.coroutines.CoroutineScope
1718
import kotlinx.coroutines.Dispatchers
1819
import kotlinx.coroutines.launch
1920
import kotlinx.coroutines.withContext
21+
import kotlin.math.min
2022

2123
@Service(Service.Level.PROJECT)
2224
class OpcodesDumperService(var project: Project) : Disposable, DumperServiceInterface {
2325
var consoleView: ConsoleView? = null
2426

27+
val state = DebugLevelState.getInstance(project)
28+
2529
override fun dispose() {
2630
consoleView?.dispose()
2731
}
@@ -42,6 +46,8 @@ class OpcodesDumperService(var project: Project) : Disposable, DumperServiceInte
4246
// 1>/dev/null
4347

4448
val interpreterPath = interpreter.pathToPhpExecutable ?: return
49+
val debugLevel = max(1, min(2, state.getDebugLevel()))
50+
4551
val commandArgs = buildList {
4652
add(interpreterPath)
4753
add("-l")
@@ -50,7 +56,7 @@ class OpcodesDumperService(var project: Project) : Disposable, DumperServiceInte
5056

5157
add("-dopcache.enable_cli=1")
5258
add("-dopcache.save_comments=1")
53-
add("-dopcache.opt_debug_level=0x10000")
59+
add("-dopcache.opt_debug_level=0x${debugLevel}0000")
5460
add("-dopcache.optimization_level=0")
5561

5662
add(file)
@@ -82,4 +88,4 @@ class OpcodesDumperService(var project: Project) : Disposable, DumperServiceInte
8288

8389
processHandler.startNotify()
8490
}
85-
}
91+
}

0 commit comments

Comments
 (0)