Skip to content

Commit 18818dc

Browse files
committed
feat(refactor): add post-action support for refactoring and code completion #129
This commit introduces a new `postAction` field to the `ChatContext` class, which allows for custom actions to be performed after a refactoring or code completion suggestion is applied. The `ChatCodingPanel` now includes a `SuggestionPanel` to display the suggested code, and the `ChatCodingService` has been updated to invoke the `postAction` callback instead of the deprecated `replaceSelectedText` callback. Additionally, the `RefactorThisAction` now includes a more detailed static analysis result and uses a random key from the `refactorIntentionsKeys` to generate a suggestion message.
1 parent c2d0d3c commit 18818dc

File tree

5 files changed

+41
-6
lines changed

5 files changed

+41
-6
lines changed

docs/features/chat-with-code.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ permalink: /features/chat-width-code
1515

1616
## Refactor this
1717

18+
In [#129](https://github.com/unit-mesh/auto-dev/issues/129), we provide more information for you to refactor code.
19+
1820
## Chat with this
1921

2022
## Write test for this

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ 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
78
import com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerEx
89
import com.intellij.lang.LanguageCommenters
910
import com.intellij.openapi.actionSystem.AnActionEvent
@@ -13,7 +14,6 @@ import com.intellij.openapi.editor.Editor
1314
import com.intellij.openapi.project.Project
1415
import com.intellij.psi.PsiElement
1516

16-
1717
class RefactorThisAction : ChatBaseAction() {
1818
init{
1919
val presentation = getTemplatePresentation()
@@ -42,7 +42,7 @@ class RefactorThisAction : ChatBaseAction() {
4242
val commentSymbol = commentPrefix(element)
4343

4444
return collectProblems(project, editor, element)?.let {
45-
"\n\n$commentSymbol relative static analysis result: $it"
45+
"\n\n$commentSymbol relative static analysis result: \n\n$it"
4646
} ?: ""
4747
}
4848

@@ -101,9 +101,16 @@ class RefactorThisAction : ChatBaseAction() {
101101
val start = primaryCaret.selectionStart;
102102
val end = primaryCaret.selectionEnd
103103

104+
// get random key from refactorIntentionsKeys
105+
val key = refactorIntentionsKeys.random()
106+
val msg = AutoDevBundle.message(key)
107+
104108
return { response ->
109+
panel.showSuggestion(msg)
110+
111+
val code = Code.parse(response).text
105112
WriteCommandAction.runWriteCommandAction(project) {
106-
document.replaceString(start, end, response)
113+
document.replaceString(start, end, code)
107114
}
108115
}
109116
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import kotlinx.coroutines.Dispatchers
3636
import kotlinx.coroutines.delay
3737
import kotlinx.coroutines.flow.*
3838
import kotlinx.coroutines.withContext
39+
import org.jetbrains.annotations.Nls
40+
import java.awt.BorderLayout
3941
import java.awt.event.ActionListener
4042
import java.awt.event.MouseAdapter
4143
import java.awt.event.MouseEvent
@@ -56,6 +58,8 @@ class ChatCodingPanel(private val chatCodingService: ChatCodingService, val disp
5658
private val delaySeconds: String
5759
get() = AutoDevSettingsState.getInstance().delaySeconds
5860

61+
private var suggestionPanel: JPanel = JPanel(BorderLayout())
62+
5963
init {
6064
focusMouseListener = object : MouseAdapter() {
6165
override fun mouseClicked(e: MouseEvent?) {
@@ -115,6 +119,7 @@ class ChatCodingPanel(private val chatCodingService: ChatCodingService, val disp
115119

116120
panelContent = panel {
117121
row { cell(myScrollPane).fullWidth().fullHeight() }.resizableRow()
122+
row { cell(suggestionPanel).fullWidth() }
118123
row { cell(progressBar).fullWidth() }
119124
row { cell(actionLink).alignRight() }
120125
row {
@@ -301,5 +306,26 @@ class ChatCodingPanel(private val chatCodingService: ChatCodingService, val disp
301306
fun moveCursorToStart() {
302307
inputSection.moveCursorToStart()
303308
}
309+
310+
fun showSuggestion(msg: @Nls String) {
311+
val label = panel {
312+
row {
313+
cell(JBLabel(msg)).fullWidth().also {
314+
it.component.addMouseListener(object : MouseAdapter() {
315+
override fun mouseClicked(e: MouseEvent?) {
316+
inputSection.text = msg
317+
inputSection.requestFocus()
318+
319+
suggestionPanel.removeAll()
320+
updateUI()
321+
}
322+
})
323+
}
324+
}
325+
}
326+
327+
suggestionPanel.add(label)
328+
updateUI()
329+
}
304330
}
305331

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ class ChatCodingService(var actionType: ChatActionType, val project: Project) {
6262
LLMCoroutineScope.scope(project).launch {
6363
when {
6464
actionType === ChatActionType.REFACTOR -> ui.updateReplaceableContent(response) {
65-
context?.replaceSelectedText?.invoke(getCodeSection(it, context.prefixText, context.suffixText))
65+
context?.postAction?.invoke(it)
6666
}
6767

6868
actionType === ChatActionType.CODE_COMPLETE -> ui.updateReplaceableContent(response) {
69-
context?.replaceSelectedText?.invoke(getCodeSection(it, context.prefixText, context.suffixText))
69+
context?.postAction?.invoke(getCodeSection(it, context.prefixText, context.suffixText))
7070
}
7171

7272
else -> ui.updateMessage(response)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package cc.unitmesh.devti.gui.chat
22

33
data class ChatContext(
4-
val replaceSelectedText: ((response: String) -> Unit)? = null,
4+
val postAction: ((response: String) -> Unit)? = null,
55
val prefixText: String,
66
val suffixText: String
77
)

0 commit comments

Comments
 (0)