Skip to content

Commit cff5ad0

Browse files
committed
refactor: use coroutines instead
1 parent 84f0988 commit cff5ad0

File tree

8 files changed

+35
-51
lines changed

8 files changed

+35
-51
lines changed

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

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

3-
import com.intellij.ide.plugins.PluginManagerCore.isUnitTestMode
43
import com.intellij.openapi.Disposable
5-
import com.intellij.openapi.application.ModalityState
6-
import com.intellij.openapi.application.ReadAction
74
import com.intellij.openapi.components.Service
85
import com.intellij.openapi.project.Project
9-
import com.intellij.util.concurrency.AppExecutorUtil
10-
import java.util.concurrent.Callable
11-
12-
13-
inline fun <R> Project.nonBlocking(crossinline block: () -> R, crossinline uiContinuation: (R) -> Unit) {
14-
if (isUnitTestMode) {
15-
val result = block()
16-
uiContinuation(result)
17-
} else {
18-
ReadAction.nonBlocking(Callable { block() })
19-
.inSmartMode(this)
20-
.expireWith(DumpPluginDisposable.getInstance(this))
21-
.finishOnUiThread(ModalityState.current()) { uiContinuation(it) }
22-
.submit(AppExecutorUtil.getAppExecutorService())
23-
}
24-
}
256

