@@ -8,28 +8,13 @@ import cc.unitmesh.devti.observer.plan.AgentTaskEntry
8
8
import cc.unitmesh.devti.observer.plan.MarkdownPlanParser
9
9
import cc.unitmesh.devti.observer.plan.PlanUpdateListener
10
10
import cc.unitmesh.devti.settings.AutoDevSettingsState
11
+ import cc.unitmesh.devti.util.PatchConverter
11
12
import cc.unitmesh.devti.util.parser.MarkdownCodeHelper
12
13
import com.intellij.openapi.application.ApplicationManager
13
- import com.intellij.openapi.application.ReadAction
14
14
import com.intellij.openapi.components.Service
15
15
import com.intellij.openapi.diagnostic.logger
16
16
import com.intellij.openapi.diff.impl.patch.TextFilePatch
17
- import com.intellij.openapi.diff.impl.patch.apply.GenericPatchApplier
18
- import com.intellij.openapi.fileEditor.FileDocumentManager
19
17
import com.intellij.openapi.project.Project
20
- import com.intellij.openapi.util.ThrowableComputable
21
- import com.intellij.openapi.vcs.FilePath
22
- import com.intellij.openapi.vcs.FileStatus
23
- import com.intellij.openapi.vcs.VcsBundle
24
- import com.intellij.openapi.vcs.VcsException
25
- import com.intellij.openapi.vcs.changes.Change
26
- import com.intellij.openapi.vcs.changes.ContentRevision
27
- import com.intellij.openapi.vcs.changes.CurrentContentRevision
28
- import com.intellij.openapi.vcs.changes.TextRevisionNumber
29
- import com.intellij.openapi.vcs.history.VcsRevisionNumber
30
- import com.intellij.openapi.vfs.VirtualFile
31
- import com.intellij.vcsUtil.VcsUtil
32
- import org.jetbrains.annotations.NonNls
33
18
import java.io.File
34
19
import java.io.IOException
35
20
@@ -52,15 +37,15 @@ class AgentStateService(val project: Project) {
52
37
fun addToChange (patch : TextFilePatch ) {
53
38
val baseDir = File (project.basePath!! )
54
39
if (patch.afterName != null ) {
55
- val newChangePath = getAbsolutePath(baseDir, patch.afterName).canonicalPath
40
+ val newChangePath = PatchConverter . getAbsolutePath(baseDir, patch.afterName).canonicalPath
56
41
state.changes.removeIf {
57
42
val afterRevision = it.afterRevision
58
43
afterRevision != null && File (afterRevision.file.path).canonicalPath == newChangePath
59
44
}
60
45
}
61
46
62
47
try {
63
- val change = createChange(patch)
48
+ val change = PatchConverter . createChange(project, patch)
64
49
state.changes.add(change)
65
50
66
51
ApplicationManager .getApplication().messageBus
@@ -71,92 +56,6 @@ class AgentStateService(val project: Project) {
71
56
}
72
57
}
73
58
74
- private fun createChange (patch : TextFilePatch ): Change {
75
- val baseDir = File (project.basePath!! )
76
- val beforePath = patch.beforeName
77
- val afterPath = patch.afterName
78
-
79
- val fileStatus = when {
80
- patch.isNewFile -> {
81
- FileStatus .ADDED
82
- }
83
- patch.isDeletedFile -> {
84
- FileStatus .DELETED
85
- }
86
- else -> {
87
- FileStatus .MODIFIED
88
- }
89
- }
90
-
91
- val beforeFilePath = VcsUtil .getFilePath(getAbsolutePath(baseDir, beforePath), false )
92
- val afterFilePath = VcsUtil .getFilePath(getAbsolutePath(baseDir, afterPath), false )
93
-
94
- var beforeRevision: ContentRevision ? = null
95
- if (fileStatus != = FileStatus .ADDED ) {
96
- beforeRevision = object : CurrentContentRevision (beforeFilePath) {
97
- override fun getRevisionNumber (): VcsRevisionNumber {
98
- return TextRevisionNumber (VcsBundle .message(" local.version.title" ))
99
- }
100
- }
101
- }
102
-
103
- var afterRevision: ContentRevision ? = null
104
- if (fileStatus != = FileStatus .DELETED ) {
105
- afterRevision = object : CurrentContentRevision (beforeFilePath) {
106
- override fun getRevisionNumber (): VcsRevisionNumber =
107
- TextRevisionNumber (VcsBundle .message(" local.version.title" ))
108
-
109
- override fun getVirtualFile (): VirtualFile ? = afterFilePath.virtualFile
110
- override fun getFile (): FilePath = afterFilePath
111
- override fun getContent (): @NonNls String? {
112
- when {
113
- patch.isNewFile -> {
114
- return patch.singleHunkPatchText
115
- }
116
- patch.isDeletedFile -> {
117
- return null
118
- }
119
- else -> {
120
- val localContent: String = loadLocalContent()
121
- val appliedPatch = GenericPatchApplier .apply (localContent, patch.hunks)
122
- // / sometimes llm will return a wrong patch which the content is not correct
123
- if (appliedPatch != null ) {
124
- return appliedPatch.patchedText
125
- }
126
-
127
- return patch.singleHunkPatchText
128
- }
129
- }
130
- }
131
-
132
- @Throws(VcsException ::class )
133
- private fun loadLocalContent (): String {
134
- return ReadAction .compute<String ?, VcsException ?>(ThrowableComputable {
135
- val file: VirtualFile ? = beforeFilePath.virtualFile
136
- if (file == null ) return @ThrowableComputable null
137
- val doc = FileDocumentManager .getInstance().getDocument(file)
138
- if (doc == null ) return @ThrowableComputable null
139
- doc.text
140
- })
141
- }
142
- }
143
- }
144
-
145
- return Change (beforeRevision, afterRevision, fileStatus)
146
- }
147
-
148
- private fun getAbsolutePath (baseDir : File , relativePath : String ): File {
149
- var file: File ?
150
- try {
151
- file = File (baseDir, relativePath).getCanonicalFile()
152
- } catch (e: IOException ) {
153
- logger<AgentStateService >().info(e)
154
- file = File (baseDir, relativePath)
155
- }
156
-
157
- return file
158
- }
159
-
160
59
fun buildOriginIntention (): String? {
161
60
val intention = state.messages
162
61
.firstOrNull { it.role.lowercase() == " user" }
0 commit comments