Skip to content

Commit b80cdbd

Browse files
committed
refactor(diff): improve thread handling and resource disposal
- Replace `executeOnPooledThread` with `invokeLater` for better UI thread safety. - Add `whenDisposed` for proper editor resource cleanup. - Refactor auto-repair logic into `executeAutoRepair` method for clarity. - Ensure file operations are wrapped in `runWriteAction` for thread safety.
1 parent 8ac27e1 commit b80cdbd

File tree

6 files changed

+61
-36
lines changed

6 files changed

+61
-36
lines changed

core/src/main/kotlin/cc/unitmesh/devti/inline/AutoDevInlineChatPanel.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,10 @@ class AutoDevInlineChatPanel(val editor: Editor) : JPanel(GridBagLayout()), Edit
6565
}
6666
}
6767

68-
panelView.resize()
69-
panelView.onFinish(suggestion.toString())
68+
invokeLater {
69+
panelView.resize()
70+
panelView.onFinish(suggestion.toString())
71+
}
7072
}
7173

7274
panelView

core/src/main/kotlin/cc/unitmesh/devti/sketch/ui/patch/DiffLangSketch.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@ import cc.unitmesh.devti.AutoDevIcons
55
import cc.unitmesh.devti.AutoDevNotifications
66
import cc.unitmesh.devti.sketch.ui.ExtensionLangSketch
77
import cc.unitmesh.devti.util.findFile
8+
import cc.unitmesh.devti.util.whenDisposed
89
import com.intellij.icons.AllIcons
910
import com.intellij.lang.Language
11+
import com.intellij.openapi.Disposable
1012
import com.intellij.openapi.application.ApplicationManager
1113
import com.intellij.openapi.command.CommandProcessor
1214
import com.intellij.openapi.command.UndoConfirmationPolicy
1315
import com.intellij.openapi.command.undo.UndoManager
1416
import com.intellij.openapi.diff.impl.patch.PatchReader
1517
import com.intellij.openapi.diff.impl.patch.TextFilePatch
1618
import com.intellij.openapi.editor.Editor
19+
import com.intellij.openapi.editor.EditorFactory
1720
import com.intellij.openapi.fileEditor.FileEditorManager
1821
import com.intellij.openapi.fileEditor.FileEditorProvider
1922
import com.intellij.openapi.fileEditor.TextEditor
@@ -199,7 +202,9 @@ class DiffLangSketch(private val myProject: Project, private var patchContent: S
199202
MyApplyPatchFromClipboardDialog(myProject, patchContent).show()
200203
return
201204
} else {
202-
showSingleDiff(this@DiffLangSketch.myProject, this@DiffLangSketch.patchContent) { handleAcceptAction() }
205+
showSingleDiff(this@DiffLangSketch.myProject, this@DiffLangSketch.patchContent, this) {
206+
handleAcceptAction()
207+
}
203208
}
204209
}
205210

@@ -214,14 +219,19 @@ class DiffLangSketch(private val myProject: Project, private var patchContent: S
214219
override fun dispose() {}
215220
}
216221

