Skip to content

Commit aa50626

Browse files
siakmun-awsrli
andauthored
Fix rejected files display inconsistency between VS Code and JetBrains (#5169)
* Fix rejected files display inconsistency between VS Code and JetBrains * Refactor * Refactor * Refactor * Clean * Clean * Changed from `when` to whenever * Fixing detekt * Fixing detekt * Fixing detekt --------- Co-authored-by: Richard Li <[email protected]>
1 parent b21abc8 commit aa50626

File tree

4 files changed

+66
-33
lines changed

4 files changed

+66
-33
lines changed

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,6 @@ class FeatureDevController(
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(
@@ -313,8 +312,6 @@ class FeatureDevController(
313312
deletedFiles.find { it.zipFilePath == fileToUpdate }?.let { it.rejected = !it.rejected }
314313
}
315314

316-
// FIXME: This is a kludge that is hiding the fact that insertChanges is updating the file tree above this point to
317-
// an incorrect state. Update the state of the tree view:
318315
messenger.updateFileComponent(message.tabId, filePaths, deletedFiles, messageId)
319316

320317
// Then, if the accepted file is not a deletion, open a diff to show the changes are applied:
@@ -421,14 +418,14 @@ class FeatureDevController(
421418
credentialStartUrl = getStartUrl(project = context.project)
422419
)
423420

424-
// Caution: insertChanges has multiple responsibilities.
425-
// The filter here results in rejected files being hidden from the tree after continuing, by design.
426-
// However, it is critical that we don't hide already-accepted files. Inside insertChanges, it
427-
// filters to only update the subset of passed files that aren't accepted or rejected already.
428421
session.insertChanges(
429-
filePaths = filePaths.filter { !it.rejected },
430-
deletedFiles = deletedFiles.filter { !it.rejected },
431-
references = references,
422+
filePaths = filePaths,
423+
deletedFiles = deletedFiles,
424+
references = references
425+
)
426+
session.updateFilesPaths(
427+
filePaths = filePaths,
428+
deletedFiles = deletedFiles,
432429
messenger
433430
)
434431

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

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,29 @@ class Session(val tabID: String, val project: Project) {
111111
this._codeResultMessageId = messageId
112112
}
113113

114+
suspend fun updateFilesPaths(
115+
filePaths: List<NewFileZipInfo>,
116+
deletedFiles: List<DeletedFileInfo>,
117+
messenger: MessagePublisher,
118+
disableFileActions: Boolean = false,
119+
) {
120+
val codeResultMessageId = this._codeResultMessageId
121+
if (codeResultMessageId != null) {
122+
messenger.updateFileComponent(this.tabID, filePaths, deletedFiles, codeResultMessageId, disableFileActions)
123+
}
124+
}
125+
114126
/**
115127
* Triggered by the Insert code follow-up button to apply code changes.
116128
*/
117129
suspend fun insertChanges(
118130
filePaths: List<NewFileZipInfo>,
119131
deletedFiles: List<DeletedFileInfo>,
120132
references: List<CodeReferenceGenerated>,
121-
messenger: MessagePublisher,
122133
) {
123-
val selectedSourceFolder = context.selectedSourceFolder.toNioPath()
124134
val newFilePaths = filePaths.filter { !it.rejected && !it.changeApplied }
125135
val newDeletedFiles = deletedFiles.filter { !it.rejected && !it.changeApplied }
136+
val selectedSourceFolder = context.selectedSourceFolder.toNioPath()
126137

127138
runCatching {
128139
var insertedLines = 0
@@ -159,23 +170,39 @@ class Session(val tabID: String, val project: Project) {
159170
}
160171
}.onFailure { /* Noop on diff telemetry failure */ }
161172

162-
newFilePaths.forEach {
163-
resolveAndCreateOrUpdateFile(selectedSourceFolder, it.zipFilePath, it.fileContent)
164-
it.changeApplied = true
165-
}
173+
insertNewFiles(newFilePaths)
166174

167-
newDeletedFiles.forEach {
168-
resolveAndDeleteFile(selectedSourceFolder, it.zipFilePath)
169-
it.changeApplied = true
170-
}
175+
applyDeleteFiles(newDeletedFiles)
171176

172177
ReferenceLogController.addReferenceLog(references, project)
173178

174179
// Taken from https://intellij-support.jetbrains.com/hc/en-us/community/posts/206118439-Refresh-after-external-changes-to-project-structure-and-sources
175180
VfsUtil.markDirtyAndRefresh(true, true, true, context.selectedSourceFolder)
176-
val codeResultMessageId = this._codeResultMessageId
177-
if (codeResultMessageId != null) {
178-
messenger.updateFileComponent(this.tabID, filePaths, deletedFiles, codeResultMessageId)
181+
}
182+
183+
// Suppressing because insertNewFiles needs to be a suspend function in order to be tested
184+
@Suppress("RedundantSuspendModifier")
185+
suspend fun insertNewFiles(
186+
filePaths: List<NewFileZipInfo>,
187+
) {
188+
val selectedSourceFolder = context.selectedSourceFolder.toNioPath()
189+
190+
filePaths.forEach {
191+
resolveAndCreateOrUpdateFile(selectedSourceFolder, it.zipFilePath, it.fileContent)
192+
it.changeApplied = true
193+
}
194+
}
195+
196+
// Suppressing because applyDeleteFiles needs to be a suspend function in order to be tested
197+
@Suppress("RedundantSuspendModifier")
198+
suspend fun applyDeleteFiles(
199+
deletedFiles: List<DeletedFileInfo>,
200+
) {
201+
val selectedSourceFolder = context.selectedSourceFolder.toNioPath()
202+
203+
deletedFiles.forEach {
204+
resolveAndDeleteFile(selectedSourceFolder, it.zipFilePath)
205+
it.changeApplied = true
179206
}
180207
}
181208

@@ -188,10 +215,7 @@ class Session(val tabID: String, val project: Project) {
188215
return
189216
}
190217

191-
val codeResultMessageId = this._codeResultMessageId
192-
if (codeResultMessageId != null) {
193-
messenger.updateFileComponent(this.tabID, filePaths, deletedFiles, codeResultMessageId, disableFileActions = true)
194-
}
218+
updateFilesPaths(filePaths, deletedFiles, messenger, disableFileActions = true)
195219
this._codeResultMessageId = null
196220
}
197221

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,22 +241,34 @@ class FeatureDevControllerTest : FeatureDevTestBase() {
241241
),
242242
)
243243

244-
doReturn(Unit).`when`(spySession).insertChanges(any(), any(), any(), any())
244+
doReturn(Unit).whenever(spySession).insertChanges(any(), any(), any())
245+
doReturn(Unit).whenever(spySession).insertNewFiles(any())
246+
doReturn(Unit).whenever(spySession).applyDeleteFiles(any())
245247

246248
spySession.preloader(userMessage, messenger)
247249
controller.processFollowupClickedMessage(message)
248250

249251
mockitoVerify(
250252
spySession,
251253
times(1),
252-
).insertChanges(listOf(newFileContents[0]), listOf(deletedFiles[0]), testReferences, messenger) // insert changes for only non rejected files
254+
).insertChanges(newFileContents, deletedFiles, testReferences) // updates for all files
253255
coVerifyOrder {
254256
AmazonqTelemetry.isAcceptedCodeChanges(
255257
amazonqNumberOfFilesAccepted = 2.0, // it should be 2 files per test setup
256258
amazonqConversationId = spySession.conversationId,
257259
enabled = true,
258260
createTime = any(),
259261
)
262+
263+
// insert changes for only non rejected files
264+
spySession.insertNewFiles(listOf(newFileContents[0]))
265+
spySession.applyDeleteFiles(listOf(deletedFiles[0]))
266+
267+
spySession.updateFilesPaths(
268+
filePaths = newFileContents,
269+
deletedFiles = deletedFiles,
270+
messenger
271+
)
260272
messenger.sendAnswer(
261273
tabId = testTabId,
262274
message = message("amazonqFeatureDev.code_generation.updated_code"),
@@ -468,7 +480,7 @@ class FeatureDevControllerTest : FeatureDevTestBase() {
468480
),
469481
)
470482
doReturn(testConversationId).`when`(spySession).conversationId
471-
doReturn(Unit).`when`(spySession).insertChanges(any(), any(), any(), any())
483+
doReturn(Unit).`when`(spySession).insertChanges(any(), any(), any())
472484

473485
mockkObject(AmazonqTelemetry)
474486
every {
@@ -491,7 +503,7 @@ class FeatureDevControllerTest : FeatureDevTestBase() {
491503
mockitoVerify(
492504
spySession,
493505
times(1),
494-
).insertChanges(listOf(newFileContents[0]), listOf(), testReferences, messenger)
506+
).insertChanges(listOf(newFileContents[0]), listOf(), testReferences)
495507

496508
// Does not continue automatically, because files are remaining:
497509
mockitoVerify(
@@ -525,7 +537,7 @@ class FeatureDevControllerTest : FeatureDevTestBase() {
525537
),
526538
)
527539
doReturn(testConversationId).`when`(spySession).conversationId
528-
doReturn(Unit).`when`(spySession).insertChanges(any(), any(), any(), any())
540+
doReturn(Unit).`when`(spySession).insertChanges(any(), any(), any())
529541

530542
mockkObject(AmazonqTelemetry)
531543
every {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class SessionTest : FeatureDevTestBase() {
8383
whenever(session.context.selectedSourceFolder.toNioPath()).thenReturn(Path(""))
8484

8585
runBlocking {
86-
session.insertChanges(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)