Skip to content

Commit e6ec4de

Browse files
committed
feat(chat): improve refactoring support by adding post-action support and code completion #129
This commitfeat(chat): enhances the refact improveoring refactoring support by adding post-action support and code completion capabilities of theThis commit enhances chat coding panel the by ref introducingact aoring new capabilities post of- theaction chat support coding feature panel and by ref introducingact aoring new this post action-.action The support post feature- andaction ref supportact allowsoring for this custom action actions. to The be post performed- afteraction code feature completion allows, for providing custom a actions more to flexible be and performed customizable after ref codeact completionoring, experience providing a. Additionally more, flexible the and code customizable completion ref featureact hasoring experience been improved. to Additionally provide, more the accurate code completion and functionality relevant suggestions has been to the improved user to provide. more These changes accurate and result relevant in suggestions a to more the efficient and user. user These- changesfriendly collectively ref resultact inoring a process more within robust the and chat user coding- panelfriendly. refactoring experience within the chat coding panel.
1 parent 18818dc commit e6ec4de

File tree

7 files changed

+44
-41
lines changed

7 files changed

+44
-41
lines changed

src/main/kotlin/cc/unitmesh/devti/AutoDevIcons.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,7 @@ object AutoDevIcons {
4040

4141
@JvmField
4242
val InsertCode: Icon = IconLoader.getIcon("/icons/insert-code.svg", AutoDevIcons::class.java)
43+
44+
@JvmField
45+
val Idea: Icon = IconLoader.getIcon("/icons/idea.svg", AutoDevIcons::class.java)
4346
}

src/main/kotlin/cc/unitmesh/devti/actions/chat/RefactorThisAction.kt

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,20 @@ import cc.unitmesh.devti.AutoDevBundle
44
import cc.unitmesh.devti.actions.chat.base.ChatBaseAction
55
import cc.unitmesh.devti.gui.chat.ChatActionType
66
import cc.unitmesh.devti.gui.chat.ChatCodingPanel
7-
import cc.unitmesh.devti.util.parser.Code
87
import com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerEx
98
import com.intellij.lang.LanguageCommenters
109
import com.intellij.openapi.actionSystem.AnActionEvent
1110
import com.intellij.openapi.actionSystem.CommonDataKeys
12-
import com.intellij.openapi.command.WriteCommandAction
1311
import com.intellij.openapi.editor.Editor
1412
import com.intellij.openapi.project.Project
1513
import com.intellij.psi.PsiElement
1614

