Skip to content

Commit dbfd64c

Browse files
committed
chore: pass service to an action
1 parent d1a10b0 commit dbfd64c

File tree

8 files changed

+198
-69
lines changed

8 files changed

+198
-69
lines changed

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

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

33
import com.github.xepozz.php_dump.panel.OpcodesTerminalPanel
4+
import com.github.xepozz.php_dump.panel.TokensObjectTerminalPanel
45
import com.github.xepozz.php_dump.panel.TokensTerminalPanel
56
import com.intellij.openapi.project.DumbAware
67
import com.intellij.openapi.project.Project
@@ -19,6 +20,7 @@ open class CompositeWindowFactory : ToolWindowFactory, DumbAware {
1920

2021
val opcodesTerminalLayout = OpcodesTerminalPanel(project)
2122
val tokensTerminalLayout = TokensTerminalPanel(project)
23+
val tokensObjectTerminalLayout = TokensObjectTerminalPanel(project)
2224

2325
contentFactory.apply {
2426
this.createContent(opcodesTerminalLayout, "Opcodes", false).apply {
@@ -37,6 +39,14 @@ open class CompositeWindowFactory : ToolWindowFactory, DumbAware {
3739
}
3840
)
3941
}
42+
this.createContent(tokensObjectTerminalLayout, "Tokens Object", false).apply {
43+
contentManager.addContent(
44+
this.apply {
45+
this.isPinnable = true
46+
this.isCloseable = false
47+
}
48+
)
49+
}
4050
}
4151
}
4252
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
package com.github.xepozz.php_dump.actions
22

3-
import com.github.xepozz.php_dump.services.TokensDumperService
3+
import com.github.xepozz.php_dump.services.DumperServiceInterface
44
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
88
import com.intellij.openapi.fileEditor.FileEditorManager
99

