Skip to content

Commit 19aac7d

Browse files
committed
refactor(sketch): remove redundant runReadAction calls #257
- Remove unnecessary `runReadAction` calls in `LocalSearchInsCommand` and `SketchToolWindow`. - Simplify `SketchInputListener` by removing `manualSend` method and inline listener creation. - Clean up unused imports and redundant
1 parent 415bca3 commit 19aac7d

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

core/src/main/kotlin/cc/unitmesh/devti/gui/chat/ui/AutoDevInputListener.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ interface AutoDevInputListener : EventListener {
1212
fun editorAdded(editor: EditorEx) {}
1313
fun onSubmit(component: AutoDevInputSection, trigger: AutoDevInputTrigger) {}
1414
fun onStop(component: AutoDevInputSection) {}
15+
fun manualSend(userInput: String) {}
1516
}

core/src/main/kotlin/cc/unitmesh/devti/sketch/SketchInputListener.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package cc.unitmesh.devti.sketch
22

33
import cc.unitmesh.devti.AutoDevBundle
4-
import cc.unitmesh.devti.devin.InsCommand
5-
import cc.unitmesh.devti.devin.InsCommandListener
6-
import cc.unitmesh.devti.devin.InsCommandStatus
7-
import cc.unitmesh.devti.devin.dataprovider.BuiltinCommand
84
import cc.unitmesh.devti.gui.chat.ChatCodingService
95
import cc.unitmesh.devti.gui.chat.ui.AutoDevInputListener
106
import cc.unitmesh.devti.gui.chat.ui.AutoDevInputSection
@@ -18,7 +14,6 @@ import com.intellij.openapi.Disposable
1814
import com.intellij.openapi.application.ApplicationManager
1915
import com.intellij.openapi.application.invokeLater
2016
import com.intellij.openapi.project.Project
21-
import com.intellij.openapi.vfs.VirtualFile
2217
import kotlinx.coroutines.flow.cancellable
2318
import kotlinx.coroutines.launch
2419

@@ -52,6 +47,10 @@ class SketchInputListener(
5247
return
5348
}
5449

50+
manualSend(userInput)
51+
}
52+
53+
override fun manualSend(userInput: String) {
5554
val postProcessors = LanguagePromptProcessor.instance("DevIn").firstOrNull()
5655
val compiledInput = postProcessors?.compile(project, userInput) ?: userInput
5756

core/src/main/kotlin/cc/unitmesh/devti/sketch/SketchToolWindow.kt

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cc.unitmesh.devti.sketch
22

33
import cc.unitmesh.devti.alignRight
4+
import cc.unitmesh.devti.devin.dataprovider.BuiltinCommand
45
import cc.unitmesh.devti.gui.chat.ChatCodingService
56
import cc.unitmesh.devti.gui.chat.message.ChatActionType
67
import cc.unitmesh.devti.gui.chat.ui.AutoDevInputSection
@@ -17,6 +18,7 @@ import com.intellij.icons.AllIcons
1718
import com.intellij.ide.scratch.ScratchRootType
1819
import com.intellij.openapi.Disposable
1920
import com.intellij.openapi.application.runInEdt
21+
import com.intellij.openapi.application.runReadAction
2022
import com.intellij.openapi.editor.Editor
2123
import com.intellij.openapi.fileTypes.PlainTextLanguage
2224
import com.intellij.openapi.project.Project
@@ -78,7 +80,9 @@ class SketchToolWindow(val project: Project, val editor: Editor?, private val sh
7880
if (showInput) {
7981
row {
8082
checkBox("AI 降临模式(全自动化)").apply {
81-
AutoSketchMode.getInstance(project).isEnable = this.component.isSelected
83+
this.component.addActionListener {
84+
AutoSketchMode.getInstance(project).isEnable = this.component.isSelected
85+
}
8286
}
8387
}
8488
}
@@ -99,6 +103,8 @@ class SketchToolWindow(val project: Project, val editor: Editor?, private val sh
99103

100104
var handleCancel: ((String) -> Unit)? = null
101105

106+
private val listener = SketchInputListener(project, chatCodingService, this)
107+
102108
init {
103109
contentPanel.add(scrollPanel, BorderLayout.CENTER)
104110
contentPanel.addKeyListener(object : KeyAdapter() {
@@ -118,7 +124,7 @@ class SketchToolWindow(val project: Project, val editor: Editor?, private val sh
118124
border = JBUI.Borders.empty(8)
119125
}
120126

121-
shireInput.addListener(SketchInputListener(project, chatCodingService, this))
127+
shireInput.addListener(listener)
122128
contentPanel.add(shireInput, BorderLayout.SOUTH)
123129
}
124130

@@ -252,12 +258,25 @@ class SketchToolWindow(val project: Project, val editor: Editor?, private val sh
252258
val devinCodeFence = codeFenceList.filter { it.language.displayName == "DevIn" }
253259

254260
devinCodeFence.forEach {
261+
val commands = setOf(
262+
BuiltinCommand.DIR,
263+
BuiltinCommand.LOCAL_SEARCH,
264+
BuiltinCommand.REV,
265+
BuiltinCommand.STRUCTURE,
266+
BuiltinCommand.SYMBOL,
267+
BuiltinCommand.DATABASE
268+
)
269+
if (commands.any { command -> it.text.contains("/" + command.commandName + ":") }) {
270+
return listener.manualSend(it.text)
271+
}
272+
255273
val scratchFile = ScratchRootType.getInstance()
256274
.createScratchFile(project, "sketch.shire", it.language, it.text)
257275
?: return
258276

259-
val psiFile = PsiManager.getInstance(project).findFile(scratchFile)
260-
?: return
277+
val psiFile = runReadAction {
278+
PsiManager.getInstance(project).findFile(scratchFile)
279+
} ?: return
261280

262281
RunService.provider(project, scratchFile)
263282
?.runFile(project, scratchFile, psiFile, isFromToolAction = true)

exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/compiler/exec/LocalSearchInsCommand.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import cc.unitmesh.devti.devin.InsCommand
44
import cc.unitmesh.devti.devin.dataprovider.BuiltinCommand
55
import cc.unitmesh.devti.gui.chat.ui.relativePath
66
import cc.unitmesh.devti.language.utils.canBeAdded
7+
import com.intellij.openapi.application.runReadAction
78
import com.intellij.openapi.project.Project
89
import com.intellij.openapi.roots.ProjectFileIndex
910
import com.intellij.openapi.vfs.VirtualFile
@@ -34,7 +35,7 @@ class LocalSearchInsCommand(val myProject: Project, private val scope: String, v
3435
throw IllegalArgumentException("Text length should be more than 4")
3536
}
3637

37-
val textSearch = search(myProject, text, OVERLAP)
38+
val textSearch = runReadAction { search(myProject, text, OVERLAP) }
3839
return textSearch.map { (file, lines) ->
3940
val filePath = file.relativePath(myProject)
4041
val linesWithContext = lines.joinToString("\n")

0 commit comments

Comments
 (0)