217-
fun showSingleDiff(project: Project, patchContent: String, handleAccept: (() -> Unit)?) {
222+
fun showSingleDiff(project: Project, patchContent: String, disposable: Disposable, handleAccept: (() -> Unit)?) {
218223
val editorProvider = FileEditorProvider.EP_FILE_EDITOR_PROVIDER.extensionList.firstOrNull {
219224
it.javaClass.simpleName == "DiffPatchFileEditorProvider" || it.javaClass.simpleName == "DiffEditorProvider"
220225
}
221226

222227
if (editorProvider != null) {
223228
val virtualFile = LightVirtualFile("AutoDev-Diff-Lang.diff", patchContent)
224229
val editor = editorProvider.createEditor(project, virtualFile)
230+
231+
disposable.whenDisposed {
232+
editor.dispose()
233+
}
234+
225235
object : DialogWrapper(project) {
226236
init {
227237
title = "Diff Preview"

core/src/main/kotlin/cc/unitmesh/devti/sketch/ui/patch/SingleFileDiffSketch.kt

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -140,41 +140,44 @@ class SingleFileDiffSketch(
140140
mainPanel.add(myHeaderPanel)
141141
mainPanel.add(contentPanel)
142142

143-
ApplicationManager.getApplication().executeOnPooledThread {
143+
ApplicationManager.getApplication().invokeLater {
144144
lintCheckForNewCode(currentFile)
145145
}
146146

147147
if (isAutoRepair && appliedPatch?.status != ApplyPatchStatus.SUCCESS) {
148-
ApplicationManager.getApplication().executeOnPooledThread {
149-
applyDiffRepairSuggestionSync(myProject, oldCode, newCode, { fixedCode ->
150-
createPatchFromCode(oldCode, fixedCode)?.let { patch ->
151-
this@SingleFileDiffSketch.patch = patch
152-
appliedPatch = try {
153-
GenericPatchApplier.apply(oldCode, patch.hunks)
154-
} catch (e: Exception) {
155-
logger<SingleFileDiffSketch>().warn("Failed to apply patch: ${patch.beforeFileName}", e)
156-
null
157-
}
158-
159-
invokeLater {
160-
runWriteAction {
161-
currentFile.writeText(fixedCode)
162-
}
163-
}
148+
ApplicationManager.getApplication().invokeLater {
149+
try {
150+
executeAutoRepair()
151+
} catch (e: Exception) {
152+
logger<SingleFileDiffSketch>().error("Failed to execute auto repair", e)
153+
}
154+
}
155+
}
156+
}
164157

165-
createActionButtons(currentFile, appliedPatch, patch).let { actions ->
166-
actionPanel.removeAll()
167-
actions.forEach { button ->
168-
actionPanel.add(button)
169-
}
170-
}
158+
private fun executeAutoRepair() {
159+
applyDiffRepairSuggestionSync(myProject, oldCode, newCode, { fixedCode ->
160+
createPatchFromCode(oldCode, fixedCode)?.let { patch ->
161+
this@SingleFileDiffSketch.patch = patch
162+
appliedPatch = try {
163+
GenericPatchApplier.apply(oldCode, patch.hunks)
164+
} catch (e: Exception) {
165+
logger<SingleFileDiffSketch>().warn("Failed to apply patch: ${patch.beforeFileName}", e)
166+
null
167+
}
171168

172-
this.mainPanel.revalidate()
173-
this.mainPanel.repaint()
169+
val file = LightVirtualFile(currentFile, fixedCode, LocalTimeCounter.currentTime())
170+
createActionButtons(file, appliedPatch, patch).let { actions ->
171+
actionPanel.removeAll()
172+
actions.forEach { button ->
173+
actionPanel.add(button)
174174
}
175-
});
175+
}
176+
177+
this.mainPanel.revalidate()
178+
this.mainPanel.repaint()
176179
}
177-
}
180+
})
178181
}
179182

180183
private fun createActionButtons(
@@ -293,8 +296,10 @@ fun VirtualFile.writeText(content: String) {
293296

294297
@Throws(IOException::class)
295298
fun saveText(file: VirtualFile, text: String) {
296-
val charset = file.getCharset()
297-
file.getOutputStream(file).use { stream ->
298-
stream.write(text.toByteArray(charset))
299+
val charset = file.charset
300+
runWriteAction {
301+
file.getOutputStream(file).use { stream ->
302+
stream.write(text.toByteArray(charset))
303+
}
299304
}
300305
}

exts/ext-database/src/241/main/kotlin/cc/unitmesh/database/util/SQLExecutor.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,13 @@ object SQLExecutor {
134134
future.complete(result.toString())
135135
}
136136
}
137+
138+
override fun afterLastRowAdded(
139+
context: GridDataRequest.Context,
140+
total: Int
141+
) {
142+
future.complete(result.toString())
143+
}
137144
})
138145

139146
val request =

exts/ext-database/src/243/main/kotlin/cc/unitmesh/database/util/SQLExecutor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ object SQLExecutor {
140140
object : com.intellij.database.datagrid.DataRequest.QueryRequest(session, query,
141141
newConstraints(dataSource.dbms), null) {}
142142
messageBus.dataProducer.processRequest(request)
143-
return future.get()
143+
return future.get(5, java.util.concurrent.TimeUnit.SECONDS)
144144
}
145145

146146
private fun createConsole(project: Project, file: LightVirtualFile): JdbcConsole? {

java/src/main/kotlin/cc/unitmesh/idea/provider/JavaRefactoringTool.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ class JavaRefactoringTool : RefactoringTool {
4444
val methodName = elementInfo.methodName
4545
val className = elementInfo.className
4646

47-
val psiMethod: PsiMethod? =
47+
val psiMethod: PsiMethod? = runReadAction {
4848
psiFile.classes.firstOrNull { it.name == className }
4949
?.methods?.firstOrNull { it.name == methodName }
50+
}
5051

5152
psiMethod ?: psiFile
5253
} else {

0 commit comments

Comments
 (0)