10-
class RunDumpTokensCommandAction() : AnAction("Dump Tokens in Terminal", null, AllIcons.Actions.Execute) {
10+
class RunDumpTokensCommandAction(
11+
val dumpService: DumperServiceInterface
12+
) : AnAction("Dump Tokens in Terminal", null, AllIcons.Actions.Execute) {
1113
override fun actionPerformed(e: AnActionEvent) {
1214
val project = e.project ?: return
1315
println("project $project")
1416
val editor = FileEditorManager.getInstance(project).selectedTextEditor ?: return
1517
val file = editor.virtualFile ?: return
1618
println("file $file")
1719

18-
TokensDumperService.dump(file, project)
20+
dumpService.dump(file)
1921
}
2022

2123
override fun getActionUpdateThread() = ActionUpdateThread.BGT
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.github.xepozz.php_dump.panel
2+
3+
import com.github.xepozz.php_dump.actions.RunDumpTokensCommandAction
4+
import com.github.xepozz.php_dump.services.TokensObjectDumperService
5+
import com.intellij.execution.filters.TextConsoleBuilderFactory
6+
import com.intellij.openapi.actionSystem.ActionManager
7+
import com.intellij.openapi.actionSystem.DefaultActionGroup
8+
import com.intellij.openapi.fileEditor.FileEditorManager
9+
import com.intellij.openapi.project.Project
10+
import com.intellij.openapi.ui.SimpleToolWindowPanel
11+
import java.awt.BorderLayout
12+
import java.awt.GridLayout
13+
import java.awt.event.ComponentAdapter
14+
import java.awt.event.ComponentEvent
15+
import javax.swing.JComponent
16+
import javax.swing.JPanel
17+
18+
class TokensObjectTerminalPanel(
19+
val project: Project,
20+
) : SimpleToolWindowPanel(false, false), RefreshablePanel {
21+
var viewComponent: JComponent
22+
var service: TokensObjectDumperService
23+
24+
init {
25+
val consoleView = TextConsoleBuilderFactory.getInstance().createBuilder(project).console
26+
viewComponent = consoleView.component
27+
28+
service = project.getService(TokensObjectDumperService::class.java)
29+
service.consoleView = consoleView
30+
31+
createToolBar()
32+
createContent()
33+
}
34+
35+
private fun createToolBar() {
36+
val actionGroup = DefaultActionGroup()
37+
actionGroup.add(RunDumpTokensCommandAction(service))
38+
actionGroup.addSeparator()
39+
// actionGroup.add(OpenSettingsAction())
40+
41+
val actionToolbar = ActionManager.getInstance().createActionToolbar("Opcodes Toolbar", actionGroup, false)
42+
actionToolbar.targetComponent = this
43+
44+
val toolBarPanel = JPanel(GridLayout())
45+
toolBarPanel.add(actionToolbar.component)
46+
47+
toolbar = toolBarPanel
48+
}
49+
50+
private fun createContent() {
51+
val responsivePanel = JPanel(BorderLayout())
52+
responsivePanel.add(viewComponent, BorderLayout.CENTER)
53+
responsivePanel.addComponentListener(object : ComponentAdapter() {
54+
override fun componentResized(e: ComponentEvent?) {
55+
viewComponent.revalidate()
56+
viewComponent.repaint()
57+
}
58+
})
59+
60+
setContent(responsivePanel)
61+
}
62+
63+
override fun refresh(project: Project) {
64+
val editor = FileEditorManager.getInstance(project).selectedTextEditor ?: return
65+
val virtualFile = editor.virtualFile ?: return
66+
67+
service.dump(virtualFile)
68+
}
69+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class TokensTerminalPanel(
3434

3535
private fun createToolBar() {
3636
val actionGroup = DefaultActionGroup()
37-
actionGroup.add(RunDumpTokensCommandAction())
37+
actionGroup.add(RunDumpTokensCommandAction(service))
3838
actionGroup.addSeparator()
3939
// actionGroup.add(OpenSettingsAction())
4040

@@ -64,6 +64,6 @@ class TokensTerminalPanel(
6464
val editor = FileEditorManager.getInstance(project).selectedTextEditor ?: return
6565
val virtualFile = editor.virtualFile ?: return
6666

67-
service.dump(virtualFile.path)
67+
service.dump(virtualFile)
6868
}
6969
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.github.xepozz.php_dump.services
2+
3+
import com.intellij.openapi.vfs.VirtualFile
4+
5+
interface DumperServiceInterface {
6+
fun dump(file: VirtualFile) {
7+
dump(file.path)
8+
}
9+
fun dump(file: String)
10+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.github.xepozz.php_dump.services
2+
3+
import com.intellij.execution.configurations.GeneralCommandLine
4+
import com.intellij.execution.process.KillableColoredProcessHandler
5+
import com.intellij.execution.process.ProcessAdapter
6+
import com.intellij.openapi.project.Project
7+
import com.jetbrains.php.config.PhpProjectConfigurationFacade
8+
import com.jetbrains.php.config.interpreters.PhpInterpretersManagerImpl
9+
import kotlinx.coroutines.CoroutineScope
10+
import kotlinx.coroutines.Dispatchers
11+
import kotlinx.coroutines.launch
12+
import kotlinx.coroutines.withContext
13+
14+
object PhpCommandExecutor {
15+
fun execute(file: String, phpSnippet: String, project: Project, processListener: ProcessAdapter) {
16+
val interpretersManager = PhpInterpretersManagerImpl.getInstance(project)
17+
val interpreter = PhpProjectConfigurationFacade.getInstance(project).interpreter
18+
?: interpretersManager.interpreters.firstOrNull() ?: return
19+
20+
val commandArgs = buildList {
21+
interpreter.apply {
22+
println("interpreter: $this")
23+
add(this.pathToPhpExecutable!!)
24+
}
25+
add("-r")
26+
add(phpSnippet)
27+
28+
add(file)
29+
}
30+
31+
CoroutineScope(Dispatchers.IO).launch {
32+
executeCommand(commandArgs, processListener)
33+
}
34+
}
35+
36+
private suspend fun executeCommand(commandArgs: List<String>, processListener: ProcessAdapter) =
37+
withContext(Dispatchers.IO) {
38+
val command = GeneralCommandLine(commandArgs)
39+
command.withRedirectErrorStream(false)
40+
41+
println("running command ${command.commandLineString}")
42+
val processHandler = KillableColoredProcessHandler.Silent(command)
43+
processHandler.setShouldKillProcessSoftly(false)
44+
processHandler.setShouldDestroyProcessRecursively(true)
45+
processHandler.addProcessListener(processListener)
46+
47+
processHandler.startNotify()
48+
}
49+
}
Lines changed: 4 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.github.xepozz.php_dump.services
22

3-
import com.intellij.execution.configurations.GeneralCommandLine
4-
import com.intellij.execution.process.KillableColoredProcessHandler
53
import com.intellij.execution.process.ProcessAdapter
64
import com.intellij.execution.process.ProcessEvent
75
import com.intellij.execution.process.ProcessOutputTypes
@@ -11,45 +9,16 @@ import com.intellij.openapi.Disposable
119
import com.intellij.openapi.components.Service
1210
import com.intellij.openapi.project.Project
1311
import com.intellij.openapi.util.Key
14-
import com.intellij.openapi.vfs.VirtualFile
15-
import com.jetbrains.php.config.PhpProjectConfigurationFacade
16-
import com.jetbrains.php.config.interpreters.PhpInterpretersManagerImpl
17-
import kotlinx.coroutines.CoroutineScope
18-
import kotlinx.coroutines.Dispatchers
19-
import kotlinx.coroutines.launch
20-
import kotlinx.coroutines.withContext
2112

2213
@Service(Service.Level.PROJECT)
23-
class TokensDumperService(var project: Project) : Disposable {
14+
class TokensDumperService(var project: Project) : Disposable, DumperServiceInterface {
2415
var consoleView: ConsoleView? = null
2516

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-
3417
override fun dispose() {
3518
consoleView?.dispose()
3619
}
3720

38-
fun dump(file: String) {
39-
val interpretersManager = PhpInterpretersManagerImpl.getInstance(project)
40-
val interpreter = PhpProjectConfigurationFacade.getInstance(project).interpreter
41-
?: interpretersManager.interpreters.firstOrNull() ?: return
42-
43-
//php -l \
44-
// -ddisplay_errors=0 \
45-
// -derror_reporting=0 \
46-
// -dopcache.enable_cli=1 \
47-
// -dopcache.save_comments=1 \
48-
// -dopcache.opt_debug_level=0x10000 \
49-
// -dopcache.optimization_level=0 \
50-
// playground/test.php \
51-
// 1>/dev/null
52-
21+
override fun dump(file: String) {
5322
// language=injectablephp
5423
val phpSnippet = $$"""
5524
print_r(
@@ -68,43 +37,14 @@ class TokensDumperService(var project: Project) : Disposable {
6837
);
6938
""".trimIndent()
7039

71-
val commandArgs = buildList {
72-
interpreter.apply {
73-
println("interpreter: $this")
74-
add(this.pathToPhpExecutable!!)
75-
}
76-
add("-r")
77-
add(phpSnippet)
78-
79-
add(file)
80-
}
81-
82-
CoroutineScope(Dispatchers.IO).launch {
83-
executeCommand(commandArgs)
84-
}
85-
}
86-
87-
private suspend fun executeCommand(commandArgs: List<String>) = withContext(Dispatchers.IO) {
88-
val command = GeneralCommandLine(commandArgs)
89-
command.withRedirectErrorStream(false)
90-
91-
println("running command ${command.commandLineString}")
92-
val processHandler = KillableColoredProcessHandler.Silent(command)
93-
processHandler.setShouldKillProcessSoftly(false)
94-
processHandler.setShouldDestroyProcessRecursively(true)
95-
processHandler.addProcessListener(object : ProcessAdapter() {
40+
consoleView?.clear()
41+
PhpCommandExecutor.execute(file, phpSnippet, project, object : ProcessAdapter() {
9642
override fun onTextAvailable(event: ProcessEvent, outputType: Key<*>) {
9743
when (outputType) {
9844
ProcessOutputTypes.STDERR -> consoleView?.print(event.text, ConsoleViewContentType.ERROR_OUTPUT)
9945
ProcessOutputTypes.STDOUT -> consoleView?.print(event.text, ConsoleViewContentType.NORMAL_OUTPUT)
10046
}
10147
}
10248
})
103-
104-
consoleView?.clear()
105-
// consoleView?.attachToProcess(processHandler)
106-
// consoleView?.requestScrollingToEnd()
107-
108-
processHandler.startNotify()
10949
}
11050
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.github.xepozz.php_dump.services
2+
3+
import com.intellij.execution.process.ProcessAdapter
4+
import com.intellij.execution.process.ProcessEvent
5+
import com.intellij.execution.process.ProcessOutputTypes
6+
import com.intellij.execution.ui.ConsoleView
7+
import com.intellij.execution.ui.ConsoleViewContentType
8+
import com.intellij.openapi.Disposable
9+
import com.intellij.openapi.components.Service
10+
import com.intellij.openapi.project.Project
11+
import com.intellij.openapi.util.Key
12+
13+
@Service(Service.Level.PROJECT)
14+
class TokensObjectDumperService(var project: Project) : Disposable, DumperServiceInterface {
15+
var consoleView: ConsoleView? = null
16+
17+
override fun dispose() {
18+
consoleView?.dispose()
19+
}
20+
21+
override fun dump(file: String) {
22+
// language=injectablephp
23+
val phpSnippet = $$"""
24+
print_r(
25+
array_map(
26+
function (PhpToken $token) {
27+
return [
28+
'line' => $token->line,
29+
'pos' => $token->pos,
30+
'name' => $token->getTokenName(),
31+
'value' => $token->text,
32+
];
33+
},
34+
\PhpToken::tokenize(file_get_contents($argv[1])),
35+
)
36+
);
37+
""".trimIndent()
38+
39+
consoleView?.clear()
40+
PhpCommandExecutor.execute(file, phpSnippet, project, object : ProcessAdapter() {
41+
override fun onTextAvailable(event: ProcessEvent, outputType: Key<*>) {
42+
when (outputType) {
43+
ProcessOutputTypes.STDERR -> consoleView?.print(event.text, ConsoleViewContentType.ERROR_OUTPUT)
44+
ProcessOutputTypes.STDOUT -> consoleView?.print(event.text, ConsoleViewContentType.NORMAL_OUTPUT)
45+
}
46+
}
47+
})
48+
}
49+
}

0 commit comments

Comments
 (0)