Skip to content

Commit 811342e

Browse files
committed
refactor(rename): handle exceptions and improve logging for rename suggestions #129
This commit refactors the `RenameLookupManagerListener` class to handle exceptions more gracefully and improve logging during the rename suggestion process. It introduces a new `cancellable()` extension function on `Flow` to support cancellation of the string collection job, and ensures that the `lookupImpl` refresh and calculation are only performed when the lookup is not disposed and the coroutine job is active. Additionally, the commit fixes a bug where the string job was not being cancelled when the lookup was cancelled.
1 parent ce20d91 commit 811342e

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/main/kotlin/cc/unitmesh/devti/practise/RenameLookupManagerListener.kt

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ import com.intellij.psi.PsiNameIdentifierOwner
1818
import com.intellij.psi.PsiWhiteSpace
1919
import com.intellij.psi.impl.source.tree.LeafPsiElement
2020
import com.intellij.psi.util.PsiEditorUtil
21-
import kotlinx.coroutines.cancel
21+
import kotlinx.coroutines.*
2222
import kotlinx.coroutines.flow.*
23-
import kotlinx.coroutines.launch
2423

2524
class RenameLookupManagerListener(val project: Project) : LookupManagerListener {
2625
private val llm = LlmFactory.instance.create(project)
@@ -55,25 +54,32 @@ class RenameLookupManagerListener(val project: Project) : LookupManagerListener
5554
val stringJob = LLMCoroutineScope.scope(project).launch {
5655
AutoDevStatusService.notifyApplication(AutoDevStatus.InProgress)
5756

57+
val runJob = currentCoroutineContext().job
5858
try {
5959
val stringFlow: Flow<String> = llm.stream(promptText, "", false)
6060
val sb = StringBuilder()
61-
stringFlow.collect {
61+
62+
stringFlow.cancellable().collect {
63+
if (runJob.job.isCancelled) {
64+
currentCoroutineContext().job.cancel()
65+
}
6266
sb.append(it)
6367
}
6468
val result = sb.toString()
6569
logger.info("result: $result")
6670
extractSuggestionsFromString(result).filter { it.isNotBlank() }.map {
6771
runReadAction {
68-
if (!lookupImpl.isLookupDisposed) {
72+
if (!lookupImpl.isLookupDisposed && runJob.isActive) {
6973
lookupImpl.addItem(CustomRenameLookupElement(it), PrefixMatcher.ALWAYS_TRUE)
7074
}
7175
}
7276
}
7377

7478
runInEdt {
75-
lookupImpl.isCalculating = false
76-
lookupImpl.refreshUi(true, false)
79+
if (!lookupImpl.isLookupDisposed && runJob.isActive) {
80+
lookupImpl.isCalculating = false
81+
lookupImpl.refreshUi(true, false)
82+
}
7783
}
7884
} catch (e: Exception) {
7985
AutoDevStatusService.notifyApplication(AutoDevStatus.Error)
@@ -86,7 +92,6 @@ class RenameLookupManagerListener(val project: Project) : LookupManagerListener
8692
lookupImpl.addLookupListener(object : LookupListener {
8793
override fun lookupCanceled(event: LookupEvent) {
8894
AutoDevStatusService.notifyApplication(AutoDevStatus.Ready)
89-
stringJob.cancel()
9095
}
9196
})
9297

0 commit comments

Comments
 (0)