1
1
package cc.unitmesh.devti.sketch.ui.patch
2
2
3
3
import cc.unitmesh.devti.AutoDevBundle
4
+ import cc.unitmesh.devti.AutoDevNotifications
5
+ import cc.unitmesh.devti.diff.DiffStreamHandler
4
6
import com.intellij.diff.DiffContentFactoryEx
5
7
import com.intellij.diff.chains.SimpleDiffRequestChain
6
8
import com.intellij.diff.chains.SimpleDiffRequestProducer
@@ -26,8 +28,17 @@ import com.intellij.ui.dsl.builder.AlignX
26
28
import com.intellij.ui.dsl.builder.RightGap
27
29
import com.intellij.ui.dsl.builder.panel
28
30
import cc.unitmesh.devti.sketch.ui.LangSketch
31
+ import cc.unitmesh.devti.util.findFile
32
+ import com.intellij.openapi.command.CommandProcessor
33
+ import com.intellij.openapi.command.UndoConfirmationPolicy
34
+ import com.intellij.openapi.diff.impl.patch.ApplyPatchStatus
35
+ import com.intellij.openapi.vcs.changes.patch.AbstractFilePatchInProgress
36
+ import com.intellij.openapi.vcs.changes.patch.MatchPatchPaths
29
37
import com.intellij.openapi.vfs.VfsUtilCore
38
+ import com.intellij.psi.PsiDocumentManager
39
+ import com.intellij.util.containers.MultiMap
30
40
import java.awt.BorderLayout
41
+ import java.awt.Dimension
31
42
import java.awt.event.MouseAdapter
32
43
import java.awt.event.MouseEvent
33
44
import javax.swing.BorderFactory
@@ -45,6 +56,8 @@ class SingleFileDiffView(
45
56
private val myHeaderPanel: JPanel = JPanel (BorderLayout ())
46
57
private var filePanel: DialogPanel ? = null
47
58
var diffFile: ChainDiffVirtualFile ? = null
59
+ private val appliedPatch = GenericPatchApplier .apply (virtualFile.readText(), patch.hunks)
60
+ private val oldCode = virtualFile.readText()
48
61
49
62
init {
50
63
val contentPanel = JPanel (BorderLayout ())
@@ -142,11 +155,9 @@ class SingleFileDiffView(
142
155
val fileEditor = FileEditorManager .getInstance(myProject).getSelectedEditor(virtualFile)
143
156
144
157
val rollback = JButton (AllIcons .Actions .Rollback ).apply {
158
+ preferredSize = Dimension (32 , 32 )
145
159
toolTipText = AutoDevBundle .message(" sketch.patch.action.rollback.tooltip" )
146
160
isEnabled = undoManager.isUndoAvailable(fileEditor)
147
- border = null
148
- isFocusPainted = false
149
- isContentAreaFilled = false
150
161
151
162
addMouseListener(object : MouseAdapter () {
152
163
override fun mouseClicked (e : MouseEvent ? ) {
@@ -157,9 +168,70 @@ class SingleFileDiffView(
157
168
})
158
169
}
159
170
160
- return listOf (rollback)
171
+ val runStreamButton = JButton (AllIcons .Actions .RunAll ).apply {
172
+ preferredSize = Dimension (32 , 32 )
173
+ toolTipText = AutoDevBundle .message(" sketch.patch.action.runDiff.tooltip" )
174
+ isEnabled = appliedPatch?.status == ApplyPatchStatus .SUCCESS
175
+
176
+ addActionListener {
177
+ val document = FileDocumentManager .getInstance().getDocument(virtualFile) ? : return @addActionListener
178
+ PsiDocumentManager .getInstance(myProject).commitDocument(document)
179
+ CommandProcessor .getInstance().executeCommand(myProject, {
180
+ showStreamDiff()
181
+ }, " RunStream" , null , UndoConfirmationPolicy .REQUEST_CONFIRMATION , false )
182
+ }
183
+ }
184
+
185
+ val repairButton = JButton (AllIcons .Toolwindows .ToolWindowBuild ).apply {
186
+ preferredSize = Dimension (32 , 32 )
187
+ toolTipText = AutoDevBundle .message(" sketch.patch.action.repairDiff.tooltip" )
188
+ isEnabled = appliedPatch?.status != ApplyPatchStatus .SUCCESS
189
+
190
+ addActionListener {
191
+ FileEditorManager .getInstance(myProject).openFile(virtualFile, true )
192
+ val editor = FileEditorManager .getInstance(myProject).selectedTextEditor ? : return @addActionListener
193
+
194
+ val diffStreamHandler = DiffStreamHandler (
195
+ myProject,
196
+ editor = editor,
197
+ 0 ,
198
+ oldCode.lines().size,
199
+ onClose = {
200
+ },
201
+ onFinish = {
202
+
203
+ })
204
+
205
+ diffStreamHandler.streamDiffLinesToEditor(
206
+ oldCode, " Please repair the diff, return all repaird code. \n " +
207
+ " Here is the original code: \n\n $oldCode " + " \n\n Here is the patched code: \n\n ${patch.singleHunkPatchText} "
208
+ )
209
+ }
210
+ }
211
+
212
+ return listOf (rollback, runStreamButton, repairButton)
161
213
}
162
214
215
+ private fun showStreamDiff () {
216
+ FileEditorManager .getInstance(myProject).openFile(virtualFile, true )
217
+ val editor = FileEditorManager .getInstance(myProject).selectedTextEditor ? : return
218
+ val newText = appliedPatch!! .patchedText
219
+
220
+ val diffStreamHandler = DiffStreamHandler (
221
+ myProject,
222
+ editor = editor,
223
+ 0 ,
224
+ oldCode.lines().size,
225
+ onClose = {
226
+ },
227
+ onFinish = {
228
+
229
+ })
230
+
231
+ diffStreamHandler.normalDiff(oldCode, newText)
232
+ }
233
+
234
+
163
235
override fun getViewText (): String = virtualFile.readText()
164
236
165
237
override fun updateViewText (text : String ) {}
0 commit comments