@@ -11,7 +11,9 @@ import com.intellij.icons.AllIcons
11
11
import com.intellij.openapi.actionSystem.AnAction
12
12
import com.intellij.openapi.actionSystem.AnActionEvent
13
13
import com.intellij.openapi.application.runInEdt
14
+ import com.intellij.openapi.application.runWriteAction
14
15
import com.intellij.openapi.fileEditor.FileDocumentManager
16
+ import com.intellij.openapi.fileEditor.FileEditorManager
15
17
import com.intellij.openapi.project.Project
16
18
import com.intellij.openapi.ui.DialogWrapper
17
19
import com.intellij.openapi.vcs.changes.Change
@@ -35,6 +37,26 @@ class PlannerResultSummary(
35
37
private val changesPanel = JPanel (GridLayout (0 , 1 , 0 , 1 ))
36
38
private val statsLabel = JBLabel (AutoDevBundle .message(" planner.stats.changes.empty" ))
37
39
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
+ }
38
60
39
61
interface ChangeActionListener {
40
62
fun onView (change : Change )
@@ -62,41 +84,10 @@ class PlannerResultSummary(
62
84
63
85
private var changeActionListener: ChangeActionListener = object : ChangeActionListener {
64
86
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)
97
90
}
98
-
99
- dialog.show()
100
91
}
101
92
}
102
93
@@ -111,7 +102,7 @@ class PlannerResultSummary(
111
102
val file = change.virtualFile ? : return
112
103
val content = change.afterRevision?.content ? : change.beforeRevision?.content
113
104
114
- runInEdt {
105
+ runWriteAction {
115
106
if (content != null ) {
116
107
val document = FileDocumentManager .getInstance().getDocument(file)
117
108
document?.setText(content)
@@ -120,6 +111,45 @@ class PlannerResultSummary(
120
111
}
121
112
}
122
113
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
+
123
153
init {
124
154
border = JBUI .Borders .customLine(UIUtil .getBoundsColor(), 1 , 0 , 0 , 0 )
125
155
@@ -138,28 +168,6 @@ class PlannerResultSummary(
138
168
}
139
169
140
170
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
-
163
171
add(discardAllButton)
164
172
add(acceptAllButton)
165
173
}
@@ -193,6 +201,9 @@ class PlannerResultSummary(
193
201
foreground = UIUtil .getLabelDisabledForeground()
194
202
border = JBUI .Borders .empty(8 )
195
203
})
204
+
205
+ discardAllButton.isVisible = false
206
+ acceptAllButton.isVisible = false
196
207
} else {
197
208
statsLabel.text = " - " + AutoDevBundle .message(" planner.stats.changes.count" , changes.size)
198
209
changes.forEach { change ->
@@ -202,6 +213,9 @@ class PlannerResultSummary(
202
213
val changePanel = createChangeItemPanel(change, fileName, filePath)
203
214
changesPanel.add(changePanel)
204
215
}
216
+
217
+ discardAllButton.isVisible = true
218
+ acceptAllButton.isVisible = true
205
219
}
206
220
207
221
changesPanel.revalidate()
@@ -232,7 +246,9 @@ class PlannerResultSummary(
232
246
addHyperlinkListener(object : HyperlinkListener {
233
247
override fun hyperlinkUpdate (e : HyperlinkEvent ) {
234
248
if (e.eventType == HyperlinkEvent .EventType .ACTIVATED ) {
235
- changeActionListener.onView(change)
249
+ change.virtualFile?.also {
250
+ FileEditorManager .getInstance(project).openFile(it, true )
251
+ }
236
252
}
237
253
}
238
254
})
@@ -293,4 +309,3 @@ class PlannerResultSummary(
293
309
return KeyboardAccessibleActionButton (anAction)
294
310
}
295
311
}
296
-
0 commit comments