Skip to content

Commit 94a39b3

Browse files
committed
feat(gui): enhance change management in planner results #352
- Add accept all and discard all functionality - Improve change view with diff viewer and accept/discard options- Optimize performance with runWriteAction for file modifications - Update UI to show/hide accept/discard buttons based on changes
1 parent 5206cce commit 94a39b3

File tree

1 file changed

+74
-59
lines changed

1 file changed

+74
-59
lines changed

core/src/main/kotlin/cc/unitmesh/devti/gui/planner/PlannerResultSummary.kt

Lines changed: 74 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import com.intellij.icons.AllIcons
1111
import com.intellij.openapi.actionSystem.AnAction
1212
import com.intellij.openapi.actionSystem.AnActionEvent
1313
import com.intellij.openapi.application.runInEdt
14+
import com.intellij.openapi.application.runWriteAction
1415
import com.intellij.openapi.fileEditor.FileDocumentManager
16+
import com.intellij.openapi.fileEditor.FileEditorManager
1517
import com.intellij.openapi.project.Project
1618
import com.intellij.openapi.ui.DialogWrapper
1719
import com.intellij.openapi.vcs.changes.Change
@@ -35,6 +37,26 @@ class PlannerResultSummary(
3537
private val changesPanel = JPanel(GridLayout(0, 1, 0, 1))
3638
private val statsLabel = JBLabel(AutoDevBundle.message("planner.stats.changes.empty"))
3739
private val rollbackWorker = RollbackWorker(project)
40+
private val discardAllButton = HyperlinkLabel(AutoDevBundle.message("planner.action.discard.all")).apply {
41+
icon = AllIcons.Actions.Cancel
42+
addHyperlinkListener(object : HyperlinkListener {
43+
override fun hyperlinkUpdate(e: HyperlinkEvent) {
44+
if (e.eventType == HyperlinkEvent.EventType.ACTIVATED) {
45+
globalActionListener?.onDiscardAll()
46+
}
47+
}
48+
})
49+
}
50+
private val acceptAllButton = HyperlinkLabel(AutoDevBundle.message("planner.action.accept.all")).apply {
51+
icon = AllIcons.Actions.Commit
52+
addHyperlinkListener(object : HyperlinkListener {
53+
override fun hyperlinkUpdate(e: HyperlinkEvent) {
54+
if (e.eventType == HyperlinkEvent.EventType.ACTIVATED) {
55+
globalActionListener?.onAcceptAll()
56+
}
57+
}
58+
})
59+
}
3860

3961
interface ChangeActionListener {
4062
fun onView(change: Change)
@@ -62,41 +84,10 @@ class PlannerResultSummary(
6284

6385
private var changeActionListener: ChangeActionListener = object : ChangeActionListener {
6486
override fun onView(change: Change) {
65-
change.virtualFile?.also {
66-
val diffFactory = DiffContentFactoryEx.getInstanceEx()
67-
val oldCode = change.beforeRevision?.content ?: return
68-
val newCode = change.afterRevision?.content ?: return
69-
val currentDocContent = diffFactory.create(project, oldCode)
70-
val newDocContent = diffFactory.create(newCode)
71-
72-
val diffRequest =
73-
SimpleDiffRequest("Diff", currentDocContent, newDocContent, "Original", "AI suggestion")
74-
75-
val diffViewer = SimpleDiffViewer(object : DiffContext() {
76-
override fun getProject() = this@PlannerResultSummary.project
77-
override fun isWindowFocused() = false
78-
override fun isFocusedInWindow() = false
79-
override fun requestFocusInWindow() = Unit
80-
}, diffRequest)
81-
diffViewer.init()
82-
83-
val dialog = object : DialogWrapper(project) {
84-
init {
85-
init()
86-
title = "Diff Viewer"
87-
}
88-
89-
override fun createCenterPanel(): JComponent = diffViewer.component
90-
override fun doOKAction() {
91-
super.doOKAction()
92-
changeActionListener.onAccept(change)
93-
}
94-
override fun doCancelAction() {
95-
super.doCancelAction()
96-
}
87+
runWriteAction {
88+
change.virtualFile?.also {
89+
showDiffView(change)
9790
}
98-
99-
dialog.show()
10091
}
10192
}
10293

@@ -111,7 +102,7 @@ class PlannerResultSummary(
111102
val file = change.virtualFile ?: return
112103
val content = change.afterRevision?.content ?: change.beforeRevision?.content
113104

114-
runInEdt {
105+
runWriteAction {
115106
if (content != null) {
116107
val document = FileDocumentManager.getInstance().getDocument(file)
117108
document?.setText(content)
@@ -120,6 +111,45 @@ class PlannerResultSummary(
120111
}
121112
}
122113

114+
private fun showDiffView(change: Change) {
115+
val diffFactory = DiffContentFactoryEx.getInstanceEx()
116+
val oldCode = change.beforeRevision?.content ?: return
117+
val newCode = change.afterRevision?.content ?: return
118+
val currentDocContent = diffFactory.create(project, oldCode)
119+
val newDocContent = diffFactory.create(newCode)
120+
121+
val diffRequest =
122+
SimpleDiffRequest("Diff", currentDocContent, newDocContent, "Original", "AI suggestion")
123+
124+
val diffViewer = SimpleDiffViewer(object : DiffContext() {
125+
override fun getProject() = this@PlannerResultSummary.project
126+
override fun isWindowFocused() = false
127+
override fun isFocusedInWindow() = false
128+
override fun requestFocusInWindow() = Unit
129+
}, diffRequest)
130+
diffViewer.init()
131+
132+
val dialog = object : DialogWrapper(project) {
133+
init {
134+
init()
135+
title = "Diff Viewer"
136+
setOKButtonText("Apply")
137+
}
138+
139+
override fun createCenterPanel(): JComponent = diffViewer.component
140+
override fun doOKAction() {
141+
super.doOKAction()
142+
changeActionListener.onAccept(change)
143+
}
144+
145+
override fun doCancelAction() {
146+
super.doCancelAction()
147+
}
148+
}
149+
150+
dialog.show()
151+
}
152+
123153
init {
124154
border = JBUI.Borders.customLine(UIUtil.getBoundsColor(), 1, 0, 0, 0)
125155

@@ -138,28 +168,6 @@ class PlannerResultSummary(
138168
}
139169

140170
val actionsPanel = JPanel(FlowLayout(FlowLayout.RIGHT, 4, 0)).apply {
141-
val discardAllButton = HyperlinkLabel(AutoDevBundle.message("planner.action.discard.all")).apply {
142-
icon = AllIcons.Actions.Cancel
143-
addHyperlinkListener(object : HyperlinkListener {
144-
override fun hyperlinkUpdate(e: HyperlinkEvent) {
145-
if (e.eventType == HyperlinkEvent.EventType.ACTIVATED) {
146-
globalActionListener?.onDiscardAll()
147-
}
148-
}
149-
})
150-
}
151-
152-
val acceptAllButton = HyperlinkLabel(AutoDevBundle.message("planner.action.accept.all")).apply {
153-
icon = AllIcons.Actions.Commit
154-
addHyperlinkListener(object : HyperlinkListener {
155-
override fun hyperlinkUpdate(e: HyperlinkEvent) {
156-
if (e.eventType == HyperlinkEvent.EventType.ACTIVATED) {
157-
globalActionListener?.onAcceptAll()
158-
}
159-
}
160-
})
161-
}
162-
163171
add(discardAllButton)
164172
add(acceptAllButton)
165173
}
@@ -193,6 +201,9 @@ class PlannerResultSummary(
193201
foreground = UIUtil.getLabelDisabledForeground()
194202
border = JBUI.Borders.empty(8)
195203
})
204+
205+
discardAllButton.isVisible = false
206+
acceptAllButton.isVisible = false
196207
} else {
197208
statsLabel.text = " - " + AutoDevBundle.message("planner.stats.changes.count", changes.size)
198209
changes.forEach { change ->
@@ -202,6 +213,9 @@ class PlannerResultSummary(
202213
val changePanel = createChangeItemPanel(change, fileName, filePath)
203214
changesPanel.add(changePanel)
204215
}
216+
217+
discardAllButton.isVisible = true
218+
acceptAllButton.isVisible = true
205219
}
206220

207221
changesPanel.revalidate()
@@ -232,7 +246,9 @@ class PlannerResultSummary(
232246
addHyperlinkListener(object : HyperlinkListener {
233247
override fun hyperlinkUpdate(e: HyperlinkEvent) {
234248
if (e.eventType == HyperlinkEvent.EventType.ACTIVATED) {
235-
changeActionListener.onView(change)
249+
change.virtualFile?.also {
250+
FileEditorManager.getInstance(project).openFile(it, true)
251+
}
236252
}
237253
}
238254
})
@@ -293,4 +309,3 @@ class PlannerResultSummary(
293309
return KeyboardAccessibleActionButton(anAction)
294310
}
295311
}
296-

0 commit comments

Comments
 (0)