Skip to content

Commit 82caa05

Browse files
committed
fix(refactor): refactor rename lookup manager listener to use custom rename lookup element #129
The rename lookup manager listener has been refactored to use a custom rename lookup element class, which improves the handling of insertions and the rendering of lookup elements with the correct icon. This change also includes the creation of a new file for the custom rename lookup element class, which is now located in the `cc.unitmesh.devti.practise` package.
1 parent 559edb3 commit 82caa05

File tree

2 files changed

+30
-25
lines changed

2 files changed

+30
-25
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package cc.unitmesh.devti.practise
2+
3+
import cc.unitmesh.devti.AutoDevIcons
4+
import com.intellij.codeInsight.completion.InsertionContext
5+
import com.intellij.codeInsight.lookup.LookupElement
6+
import com.intellij.codeInsight.lookup.LookupElementPresentation
7+
import com.intellij.codeInsight.template.impl.TemplateManagerImpl
8+
9+
class CustomRenameLookupElement(val name: String) : LookupElement() {
10+
override fun getLookupString(): String = name
11+
12+
override fun handleInsert(context: InsertionContext) {
13+
val editor = context.editor
14+
val templateState = TemplateManagerImpl.getTemplateState(editor)
15+
16+
if (templateState != null && !templateState.isFinished) {
17+
templateState.considerNextTabOnLookupItemSelected(this)
18+
}
19+
}
20+
21+
override fun renderElement(presentation: LookupElementPresentation) {
22+
presentation.icon = AutoDevIcons.Idea
23+
super.renderElement(presentation)
24+
}
25+
}

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

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
package cc.unitmesh.devti.practise
22

3-
import cc.unitmesh.devti.AutoDevIcons
43
import cc.unitmesh.devti.llms.LlmFactory
54
import cc.unitmesh.devti.settings.coder.coderSetting
65
import cc.unitmesh.devti.statusbar.AutoDevStatus
76
import cc.unitmesh.devti.statusbar.AutoDevStatusService
87
import cc.unitmesh.devti.util.LLMCoroutineScope
9-
import com.intellij.codeInsight.completion.InsertionContext
108
import com.intellij.codeInsight.completion.PrefixMatcher
119
import com.intellij.codeInsight.lookup.*
1210
import com.intellij.codeInsight.lookup.impl.LookupImpl
13-
import com.intellij.codeInsight.template.impl.TemplateManagerImpl
1411
import com.intellij.openapi.application.runInEdt
1512
import com.intellij.openapi.application.runReadAction
1613
import com.intellij.openapi.diagnostic.logger
@@ -71,9 +68,11 @@ class RenameLookupManagerListener(val project: Project) : LookupManagerListener
7168
}
7269
val result = sb.toString()
7370
logger.info("result: $result")
74-
parseSuggestions(result).filter { it.isNotBlank() }.map {
71+
extractSuggestionsFromString(result).filter { it.isNotBlank() }.map {
7572
runReadAction {
76-
lookupImpl.addItem(CustomRenameLookupElement(it), PrefixMatcher.ALWAYS_TRUE)
73+
if (!lookupImpl.isLookupDisposed) {
74+
lookupImpl.addItem(CustomRenameLookupElement(it), PrefixMatcher.ALWAYS_TRUE)
75+
}
7776
}
7877
}
7978

@@ -99,29 +98,10 @@ class RenameLookupManagerListener(val project: Project) : LookupManagerListener
9998
stringJob.start()
10099
}
101100

102-
private fun parseSuggestions(result: String) = result.split("\n").map {
101+
private fun extractSuggestionsFromString(result: String) = result.split("\n").map {
103102
it.replace(Regex("^\\d+\\."), "")
104103
.trim()
105104
.removeSurrounding("`")
106105
.removeSuffix("()")
107106
}
108-
109-
}
110-
111-
class CustomRenameLookupElement(val name: String) : LookupElement() {
112-
override fun getLookupString(): String = name
113-
114-
override fun handleInsert(context: InsertionContext) {
115-
val editor = context.editor
116-
val templateState = TemplateManagerImpl.getTemplateState(editor)
117-
118-
if (templateState != null && !templateState.isFinished) {
119-
templateState.considerNextTabOnLookupItemSelected(this)
120-
}
121-
}
122-
123-
override fun renderElement(presentation: LookupElementPresentation) {
124-
presentation.icon = AutoDevIcons.Idea
125-
super.renderElement(presentation)
126-
}
127107
}

0 commit comments

Comments
 (0)