Skip to content

Commit 46e7ef5

Browse files
author
Xiao Chen
committed
fix(amazonq): /doc close diff and open readme in preview mode after accept change
1 parent a46f923 commit 46e7ef5

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type" : "bugfix",
3+
"description" : "Amazon Q /doc: close diff tab and open README file in preview mode after user accept changes"
4+
}

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/amazonqDoc/controller/DocController.kt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ import com.intellij.diff.util.DiffUserDataKeys
1313
import com.intellij.ide.BrowserUtil
1414
import com.intellij.openapi.application.runInEdt
1515
import com.intellij.openapi.fileEditor.FileEditorManager
16+
import com.intellij.openapi.fileEditor.TextEditorWithPreview
17+
import com.intellij.openapi.vfs.LocalFileSystem
1618
import com.intellij.openapi.vfs.VfsUtil
19+
import com.intellij.openapi.vfs.originalFileOrSelf
1720
import com.intellij.openapi.wm.ToolWindowManager
1821
import com.intellij.testFramework.LightVirtualFile
1922
import kotlinx.coroutines.withContext
@@ -298,8 +301,11 @@ class DocController(
298301
messenger.sendChatInputEnabledMessage(message.tabId, false)
299302
}
300303

304+
private val diffVirtualFiles = mutableMapOf<String, ChainDiffVirtualFile>()
305+
301306
private suspend fun acceptChanges(message: IncomingDocMessage.FollowupClicked) {
302307
insertCode(message.tabId)
308+
previewReadmeFile(message.tabId)
303309
}
304310

305311
private suspend fun promptForDocTarget(tabId: String) {
@@ -420,6 +426,8 @@ class DocController(
420426
request.putUserData(DiffUserDataKeys.FORCE_READ_ONLY, true)
421427

422428
val newDiff = ChainDiffVirtualFile(SimpleDiffRequestChain(request), message.filePath)
429+
430+
diffVirtualFiles[message.filePath] = newDiff
423431
DiffEditorTabFilesManager.getInstance(context.project).showDiffFile(newDiff, true)
424432
}
425433
}
@@ -1047,6 +1055,34 @@ class DocController(
10471055
session.sendDocTelemetryEvent(null, docAcceptanceEvent)
10481056
}
10491057

1058+
private fun previewReadmeFile(tabId: String) {
1059+
val session = getSessionInfo(tabId)
1060+
var filePaths: List<NewFileZipInfo> = emptyList()
1061+
1062+
when (val state = session.sessionState) {
1063+
is PrepareDocGenerationState -> {
1064+
filePaths = state.filePaths
1065+
}
1066+
}
1067+
1068+
if (filePaths.isNotEmpty()) {
1069+
val filePath = filePaths[0].zipFilePath
1070+
val existingDiff = diffVirtualFiles[filePath]
1071+
1072+
val newFilePath = session.context.addressableRoot.toNioPath().resolve(filePath)
1073+
val readmeVirtualFile = LocalFileSystem.getInstance().refreshAndFindFileByPath(newFilePath.toString())
1074+
1075+
runInEdt {
1076+
if (existingDiff != null) {
1077+
FileEditorManager.getInstance(getProject()).closeFile(existingDiff)
1078+
}
1079+
if (readmeVirtualFile != null) {
1080+
TextEditorWithPreview.openPreviewForFile(getProject(), readmeVirtualFile.originalFileOrSelf())
1081+
}
1082+
}
1083+
}
1084+
}
1085+
10501086
fun getProject() = context.project
10511087

10521088
private fun getSessionInfo(tabId: String) = chatSessionStorage.getSession(tabId, context.project)

0 commit comments

Comments
 (0)