1715
class RefactorThisAction : ChatBaseAction() {
18-
init{
16+
init {
1917
val presentation = getTemplatePresentation()
2018
presentation.text = AutoDevBundle.message("settings.autodev.rightClick.refactor")
2119
}
20+
2221
override fun getActionType(): ChatActionType = ChatActionType.REFACTOR
2322
override fun update(e: AnActionEvent) {
2423
val editor = e.getData(CommonDataKeys.EDITOR)
@@ -42,7 +41,7 @@ class RefactorThisAction : ChatBaseAction() {
4241
val commentSymbol = commentPrefix(element)
4342

4443
return collectProblems(project, editor, element)?.let {
45-
"\n\n$commentSymbol relative static analysis result: \n\n$it"
44+
"\n\n$commentSymbol relative static analysis result:\n$it"
4645
} ?: ""
4746
}
4847

@@ -93,25 +92,12 @@ class RefactorThisAction : ChatBaseAction() {
9392
)
9493

9594
override fun chatCompletedPostAction(event: AnActionEvent, panel: ChatCodingPanel): (response: String) -> Unit {
96-
val editor = event.getRequiredData(CommonDataKeys.EDITOR)
97-
val project = event.getRequiredData(CommonDataKeys.PROJECT)
98-
val document = editor.document
99-
100-
val primaryCaret = editor.caretModel.primaryCaret;
101-
val start = primaryCaret.selectionStart;
102-
val end = primaryCaret.selectionEnd
103-
10495
// get random key from refactorIntentionsKeys
10596
val key = refactorIntentionsKeys.random()
10697
val msg = AutoDevBundle.message(key)
10798

108-
return { response ->
99+
return {
109100
panel.showSuggestion(msg)
110-
111-
val code = Code.parse(response).text
112-
WriteCommandAction.runWriteCommandAction(project) {
113-
document.replaceString(start, end, code)
114-
}
115101
}
116102
}
117103
}

src/main/kotlin/cc/unitmesh/devti/actions/chat/base/ChatBaseAction.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ abstract class ChatBaseAction : AnAction() {
5454
prompt += addAdditionPrompt(project, editor, element)
5555
prompter.initContext(getActionType(), prompt, file, project, caretModel?.offset ?: 0, element)
5656

57-
sendToChatPanel(project) { panel: ChatCodingPanel, service ->
57+
sendToChatPanel(project, getActionType()) { panel: ChatCodingPanel, service ->
5858
val chatContext = ChatContext(
5959
chatCompletedPostAction(event, panel),
6060
prompt,

src/main/kotlin/cc/unitmesh/devti/gui/SendToWindow.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ fun sendToChatPanel(project: Project, runnable: (ChatCodingPanel, ChatCodingServ
3939
sendToChatWindow(project, actionType, runnable)
4040
}
4141

42+
fun sendToChatPanel(project: Project, actionType: ChatActionType, runnable: (ChatCodingPanel, ChatCodingService) -> Unit) {
43+
sendToChatWindow(project, actionType, runnable)
44+
}
45+
4246
fun sendToChatPanel(project: Project, actionType: ChatActionType, prompter: ContextPrompter) {
4347
sendToChatWindow(project, actionType) { contentPanel, chatCodingService ->
4448
chatCodingService.handlePromptAndResponse(contentPanel, prompter, newChatContext = true)

src/main/kotlin/cc/unitmesh/devti/gui/chat/ChatCodingPanel.kt

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
package cc.unitmesh.devti.gui.chat
22

3-
import cc.unitmesh.devti.AutoDevBundle
4-
import cc.unitmesh.devti.alignRight
3+
import cc.unitmesh.devti.*
54
import cc.unitmesh.devti.agent.model.CustomAgentConfig
65
import cc.unitmesh.devti.agent.view.WebBlock
76
import cc.unitmesh.devti.agent.view.WebBlockView
8-
import cc.unitmesh.devti.fullHeight
9-
import cc.unitmesh.devti.fullWidth
107
import cc.unitmesh.devti.gui.chat.welcome.WelcomePanel
118
import cc.unitmesh.devti.provider.ContextPrompter
129
import cc.unitmesh.devti.provider.devins.LanguagePromptProcessor
@@ -28,6 +25,7 @@ import com.intellij.ui.JBColor
2825
import com.intellij.ui.components.JBLabel
2926
import com.intellij.ui.components.JBScrollPane
3027
import com.intellij.ui.components.panels.VerticalLayout
28+
import com.intellij.ui.dsl.builder.RightGap
3129
import com.intellij.ui.dsl.builder.panel
3230
import com.intellij.util.ui.JBFont
3331
import com.intellij.util.ui.JBUI
@@ -38,7 +36,6 @@ import kotlinx.coroutines.flow.*
3836
import kotlinx.coroutines.withContext
3937
import org.jetbrains.annotations.Nls
4038
import java.awt.BorderLayout
41-
import java.awt.event.ActionListener
4239
import java.awt.event.MouseAdapter
4340
import java.awt.event.MouseEvent
4441
import javax.swing.*
@@ -55,8 +52,7 @@ class ChatCodingPanel(private val chatCodingService: ChatCodingService, val disp
5552
private val focusMouseListener: MouseAdapter
5653
private var panelContent: DialogPanel
5754
private val myScrollPane: JBScrollPane
58-
private val delaySeconds: String
59-
get() = AutoDevSettingsState.getInstance().delaySeconds
55+
private val delaySeconds: String get() = AutoDevSettingsState.getInstance().delaySeconds
6056

6157
private var suggestionPanel: JPanel = JPanel(BorderLayout())
6258

@@ -198,24 +194,18 @@ class ChatCodingPanel(private val chatCodingService: ChatCodingService, val disp
198194
* Updates the replaceable content in the UI using the provided `Flow<String>`.
199195
*
200196
* @param content The flow of strings to update the UI with.
201-
* @param replaceSelectedText A function that is called when the "Replace Selection" button is clicked,
197+
* @param postAction A function that is called when the "Replace Selection" button is clicked,
202198
* passing the current text to be replaced in the editor.
203199
*/
204-
suspend fun updateReplaceableContent(content: Flow<String>, replaceSelectedText: (text: String) -> Unit) {
200+
suspend fun updateReplaceableContent(content: Flow<String>, postAction: (text: String) -> Unit) {
205201
myList.remove(myList.componentCount - 1)
206202
val text = updateMessageInUi(content)
207203

208-
val jButton = JButton(AutoDevBundle.message("chat.panel.replaceSelection"))
209-
val listener = ActionListener {
210-
replaceSelectedText(text)
211-
myList.remove(myList.componentCount - 1)
212-
}
213-
jButton.addActionListener(listener)
214-
myList.add(jButton)
215-
216204
progressBar.isIndeterminate = false
217205
progressBar.isVisible = false
218206
updateUI()
207+
208+
postAction(text)
219209
}
220210

221211
private suspend fun updateMessageInUi(content: Flow<String>): String {
@@ -310,7 +300,8 @@ class ChatCodingPanel(private val chatCodingService: ChatCodingService, val disp
310300
fun showSuggestion(msg: @Nls String) {
311301
val label = panel {
312302
row {
313-
cell(JBLabel(msg)).fullWidth().also {
303+
icon(AutoDevIcons.Idea).gap(RightGap.SMALL)
304+
text(msg).also {
314305
it.component.addMouseListener(object : MouseAdapter() {
315306
override fun mouseClicked(e: MouseEvent?) {
316307
inputSection.text = msg

src/main/kotlin/cc/unitmesh/devti/gui/chat/ChatCodingService.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,6 @@ class ChatCodingService(var actionType: ChatActionType, val project: Project) {
6565
context?.postAction?.invoke(it)
6666
}
6767

68-
actionType === ChatActionType.CODE_COMPLETE -> ui.updateReplaceableContent(response) {
69-
context?.postAction?.invoke(getCodeSection(it, context.prefixText, context.suffixText))
70-
}
71-
7268
else -> ui.updateMessage(response)
7369
}
7470
}

src/main/resources/icons/idea.svg

Lines changed: 23 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)