Skip to content

Commit 2929794

Browse files
committed
refactor(devti): implement shelf-based change management #352
- Replace `Change` objects with `ShelvedChange` for better change handling - Create `PlannerResultSummary` class to summarize planner results - Update `AgentStateService` to use `ShelvedChange` and `FilePatch` - Remove unnecessary `ChangeListObserver` functionality - Integrate change recording in `SingleFileDiffSketch`
1 parent 21f6427 commit 2929794

File tree

4 files changed

+53
-32
lines changed

4 files changed

+53
-32
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package cc.unitmesh.devti.gui.planner
2+
3+
import com.intellij.openapi.project.Project
4+
import com.intellij.openapi.vcs.changes.Change
5+
6+
class PlannerResultSummary(
7+
val project: Project,
8+
val changes: List<Change>
9+
) {
10+
fun getDiffContent(change: Change) {
11+
change.afterRevision?.file?.name
12+
change.afterRevision?.file?.path
13+
change.afterRevision?.content
14+
}
15+
}
Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,14 @@
11
package cc.unitmesh.devti.observer
22

3-
import cc.unitmesh.devti.observer.agent.AgentStateService
43
import cc.unitmesh.devti.provider.observer.AgentObserver
54
import com.intellij.openapi.Disposable
65
import com.intellij.openapi.project.Project
7-
import com.intellij.openapi.vcs.changes.Change
8-
import com.intellij.openapi.vcs.changes.ChangeList
9-
import com.intellij.openapi.vcs.changes.ChangeListAdapter
10-
import com.intellij.openapi.vcs.changes.ChangeListManager
11-
import com.intellij.util.messages.MessageBusConnection
126

137
class ChangeListObserver : AgentObserver, Disposable {
14-
private var connection: MessageBusConnection? = null
15-
168
override fun onRegister(project: Project) {
17-
ChangeListManager.getInstance(project).addChangeListListener(object : ChangeListAdapter() {
18-
override fun changesAdded(changes: Collection<Change?>?, toList: ChangeList?) {
19-
super.changesAdded(changes, toList)
20-
if (toList != null) {
21-
project.getService(AgentStateService::class.java).updateChanges(toList.changes)
22-
}
23-
}
249

25-
override fun changesRemoved(changes: Collection<Change?>?, fromList: ChangeList?) {
26-
super.changesRemoved(changes, fromList)
27-
if (fromList != null) {
28-
project.getService(AgentStateService::class.java).updateChanges(fromList.changes)
29-
}
30-
}
31-
}, this)
3210
}
3311

3412
override fun dispose() {
35-
connection?.disconnect()
3613
}
3714
}

core/src/main/kotlin/cc/unitmesh/devti/observer/agent/AgentStateService.kt

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import cc.unitmesh.devti.observer.plan.AgentTaskEntry
88
import cc.unitmesh.devti.observer.plan.MarkdownPlanParser
99
import cc.unitmesh.devti.observer.plan.PlanUpdateListener
1010
import cc.unitmesh.devti.settings.AutoDevSettingsState
11-
import cc.unitmesh.devti.settings.customize.customizeSetting
1211
import cc.unitmesh.devti.util.parser.MarkdownCodeHelper
1312
import com.intellij.openapi.application.ApplicationManager
1413
import com.intellij.openapi.components.Service
1514
import com.intellij.openapi.diagnostic.logger
15+
import com.intellij.openapi.diff.impl.patch.FilePatch
1616
import com.intellij.openapi.project.Project
17-
import com.intellij.openapi.vcs.changes.Change
17+
import com.intellij.openapi.vcs.FileStatus
18+
import com.intellij.openapi.vcs.changes.shelf.ShelvedChange
19+
import java.nio.file.Path
1820

1921
@Service(Service.Level.PROJECT)
2022
class AgentStateService(val project: Project) {
@@ -32,9 +34,30 @@ class AgentStateService(val project: Project) {
3234
logger<AgentStateService>().info("Called agent tools:\n ${state.usedTools.joinToString("\n")}")
3335
}
3436

35-
fun updateChanges(changes: Collection<Change?>?) {
36-
val allChanges = changes?.filterNotNull()?.map { it } ?: emptyList()
37-
state.changes = allChanges.toMutableList()
37+
fun addToChange(path: Path, change: FilePatch) {
38+
val shelvedChange = createShelvedChange(project, path, change)
39+
if (shelvedChange != null) {
40+
state.changes.add(shelvedChange.change)
41+
}
42+
}
43+
44+
fun createShelvedChange(project: Project, patchPath: Path, patch: FilePatch): ShelvedChange? {
45+
val beforeName: String? = patch.beforeName
46+
val afterName: String? = patch.afterName
47+
if (beforeName == null || afterName == null) {
48+
logger<AgentStateService>().warn("Failed to parse the file patch: [$patchPath]:$patch")
49+
return null
50+
}
51+
52+
val status = if (patch.isNewFile) {
53+
FileStatus.ADDED
54+
} else if (patch.isDeletedFile) {
55+
FileStatus.DELETED
56+
} else {
57+
FileStatus.MODIFIED
58+
}
59+
60+
return ShelvedChange.create(project, patchPath, beforeName, afterName, status)
3861
}
3962

4063
fun buildOriginIntention(): String? {

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cc.unitmesh.devti.sketch.ui.patch
22

33
import cc.unitmesh.devti.AutoDevBundle
44
import cc.unitmesh.devti.AutoDevIcons
5+
import cc.unitmesh.devti.observer.agent.AgentStateService
56
import cc.unitmesh.devti.settings.coder.coderSetting
67
import cc.unitmesh.devti.sketch.AutoSketchMode
78
import cc.unitmesh.devti.sketch.lint.SketchCodeInspection
@@ -159,6 +160,11 @@ class SingleFileDiffSketch(
159160
mainPanel.add(myHeaderPanel)
160161
mainPanel.add(contentPanel)
161162

163+
invokeLater {
164+
myProject.getService<AgentStateService>(AgentStateService::class.java)
165+
.addToChange(currentFile.toNioPath(), patch)
166+
}
167+
162168
if (myProject.coderSetting.state.enableDiffViewer && appliedPatch?.status == ApplyPatchStatus.SUCCESS) {
163169
invokeLater {
164170
val diffPanel = createDiffViewer(oldCode, newCode)
@@ -182,23 +188,23 @@ class SingleFileDiffSketch(
182188
override fun requestFocusInWindow() = Unit
183189
}, diffRequest)
184190
diffViewer.init()
185-
191+
186192
val wrapperPanel = JPanel(BorderLayout())
187193
wrapperPanel.add(diffViewer.component, BorderLayout.CENTER)
188-
194+
189195
// Set preferred height to 25% of parent (will be determined when laid out)
190196
// with a minimum of 200 pixels
191197
wrapperPanel.preferredSize = java.awt.Dimension(
192198
wrapperPanel.preferredSize.width,
193199
maxOf(200, (mainPanel.height * 0.25).toInt())
194200
)
195-
201+
196202
// Set maximum height to prevent excessive growth
197203
wrapperPanel.maximumSize = java.awt.Dimension(
198204
Int.MAX_VALUE,
199205
maxOf(200, (mainPanel.height * 0.25).toInt())
200206
)
201-
207+
202208
return wrapperPanel
203209
}
204210

0 commit comments

Comments
 (0)