267
@Service(Service.Level.PROJECT)
278
class DumpPluginDisposable : Disposable {

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package com.github.xepozz.php_dump.panel
33
import com.github.xepozz.php_dump.actions.CollapseTreeAction
44
import com.github.xepozz.php_dump.actions.ExpandTreeAction
55
import com.github.xepozz.php_dump.actions.RefreshAction
6-
import com.github.xepozz.php_dump.nonBlocking
76
import com.github.xepozz.php_dump.services.OpcacheSettingsTreeDumperService
87
import com.github.xepozz.php_dump.stubs.any_tree.AnyNodeList
98
import com.github.xepozz.php_dump.stubs.any_tree.AnyRootNode
@@ -24,7 +23,9 @@ import com.intellij.ui.tree.AsyncTreeModel
2423
import com.intellij.ui.tree.StructureTreeModel
2524
import com.intellij.ui.treeStructure.Tree
2625
import com.intellij.util.ui.tree.TreeUtil
27-
import kotlinx.coroutines.runBlocking
26+
import kotlinx.coroutines.CoroutineScope
27+
import kotlinx.coroutines.Dispatchers
28+
import kotlinx.coroutines.launch
2829
import java.awt.BorderLayout
2930
import java.awt.GridLayout
3031
import javax.swing.JPanel
@@ -36,6 +37,7 @@ import javax.swing.tree.DefaultTreeModel
3637
class OpcacheSettingsPanel(private val project: Project) :
3738
SimpleToolWindowPanel(false, false),
3839
RefreshablePanel, Disposable {
40+
val fileEditorManager = FileEditorManager.getInstance(project)
3941
private val progressBar = JProgressBar()
4042

4143
private val treeModel = StructureTreeModel(TokensTreeStructure(RootNode(null)), this)
@@ -90,12 +92,12 @@ class OpcacheSettingsPanel(private val project: Project) :
9092
}
9193

9294
private fun refreshData() {
93-
progressBar.setIndeterminate(true)
94-
progressBar.isVisible = true
95-
tree.emptyText.text = "Loading..."
95+
CoroutineScope(Dispatchers.IO).launch {
96+
progressBar.setIndeterminate(true)
97+
progressBar.isVisible = true
98+
tree.emptyText.text = "Loading..."
9699

97-
98-
project.nonBlocking({ getViewData() }) { result ->
100+
val result = getViewData()
99101
tree.emptyText.text = "Nothing to show"
100102
rebuildTree(result)
101103

@@ -113,12 +115,12 @@ class OpcacheSettingsPanel(private val project: Project) :
113115
TreeUtil.expandAll(tree)
114116
}
115117

116-
private fun getViewData(): AnyNodeList {
118+
private suspend fun getViewData(): AnyNodeList {
117119
val result = AnyNodeList()
118-
val editor = FileEditorManager.getInstance(project).selectedTextEditor ?: return result
120+
val editor = fileEditorManager.selectedTextEditor ?: return result
119121
val virtualFile = editor.virtualFile ?: return result
120122

121-
return runBlocking { service.dump(virtualFile) } as? AnyNodeList ?: result
123+
return service.dump(virtualFile) as? AnyNodeList ?: result
122124
}
123125

124126
override fun refresh(project: Project, type: RefreshType) {

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package com.github.xepozz.php_dump.panel
33
import com.github.xepozz.php_dump.actions.CollapseTreeAction
44
import com.github.xepozz.php_dump.actions.ExpandTreeAction
55
import com.github.xepozz.php_dump.actions.RefreshAction
6-
import com.github.xepozz.php_dump.nonBlocking
76
import com.github.xepozz.php_dump.services.TokensTreeDumperService
87
import com.github.xepozz.php_dump.stubs.token_object.TokensList
98
import com.github.xepozz.php_dump.tree.RootNode
@@ -29,7 +28,9 @@ import com.intellij.ui.tree.AsyncTreeModel
2928
import com.intellij.ui.tree.StructureTreeModel
3029
import com.intellij.ui.treeStructure.Tree
3130
import com.intellij.util.ui.tree.TreeUtil
32-
import kotlinx.coroutines.runBlocking
31+
import kotlinx.coroutines.CoroutineScope
32+
import kotlinx.coroutines.Dispatchers
33+
import kotlinx.coroutines.launch
3334
import java.awt.BorderLayout
3435
import java.awt.GridLayout
3536
import javax.swing.JPanel
@@ -42,6 +43,7 @@ import javax.swing.tree.TreePath
4243
class TokenTreePanel(private val project: Project) :
4344
SimpleToolWindowPanel(false, false),
4445
RefreshablePanel, Disposable {
46+
val fileEditorManager = FileEditorManager.getInstance(project)
4547
private val progressBar = JProgressBar()
4648

4749
private val treeModel = StructureTreeModel(TokensTreeStructure(RootNode(null)), this)
@@ -132,12 +134,12 @@ class TokenTreePanel(private val project: Project) :
132134
}
133135

134136
private fun refreshData() {
135-
progressBar.setIndeterminate(true)
136-
progressBar.isVisible = true
137-
tree.emptyText.text = "Loading..."
137+
CoroutineScope(Dispatchers.IO).launch {
138+
progressBar.setIndeterminate(true)
139+
progressBar.isVisible = true
140+
tree.emptyText.text = "Loading..."
138141

139-
140-
project.nonBlocking({ getViewData() }) { result ->
142+
val result = getViewData()
141143
tree.emptyText.text = "Nothing to show"
142144
rebuildTree(result)
143145

@@ -155,12 +157,12 @@ class TokenTreePanel(private val project: Project) :
155157
TreeUtil.expandAll(tree)
156158
}
157159

158-
private fun getViewData(): TokensList {
160+
private suspend fun getViewData(): TokensList {
159161
val result = TokensList()
160-
val editor = FileEditorManager.getInstance(project).selectedTextEditor ?: return result
162+
val editor = fileEditorManager.selectedTextEditor ?: return result
161163
val virtualFile = editor.virtualFile ?: return result
162164

163-
val runBlocking = runBlocking { service.dump(virtualFile) }
165+
val runBlocking = service.dump(virtualFile)
164166
// println("result is $runBlocking")
165167

166168
return runBlocking as? TokensList ?: result

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import com.intellij.openapi.ui.SimpleToolWindowPanel
1818
import kotlinx.coroutines.CoroutineScope
1919
import kotlinx.coroutines.Dispatchers
2020
import kotlinx.coroutines.launch
21-
import kotlinx.coroutines.runBlocking
2221
import java.awt.BorderLayout
2322
import java.awt.GridLayout
2423
import java.awt.event.ComponentAdapter
@@ -95,7 +94,7 @@ class TokensTerminalPanel(
9594
val virtualFile = editor.virtualFile ?: return
9695

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

10099
consoleView.clear()
101100
consoleView.print(result as? String ?: "No output", ConsoleViewContentType.NORMAL_OUTPUT)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class OpcacheSettingsTreeDumperService(var project: Project) : DumperServiceInte
6767
}
6868
// println("result tree: $tree")
6969

70-
return@withContext tree
70+
tree
7171
}
7272
}
7373
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ import kotlinx.coroutines.withContext
1717
@Service(Service.Level.PROJECT)
1818
class OpcodesDumperService(var project: Project) : DumperServiceInterface {
1919
val state = PhpDumpSettingsService.getInstance(project)
20-
override suspend fun dump(file: String): Any? {
21-
val interpretersManager = PhpInterpretersManagerImpl.getInstance(project)
22-
val interpreter = PhpProjectConfigurationFacade.getInstance(project).interpreter
23-
?: interpretersManager.interpreters.firstOrNull() ?: return null
20+
val interpretersManager = PhpInterpretersManagerImpl.getInstance(project)
21+
val interpreter = PhpProjectConfigurationFacade.getInstance(project).interpreter
22+
?: interpretersManager.interpreters.firstOrNull()
2423

25-
val interpreterPath = interpreter.pathToPhpExecutable ?: return null
24+
override suspend fun dump(file: String): Any? {
25+
val interpreterPath = interpreter?.pathToPhpExecutable ?: return null
2626
val debugLevel = state.debugLevel.value
2727
val preloadFile = state.preloadFile
2828
val command = GeneralCommandLine(buildList {
@@ -63,7 +63,7 @@ class OpcodesDumperService(var project: Project) : DumperServiceInterface {
6363
}, listOf("-dopcache.enable_cli=1"))
6464

6565

66-
return@withContext output.toString()
66+
output.toString()
6767
}
6868
}
6969
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import kotlinx.coroutines.withContext
1515
class TokensDumperService(var project: Project) : DumperServiceInterface {
1616
val state = PhpDumpSettingsService.getInstance(project)
1717

18-
override suspend fun dump(file: String): Any? {
18+
override suspend fun dump(file: String): Any {
1919
val phpSnippet = if (state.tokensObject) {
2020
// language=injectablephp
2121
$$"""
@@ -65,7 +65,7 @@ class TokensDumperService(var project: Project) : DumperServiceInterface {
6565
}
6666
})
6767

68-
return@withContext output.toString()
68+
output.toString()
6969
}
7070
}
7171
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class TokensTreeDumperService(var project: Project) : DumperServiceInterface {
5050
val tree = TokenParser.parseTokens(jsonString)
5151
// println("result tree: $tree")
5252

53-
return@withContext tree
53+
tree
5454
}
5555
}
5656
}

0 commit comments

Comments
 (0)