@@ -5,26 +5,35 @@ import com.github.xepozz.php_dump.actions.EditPhpSnippetAction
55import com.github.xepozz.php_dump.actions.ExpandTreeAction
66import com.github.xepozz.php_dump.actions.OpenPhpSettingsAction
77import com.github.xepozz.php_dump.actions.RefreshAction
8+ import com.github.xepozz.php_dump.notification.NotificationUtil
9+ import com.github.xepozz.php_dump.services.CustomDumpResult
810import com.github.xepozz.php_dump.services.CustomTreeDumperService
11+ import com.github.xepozz.php_dump.services.EditorProvider
912import com.github.xepozz.php_dump.stubs.token_object.TokensList
1013import com.github.xepozz.php_dump.toolWindow.tabs.CompositeWindowTabsState
1114import com.github.xepozz.php_dump.tree.RootNode
1215import com.github.xepozz.php_dump.tree.TokenNode
1316import com.github.xepozz.php_dump.tree.TokensTreeStructure
17+ import com.intellij.icons.AllIcons
1418import com.intellij.ide.util.treeView.AbstractTreeStructure
1519import com.intellij.openapi.Disposable
1620import com.intellij.openapi.actionSystem.ActionManager
21+ import com.intellij.openapi.actionSystem.AnActionEvent
1722import com.intellij.openapi.actionSystem.DefaultActionGroup
23+ import com.intellij.openapi.actionSystem.ToggleAction
1824import com.intellij.openapi.application.EDT
25+ import com.intellij.openapi.application.runWriteAction
1926import com.intellij.openapi.editor.markup.EffectType
2027import com.intellij.openapi.editor.markup.HighlighterLayer
2128import com.intellij.openapi.editor.markup.HighlighterTargetArea
2229import com.intellij.openapi.editor.markup.TextAttributes
2330import com.intellij.openapi.fileEditor.FileEditorManager
2431import com.intellij.openapi.project.Project
2532import com.intellij.openapi.ui.SimpleToolWindowPanel
33+ import com.intellij.openapi.vfs.findDocument
2634import com.intellij.pom.Navigatable
2735import com.intellij.psi.PsiDocumentManager
36+ import com.intellij.testFramework.LightVirtualFile
2837import com.intellij.ui.JBColor
2938import com.intellij.ui.TreeUIHelper
3039import com.intellij.ui.components.JBScrollPane
@@ -37,6 +46,7 @@ import kotlinx.coroutines.Dispatchers
3746import kotlinx.coroutines.launch
3847import java.awt.BorderLayout
3948import java.awt.GridLayout
49+ import javax.swing.JComponent
4050import javax.swing.JPanel
4151import javax.swing.JProgressBar
4252import javax.swing.SwingUtilities
@@ -53,6 +63,7 @@ class CustomTreePanel(
5363 val fileEditorManager = FileEditorManager .getInstance(project)
5464 private val progressBar = JProgressBar ()
5565
66+ var rawOutput = " "
5667 private val treeModel = StructureTreeModel (TokensTreeStructure (RootNode (null )), this )
5768 private val tree = Tree (DefaultTreeModel (DefaultMutableTreeNode ())).apply {
5869 setModel(AsyncTreeModel (treeModel, this @CustomTreePanel))
@@ -68,7 +79,12 @@ class CustomTreePanel(
6879 }, true )
6980 }
7081 val service: CustomTreeDumperService = project.getService(CustomTreeDumperService ::class .java)
82+ val editorProvider: EditorProvider = project.getService(EditorProvider ::class .java)
7183
84+ val scrollPane = JBScrollPane (tree)
85+ var contentPanel: JComponent = JPanel (BorderLayout ()).apply { add(scrollPane) }
86+ val rawVirtualFile = LightVirtualFile (" Raw Output" , " " )
87+ val rawPanel = editorProvider.getOrCreateEditorFor(rawVirtualFile).component
7288
7389 init {
7490 treeModel.invalidateAsync()
@@ -81,6 +97,7 @@ class CustomTreePanel(
8197 SwingUtilities .invokeLater { refreshData() }
8298 }
8399
100+ private var showRawOutput = false
84101 fun createToolbar () {
85102 val actionGroup = DefaultActionGroup ().apply {
86103 add(RefreshAction { refreshData() })
@@ -91,6 +108,20 @@ class CustomTreePanel(
91108 add(EditPhpSnippetAction (project, tabConfig) { snippet ->
92109 tabConfig.snippet = snippet
93110 })
111+ add(object :
112+ ToggleAction (" Use Object Tokens" , " Switches engine to dump lexical tokens" , AllIcons .FileTypes .Json ) {
113+ override fun isSelected (event : AnActionEvent ): Boolean = showRawOutput
114+
115+ override fun setSelected (event : AnActionEvent , value : Boolean ) {
116+ println (" switch content" )
117+ event.presentation.icon = if (value) AllIcons .FileTypes .Json else AllIcons .FileTypes .Text
118+ showRawOutput = value
119+ contentPanel.components.forEach { contentPanel.remove(it) }
120+ contentPanel.add(if (showRawOutput) rawPanel else scrollPane)
121+ SwingUtilities .invokeLater { contentPanel.repaint() }
122+ }
123+
124+ })
94125 add(OpenPhpSettingsAction ())
95126 }
96127
@@ -106,7 +137,7 @@ class CustomTreePanel(
106137 private fun createContent () {
107138 val responsivePanel = JPanel (BorderLayout ())
108139 responsivePanel.add(progressBar, BorderLayout .NORTH )
109- responsivePanel.add(JBScrollPane (tree) , BorderLayout .CENTER )
140+ responsivePanel.add(contentPanel , BorderLayout .CENTER )
110141
111142 setContent(responsivePanel)
112143 }
@@ -153,7 +184,8 @@ class CustomTreePanel(
153184
154185 val result = getViewData()
155186 tree.emptyText.text = " Nothing to show"
156- rebuildTree(result)
187+ rebuildTree(result.tokens)
188+ rawOutput = result.raw
157189
158190 progressBar.setIndeterminate(false )
159191 progressBar.isVisible = false
@@ -169,16 +201,33 @@ class CustomTreePanel(
169201 TreeUtil .expandAll(tree)
170202 }
171203
172- private suspend fun getViewData (): TokensList {
173- val result = TokensList ()
204+ private suspend fun getViewData (): CustomDumpResult {
205+ val result = CustomDumpResult ()
174206 val editor = fileEditorManager.selectedTextEditor ? : return result
175207 val virtualFile = editor.virtualFile ? : return result
176208
177209 service.phpSnippet = tabConfig.snippet
178210
179- val runBlocking = service.dump(virtualFile)
211+ val runResult = service.dump(virtualFile) as CustomDumpResult
212+
213+ val error = runResult.error
214+ val document = rawVirtualFile.findDocument()
215+ if (document != null ) {
216+ runWriteAction {
217+ document.setText(runResult.raw ? : " " )
218+ }
219+ }
220+
221+ if (error != null ) {
222+ NotificationUtil
223+ .sendNotification(
224+ project,
225+ " Error ${error.javaClass} " ,
226+ error.message ? : " Unknown error" ,
227+ )
228+ }
180229
181- return runBlocking as ? TokensList ? : result
230+ return runResult
182231 }
183232
184233 override fun refresh (project : Project , type : RefreshType ) {
0 commit comments