Skip to content

Commit 82ab3d7

Browse files
committed
refactor(patch): move diff stream logic to SingleFileDiffView
- Relocate diff stream handling from DiffLangSketch to SingleFileDiffView. - Add repair and run stream buttons with improved UI and functionality. - Clean up unused code and streamline patch handling.
1 parent 26e4c76 commit 82ab3d7

File tree

3 files changed

+85
-63
lines changed

3 files changed

+85
-63
lines changed

core/src/main/kotlin/cc/unitmesh/devti/gui/chat/ui/AutoDevInputSection.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import cc.unitmesh.devti.llms.tokenizer.Tokenizer
99
import cc.unitmesh.devti.llms.tokenizer.TokenizerFactory
1010
import cc.unitmesh.devti.provider.RelatedClassesProvider
1111
import cc.unitmesh.devti.settings.AutoDevSettingsState
12+
import cc.unitmesh.devti.util.isInProject
1213
import com.intellij.codeInsight.lookup.LookupManagerListener
1314
import com.intellij.icons.AllIcons
1415
import com.intellij.ide.IdeTooltip
@@ -385,10 +386,12 @@ class AutoDevInputSection(private val project: Project, val disposable: Disposab
385386
}
386387

387388
val focusableComponent: JComponent get() = input
388-
}
389389

