Skip to content

Commit eb85ee8

Browse files
committed
Fix rejected files display inconsistency between VS Code and JetBrains
1 parent 69f79b1 commit eb85ee8

File tree

4 files changed

+42
-30
lines changed

4 files changed

+42
-30
lines changed

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

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

296296
if (action == "accept-change") {
297-
session.insertChanges(
297+
session.insertChangesWithoutUpdateFileComponents(
298298
filePaths = filePaths.filter { it.zipFilePath == fileToUpdate },
299299
deletedFiles = deletedFiles.filter { it.zipFilePath == fileToUpdate },
300300
references = references, // Add all references (not attributed per-file)
@@ -313,8 +313,6 @@ class FeatureDevController(
313313
deletedFiles.find { it.zipFilePath == fileToUpdate }?.let { it.rejected = !it.rejected }
314314
}
315315

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:
318316
messenger.updateFileComponent(message.tabId, filePaths, deletedFiles, messageId)
319317

320318
// Then, if the accepted file is not a deletion, open a diff to show the changes are applied:
@@ -421,13 +419,9 @@ class FeatureDevController(
421419
credentialStartUrl = getStartUrl(project = context.project)
422420
)
423421

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.
428-
session.insertChanges(
429-
filePaths = filePaths.filter { !it.rejected },
430-
deletedFiles = deletedFiles.filter { !it.rejected },
422+
session.insertChangesAndUpdateFileComponents(
423+
filePaths = filePaths,
424+
deletedFiles = deletedFiles,
431425
references = references,
432426
messenger
433427
)

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

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,40 @@ class Session(val tabID: String, val project: Project) {
114114
/**
115115
* Triggered by the Insert code follow-up button to apply code changes.
116116
*/
117-
suspend fun insertChanges(
117+
suspend fun insertChangesAndUpdateFileComponents(
118+
filePaths: List<NewFileZipInfo>,
119+
deletedFiles: List<DeletedFileInfo>,
120+
references: List<CodeReferenceGenerated>,
121+
messenger: MessagePublisher,
122+
) {
123+
insertChangesWithoutUpdateFileComponents(filePaths, deletedFiles, references, messenger)
124+
val codeResultMessageId = this._codeResultMessageId
125+
if (codeResultMessageId != null) {
126+
messenger.updateFileComponent(this.tabID, filePaths, deletedFiles, codeResultMessageId)
127+
}
128+
}
129+
130+
suspend fun insertChangesWithoutUpdateFileComponents(
118131
filePaths: List<NewFileZipInfo>,
119132
deletedFiles: List<DeletedFileInfo>,
120133
references: List<CodeReferenceGenerated>,
121134
messenger: MessagePublisher,
122135
) {
123-
val selectedSourceFolder = context.selectedSourceFolder.toNioPath()
124136
val newFilePaths = filePaths.filter { !it.rejected && !it.changeApplied }
125137
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+
) {
150+
val selectedSourceFolder = context.selectedSourceFolder.toNioPath()
126151

127152
runCatching {
128153
var insertedLines = 0
@@ -159,24 +184,15 @@ class Session(val tabID: String, val project: Project) {
159184
}
160185
}.onFailure { /* Noop on diff telemetry failure */ }
161186

162-
newFilePaths.forEach {
187+
filePaths.forEach {
163188
resolveAndCreateOrUpdateFile(selectedSourceFolder, it.zipFilePath, it.fileContent)
164189
it.changeApplied = true
165190
}
166191

167-
newDeletedFiles.forEach {
192+
deletedFiles.forEach {
168193
resolveAndDeleteFile(selectedSourceFolder, it.zipFilePath)
169194
it.changeApplied = true
170195
}
171-
172-
ReferenceLogController.addReferenceLog(references, project)
173-
174-
// Taken from https://intellij-support.jetbrains.com/hc/en-us/community/posts/206118439-Refresh-after-external-changes-to-project-structure-and-sources
175-
VfsUtil.markDirtyAndRefresh(true, true, true, context.selectedSourceFolder)
176-
val codeResultMessageId = this._codeResultMessageId
177-
if (codeResultMessageId != null) {
178-
messenger.updateFileComponent(this.tabID, filePaths, deletedFiles, codeResultMessageId)
179-
}
180196
}
181197

182198
suspend fun disableFileList(

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

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

244-
doReturn(Unit).`when`(spySession).insertChanges(any(), any(), any(), any())
244+
doReturn(Unit).`when`(spySession).insertChangesAndUpdateFileComponents(any(), any(), any(), any())
245+
doReturn(Unit).`when`(spySession).insertChanges(any(), any())
245246

246247
spySession.preloader(userMessage, messenger)
247248
controller.processFollowupClickedMessage(message)
248249

249250
mockitoVerify(
250251
spySession,
251252
times(1),
252-
).insertChanges(listOf(newFileContents[0]), listOf(deletedFiles[0]), testReferences, messenger) // insert changes for only non rejected files
253+
).insertChangesAndUpdateFileComponents(newFileContents, deletedFiles, testReferences, messenger) // updates for all files
253254
coVerifyOrder {
254255
AmazonqTelemetry.isAcceptedCodeChanges(
255256
amazonqNumberOfFilesAccepted = 2.0, // it should be 2 files per test setup
256257
amazonqConversationId = spySession.conversationId,
257258
enabled = true,
258259
createTime = any(),
259260
)
261+
spySession.insertChanges(listOf(newFileContents[0]), listOf(deletedFiles[0])) // insert changes for only non-rejected files
260262
messenger.sendAnswer(
261263
tabId = testTabId,
262264
message = message("amazonqFeatureDev.code_generation.updated_code"),
@@ -468,7 +470,7 @@ class FeatureDevControllerTest : FeatureDevTestBase() {
468470
),
469471
)
470472
doReturn(testConversationId).`when`(spySession).conversationId
471-
doReturn(Unit).`when`(spySession).insertChanges(any(), any(), any(), any())
473+
doReturn(Unit).`when`(spySession).insertChangesWithoutUpdateFileComponents(any(), any(), any(), any())
472474

473475
mockkObject(AmazonqTelemetry)
474476
every {
@@ -491,7 +493,7 @@ class FeatureDevControllerTest : FeatureDevTestBase() {
491493
mockitoVerify(
492494
spySession,
493495
times(1),
494-
).insertChanges(listOf(newFileContents[0]), listOf(), testReferences, messenger)
496+
).insertChangesWithoutUpdateFileComponents(listOf(newFileContents[0]), listOf(), testReferences, messenger)
495497

496498
// Does not continue automatically, because files are remaining:
497499
mockitoVerify(
@@ -525,7 +527,7 @@ class FeatureDevControllerTest : FeatureDevTestBase() {
525527
),
526528
)
527529
doReturn(testConversationId).`when`(spySession).conversationId
528-
doReturn(Unit).`when`(spySession).insertChanges(any(), any(), any(), any())
530+
doReturn(Unit).`when`(spySession).insertChangesWithoutUpdateFileComponents(any(), any(), any(), any())
529531

530532
mockkObject(AmazonqTelemetry)
531533
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 insertChanges`() {
68+
fun `test insertChangesAndUpdateFileComponents`() {
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.insertChanges(mockNewFile, mockDeletedFile, emptyList(), messenger)
86+
session.insertChangesAndUpdateFileComponents(mockNewFile, mockDeletedFile, emptyList(), messenger)
8787
}
8888

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

0 commit comments

Comments
 (0)