From 3324a79cb38f24abdba65f1316e80714b3937f1c Mon Sep 17 00:00:00 2001 From: Phodal Huang Date: Wed, 3 Apr 2024 13:57:44 +0800 Subject: [PATCH] feat(rename-suggestion): improve rename suggestion logic and add logging #132 This commit enhances the rename suggestion feature by introducing a more robust logic and adds logging capabilities. The changes include: - Implementing `AutoDevStatusService` to notify the application status. - Adding try-catch blocks to handle exceptions gracefully. - Modifying the string flow collection and suggestion parsing logic. - Updating the `LookupListener` to handle lookup cancellation. These improvements ensure a smoother and more reliable rename suggestion experience, with better error handling and status updates. --- .../practise/RenameLookupManagerListener.kt | 43 ++++++++++++------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/src/main/kotlin/cc/unitmesh/devti/practise/RenameLookupManagerListener.kt b/src/main/kotlin/cc/unitmesh/devti/practise/RenameLookupManagerListener.kt index 7f64861ef0..2bef1bbbd4 100644 --- a/src/main/kotlin/cc/unitmesh/devti/practise/RenameLookupManagerListener.kt +++ b/src/main/kotlin/cc/unitmesh/devti/practise/RenameLookupManagerListener.kt @@ -3,6 +3,8 @@ package cc.unitmesh.devti.practise import cc.unitmesh.devti.AutoDevIcons import cc.unitmesh.devti.llms.LlmFactory import cc.unitmesh.devti.settings.coder.coderSetting +import cc.unitmesh.devti.statusbar.AutoDevStatus +import cc.unitmesh.devti.statusbar.AutoDevStatusService import cc.unitmesh.devti.util.LLMCoroutineScope import com.intellij.codeInsight.completion.InsertionContext import com.intellij.codeInsight.completion.PrefixMatcher @@ -58,30 +60,39 @@ class RenameLookupManagerListener(val project: Project) : LookupManagerListener val stringJob = LLMCoroutineScope.scope(project).launch { - val stringFlow: Flow = llm.stream(promptText, "", false) - val sb = StringBuilder() - stringFlow.collect { - sb.append(it) - } + AutoDevStatusService.notifyApplication(AutoDevStatus.InProgress) - val result = sb.toString() - logger.info("result: $result") - parseSuggestions(result) - .filter { it.isNotBlank() } - .map { - runReadAction { - lookupImpl.addItem(RenameLookupElement(it), PrefixMatcher.ALWAYS_TRUE) - } + try { + val stringFlow: Flow = llm.stream(promptText, "", false) + val sb = StringBuilder() + stringFlow.collect { + sb.append(it) } + val result = sb.toString() + logger.info("result: $result") + parseSuggestions(result) + .filter { it.isNotBlank() } + .map { + runReadAction { + lookupImpl.addItem(RenameLookupElement(it), PrefixMatcher.ALWAYS_TRUE) + } + } - runInEdt { - lookupImpl.isCalculating = false - lookupImpl.refreshUi(true, false) + runInEdt { + lookupImpl.isCalculating = false + lookupImpl.refreshUi(true, false) + } + } catch (e: Exception) { + AutoDevStatusService.notifyApplication(AutoDevStatus.Error) + logger.error("Error in RenameLookupManagerListener", e) } + + AutoDevStatusService.notifyApplication(AutoDevStatus.Ready) } lookupImpl.addLookupListener(object : LookupListener { override fun lookupCanceled(event: LookupEvent) { + AutoDevStatusService.notifyApplication(AutoDevStatus.Ready) stringJob.cancel() } })