Skip to content

Commit

Permalink
feat(refactoring): improve RenameLookupManagerListener by adding Rena…
Browse files Browse the repository at this point in the history
…meLookupElement and using ApplicationManager to invoke the stream function in a non-blocking way, resolving #132.
  • Loading branch information
phodal committed Apr 2, 2024
1 parent 44008dc commit baf8809
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Expand Up @@ -7,7 +7,7 @@ pluginGroup = com.phodal.autodev
pluginName = AutoDev
pluginRepositoryUrl = https://github.com/unit-mesh/auto-dev
# SemVer format -> https://semver.org
pluginVersion = 1.7.5
pluginVersion = 1.8.0-SNAPSHOT

# Supported IDEs: idea, pycharm
baseIDE=idea
Expand Down
Expand Up @@ -6,6 +6,7 @@ import cc.unitmesh.devti.settings.coder.coderSetting
import com.intellij.codeInsight.completion.PrefixMatcher
import com.intellij.codeInsight.lookup.*
import com.intellij.codeInsight.lookup.impl.LookupImpl
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiWhiteSpace
import com.intellij.psi.impl.source.tree.LeafPsiElement
Expand Down Expand Up @@ -33,37 +34,42 @@ class RenameLookupManagerListener(val project: Project) : LookupManagerListener

val name = parentClass?.name ?: element.text

val prompt = runBlocking {
val stringFlow: Flow<String> = llm.stream(
"""$name is a badname. Please provide 5 better options name for follow code:
val promptText = """$name is a badname. Please provide 5 better options name for follow code:
```${element.language.displayName}
${element.text}
```
1.
""".trimIndent(),
"",
false
)

val sb = StringBuilder()
stringFlow.collect {
sb.append(it)
""".trimIndent()


ApplicationManager.getApplication().invokeLater {
runBlocking {
val stringFlow: Flow<String> = llm.stream(
promptText,
"",
false
)

val sb = StringBuilder()
stringFlow.collect {
sb.append(it)
}

val result = sb.toString()

// the prompt will be list format, split with \n and remove start number with regex
val suggestionNames = result.split("\n").map {
it.replace(Regex("^\\d+\\."), "")
.trim()
.removeSurrounding("`")
}

suggestionNames.map {
lookupImpl.addItem(RenameLookupElement(it), PrefixMatcher.ALWAYS_TRUE)
}
}

sb.toString()
}

// the prompt will be list format, split with \n and remove start number with regex
val suggestionNames = prompt.split("\n").map {
it.replace(Regex("^\\d+\\."), "")
.trim()
.removeSurrounding("`")
}

suggestionNames.map {
lookupImpl.addItem(RenameLookupElement(it), PrefixMatcher.ALWAYS_TRUE)
}
}
}
Expand All @@ -75,7 +81,3 @@ class RenameLookupElement(val name: String) : LookupElement() {
super.renderElement(presentation)
}
}

class RenameLookupListener : LookupListener {

}

0 comments on commit baf8809

Please sign in to comment.