Skip to content

Commit d0e4dac

Browse files
committed
Refactor
1 parent eb85ee8 commit d0e4dac

File tree

4 files changed

+64
-39
lines changed

4 files changed

+64
-39
lines changed

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqFeatureDev/controller/FeatureDevController.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,10 @@ class FeatureDevController(
294294
val prevInsertAction = insertAction()
295295

296296
if (action == "accept-change") {
297-
session.insertChangesWithoutUpdateFileComponents(
297+
session.insertChanges(
298298
filePaths = filePaths.filter { it.zipFilePath == fileToUpdate },
299299
deletedFiles = deletedFiles.filter { it.zipFilePath == fileToUpdate },
300300
references = references, // Add all references (not attributed per-file)
301-
messenger
302301
)
303302

304303
AmazonqTelemetry.isAcceptedCodeChanges(
@@ -419,10 +418,14 @@ class FeatureDevController(
419418
credentialStartUrl = getStartUrl(project = context.project)
420419
)
421420

422-
session.insertChangesAndUpdateFileComponents(
421+
session.insertChanges(
422+
filePaths = filePaths,
423+
deletedFiles = deletedFiles,
424+
references = references
425+
)
426+
session.updateFilesPaths(
423427
filePaths = filePaths,
424428
deletedFiles = deletedFiles,
425-
references = references,
426429
messenger
427430
)
428431

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqFeatureDev/session/Session.kt

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import software.aws.toolkits.jetbrains.services.amazonqFeatureDev.util.getChange
2424
import software.aws.toolkits.jetbrains.services.amazonqFeatureDev.util.getDiffMetrics
2525
import software.aws.toolkits.jetbrains.services.amazonqFeatureDev.util.readFileToString
2626
import software.aws.toolkits.jetbrains.services.cwc.controller.ReferenceLogController
27+
import java.nio.file.Path
2728
import java.util.HashSet
2829

2930
class Session(val tabID: String, val project: Project) {
@@ -111,42 +112,28 @@ class Session(val tabID: String, val project: Project) {
111112
this._codeResultMessageId = messageId
112113
}
113114

114-
/**
115-
* Triggered by the Insert code follow-up button to apply code changes.
116-
*/
117-
suspend fun insertChangesAndUpdateFileComponents(
115+
suspend fun updateFilesPaths(
118116
filePaths: List<NewFileZipInfo>,
119117
deletedFiles: List<DeletedFileInfo>,
120-
references: List<CodeReferenceGenerated>,
121118
messenger: MessagePublisher,
119+
disableFileActions: Boolean = false,
122120
) {
123-
insertChangesWithoutUpdateFileComponents(filePaths, deletedFiles, references, messenger)
124121
val codeResultMessageId = this._codeResultMessageId
125122
if (codeResultMessageId != null) {
126-
messenger.updateFileComponent(this.tabID, filePaths, deletedFiles, codeResultMessageId)
123+
messenger.updateFileComponent(this.tabID, filePaths, deletedFiles, codeResultMessageId, disableFileActions)
127124
}
128125
}
129126

130-
suspend fun insertChangesWithoutUpdateFileComponents(
127+
/**
128+
* Triggered by the Insert code follow-up button to apply code changes.
129+
*/
130+
suspend fun insertChanges(
131131
filePaths: List<NewFileZipInfo>,
132132
deletedFiles: List<DeletedFileInfo>,
133133
references: List<CodeReferenceGenerated>,
134-
messenger: MessagePublisher,
135134
) {
136135
val newFilePaths = filePaths.filter { !it.rejected && !it.changeApplied }
137136
val newDeletedFiles = deletedFiles.filter { !it.rejected && !it.changeApplied }
138-
insertChanges(newFilePaths, newDeletedFiles)
139-
140-
ReferenceLogController.addReferenceLog(references, project)
141-
142-
// Taken from https://intellij-support.jetbrains.com/hc/en-us/community/posts/206118439-Refresh-after-external-changes-to-project-structure-and-sources
143-
VfsUtil.markDirtyAndRefresh(true, true, true, context.selectedSourceFolder)
144-
}
145-
146-
suspend fun insertChanges(
147-
filePaths: List<NewFileZipInfo>,
148-
deletedFiles: List<DeletedFileInfo>,
149-
) {
150137
val selectedSourceFolder = context.selectedSourceFolder.toNioPath()
151138

152139
runCatching {
@@ -184,16 +171,37 @@ class Session(val tabID: String, val project: Project) {
184171
}
185172
}.onFailure { /* Noop on diff telemetry failure */ }
186173

174+
insertNewFiles(newFilePaths)
175+
176+
applyDeleteFiles(newDeletedFiles)
177+
178+
ReferenceLogController.addReferenceLog(references, project)
179+
180+
// Taken from https://intellij-support.jetbrains.com/hc/en-us/community/posts/206118439-Refresh-after-external-changes-to-project-structure-and-sources
181+
VfsUtil.markDirtyAndRefresh(true, true, true, context.selectedSourceFolder)
182+
}
183+
184+
suspend fun insertNewFiles(
185+
filePaths: List<NewFileZipInfo>,
186+
) {
187+
val selectedSourceFolder = context.selectedSourceFolder.toNioPath()
188+
187189
filePaths.forEach {
188190
resolveAndCreateOrUpdateFile(selectedSourceFolder, it.zipFilePath, it.fileContent)
189191
it.changeApplied = true
190192
}
193+
}
194+
195+
suspend fun applyDeleteFiles(
196+
deletedFiles: List<DeletedFileInfo>,
197+
) {
198+
val selectedSourceFolder = context.selectedSourceFolder.toNioPath()
191199

192200
deletedFiles.forEach {
193201
resolveAndDeleteFile(selectedSourceFolder, it.zipFilePath)
194202
it.changeApplied = true
195203
}
196-
}
204+
}
197205

198206
suspend fun disableFileList(
199207
filePaths: List<NewFileZipInfo>,
@@ -204,10 +212,7 @@ class Session(val tabID: String, val project: Project) {
204212
return
205213
}
206214

207-
val codeResultMessageId = this._codeResultMessageId
208-
if (codeResultMessageId != null) {
209-
messenger.updateFileComponent(this.tabID, filePaths, deletedFiles, codeResultMessageId, disableFileActions = true)
210-
}
215+
updateFilesPaths(filePaths, deletedFiles, messenger, disableFileActions = true)
211216
this._codeResultMessageId = null
212217
}
213218

plugins/amazonq/chat/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/amazonqFeatureDev/controller/FeatureDevControllerTest.kt

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import io.mockk.mockkStatic
1616
import io.mockk.runs
1717
import io.mockk.unmockkAll
1818
import io.mockk.verify
19+
import kotlinx.coroutines.runBlocking
1920
import kotlinx.coroutines.test.runTest
2021
import org.assertj.core.api.Assertions.assertThat
2122
import org.junit.After
@@ -30,6 +31,8 @@ import org.mockito.kotlin.reset
3031
import org.mockito.kotlin.spy
3132
import org.mockito.kotlin.times
3233
import org.mockito.kotlin.whenever
34+
import software.aws.toolkits.jetbrains.common.util.resolveAndCreateOrUpdateFile
35+
import software.aws.toolkits.jetbrains.common.util.resolveAndDeleteFile
3336
import software.aws.toolkits.jetbrains.common.util.selectFolder
3437
import software.aws.toolkits.jetbrains.services.amazonq.FeatureDevSessionContext
3538
import software.aws.toolkits.jetbrains.services.amazonq.apps.AmazonQAppInitContext
@@ -241,24 +244,34 @@ class FeatureDevControllerTest : FeatureDevTestBase() {
241244
),
242245
)
243246

244-
doReturn(Unit).`when`(spySession).insertChangesAndUpdateFileComponents(any(), any(), any(), any())
245-
doReturn(Unit).`when`(spySession).insertChanges(any(), any())
247+
doReturn(Unit).`when`(spySession).insertChanges(any(), any(), any())
248+
doReturn(Unit).`when`(spySession).insertNewFiles(any())
249+
doReturn(Unit).`when`(spySession).applyDeleteFiles(any())
246250

247251
spySession.preloader(userMessage, messenger)
248252
controller.processFollowupClickedMessage(message)
249253

250254
mockitoVerify(
251255
spySession,
252256
times(1),
253-
).insertChangesAndUpdateFileComponents(newFileContents, deletedFiles, testReferences, messenger) // updates for all files
257+
).insertChanges(newFileContents, deletedFiles, testReferences) // updates for all files
254258
coVerifyOrder {
255259
AmazonqTelemetry.isAcceptedCodeChanges(
256260
amazonqNumberOfFilesAccepted = 2.0, // it should be 2 files per test setup
257261
amazonqConversationId = spySession.conversationId,
258262
enabled = true,
259263
createTime = any(),
260264
)
261-
spySession.insertChanges(listOf(newFileContents[0]), listOf(deletedFiles[0])) // insert changes for only non-rejected files
265+
266+
// insert changes for only non rejected files
267+
spySession.insertNewFiles(listOf(newFileContents[0]))
268+
spySession.applyDeleteFiles(listOf(deletedFiles[0]))
269+
270+
spySession.updateFilesPaths(
271+
filePaths = newFileContents,
272+
deletedFiles = deletedFiles,
273+
messenger
274+
)
262275
messenger.sendAnswer(
263276
tabId = testTabId,
264277
message = message("amazonqFeatureDev.code_generation.updated_code"),
@@ -274,6 +287,10 @@ class FeatureDevControllerTest : FeatureDevTestBase() {
274287
)
275288
messenger.sendUpdatePlaceholder(testTabId, message("amazonqFeatureDev.placeholder.additional_improvements"))
276289
}
290+
291+
// insert changes for only non rejected files
292+
// verify(exactly = 1) { resolveAndCreateOrUpdateFile(any(), newFileContents[0].zipFilePath, newFileContents[0].fileContent) }
293+
// verify(exactly = 1) { resolveAndDeleteFile(any(), deletedFiles[0].zipFilePath) }
277294
}
278295

279296
@Test
@@ -470,7 +487,7 @@ class FeatureDevControllerTest : FeatureDevTestBase() {
470487
),
471488
)
472489
doReturn(testConversationId).`when`(spySession).conversationId
473-
doReturn(Unit).`when`(spySession).insertChangesWithoutUpdateFileComponents(any(), any(), any(), any())
490+
doReturn(Unit).`when`(spySession).insertChanges(any(), any(), any())
474491

475492
mockkObject(AmazonqTelemetry)
476493
every {
@@ -493,7 +510,7 @@ class FeatureDevControllerTest : FeatureDevTestBase() {
493510
mockitoVerify(
494511
spySession,
495512
times(1),
496-
).insertChangesWithoutUpdateFileComponents(listOf(newFileContents[0]), listOf(), testReferences, messenger)
513+
).insertChanges(listOf(newFileContents[0]), listOf(), testReferences)
497514

498515
// Does not continue automatically, because files are remaining:
499516
mockitoVerify(
@@ -527,7 +544,7 @@ class FeatureDevControllerTest : FeatureDevTestBase() {
527544
),
528545
)
529546
doReturn(testConversationId).`when`(spySession).conversationId
530-
doReturn(Unit).`when`(spySession).insertChangesWithoutUpdateFileComponents(any(), any(), any(), any())
547+
doReturn(Unit).`when`(spySession).insertChanges(any(), any(), any())
531548

532549
mockkObject(AmazonqTelemetry)
533550
every {

plugins/amazonq/chat/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/amazonqFeatureDev/session/SessionTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class SessionTest : FeatureDevTestBase() {
6565
}
6666

6767
@Test
68-
fun `test insertChangesAndUpdateFileComponents`() {
68+
fun `test insertChanges`() {
6969
mockkStatic("com.intellij.openapi.vfs.VfsUtil")
7070
every { VfsUtil.markDirtyAndRefresh(true, true, true, any<VirtualFile>()) } just runs
7171

@@ -83,7 +83,7 @@ class SessionTest : FeatureDevTestBase() {
8383
whenever(session.context.selectedSourceFolder.toNioPath()).thenReturn(Path(""))
8484

8585
runBlocking {
86-
session.insertChangesAndUpdateFileComponents(mockNewFile, mockDeletedFile, emptyList(), messenger)
86+
session.insertChanges(mockNewFile, mockDeletedFile, emptyList())
8787
}
8888

8989
verify(exactly = 1) { resolveAndDeleteFile(any(), "deletedTest.ts") }

0 commit comments

Comments
 (0)