390-
private fun DefaultListModel<ModelWrapper>.addIfAbsent(vfile: VirtualFile) {
391-
if (elements().asIterator().asSequence().none { it.virtualFile == vfile }) {
392-
addElement(ModelWrapper(vfile))
390+
private fun DefaultListModel<ModelWrapper>.addIfAbsent(vfile: VirtualFile) {
391+
if (!isInProject(vfile, project)) return
392+
if (elements().asIterator().asSequence().none { it.virtualFile == vfile }) {
393+
addElement(ModelWrapper(vfile))
394+
}
393395
}
396+
394397
}

core/src/main/kotlin/cc/unitmesh/devti/sketch/ui/patch/DiffLangSketch.kt

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,8 @@ class DiffLangSketch(private val myProject: Project, private var patchContent: S
5353
private val filePatches: MutableList<TextFilePatch> = myReader.textPatches
5454

5555
init {
56-
val header = createHeaderAction()
57-
58-
myHeaderPanel.add(header, BorderLayout.EAST)
56+
// val header = createHeaderAction()
57+
// myHeaderPanel.add(header, BorderLayout.EAST)
5958
mainPanel.add(myHeaderPanel)
6059
mainPanel.border = JBUI.Borders.compound(JBUI.Borders.empty(0, 10))
6160

@@ -112,67 +111,15 @@ class DiffLangSketch(private val myProject: Project, private var patchContent: S
112111
}
113112
}
114113

115-
val runStreamButton = JButton(AllIcons.Actions.RunAll).apply {
116-
this.toolTipText = AutoDevBundle.message("sketch.patch.action.runDiff.tooltip")
117-
addActionListener {
118-
showStreamDiff()
119-
}
120-
}
121-
122-
val repairButton = JButton(AllIcons.Actions.Redo).apply {
123-
this.toolTipText = AutoDevBundle.message("sketch.patch.action.repairDiff.tooltip")
124-
addActionListener {
125-
AutoDevNotifications.error(myProject, "Not implemented yet")
126-
}
127-
}
128-
129114
val panel = JPanel()
130115
panel.layout = BoxLayout(panel, BoxLayout.X_AXIS)
131116
panel.add(acceptButton)
132117
panel.add(rejectButton)
133118
panel.add(viewDiffButton)
134-
panel.add(runStreamButton)
135-
panel.add(repairButton)
136119

137120
return panel
138121
}
139122

140-
private fun showStreamDiff() {
141-
val matchedPatches =
142-
MatchPatchPaths(myProject).execute(filePatches, true)
143-
144-
val patchGroups = MultiMap<VirtualFile, AbstractFilePatchInProgress<*>>()
145-
for (patchInProgress in matchedPatches) {
146-
patchGroups.putValue(patchInProgress.base, patchInProgress)
147-
}
148-
149-
val parsedPatch = filePatches.single()
150-
151-
val findFile = myProject.findFile(parsedPatch.beforeFileName!!)
152-
if (findFile == null) {
153-
AutoDevNotifications.error(myProject, "PatchProcessor: no before file found")
154-
return
155-
}
156-
157-
FileEditorManager.getInstance(myProject).openFile(findFile, true)
158-
val editor = FileEditorManager.getInstance(myProject).selectedTextEditor ?: return
159-
val oldCode = findFile.readText()
160-
val newText = GenericPatchApplier.apply(oldCode, parsedPatch.hunks)!!.patchedText
161-
162-
val diffStreamHandler = DiffStreamHandler(
163-
myProject,
164-
editor = editor,
165-
0,
166-
oldCode.lines().size,
167-
onClose = {
168-
},
169-
onFinish = {
170-
171-
})
172-
173-
diffStreamHandler.normalDiff(oldCode, newText)
174-
}
175-
176123
private fun handleAcceptAction() {
177124
PsiDocumentManager.getInstance(myProject).commitAllDocuments();
178125
val commandProcessor: CommandProcessor = CommandProcessor.getInstance()

core/src/main/kotlin/cc/unitmesh/devti/sketch/ui/patch/SingleFileDiffView.kt

Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package cc.unitmesh.devti.sketch.ui.patch
22

33
import cc.unitmesh.devti.AutoDevBundle
4+
import cc.unitmesh.devti.AutoDevNotifications
5+
import cc.unitmesh.devti.diff.DiffStreamHandler
46
import com.intellij.diff.DiffContentFactoryEx
57
import com.intellij.diff.chains.SimpleDiffRequestChain
68
import com.intellij.diff.chains.SimpleDiffRequestProducer
@@ -26,8 +28,17 @@ import com.intellij.ui.dsl.builder.AlignX
2628
import com.intellij.ui.dsl.builder.RightGap
2729
import com.intellij.ui.dsl.builder.panel
2830
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
2937
import com.intellij.openapi.vfs.VfsUtilCore
38+
import com.intellij.psi.PsiDocumentManager
39+
import com.intellij.util.containers.MultiMap
3040
import java.awt.BorderLayout
41+
import java.awt.Dimension
3142
import java.awt.event.MouseAdapter
3243
import java.awt.event.MouseEvent
3344
import javax.swing.BorderFactory
@@ -45,6 +56,8 @@ class SingleFileDiffView(
4556
private val myHeaderPanel: JPanel = JPanel(BorderLayout())
4657
private var filePanel: DialogPanel? = null
4758
var diffFile: ChainDiffVirtualFile? = null
59+
private val appliedPatch = GenericPatchApplier.apply(virtualFile.readText(), patch.hunks)
60+
private val oldCode = virtualFile.readText()
4861

4962
init {
5063
val contentPanel = JPanel(BorderLayout())
@@ -142,11 +155,9 @@ class SingleFileDiffView(
142155
val fileEditor = FileEditorManager.getInstance(myProject).getSelectedEditor(virtualFile)
143156

144157
val rollback = JButton(AllIcons.Actions.Rollback).apply {
158+
preferredSize = Dimension(32, 32)
145159
toolTipText = AutoDevBundle.message("sketch.patch.action.rollback.tooltip")
146160
isEnabled = undoManager.isUndoAvailable(fileEditor)
147-
border = null
148-
isFocusPainted = false
149-
isContentAreaFilled = false
150161

151162
addMouseListener(object : MouseAdapter() {
152163
override fun mouseClicked(e: MouseEvent?) {
@@ -157,9 +168,70 @@ class SingleFileDiffView(
157168
})
158169
}
159170

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\nHere is the patched code: \n\n${patch.singleHunkPatchText}"
208+
)
209+
}
210+
}
211+
212+
return listOf(rollback, runStreamButton, repairButton)
161213
}
162214

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+
163235
override fun getViewText(): String = virtualFile.readText()
164236

165237
override fun updateViewText(text: String) {}

0 commit comments

Comments
 (0)