44package software.aws.toolkits.jetbrains.services.amazonq.lsp
55
66import com.intellij.diff.DiffContentFactory
7- import com.intellij.diff.DiffManager
8- import com.intellij.diff.DiffManagerEx
7+ import com.intellij.diff.chains.SimpleDiffRequestChain
8+ import com.intellij.diff.editor.ChainDiffVirtualFile
9+ import com.intellij.diff.editor.DiffEditorTabFilesManager
910import com.intellij.diff.requests.SimpleDiffRequest
1011import com.intellij.openapi.application.ApplicationManager
1112import com.intellij.openapi.fileChooser.FileChooserFactory
@@ -60,7 +61,7 @@ import java.util.concurrent.TimeUnit
6061 * Concrete implementation of [AmazonQLanguageClient] to handle messages sent from server
6162 */
6263class AmazonQLanguageClientImpl (private val project : Project ) : AmazonQLanguageClient {
63- private val openFileDiffs = mutableMapOf<String , SimpleDiffRequest >()
64+ private val openFileDiffs = mutableMapOf<String , ChainDiffVirtualFile >()
6465
6566 override fun telemetryEvent (`object `: Any ) {
6667 println (`object `)
@@ -288,9 +289,9 @@ class AmazonQLanguageClientImpl(private val project: Project) : AmazonQLanguageC
288289 try {
289290 val diffId = " ${params.originalFileUri} :${params.originalFileContent.hashCode()} :${params.fileContent.hashCode()} "
290291
291- // Check if we already have an open diff for this request
292292 val existingDiff = openFileDiffs[diffId]
293293 if (existingDiff != null ) {
294+ FileEditorManager .getInstance(project).openFile(existingDiff, true )
294295 return @supplyAsync
295296 }
296297
@@ -312,6 +313,7 @@ class AmazonQLanguageClientImpl(private val project: Project) : AmazonQLanguageC
312313 contentFactory.create(project, originalContent, virtualFile) to
313314 contentFactory.createEmpty()
314315 }
316+
315317 else -> {
316318 val newContent = params.fileContent.orEmpty()
317319 isNewFile = newContent == originalContent
@@ -320,15 +322,14 @@ class AmazonQLanguageClientImpl(private val project: Project) : AmazonQLanguageC
320322 contentFactory.createEmpty() to
321323 contentFactory.create(project, newContent, virtualFile)
322324 }
325+
323326 else -> {
324327 contentFactory.create(project, originalContent, virtualFile) to
325328 contentFactory.create(project, newContent, virtualFile)
326329 }
327330 }
328331 }
329332 }
330-
331- // Create a tracking diff request
332333 val diffRequest = object : SimpleDiffRequest (
333334 " $fileName ${message(" aws.q.lsp.client.diff_message" )} " ,
334335 leftContent,
@@ -342,21 +343,21 @@ class AmazonQLanguageClientImpl(private val project: Project) : AmazonQLanguageC
342343 ) {
343344 override fun onAssigned (isAssigned : Boolean ) {
344345 super .onAssigned(isAssigned)
345- if (isAssigned) {
346- // Diff window opened
347- openFileDiffs[diffId] = this
348- } else {
349- // Diff window closed
346+ if (! isAssigned) {
347+ // remove when window closed
350348 openFileDiffs.remove(diffId)
351349 }
352350 }
353351 }
354352
355- (DiffManager .getInstance() as DiffManagerEx ).showDiffBuiltin(project, diffRequest)
353+ val diffChain = SimpleDiffRequestChain (diffRequest)
354+ val diffVirtualFile = ChainDiffVirtualFile (diffChain, fileName)
355+ DiffEditorTabFilesManager .getInstance(project).showDiffFile(diffVirtualFile, true )
356+ openFileDiffs[diffId] = diffVirtualFile
356357 } catch (e: Exception ) {
357358 LOG .warn { " Failed to open file diff: ${e.message} " }
358359 } finally {
359- // Clean up the temporary file
360+ // Clean up the temporary file used for syntax highlight
360361 try {
361362 tempPath?.let { Files .deleteIfExists(it) }
362363 } catch (e: Exception ) {
0 commit comments