Skip to content

Commit f3e8358

Browse files
committed
refactor: simplify async work
1 parent 9a635de commit f3e8358

File tree

3 files changed

+41
-17
lines changed

3 files changed

+41
-17
lines changed
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.github.xepozz.php_dump.actions
22

33
import com.intellij.icons.AllIcons
4+
import com.intellij.openapi.actionSystem.ActionUpdateThread
45
import com.intellij.openapi.actionSystem.AnAction
56
import com.intellij.openapi.actionSystem.AnActionEvent
67

7-
class RefreshAction(private val callback: () -> Unit) :
8-
AnAction("Refresh", "Refresh", AllIcons.Actions.Refresh) {
8+
class RefreshAction(private val callback: () -> Unit) : AnAction("Refresh", "Refresh", AllIcons.Actions.Refresh) {
99
override fun actionPerformed(event: AnActionEvent) {
1010
callback()
1111
}
12+
13+
override fun getActionUpdateThread() = ActionUpdateThread.BGT
1214
}

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

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import com.github.xepozz.php_opcodes_language.language.PHPOpFileType
99
import com.intellij.icons.AllIcons
1010
import com.intellij.openapi.Disposable
1111
import com.intellij.openapi.actionSystem.ActionManager
12+
import com.intellij.openapi.actionSystem.ActionUpdateThread
1213
import com.intellij.openapi.actionSystem.AnAction
1314
import com.intellij.openapi.actionSystem.AnActionEvent
1415
import com.intellij.openapi.actionSystem.DefaultActionGroup
1516
import com.intellij.openapi.application.ApplicationManager
1617
import com.intellij.openapi.command.WriteCommandAction
1718
import com.intellij.openapi.editor.EditorFactory
18-
import com.intellij.openapi.editor.EditorKind
1919
import com.intellij.openapi.editor.ex.EditorEx
2020
import com.intellij.openapi.editor.highlighter.EditorHighlighterFactory
2121
import com.intellij.openapi.fileChooser.FileChooser
@@ -24,7 +24,9 @@ import com.intellij.openapi.fileEditor.FileEditorManager
2424
import com.intellij.openapi.project.Project
2525
import com.intellij.openapi.ui.SimpleToolWindowPanel
2626
import com.jetbrains.php.lang.PhpFileType
27-
import kotlinx.coroutines.runBlocking
27+
import kotlinx.coroutines.CoroutineScope
28+
import kotlinx.coroutines.Dispatchers
29+
import kotlinx.coroutines.launch
2830
import java.awt.BorderLayout
2931
import java.awt.GridLayout
3032
import java.awt.event.ComponentAdapter
@@ -35,11 +37,12 @@ import javax.swing.JPanel
3537
class OpcodesTerminalPanel(
3638
val project: Project,
3739
) : SimpleToolWindowPanel(false, false), RefreshablePanel, Disposable {
38-
val viewComponent: JComponent
39-
val service: OpcodesDumperService = project.getService(OpcodesDumperService::class.java)
40-
val state = PhpDumpSettingsService.getInstance(project)
41-
private val document = EditorFactory.getInstance().createDocument("")
42-
private val editor = EditorFactory.getInstance().createViewer(document, project, EditorKind.MAIN_EDITOR) as EditorEx
40+
private val viewComponent: JComponent
41+
private val service: OpcodesDumperService = project.getService(OpcodesDumperService::class.java)
42+
private val state = PhpDumpSettingsService.getInstance(project)
43+
private val editorFactory: EditorFactory = EditorFactory.getInstance()
44+
private val document = editorFactory.createDocument("")
45+
private val editor = editorFactory.createEditor(document, project, PHPOpFileType.INSTANCE, false) as EditorEx
4346

4447
init {
4548
viewComponent = editor.component
@@ -56,6 +59,9 @@ class OpcodesTerminalPanel(
5659
isBlockCursor = false
5760
isLineMarkerAreaShown = true
5861
isHighlightSelectionOccurrences = true
62+
isAutoCodeFoldingEnabled = true
63+
isSmartHome = true
64+
isShowIntentionBulb = true
5965
}
6066

6167
editor.setCaretEnabled(true)
@@ -77,6 +83,8 @@ class OpcodesTerminalPanel(
7783
document.setText("")
7884
}
7985
}
86+
87+
override fun getActionUpdateThread() = ActionUpdateThread.EDT
8088
})
8189
add(object : AnAction(
8290
"Enable Auto Refresh", "Turns on or off auto refresh of panel context",
@@ -95,6 +103,8 @@ class OpcodesTerminalPanel(
95103
e.presentation.icon = PhpDumpIcons.RERUN_AUTOMATICALLY
96104
}
97105
}
106+
107+
override fun getActionUpdateThread() = ActionUpdateThread.BGT
98108
})
99109
addSeparator()
100110
add(DefaultActionGroup("Debug Level", true).apply {
@@ -117,6 +127,9 @@ class OpcodesTerminalPanel(
117127
else -> null
118128
}
119129
}
130+
131+
override fun getActionUpdateThread() = ActionUpdateThread.BGT
132+
120133
}
121134
}
122135
.also { addAll(it) }
@@ -139,6 +152,8 @@ class OpcodesTerminalPanel(
139152
}
140153
refresh(project, RefreshType.MANUAL)
141154
}
155+
156+
override fun getActionUpdateThread() = ActionUpdateThread.BGT
142157
})
143158
}
144159

@@ -171,17 +186,19 @@ class OpcodesTerminalPanel(
171186
val editor = FileEditorManager.getInstance(project).selectedTextEditor ?: return
172187
val virtualFile = editor.virtualFile ?: return
173188

174-
val result = runBlocking { service.dump(virtualFile) }
189+
CoroutineScope(Dispatchers.IO).launch {
190+
val result = service.dump(virtualFile)
175191

176-
val content = result as? String ?: "No output"
192+
val content = result as? String ?: "No output"
177193

178-
WriteCommandAction.runWriteCommandAction(project) {
179-
document.setText(content)
194+
WriteCommandAction.runWriteCommandAction(project) {
195+
document.setText(content)
196+
}
180197
}
181198
}
182199

183200
override fun dispose() {
184-
EditorFactory.getInstance().releaseEditor(editor)
201+
editorFactory.releaseEditor(editor)
185202
}
186203
}
187204

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import com.intellij.openapi.actionSystem.DefaultActionGroup
1515
import com.intellij.openapi.fileEditor.FileEditorManager
1616
import com.intellij.openapi.project.Project
1717
import com.intellij.openapi.ui.SimpleToolWindowPanel
18+
import kotlinx.coroutines.CoroutineScope
19+
import kotlinx.coroutines.Dispatchers
20+
import kotlinx.coroutines.launch
1821
import kotlinx.coroutines.runBlocking
1922
import java.awt.BorderLayout
2023
import java.awt.GridLayout
@@ -91,9 +94,11 @@ class TokensTerminalPanel(
9194
val editor = FileEditorManager.getInstance(project).selectedTextEditor ?: return
9295
val virtualFile = editor.virtualFile ?: return
9396

94-
val result = runBlocking { service.dump(virtualFile) }
97+
CoroutineScope(Dispatchers.IO).launch {
98+
val result = runBlocking { service.dump(virtualFile) }
9599

96-
consoleView.clear()
97-
consoleView.print(result as? String ?: "No output", ConsoleViewContentType.NORMAL_OUTPUT)
100+
consoleView.clear()
101+
consoleView.print(result as? String ?: "No output", ConsoleViewContentType.NORMAL_OUTPUT)
102+
}
98103
}
99104
}

0 commit comments

Comments
 (0)